ScannerViewModel.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using InABox.Avalonia.Platform.Barcodes;
  4. using Microsoft.Maui.Devices;
  5. using PRS.Avalonia.Modules;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Threading.Tasks;
  9. namespace PRS.Avalonia.Components;
  10. public partial class ScannerViewModel : ModuleViewModel
  11. {
  12. public override string Title => "Equipment Scanner";
  13. [ObservableProperty]
  14. private bool _cameraEnabled;
  15. [ObservableProperty]
  16. private bool _processing;
  17. [ObservableProperty]
  18. private Func<IReadOnlySet<BarcodeResult>, Task<bool>>? _itemScanned;
  19. public ScannerViewModel()
  20. {
  21. }
  22. protected override Task OnActivated()
  23. {
  24. CameraEnabled = true;
  25. return base.OnActivated();
  26. }
  27. protected override Task OnDeactivated()
  28. {
  29. CameraEnabled = false;
  30. return base.OnDeactivated();
  31. }
  32. [RelayCommand]
  33. private async Task DetectionFinished(IReadOnlySet<BarcodeResult> result)
  34. {
  35. if(result.Count > 0)
  36. {
  37. if (Processing || !CameraEnabled) return;
  38. Processing = true;
  39. if(ItemScanned != null)
  40. {
  41. if(await ItemScanned.Invoke(result))
  42. {
  43. CameraEnabled = false;
  44. }
  45. }
  46. Processing = false;
  47. }
  48. }
  49. }