BarcodeForm.xaml.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. using Rb.Forms.Barcode.Pcl;
  7. using Xamarin.Forms;
  8. namespace comal.timesheets
  9. {
  10. public partial class BarcodeForm : ContentPage
  11. {
  12. public Guid JobID { get; set; }
  13. public double Latitude { get; set; }
  14. public double Longitude { get; set; }
  15. private DataTable Jobs = null;
  16. public BarcodeForm(DataTable jobs)
  17. {
  18. InitializeComponent();
  19. Jobs = jobs;
  20. MessagingCenter.Subscribe<App>(this, App.MessageOnSleep, disableScanner);
  21. MessagingCenter.Subscribe<App>(this, App.MessageOnResume, enableScanner);
  22. barcodeScanner.BarcodeChanged += animateFlash;
  23. }
  24. protected override void OnAppearing()
  25. {
  26. base.OnAppearing();
  27. enableScanner(this);
  28. }
  29. protected override void OnDisappearing()
  30. {
  31. disableScanner(this);
  32. base.OnDisappearing();
  33. }
  34. //public void DisableScanner()
  35. //{
  36. // disableScanner(null);
  37. //}
  38. private void disableScanner(object sender)
  39. {
  40. barcodeScanner.IsEnabled = false;
  41. }
  42. private void enableScanner(object sender)
  43. {
  44. barcodeScanner.IsEnabled = true;
  45. }
  46. private async Task ProcessBarcode(String barcode)
  47. {
  48. disableScanner(this);
  49. JobID = Guid.Empty;
  50. foreach (DataRow row in Jobs.Rows)
  51. {
  52. Guid jobid = row.Get<Job, Guid>(X => X.ID);
  53. if (jobid.ToString().Equals(barcode))
  54. {
  55. JobID = jobid;
  56. Latitude = row.Get<Job, double>(X => X.SiteAddress.Location.Latitude);
  57. Longitude = row.Get<Job, double>(X => X.SiteAddress.Location.Longitude);
  58. break;
  59. }
  60. }
  61. if (JobID != Guid.Empty)
  62. Navigation.PopAsync();
  63. else
  64. enableScanner(this);
  65. }
  66. private async void animateFlash(object sender, BarcodeEventArgs e)
  67. {
  68. Device.BeginInvokeOnMainThread(async () =>
  69. {
  70. await flash.FadeTo(1, 150, Easing.CubicInOut);
  71. flash.Opacity = 0;
  72. await ProcessBarcode(e.Barcode.Result);
  73. });
  74. }
  75. ~BarcodeForm()
  76. {
  77. disableScanner(this);
  78. MessagingCenter.Unsubscribe<App>(this, App.MessageOnSleep);
  79. MessagingCenter.Unsubscribe<App>(this, App.MessageOnResume);
  80. barcodeScanner.BarcodeChanged -= animateFlash;
  81. }
  82. }
  83. }