borepin/Borepin/Borepin/PageModel/ScanURNPageModel.cs
2022-04-24 22:45:23 +02:00

173 lines
5.0 KiB
C#

using Borepin.Base;
using Borepin.Service.BFFH;
using FabAccessAPI.Schema;
using Prism.Commands;
using Prism.Navigation;
using Prism.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
using ZXing;
using ZXing.Mobile;
namespace Borepin.PageModel
{
class ScanURNPageModel : ConnectionModelBase
{
#region Contructors
public ScanURNPageModel(INavigationService navigationService, IPageDialogService pageDialogService, IBFFHService bffhService) : base(navigationService, pageDialogService, bffhService)
{
AbortCommand = new DelegateCommand(async () => await AbortCommandExecute().ConfigureAwait(true));
ScannedCommand = new DelegateCommand(async () => await ScannedCommandExecuteAsync().ConfigureAwait(false));
IsVisible = true;
IsScanning = true;
}
#endregion
#region Data
public override async Task LoadData()
{
await Task.CompletedTask.ConfigureAwait(false);
}
#endregion
#region Fields
public MobileBarcodeScanningOptions ScanOptions
{
get
{
return new MobileBarcodeScanningOptions()
{
PossibleFormats = new List<BarcodeFormat>()
{
BarcodeFormat.QR_CODE,
BarcodeFormat.EAN_13
// TODO add more Barcode Formats if needed
}
};
}
//set => SetProperty(ref _ScanOptions, value);
}
private Result _ScanResult;
public Result ScanResult
{
get => _ScanResult;
set => SetProperty(ref _ScanResult, value);
}
private bool _IsScanning;
public bool IsScanning
{
get => _IsScanning;
set => SetProperty(ref _IsScanning, value);
}
private bool _IsVisible;
public bool IsVisible
{
get => _IsVisible;
set => SetProperty(ref _IsVisible, value);
}
#endregion
#region Commands
private ICommand _ScannedCommand;
public ICommand ScannedCommand
{
get => _ScannedCommand;
set => SetProperty(ref _ScannedCommand, value);
}
public async Task ScannedCommandExecuteAsync()
{
IsScanning = false;
// HACK
if(string.Equals(ScanResult.Text, "4014472002512", System.StringComparison.Ordinal))
{
Device.BeginInvokeOnMainThread(async () =>
{
await _PageDialogService.DisplayAlertAsync("YAY", "It's Bionade", "OK").ConfigureAwait(false);
IsScanning = true;
});
return;
}
// END HACK
string id = await QRToID(ScanResult.Text).ConfigureAwait(false);
if (id != null)
{
NavigationParameters parameters = new NavigationParameters
{
{ "id", id },
};
Device.BeginInvokeOnMainThread(async () =>
{
INavigationResult result = await _NavigationService.NavigateAsync("/MainPage/NavigationPage/MachineListPage/MachinePage", parameters).ConfigureAwait(false);
});
}
else
{
Device.BeginInvokeOnMainThread(async () =>
{
await _PageDialogService.DisplayAlertAsync("Alert", "QR Code is invalid", "OK").ConfigureAwait(false);
IsScanning = true;
});
}
}
public async Task<string> QRToID(string value)
{
if (!_BFFHService.IsConnected)
{
IsConnected = false;
return null;
}
MachineSystem machineSystem = (await _BFFHService.GetSession().ConfigureAwait(false)).MachineSystem;
Optional<Machine> optional = await machineSystem.Info.GetMachineURN(value).ConfigureAwait(false);
if (optional.Just == null)
{
return null;
}
return optional.Just.Id;
}
private ICommand _AbortCommand;
public ICommand AbortCommand
{
get => _AbortCommand;
set => SetProperty(ref _AbortCommand, value);
}
public async Task AbortCommandExecute()
{
IsScanning = false;
IsVisible = false;
await _NavigationService.GoBackAsync().ConfigureAwait(false);
}
#endregion
#region INavigationService
public override void OnNavigatedFrom(INavigationParameters parameters)
{
}
public override void OnNavigatedTo(INavigationParameters parameters)
{
}
#endregion
}
}