123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- using System;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- using InABox.Mobile;
- using Xamarin.Forms;
- using Xamarin.Forms.Shapes;
- using Xamarin.Forms.Xaml;
- using Xamarin.Forms.Xaml.Internals;
- using XF.Material.Forms.UI.Dialogs;
- namespace PRS.Mobile
- {
- [XamlCompilation(XamlCompilationOptions.Compile)]
- public partial class DocumentList
- {
-
- public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create(
- nameof(ItemsSource),
- typeof(ICoreRepository),
- typeof(DocumentList)
- );
- //
- // private static void ItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
- // {
- // if ((bindable is DocumentList list) && newvalue is ICoreRepository repository)
- // {
- // list.ItemsSource = null;
- // list.ItemsSource = repository;
- // list._imagelist.ItemsSource = repository?.Items;
- // list._imagelist.LastUpdated = repository?.LastUpdated ?? DateTime.MinValue;
- // }
- // }
- public ICoreRepository? ItemsSource
- {
- get => GetValue(ItemsSourceProperty) as ICoreRepository;
- set
- {
- SetValue(ItemsSourceProperty, value);
- _imagelist.ItemsSource = value?.Items;
- _imagelist.LastUpdated = value?.LastUpdated ?? DateTime.MinValue;
- }
- }
-
- public Guid ParentID { get; set; }
- public event MobileListRefreshEvent RefreshRequested;
- public bool PullToRefresh
- {
- get => _imagelist.PullToRefresh;
- set => _imagelist.PullToRefresh = value;
- }
- public bool ShowRecordCount
- {
- get => _imagelist.ShowRecordCount;
- set => _imagelist.ShowRecordCount = value;
- }
-
- public string EmptyText
- {
- get => _imagelist.EmptyText;
- set => _imagelist.EmptyText = value;
- }
-
- public DocumentList()
- {
- InitializeComponent();
- }
- private void Image_Clicked(object sender, EventArgs e)
- {
- if ((sender as MobileCard)?.BindingContext is IEntityDocumentShell eds && eds.DocumentID != Guid.Empty)
- {
-
- DocumentPage docviewer = new DocumentPage();
- docviewer.DocumentLoaded += (o, e) =>
- {
- if ((sender as MobileCard)?.BindingContext is IEntityDocumentShell eds && (eds.Thumbnail?.Any() != true))
- {
- eds.Thumbnail = docviewer.GetThumbnail();
- eds.Save("Thumbnail Updated on Mobile Device");
- }
- };
- docviewer.DocumentID = eds.DocumentID;
- Navigation.PushAsync(docviewer);
-
- }
- }
- public async Task<bool> AddImage<T, TOptions, TShell>(TOptions options, Func<TShell, Task<bool>> customiseshell = null)
- where T : MobileDocumentSource
- where TOptions : MobileImageOptions<T>
- where TShell : class, IEntityDocumentShell
- {
-
- MobileDocument file = null;
- try
- {
- file = await MobileDocument.From(options);
- }
-
- catch (Exception e)
- {
- await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
- }
-
- if (file?.Data?.Any() == true)
- {
- var ext = System.IO.Path.GetExtension(file.FileName);
- file.FileName = System.IO.Path.ChangeExtension(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ext);
- var shell = ItemsSource.AddItem() as TShell;
- shell.ParentID = ParentID;
-
- bool confirm = (customiseshell == null)
- || await customiseshell.Invoke(shell);
-
- if (confirm)
- {
- using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
- {
- shell.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256);
- shell.FileName = file.FileName;
- var docshell = EntityDocumentUtils.SaveDocument<TShell>(
- file,
- () => shell,
- "Created on Mobile Device"
- );
- Device.BeginInvokeOnMainThread(() =>
- {
- ItemsSource.Search();
- _imagelist.ItemsSource = null;
- _imagelist.ItemsSource = ItemsSource.Items;
- });
- return true;
- }
- }
- }
- return false;
- }
- private void _imagelist_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
- {
- RefreshRequested?.Invoke(this,args);
- }
- }
- }
|