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 AddImage(TOptions options, Func> customiseshell = null) where T : MobileDocumentSource where TOptions : MobileImageOptions 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( 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); } } }