using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Comal.Classes; using InABox.Clients; using InABox.Core; using InABox.Mobile; using PRS.Mobile.Modules.DocScanner; using Xamarin.Forms; using Xamarin.Forms.Xaml; using XF.Material.Forms.UI; using XF.Material.Forms.UI.Dialogs; namespace PRS.Mobile { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class DocScannerModule { private DataEntryTagModel _tags; private DataEntryDocumentModel _dataEntryDocuments; public DocScannerModule() { _dataEntryDocuments = new DataEntryDocumentModel(App.Data, () => new Filter(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID) .And(x => x.Archived).IsEqualTo(DateTime.MinValue), CoreRepository.CacheFileName ); _tags = new DataEntryTagModel(App.Data, () => null, CoreRepository.CacheFileName ); InitializeComponent(); ProgressVisible = true; RefreshData(false, true); } private void RefreshData(bool force, bool async) { _dataEntryDocuments.Refresh(force); Task[] tasks = new Task[] { //Task.Run(() => _dataEntriesDocument.Refresh(force)), Task.Run(() => _tags.Refresh(force)) }; if (async) Task.WhenAll(tasks).ContinueWith((_) => Device.BeginInvokeOnMainThread(RefreshScreen)); else { Task.WaitAll(tasks); RefreshScreen(); } } private void RefreshScreen() { ProgressVisible = false; _documents.ItemsSource = null; _documents.ItemsSource = _dataEntryDocuments.Items; } private async Task ConfirmScan(DataEntryDocumentShell documentShell) { documentShell.EmployeeID = App.Data.Me.ID; if ((_tags?.Count() ?? 0) < 1) { documentShell.TagID = _tags.Items.FirstOrDefault()?.ID ?? Guid.Empty; return true; } var tag = await DisplayActionSheet("Select Tag", "Cancel", null, _tags.Items.Select(x => x.Name).ToArray()); documentShell.TagID = _tags.Items.FirstOrDefault(x => String.Equals(x.Name, tag))?.ID ?? Guid.Empty; return !string.Equals(tag,"Cancel"); } public async Task AddImage(TOptions options, Func>? customiseshell = null) where T : MobileDocumentSource where TOptions : MobileImageOptions { 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 = _dataEntryDocuments.CreateItem(); bool confirm = (customiseshell == null) || await customiseshell.Invoke(shell); if (confirm) { shell.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256); shell.FileName = file.FileName; using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image")) { Document doc = new Document() { FileName = file.FileName, Data = file.Data, CRC = CoreUtils.CalculateCRC(file.Data), TimeStamp = DateTime.Now }; new Client().Save(doc, "Created on Mobile Device"); shell.DocumentID = doc.ID; shell.Save("Created on Mobile Device"); _dataEntryDocuments.CommitItem(shell); Dispatcher.BeginInvokeOnMainThread(() => RefreshData(true,false)); return true; } } } return false; } private async void TakePhoto_Clicked(object sender, EventArgs e) { await AddImage( PhotoUtils.CreateCameraOptions(), ConfirmScan); } private async void BrowseLibrary_Clicked(object sender, EventArgs e) { await AddImage( PhotoUtils.CreatePhotoLibraryOptions(), ConfirmScan); } private void _documents_OnRefreshRequested(object sender, MobileListRefreshEventArgs args) { RefreshData(true,false); } private void Image_Clicked(object sender, EventArgs e) { if ((sender as MobileCard)?.BindingContext is DataEntryDocumentShell shell) Navigation.PushAsync(new DocScannerEditor(shell)); } } }