Quellcode durchsuchen

avalonia: Made ScannerViewModel able to be multifunctional for scannign particular kinds of things.

Kenric Nugteren vor 2 Wochen
Ursprung
Commit
1c5f47fdbf

+ 130 - 0
PRS.Avalonia/PRS.Avalonia/Components/Scanner/ScannerViewModel.cs

@@ -1,14 +1,28 @@
+using Comal.Classes;
 using CommunityToolkit.Mvvm.ComponentModel;
 using CommunityToolkit.Mvvm.Input;
+using InABox.Avalonia;
 using InABox.Avalonia.Platform.Barcodes;
+using InABox.Core;
 using Microsoft.Maui.Devices;
 using PRS.Avalonia.Modules;
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Threading.Tasks;
 
 namespace PRS.Avalonia.Components;
 
+[Flags]
+public enum ScannerMode
+{
+    None = 0,
+    Equipment = 1,
+    Setout = 2,
+    ManufacturingPacket = 4,
+    Product = 8
+}
+
 public partial class ScannerViewModel : ModuleViewModel
 {
     public override string Title => "Equipment Scanner";
@@ -22,6 +36,9 @@ public partial class ScannerViewModel : ModuleViewModel
     [ObservableProperty]
     private Func<IReadOnlySet<BarcodeResult>, Task<bool>>? _itemScanned;
 
+    [ObservableProperty]
+    private ScannerMode _mode;
+
     public ScannerViewModel()
     {
     }
@@ -46,14 +63,127 @@ public partial class ScannerViewModel : ModuleViewModel
             if (Processing || !CameraEnabled) return;
 
             Processing = true;
+            var scanned = false;
             if(ItemScanned != null)
             {
                 if(await ItemScanned.Invoke(result))
                 {
+                    scanned = true;
                     CameraEnabled = false;
                 }
             }
+            if (!scanned && Mode != ScannerMode.None)
+            {
+                Guid? id = null;
+                foreach(var barCode in result)
+                {
+                    if (Guid.TryParse(barCode.RawValue, out var eID))
+                    {
+                        id = eID;
+                        break;
+                    }
+                }
+                if(id != null)
+                {
+                    scanned = (Mode.HasFlag(ScannerMode.Equipment) && await ScanEquipment(id.Value))
+                        || (Mode.HasFlag(ScannerMode.Setout) && await ScanSetout(id.Value))
+                        || (Mode.HasFlag(ScannerMode.ManufacturingPacket) && await ScanManufacturingPacket(id.Value))
+                        || (Mode.HasFlag(ScannerMode.Product) && await ScanProduct(id.Value));
+                }
+                if (!scanned)
+                {
+                    Vibration.Vibrate();
+                    await Task.Delay(200);
+                    Vibration.Vibrate();
+                    await Task.Delay(1000);
+                }
+            }
             Processing = false;
         }
     }
+
+    private async Task<bool> ScanSetout(Guid id)
+    {
+        var model = new DeliveryItemModel(
+            DataAccess,
+            () => new Filter<DeliveryItem>(x => x.ID).IsEqualTo(id),
+            () => DefaultCacheFileName<DeliveryItemShell>());
+        await model.RefreshAsync(true);
+
+        var item = model.FirstOrDefault();
+        if(item is null || item.SetoutID == Guid.Empty)
+        {
+            return false;
+        }
+
+        var setoutModel = new SetoutModel(
+            DataAccess,
+            () => new Filter<Setout>(x => x.ID).IsEqualTo(item.SetoutID),
+            () => DefaultCacheFileName<SetoutShell>());
+        await setoutModel.RefreshAsync(true);
+
+        var setout = setoutModel.FirstOrDefault();
+        if(setout is null)
+        {
+            return false;
+        }
+
+        Vibration.Vibrate();
+        Navigation.Navigate<ManufacturingDesignDetailsViewModel>(model =>
+        {
+            model.Shell = setout;
+        });
+        return true;
+    }
+    private async Task<bool> ScanManufacturingPacket(Guid id)
+    {
+        var model = new ManufacturingPacketModel(
+            DataAccess,
+            () => new Filter<ManufacturingPacket>(x => x.ID).IsEqualTo(id),
+            () => DefaultCacheFileName<ManufacturingPacketShell>());
+        await model.RefreshAsync(true);
+
+        var packet = model.FirstOrDefault();
+        if(packet is null)
+        {
+            return false;
+        }
+        else
+        {
+            Vibration.Vibrate();
+            Navigation.Navigate<ManufacturingPacketDetailsViewModel>(model =>
+            {
+                model.Shell = packet;
+            });
+            return true;
+        }
+    }
+    private async Task<bool> ScanProduct(Guid id)
+    {
+        return false;
+    }
+
+    private async Task<bool> ScanEquipment(Guid id)
+    {
+        var model = new EquipmentModel(
+            DataAccess,
+            () => new Filter<Equipment>(x => x.ID).IsEqualTo(id),
+            () => DefaultCacheFileName<EquipmentShell>());
+        await model.RefreshAsync(true);
+
+        var equipment = model.FirstOrDefault();
+        if(equipment is null)
+        {
+            return false;
+        }
+        else
+        {
+            Vibration.Vibrate();
+            Navigation.Navigate<EquipmentDetailsViewModel>(model =>
+            {
+                model.Shell = equipment;
+            });
+            return true;
+        }
+    }
 }

+ 1 - 45
PRS.Avalonia/PRS.Avalonia/Modules/EquipmentModule/EquipmentModuleViewModel.cs

@@ -48,51 +48,7 @@ public partial class EquipmentModuleViewModel : ModuleViewModel
     {
         Navigation.Navigate<ScannerViewModel>(model =>
         {
-            model.ItemScanned = async (results) =>
-            {
-                Guid? equipmentID = null;
-                foreach(var barCode in results)
-                {
-                    if (Guid.TryParse(barCode.RawValue, out var eID))
-                    {
-                        equipmentID = eID;
-                        break;
-                    }
-                }
-                if(equipmentID is null)
-                {
-                    Vibration.Vibrate();
-                    await Task.Delay(200);
-                    Vibration.Vibrate();
-                    await Task.Delay(1000);
-                    //await MessageDialog.ShowMessage("Invalid Code");
-                    return false;
-                }
-                else
-                {
-                    Vibration.Vibrate();
-                    var model = new EquipmentModel(
-                        DataAccess,
-                        () => new Filter<Equipment>(x => x.ID).IsEqualTo(equipmentID),
-                        () => DefaultCacheFileName<EquipmentShell>());
-                    await model.RefreshAsync(true);
-
-                    var equipment = model.FirstOrDefault();
-                    if(equipment is null)
-                    {
-                        // await MessageDialog.ShowMessage("Cannot load equipment!");
-                        return false;
-                    }
-                    else
-                    {
-                        Navigation.Navigate<EquipmentDetailsViewModel>(model =>
-                        {
-                            model.Shell = equipment;
-                        });
-                        return true;
-                    }
-                }
-            };
+            model.Mode = ScannerMode.Equipment;
         });
     }
 }