From 76f10579515b49b5319c1516f1b8dc8ef7be6685 Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Wed, 9 Feb 2022 14:22:04 -0300 Subject: [PATCH] Improved Autofocus code on ScanPage for better cancellation and exception handling #1228 (#1759) --- src/App/Pages/Vault/ScanPage.xaml.cs | 46 ++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/src/App/Pages/Vault/ScanPage.xaml.cs b/src/App/Pages/Vault/ScanPage.xaml.cs index 981bb391c..cb7092a9c 100644 --- a/src/App/Pages/Vault/ScanPage.xaml.cs +++ b/src/App/Pages/Vault/ScanPage.xaml.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AppCenter.Crashes; using Xamarin.Forms; namespace Bit.App.Pages @@ -8,9 +11,6 @@ namespace Bit.App.Pages { private readonly Action _callback; - private DateTime? _timerStarted = null; - private TimeSpan _timerMaxLength = TimeSpan.FromMinutes(3); - public ScanPage(Action callback) { _callback = callback; @@ -27,26 +27,54 @@ namespace Bit.App.Pages ToolbarItems.RemoveAt(0); } } + private CancellationTokenSource _autofocusCts; protected override void OnAppearing() { base.OnAppearing(); _zxing.IsScanning = true; - _timerStarted = DateTime.Now; - Device.StartTimer(new TimeSpan(0, 0, 2), () => + + // Fix for Autofocus, now it's done every 2 seconds so that the user does't have to do it + // https://github.com/Redth/ZXing.Net.Mobile/issues/414 + _autofocusCts?.Cancel(); + _autofocusCts = new CancellationTokenSource(); + + var autofocusCts = _autofocusCts; + + Task.Run(async () => { - if (_timerStarted == null || (DateTime.Now - _timerStarted) > _timerMaxLength) + await Task.Delay(TimeSpan.FromMinutes(3), autofocusCts.Token); + autofocusCts.Cancel(); + }); + + Device.StartTimer(TimeSpan.FromSeconds(2), () => + { + try { + if (autofocusCts.IsCancellationRequested) + { + return false; + } + + _zxing.AutoFocus(); + return true; + } + catch (Exception ex) + { + // we don't need to display anything to the user because at the most they just lose autofocus +#if !FDROID + Crashes.TrackError(ex); +#endif + autofocusCts?.Cancel(); // we also cancel here to cancel the Task.Delay as well. return false; } - _zxing.AutoFocus(); - return true; }); } protected override void OnDisappearing() { - _timerStarted = null; + _autofocusCts?.Cancel(); + _zxing.IsScanning = false; base.OnDisappearing(); }