mirror of
https://github.com/bitwarden/android.git
synced 2024-12-25 18:38:27 +03:00
Improved code for periodic Autofocus on scan for better cancellation and task handlilng (#1764)
This commit is contained in:
parent
9eed421c67
commit
92c40e2984
1 changed files with 19 additions and 20 deletions
|
@ -11,6 +11,9 @@ namespace Bit.App.Pages
|
|||
{
|
||||
private readonly Action<string> _callback;
|
||||
|
||||
private CancellationTokenSource _autofocusCts;
|
||||
private Task _continuousAutofocusTask;
|
||||
|
||||
public ScanPage(Action<string> callback)
|
||||
{
|
||||
_callback = callback;
|
||||
|
@ -27,7 +30,6 @@ namespace Bit.App.Pages
|
|||
ToolbarItems.RemoveAt(0);
|
||||
}
|
||||
}
|
||||
private CancellationTokenSource _autofocusCts;
|
||||
|
||||
protected override void OnAppearing()
|
||||
{
|
||||
|
@ -37,45 +39,42 @@ namespace Bit.App.Pages
|
|||
// 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();
|
||||
_autofocusCts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
|
||||
|
||||
var autofocusCts = _autofocusCts;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromMinutes(3), autofocusCts.Token);
|
||||
autofocusCts.Cancel();
|
||||
});
|
||||
|
||||
Device.StartTimer(TimeSpan.FromSeconds(2), () =>
|
||||
_continuousAutofocusTask = Task.Run(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
if (autofocusCts.IsCancellationRequested)
|
||||
while (!autofocusCts.IsCancellationRequested)
|
||||
{
|
||||
return false;
|
||||
await Task.Delay(TimeSpan.FromSeconds(2), autofocusCts.Token);
|
||||
Device.BeginInvokeOnMainThread(() =>
|
||||
{
|
||||
if (!autofocusCts.IsCancellationRequested)
|
||||
{
|
||||
_zxing.AutoFocus();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_zxing.AutoFocus();
|
||||
return true;
|
||||
}
|
||||
catch (TaskCanceledException) { }
|
||||
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;
|
||||
}
|
||||
});
|
||||
}, autofocusCts.Token);
|
||||
}
|
||||
|
||||
protected override void OnDisappearing()
|
||||
protected override async void OnDisappearing()
|
||||
{
|
||||
_autofocusCts?.Cancel();
|
||||
|
||||
await _continuousAutofocusTask;
|
||||
_zxing.IsScanning = false;
|
||||
|
||||
base.OnDisappearing();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue