| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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<DataEntryDocument>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
- .And(x => x.Archived).IsEqualTo(DateTime.MinValue),
- CoreRepository.CacheFileName<DataEntryDocument>
- );
- _tags = new DataEntryTagModel(App.Data,
- () => null,
- CoreRepository.CacheFileName<DataEntryTag>
- );
-
- 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<bool> 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<bool> AddImage<T, TOptions>(TOptions options, Func<DataEntryDocumentShell,Task<bool>>? customiseshell = null)
- where T : MobileDocumentSource
- where TOptions : MobileImageOptions<T>
- {
-
- 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<Document>().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<MobileDocumentCameraSource, MobileDocumentCameraOptions>(
- PhotoUtils.CreateCameraOptions(),
- ConfirmScan);
- }
- private async void BrowseLibrary_Clicked(object sender, EventArgs e)
- {
- await AddImage<MobileDocumentPhotoLibrarySource,MobileDocumentPhotoLibraryOptions>(
- 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));
- }
- }
- }
|