ScannerPage.xaml.cs 12 KB

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