DocScannerModule.xaml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.Core;
  8. using InABox.Mobile;
  9. using Xamarin.Forms;
  10. using Xamarin.Forms.Xaml;
  11. namespace PRS.Mobile
  12. {
  13. [XamlCompilation(XamlCompilationOptions.Compile)]
  14. public partial class DocScannerModule
  15. {
  16. private DataEntryTagModel _tags;
  17. private DataEntryDocumentModel _dataEntriesDocument;
  18. public DocScannerModule()
  19. {
  20. _dataEntriesDocument = new DataEntryDocumentModel(App.Data,
  21. () => new Filter<DataEntryDocument>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  22. .And(x => x.Archived).IsEqualTo(DateTime.MinValue)
  23. ) { FileName = "scans.data" };
  24. _tags = new DataEntryTagModel(App.Data, null) { FileName = "scantags.data" };
  25. InitializeComponent();
  26. ProgressVisible = true;
  27. RefreshData(false, true);
  28. }
  29. private void RefreshData(bool force, bool async)
  30. {
  31. _dataEntriesDocument.Refresh(force);
  32. Task[] tasks = new Task[]
  33. {
  34. //Task.Run(() => _dataEntriesDocument.Refresh(force)),
  35. Task.Run(() => _tags.Refresh(force))
  36. };
  37. if (async)
  38. Task.WhenAll(tasks).ContinueWith((_) => Device.BeginInvokeOnMainThread(RefreshScreen));
  39. else
  40. {
  41. Task.WaitAll(tasks);
  42. RefreshScreen();
  43. }
  44. }
  45. private void RefreshScreen()
  46. {
  47. ProgressVisible = false;
  48. _documents.ItemsSource = null;
  49. _documents.ItemsSource = _dataEntriesDocument;
  50. }
  51. private async Task<bool> ConfirmScan(DataEntryDocumentShell documentShell)
  52. {
  53. documentShell.EmployeeID = App.Data.Me.ID;
  54. if (!_tags.Any())
  55. return true;
  56. var tag = await DisplayActionSheet("Select Tag", "Cancel", null, _tags.Items.Select(x => x.Name).ToArray());
  57. documentShell.TagID = _tags.Items.FirstOrDefault(x => String.Equals(x.Name, tag))?.ID ?? Guid.Empty;
  58. return !string.Equals(tag,"Cancel");
  59. }
  60. private async void TakePhoto_Clicked(object sender, EventArgs e)
  61. {
  62. await _documents.AddImage<MobileDocumentCameraSource, MobileDocumentCameraOptions,DataEntryDocumentShell>(
  63. PhotoUtils.CreateCameraOptions(),
  64. ConfirmScan);
  65. }
  66. private async void BrowseLibrary_Clicked(object sender, EventArgs e)
  67. {
  68. await _documents.AddImage<MobileDocumentPhotoLibrarySource,MobileDocumentPhotoLibraryOptions,DataEntryDocumentShell>(
  69. PhotoUtils.CreatePhotoLibraryOptions(),
  70. ConfirmScan);
  71. }
  72. private void _documents_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  73. {
  74. RefreshData(true,false);
  75. }
  76. }
  77. }