ScannerPage.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using Xamarin.Essentials;
  9. using Xamarin.Forms;
  10. using ZXing;
  11. using Xamarin.Forms.PlatformConfiguration.AndroidSpecific;
  12. using XF.Material.Forms.UI.Dialogs;
  13. using comal.timesheets.QAForms;
  14. namespace comal.timesheets
  15. {
  16. public partial class ScannerPage : ContentPage
  17. {
  18. Dictionary<string, Guid> barcodeSetoutIDs = new Dictionary<string, Guid>();
  19. bool cacheLoaded = false;
  20. private bool bDocViwerOpen;
  21. bool bProcessing = false;
  22. public String Result { get; private set; }
  23. public delegate bool OnScanEvent(object sender, String barcode);
  24. public event OnScanEvent OnScan;
  25. public ScannerPage()
  26. {
  27. InitializeComponent();
  28. //LoadCache();
  29. NavigationPage.SetHasBackButton(this, false);
  30. var options = new ZXing.Mobile.MobileBarcodeScanningOptions()
  31. {
  32. PossibleFormats = new List<ZXing.BarcodeFormat>() { ZXing.BarcodeFormat.QR_CODE },
  33. AutoRotate = false,
  34. TryInverted = true,
  35. TryHarder = true,
  36. };
  37. _scanView.Options = options;
  38. _scanView.IsAnalyzing = false;
  39. _scanView.IsScanning = true;
  40. }
  41. private void LoadCache()
  42. {
  43. Task.Run(() =>
  44. {
  45. CoreTable table = new Client<DeliveryItem>().Query
  46. (
  47. new Filter<DeliveryItem>(x => x.Barcode).IsNotEqualTo("").
  48. And(x => x.ManufacturingPacketLink.SetoutLink.ID).IsNotEqualTo(Guid.Empty)
  49. .And(x => x.Created).IsGreaterThanOrEqualTo(DateTime.Now.AddDays(-200)),
  50. new Columns<DeliveryItem>(x => x.ManufacturingPacketLink.SetoutLink.ID, x => x.Barcode)
  51. );
  52. if (table.Rows.Any())
  53. {
  54. foreach (CoreRow row in table.Rows)
  55. {
  56. Guid id = row.Get<DeliveryItem, Guid>(x => x.ManufacturingPacketLink.SetoutLink.ID);
  57. string barcode = row.Get<DeliveryItem, string>(x => x.Barcode);
  58. if (id != Guid.Empty && !string.IsNullOrWhiteSpace(barcode))
  59. {
  60. if (!barcodeSetoutIDs.ContainsKey(barcode))
  61. barcodeSetoutIDs.Add(barcode, id);
  62. }
  63. }
  64. }
  65. cacheLoaded = true;
  66. });
  67. }
  68. protected override void OnAppearing()
  69. {
  70. base.OnAppearing();
  71. _scanView.IsAnalyzing = true;
  72. }
  73. protected override void OnDisappearing()
  74. {
  75. _scanView.IsAnalyzing = false;
  76. base.OnDisappearing();
  77. }
  78. public void _scanView_OnScanResult(ZXing.Result result)
  79. {
  80. if (bProcessing)
  81. return;
  82. Device.BeginInvokeOnMainThread(() =>
  83. {
  84. bProcessing = true;
  85. Vibration.Vibrate();
  86. bool bOK = true;
  87. if (OnScan != null)
  88. bOK = OnScan(this, result.Text);
  89. if (bOK)
  90. {
  91. Result = result.Text;
  92. ProcessCode(Result);
  93. }
  94. bProcessing = false;
  95. });
  96. }
  97. private async void ProcessCode(string code)
  98. {
  99. if (code.StartsWith("http")) //barcode is a url
  100. {
  101. if (code.Contains("Equipment"))
  102. {
  103. if (code.Contains("id"))
  104. {
  105. List<string> options = new List<string>() { "View Equipment in Timebench", "Open in Web Browser" };
  106. Guid equipmentid = Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36));
  107. Guid kanbanid = Guid.Empty;
  108. CoreTable table = new Client<Kanban>().Query(
  109. new Filter<Kanban>
  110. (
  111. x => x.Equipment.ID).IsEqualTo(equipmentid)
  112. .And(x => x.Type.ID).IsEqualTo(Guid.Parse("b8608b60-4afc-4b5b-8d94-e740a0c86f7c"))//PRE-START type
  113. .And(x => x.Completed).IsEqualTo(DateTime.MinValue),
  114. new Columns<Kanban>(x => x.ID, x => x.Completed)
  115. );
  116. if (table.Rows.Any())
  117. {
  118. kanbanid = Guid.Parse(table.Rows.FirstOrDefault().Values[0].ToString());
  119. if (kanbanid != Guid.Empty)
  120. options.Add("Do Pre-Start");
  121. }
  122. string[] array = options.ToArray();
  123. string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, array);
  124. switch (chosenOption)
  125. {
  126. case "View Equipment in Timebench":
  127. OpenEquipmentViewer(code);
  128. break;
  129. case "Open in Web Browser":
  130. OpenWebBrowser(code);
  131. break;
  132. case "Do Pre-Start":
  133. DoPrestart(kanbanid);
  134. break;
  135. case "Cancel":
  136. return;
  137. default:
  138. return;
  139. }
  140. }
  141. }
  142. }
  143. else if (QueryBarcode(code)) //barcode is delivery item / setout
  144. {
  145. var frame_form = new FrameDetailsPage(code);
  146. Navigation.PushAsync(frame_form);
  147. bProcessing = false;
  148. }
  149. bProcessing = false;
  150. }
  151. private bool QueryBarcode(string code)
  152. {
  153. CoreTable table = new Client<DeliveryItem>().Query(new Filter<DeliveryItem>(x => x.Barcode).IsEqualTo(code)
  154. , new Columns<DeliveryItem>(x => x.ID));
  155. return table.Rows.Any();
  156. }
  157. private async void DoPrestart(Guid kanbanid)
  158. {
  159. //using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  160. //{
  161. // KanbanForm form = new KanbanForm();
  162. // CoreTable formTable = new Client<KanbanForm>().Query(new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanbanid));
  163. // if (formTable.Rows.Any())
  164. // {
  165. // form = formTable.Rows.FirstOrDefault().ToObject<KanbanForm>();
  166. // }
  167. // if (form.ID == Guid.Empty)
  168. // {
  169. // DisplayAlert("Error", "No form found for this task - please check the task", "Cancel");
  170. // return;
  171. // }
  172. // CoreTable table = new Client<DigitalFormLayout>().Query(
  173. // new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Description).IsEqualTo(form.Form.Description),
  174. // new Columns<DigitalFormLayout>(x => x.Description, x => x.ID, x => x.Code, x => x.Form.AppliesTo, x => x.Form.ID, x => x.Layout),
  175. // new SortOrder<DigitalFormLayout>(x => x.Description)
  176. // );
  177. // CoreRow row = table.Rows.FirstOrDefault();
  178. // DigitalFormLayout layout = row.ToObject<DigitalFormLayout>();
  179. // QAFormHost digitalFormHost = new QAFormHost(layout, form, false);
  180. // digitalFormHost.PreStartFormSaved += (d) =>
  181. // {
  182. // Kanban kanban = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanid),
  183. // new Columns<Kanban>(x => x.ID, x => x.Completed, x => x.Category)
  184. // ).Rows.FirstOrDefault().ToObject<Kanban>();
  185. // kanban.Category = "Complete";
  186. // kanban.Completed = DateTime.Now;
  187. // new Client<Kanban>().Save(kanban, "Pre Start form completed on Timebench");
  188. // };
  189. // Navigation.PushAsync(digitalFormHost);
  190. //}
  191. }
  192. private async void OpenWebBrowser(string code)
  193. {
  194. string fullUrl = code;
  195. Xamarin.Forms.WebView webView = new Xamarin.Forms.WebView() { Source = fullUrl };
  196. webView.On<Xamarin.Forms.PlatformConfiguration.Android>().EnableZoomControls(true);
  197. webView.On<Xamarin.Forms.PlatformConfiguration.Android>().DisplayZoomControls(true);
  198. bDocViwerOpen = true;
  199. var progress = await MaterialDialog.Instance.LoadingDialogAsync(message: "Connecting to server");
  200. webView.Navigated += (object sender2, WebNavigatedEventArgs e2) => { Device.BeginInvokeOnMainThread(async () => { await progress.DismissAsync(); }); };
  201. _scanView.IsAnalyzing = false;
  202. titleLbl.Text = "PRS Web Browser";
  203. Content = webView;
  204. }
  205. void BackBtn_Clicked(object sender, EventArgs e)
  206. {
  207. if (bDocViwerOpen)
  208. {
  209. titleLbl.Text = "Scanner";
  210. bDocViwerOpen = false;
  211. Content = scannerGrid;
  212. _scanView.IsAnalyzing = true;
  213. }
  214. else
  215. {
  216. Navigation.PopAsync();
  217. }
  218. }
  219. private async void OpenEquipmentViewer(string code)
  220. {
  221. try
  222. {
  223. using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading"))
  224. {
  225. CoreTable table = new Client<Equipment>().Query(
  226. new Filter<Equipment>(x => x.ID).IsEqualTo(Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36))),
  227. new Columns<Equipment>(
  228. x => x.ID, //0
  229. x => x.GroupLink.ID, //1
  230. x => x.GroupLink.Description, //2
  231. x => x.GroupLink.Code, //3
  232. x => x.Description, //4
  233. x => x.Notes, //5
  234. x => x.SpecificationSheet.ID //6
  235. ));
  236. if (table.Rows.Any())
  237. {
  238. List<object> list = table.Rows.FirstOrDefault().Values;
  239. if (list[0] == null) list[0] = Guid.Empty; //0
  240. if (list[1] == null) list[1] = Guid.Empty; //1
  241. if (list[2] == null) list[2] = ""; //2
  242. if (list[3] == null) list[3] = ""; //3
  243. if (list[4] == null) list[4] = ""; //4
  244. if (list[5] == null) list[5] = ""; //5
  245. if (list[6] == null) list[6] = Guid.Empty; //6
  246. Equipment equipment = new Equipment
  247. {
  248. ID = Guid.Parse(list[0].ToString()),
  249. Description = list[4].ToString(),
  250. Notes = list[5].ToString(),
  251. };
  252. equipment.GroupLink.ID = Guid.Parse(list[1].ToString());
  253. equipment.GroupLink.Description = list[2].ToString();
  254. equipment.GroupLink.Code = list[3].ToString();
  255. equipment.SpecificationSheet.ID = Guid.Parse(list[6].ToString());
  256. EquipmentDetailsPage page = new EquipmentDetailsPage(equipment.ID);
  257. Navigation.PushAsync(page);
  258. }
  259. }
  260. }
  261. catch (Exception e)
  262. {
  263. string message = e.Message;
  264. }
  265. }
  266. void Cancel_Clicked(System.Object sender, System.EventArgs e)
  267. {
  268. Result = "";
  269. Navigation.PopModalAsync();
  270. }
  271. }
  272. }