using Comal.Classes; using InABox.Clients; using InABox.Core; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Xamarin.Essentials; using Xamarin.Forms; using ZXing; using Xamarin.Forms.PlatformConfiguration.AndroidSpecific; using XF.Material.Forms.UI.Dialogs; using comal.timesheets.QAForms; namespace comal.timesheets { public partial class ScannerPage : ContentPage { Dictionary barcodeSetoutIDs = new Dictionary(); bool cacheLoaded = false; private bool bDocViwerOpen; bool bProcessing = false; public String Result { get; private set; } public delegate bool OnScanEvent(object sender, String barcode); public event OnScanEvent OnScan; public ScannerPage() { InitializeComponent(); LoadCache(); NavigationPage.SetHasBackButton(this, false); var options = new ZXing.Mobile.MobileBarcodeScanningOptions() { PossibleFormats = new List() { ZXing.BarcodeFormat.QR_CODE }, AutoRotate = false, TryInverted = true, TryHarder = true, }; _scanView.Options = options; _scanView.IsAnalyzing = false; _scanView.IsScanning = true; } private void LoadCache() { Task.Run(() => { CoreTable table = new Client().Query ( new Filter(x => x.Barcode).IsNotEqualTo(""). And(x => x.ManufacturingPacketLink.SetoutLink.ID).IsNotEqualTo(Guid.Empty), new Columns(x => x.ManufacturingPacketLink.SetoutLink.ID, x => x.Barcode) ); if (table.Rows.Any()) { foreach (CoreRow row in table.Rows) { List list = row.Values; if (list[0] == null) list[0] = Guid.Empty; if (list[1] == null) list[1] = ""; Guid id = Guid.Parse(list[0].ToString()); string barcode = list[1].ToString(); if (id != Guid.Empty && !string.IsNullOrWhiteSpace(barcode)) { if (!barcodeSetoutIDs.ContainsKey(barcode)) barcodeSetoutIDs.Add(barcode, id); } } } cacheLoaded = true; }); } protected override void OnAppearing() { base.OnAppearing(); _scanView.IsAnalyzing = true; } protected override void OnDisappearing() { _scanView.IsAnalyzing = false; base.OnDisappearing(); } public void _scanView_OnScanResult(ZXing.Result result) { if (bProcessing) return; Device.BeginInvokeOnMainThread(() => { bProcessing = true; Vibration.Vibrate(); bool bOK = true; if (OnScan != null) bOK = OnScan(this, result.Text); if (bOK) { Result = result.Text; ProcessCode(Result); } bProcessing = false; }); } private async void ProcessCode(string code) { if (code.StartsWith("http")) //barcode is a url { if (code.Contains("Equipment")) { if (code.Contains("id")) { List options = new List() { "View Equipment in Timebench", "Open in Web Browser" }; Guid equipmentid = Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36)); Guid kanbanid = Guid.Empty; CoreTable table = new Client().Query( new Filter ( x => x.Equipment.ID).IsEqualTo(equipmentid) .And(x => x.Type.ID).IsEqualTo(Guid.Parse("b8608b60-4afc-4b5b-8d94-e740a0c86f7c"))//PRE-START type .And(x => x.Completed).IsEqualTo(DateTime.MinValue), new Columns(x => x.ID, x => x.Completed) ); if (table.Rows.Any()) { kanbanid = Guid.Parse(table.Rows.FirstOrDefault().Values[0].ToString()); if (kanbanid != Guid.Empty) options.Add("Do Pre-Start"); } string[] array = options.ToArray(); string chosenOption = await DisplayActionSheet("Choose an Option", "Cancel", null, array); switch (chosenOption) { case "View Equipment in Timebench": OpenEquipmentViewer(code); break; case "Open in Web Browser": OpenWebBrowser(code); break; case "Do Pre-Start": DoPrestart(kanbanid); break; case "Cancel": return; default: return; } } } } else if (!cacheLoaded) { DisplayAlert("Alert", "Cache not loaded yet. Please try again in a few seconds", "OK"); bProcessing = false; return; } if (barcodeSetoutIDs.ContainsKey(code)) //barcode is delivery item / setout { var frame_form = new FrameDetailsPage(code); Navigation.PushAsync(frame_form); bProcessing = false; } bProcessing = false; } private async void DoPrestart(Guid kanbanid) { //using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) //{ // KanbanForm form = new KanbanForm(); // CoreTable formTable = new Client().Query(new Filter(x => x.Parent.ID).IsEqualTo(kanbanid)); // if (formTable.Rows.Any()) // { // form = formTable.Rows.FirstOrDefault().ToObject(); // } // if (form.ID == Guid.Empty) // { // DisplayAlert("Error", "No form found for this task - please check the task", "Cancel"); // return; // } // CoreTable table = new Client().Query( // new Filter(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Description).IsEqualTo(form.Form.Description), // new Columns(x => x.Description, x => x.ID, x => x.Code, x => x.Form.AppliesTo, x => x.Form.ID, x => x.Layout), // new SortOrder(x => x.Description) // ); // CoreRow row = table.Rows.FirstOrDefault(); // DigitalFormLayout layout = row.ToObject(); // QAFormHost digitalFormHost = new QAFormHost(layout, form, false); // digitalFormHost.PreStartFormSaved += (d) => // { // Kanban kanban = new Client().Query(new Filter(x => x.ID).IsEqualTo(kanbanid), // new Columns(x => x.ID, x => x.Completed, x => x.Category) // ).Rows.FirstOrDefault().ToObject(); // kanban.Category = "Complete"; // kanban.Completed = DateTime.Now; // new Client().Save(kanban, "Pre Start form completed on Timebench"); // }; // Navigation.PushAsync(digitalFormHost); //} } private async void OpenWebBrowser(string code) { string fullUrl = code; Xamarin.Forms.WebView webView = new Xamarin.Forms.WebView() { Source = fullUrl }; webView.On().EnableZoomControls(true); webView.On().DisplayZoomControls(true); bDocViwerOpen = true; var progress = await MaterialDialog.Instance.LoadingDialogAsync(message: "Connecting to server"); webView.Navigated += (object sender2, WebNavigatedEventArgs e2) => { Device.BeginInvokeOnMainThread(async () => { await progress.DismissAsync(); }); }; _scanView.IsAnalyzing = false; titleLbl.Text = "PRS Web Browser"; Content = webView; } void BackBtn_Clicked(object sender, EventArgs e) { if (bDocViwerOpen) { titleLbl.Text = "Scanner"; bDocViwerOpen = false; Content = scannerGrid; _scanView.IsAnalyzing = true; } else { Navigation.PopAsync(); } } private async void OpenEquipmentViewer(string code) { try { using (await MaterialDialog.Instance.LoadingDialogAsync(message: "Loading")) { CoreTable table = new Client().Query( new Filter(x => x.ID).IsEqualTo(Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36))), new Columns( x => x.ID, //0 x => x.GroupLink.ID, //1 x => x.GroupLink.Description, //2 x => x.GroupLink.Code, //3 x => x.Description, //4 x => x.Notes, //5 x => x.SpecificationSheet.ID //6 )); if (table.Rows.Any()) { List list = table.Rows.FirstOrDefault().Values; if (list[0] == null) list[0] = Guid.Empty; //0 if (list[1] == null) list[1] = Guid.Empty; //1 if (list[2] == null) list[2] = ""; //2 if (list[3] == null) list[3] = ""; //3 if (list[4] == null) list[4] = ""; //4 if (list[5] == null) list[5] = ""; //5 if (list[6] == null) list[6] = Guid.Empty; //6 Equipment equipment = new Equipment { ID = Guid.Parse(list[0].ToString()), Description = list[4].ToString(), Notes = list[5].ToString(), }; equipment.GroupLink.ID = Guid.Parse(list[1].ToString()); equipment.GroupLink.Description = list[2].ToString(); equipment.GroupLink.Code = list[3].ToString(); equipment.SpecificationSheet.ID = Guid.Parse(list[6].ToString()); EquipmentDetailsPage page = new EquipmentDetailsPage(equipment.ID); Navigation.PushAsync(page); } } } catch (Exception e) { string message = e.Message; } } void Cancel_Clicked(System.Object sender, System.EventArgs e) { Result = ""; Navigation.PopModalAsync(); } } }