DocScannerModule.xaml.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Comal.Classes;
  7. using InABox.Clients;
  8. using InABox.Core;
  9. using InABox.Mobile;
  10. using PRS.Mobile.Modules.DocScanner;
  11. using Xamarin.Forms;
  12. using Xamarin.Forms.Xaml;
  13. using XF.Material.Forms.UI;
  14. using XF.Material.Forms.UI.Dialogs;
  15. namespace PRS.Mobile
  16. {
  17. [XamlCompilation(XamlCompilationOptions.Compile)]
  18. public partial class DocScannerModule
  19. {
  20. private DataEntryTagModel _tags;
  21. private DataEntryDocumentModel _dataEntryDocuments;
  22. public DocScannerModule()
  23. {
  24. _dataEntryDocuments = new DataEntryDocumentModel(App.Data,
  25. () => new Filter<DataEntryDocument>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  26. .And(x => x.Archived).IsEqualTo(DateTime.MinValue)
  27. ) { FileName = "scans.data" };
  28. _tags = new DataEntryTagModel(App.Data, null) { FileName = "scantags.data" };
  29. InitializeComponent();
  30. ProgressVisible = true;
  31. RefreshData(false, true);
  32. }
  33. private void RefreshData(bool force, bool async)
  34. {
  35. _dataEntryDocuments.Refresh(force);
  36. Task[] tasks = new Task[]
  37. {
  38. //Task.Run(() => _dataEntriesDocument.Refresh(force)),
  39. Task.Run(() => _tags.Refresh(force))
  40. };
  41. if (async)
  42. Task.WhenAll(tasks).ContinueWith((_) => Device.BeginInvokeOnMainThread(RefreshScreen));
  43. else
  44. {
  45. Task.WaitAll(tasks);
  46. RefreshScreen();
  47. }
  48. }
  49. private void RefreshScreen()
  50. {
  51. ProgressVisible = false;
  52. _documents.ItemsSource = null;
  53. _documents.ItemsSource = _dataEntryDocuments.Items;
  54. }
  55. private async Task<bool> ConfirmScan(DataEntryDocumentShell documentShell)
  56. {
  57. documentShell.EmployeeID = App.Data.Me.ID;
  58. if ((_tags?.Count() ?? 0) < 1)
  59. {
  60. documentShell.TagID = _tags.Items.FirstOrDefault()?.ID ?? Guid.Empty;
  61. return true;
  62. }
  63. var tag = await DisplayActionSheet("Select Tag", "Cancel", null, _tags.Items.Select(x => x.Name).ToArray());
  64. documentShell.TagID = _tags.Items.FirstOrDefault(x => String.Equals(x.Name, tag))?.ID ?? Guid.Empty;
  65. return !string.Equals(tag,"Cancel");
  66. }
  67. public async Task<bool> AddImage<T, TOptions>(TOptions options, Func<DataEntryDocumentShell,Task<bool>>? customiseshell = null)
  68. where T : MobileDocumentSource
  69. where TOptions : MobileImageOptions<T>
  70. {
  71. MobileDocument file = null;
  72. try
  73. {
  74. file = await MobileDocument.From(options);
  75. }
  76. catch (Exception e)
  77. {
  78. await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
  79. }
  80. if (file?.Data?.Any() == true)
  81. {
  82. var ext = System.IO.Path.GetExtension(file.FileName);
  83. file.FileName = System.IO.Path.ChangeExtension(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ext);
  84. var shell = _dataEntryDocuments.CreateItem();
  85. bool confirm = (customiseshell == null) || await customiseshell.Invoke(shell);
  86. if (confirm)
  87. {
  88. shell.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256);
  89. shell.FileName = file.FileName;
  90. using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
  91. {
  92. Document doc = new Document()
  93. {
  94. FileName = file.FileName,
  95. Data = file.Data,
  96. CRC = CoreUtils.CalculateCRC(file.Data),
  97. TimeStamp = DateTime.Now
  98. };
  99. new Client<Document>().Save(doc, "Created on Mobile Device");
  100. shell.DocumentID = doc.ID;
  101. shell.Save("Created on Mobile Device");
  102. _dataEntryDocuments.CommitItem(shell);
  103. Dispatcher.BeginInvokeOnMainThread(() => RefreshData(true,false));
  104. return true;
  105. }
  106. }
  107. }
  108. return false;
  109. }
  110. private async void TakePhoto_Clicked(object sender, EventArgs e)
  111. {
  112. await AddImage<MobileDocumentCameraSource, MobileDocumentCameraOptions>(
  113. PhotoUtils.CreateCameraOptions(),
  114. ConfirmScan);
  115. }
  116. private async void BrowseLibrary_Clicked(object sender, EventArgs e)
  117. {
  118. await AddImage<MobileDocumentPhotoLibrarySource,MobileDocumentPhotoLibraryOptions>(
  119. PhotoUtils.CreatePhotoLibraryOptions(),
  120. ConfirmScan);
  121. }
  122. private void _documents_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  123. {
  124. RefreshData(true,false);
  125. }
  126. private void Image_Clicked(object sender, EventArgs e)
  127. {
  128. if ((sender as MobileCard)?.BindingContext is DataEntryDocumentShell shell)
  129. Navigation.PushAsync(new DocScannerEditor(shell));
  130. }
  131. }
  132. }