DocScannerModule.xaml.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. CoreRepository.CacheFileName<DataEntryDocument>
  28. );
  29. _tags = new DataEntryTagModel(App.Data,
  30. () => null,
  31. CoreRepository.CacheFileName<DataEntryTag>
  32. );
  33. InitializeComponent();
  34. ProgressVisible = true;
  35. RefreshData(false, true);
  36. }
  37. private void RefreshData(bool force, bool async)
  38. {
  39. _dataEntryDocuments.Refresh(force);
  40. Task[] tasks = new Task[]
  41. {
  42. //Task.Run(() => _dataEntriesDocument.Refresh(force)),
  43. Task.Run(() => _tags.Refresh(force))
  44. };
  45. if (async)
  46. Task.WhenAll(tasks).ContinueWith((_) => Device.BeginInvokeOnMainThread(RefreshScreen));
  47. else
  48. {
  49. Task.WaitAll(tasks);
  50. RefreshScreen();
  51. }
  52. }
  53. private void RefreshScreen()
  54. {
  55. ProgressVisible = false;
  56. _documents.ItemsSource = null;
  57. _documents.ItemsSource = _dataEntryDocuments.Items;
  58. }
  59. private async Task<bool> ConfirmScan(DataEntryDocumentShell documentShell)
  60. {
  61. documentShell.EmployeeID = App.Data.Me.ID;
  62. if ((_tags?.Count() ?? 0) < 1)
  63. {
  64. documentShell.TagID = _tags.Items.FirstOrDefault()?.ID ?? Guid.Empty;
  65. return true;
  66. }
  67. var tag = await DisplayActionSheet("Select Tag", "Cancel", null, _tags.Items.Select(x => x.Name).ToArray());
  68. documentShell.TagID = _tags.Items.FirstOrDefault(x => String.Equals(x.Name, tag))?.ID ?? Guid.Empty;
  69. return !string.Equals(tag,"Cancel");
  70. }
  71. public async Task<bool> AddImage<T, TOptions>(TOptions options, Func<DataEntryDocumentShell,Task<bool>>? customiseshell = null)
  72. where T : MobileDocumentSource
  73. where TOptions : MobileImageOptions<T>
  74. {
  75. MobileDocument file = null;
  76. try
  77. {
  78. file = await MobileDocument.From(options);
  79. }
  80. catch (Exception e)
  81. {
  82. await MaterialDialog.Instance.AlertAsync(e.Message, "ERROR");
  83. }
  84. if (file?.Data?.Any() == true)
  85. {
  86. var ext = System.IO.Path.GetExtension(file.FileName);
  87. file.FileName = System.IO.Path.ChangeExtension(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ext);
  88. var shell = _dataEntryDocuments.CreateItem();
  89. bool confirm = (customiseshell == null) || await customiseshell.Invoke(shell);
  90. if (confirm)
  91. {
  92. shell.Thumbnail = MobileUtils.ImageTools.CreateThumbnail(file.Data, 256, 256);
  93. shell.FileName = file.FileName;
  94. using (await MaterialDialog.Instance.LoadingDialogAsync("Saving Image"))
  95. {
  96. Document doc = new Document()
  97. {
  98. FileName = file.FileName,
  99. Data = file.Data,
  100. CRC = CoreUtils.CalculateCRC(file.Data),
  101. TimeStamp = DateTime.Now
  102. };
  103. new Client<Document>().Save(doc, "Created on Mobile Device");
  104. shell.DocumentID = doc.ID;
  105. shell.Save("Created on Mobile Device");
  106. _dataEntryDocuments.CommitItem(shell);
  107. Dispatcher.BeginInvokeOnMainThread(() => RefreshData(true,false));
  108. return true;
  109. }
  110. }
  111. }
  112. return false;
  113. }
  114. private async void TakePhoto_Clicked(object sender, EventArgs e)
  115. {
  116. await AddImage<MobileDocumentCameraSource, MobileDocumentCameraOptions>(
  117. PhotoUtils.CreateCameraOptions(),
  118. ConfirmScan);
  119. }
  120. private async void BrowseLibrary_Clicked(object sender, EventArgs e)
  121. {
  122. await AddImage<MobileDocumentPhotoLibrarySource,MobileDocumentPhotoLibraryOptions>(
  123. PhotoUtils.CreatePhotoLibraryOptions(),
  124. ConfirmScan);
  125. }
  126. private void _documents_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  127. {
  128. RefreshData(true,false);
  129. }
  130. private void Image_Clicked(object sender, EventArgs e)
  131. {
  132. if ((sender as MobileCard)?.BindingContext is DataEntryDocumentShell shell)
  133. Navigation.PushAsync(new DocScannerEditor(shell));
  134. }
  135. }
  136. }