123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- 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<string, Guid> barcodeSetoutIDs = new Dictionary<string, Guid>();
- 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>() { 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<DeliveryItem>().Query
- (
- new Filter<DeliveryItem>(x => x.Barcode).IsNotEqualTo("").
- And(x => x.ManufacturingPacketLink.SetoutLink.ID).IsNotEqualTo(Guid.Empty),
- new Columns<DeliveryItem>(x => x.ManufacturingPacketLink.SetoutLink.ID, x => x.Barcode)
- );
- if (table.Rows.Any())
- {
- foreach (CoreRow row in table.Rows)
- {
- List<object> 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<string> options = new List<string>() { "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<Kanban>().Query(
- new Filter<Kanban>
- (
- 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<Kanban>(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<KanbanForm>().Query(new Filter<KanbanForm>(x => x.Parent.ID).IsEqualTo(kanbanid));
- // if (formTable.Rows.Any())
- // {
- // form = formTable.Rows.FirstOrDefault().ToObject<KanbanForm>();
- // }
- // if (form.ID == Guid.Empty)
- // {
- // DisplayAlert("Error", "No form found for this task - please check the task", "Cancel");
- // return;
- // }
- // CoreTable table = new Client<DigitalFormLayout>().Query(
- // new Filter<DigitalFormLayout>(x => x.Type).IsEqualTo(DFLayoutType.Mobile).And(x => x.Active).IsEqualTo(true).And(x => x.Form.Description).IsEqualTo(form.Form.Description),
- // new Columns<DigitalFormLayout>(x => x.Description, x => x.ID, x => x.Code, x => x.Form.AppliesTo, x => x.Form.ID, x => x.Layout),
- // new SortOrder<DigitalFormLayout>(x => x.Description)
- // );
- // CoreRow row = table.Rows.FirstOrDefault();
- // DigitalFormLayout layout = row.ToObject<DigitalFormLayout>();
- // QAFormHost digitalFormHost = new QAFormHost(layout, form, false);
- // digitalFormHost.PreStartFormSaved += (d) =>
- // {
- // Kanban kanban = new Client<Kanban>().Query(new Filter<Kanban>(x => x.ID).IsEqualTo(kanbanid),
- // new Columns<Kanban>(x => x.ID, x => x.Completed, x => x.Category)
- // ).Rows.FirstOrDefault().ToObject<Kanban>();
- // kanban.Category = "Complete";
- // kanban.Completed = DateTime.Now;
- // new Client<Kanban>().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<Xamarin.Forms.PlatformConfiguration.Android>().EnableZoomControls(true);
- webView.On<Xamarin.Forms.PlatformConfiguration.Android>().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<Equipment>().Query(
- new Filter<Equipment>(x => x.ID).IsEqualTo(Guid.Parse(code.Substring(code.IndexOf("id") + 3, 36))),
- new Columns<Equipment>(
- 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<object> 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();
- }
- }
- }
|