ScanModule.xaml.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 ScanModule
  15. {
  16. private ScanTagModel _tags;
  17. private ScanModel _scans;
  18. public ScanModule()
  19. {
  20. _scans = new ScanModel(App.Data,
  21. () => new Filter<Scan>(x => x.Employee.ID).IsEqualTo(App.Data.Me.ID)
  22. .And(x => x.Processed).IsEqualTo(false)
  23. ) { FileName = "scans.data" };
  24. _tags = new ScanTagModel(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. Task[] tasks = new Task[]
  32. {
  33. Task.Run(() => _scans.Refresh(force)),
  34. Task.Run(() => _tags.Refresh(force))
  35. };
  36. if (async)
  37. Task.WhenAll(tasks).ContinueWith((_) => Device.BeginInvokeOnMainThread(RefreshScreen));
  38. else
  39. {
  40. Task.WaitAll(tasks);
  41. RefreshScreen();
  42. }
  43. }
  44. private void RefreshScreen()
  45. {
  46. ProgressVisible = false;
  47. _documents.ItemsSource = null;
  48. _documents.ItemsSource = _scans;
  49. }
  50. private async Task<bool> ConfirmScan(ScanShell shell)
  51. {
  52. shell.EmployeeID = App.Data.Me.ID;
  53. if (!_tags.Any())
  54. return true;
  55. var tag = await DisplayActionSheet("Select Tag", "Cancel", null, _tags.Items.Select(x => x.Name).ToArray());
  56. shell.TagID = _tags.Items.FirstOrDefault(x => String.Equals(x.Name, tag))?.ID ?? Guid.Empty;
  57. return !string.Equals(tag,"Cancel");
  58. }
  59. private async void TakePhoto_Clicked(object sender, EventArgs e)
  60. {
  61. await _documents.AddImage<MobileDocumentCameraSource,ScanShell>(false, ConfirmScan);
  62. }
  63. private async void BrowseLibrary_Clicked(object sender, EventArgs e)
  64. {
  65. await _documents.AddImage<MobileDocumentLibrarySource,ScanShell>(false, ConfirmScan);
  66. }
  67. private void _documents_OnRefreshRequested(object sender, MobileListRefreshEventArgs args)
  68. {
  69. RefreshData(true,false);
  70. }
  71. }
  72. }