mirror of
https://github.com/bitwarden/mobile.git
synced 2024-11-22 11:35:21 +01:00
Improved code for periodic Autofocus on scan for better cancellation and task handlilng (#1764)
This commit is contained in:
parent
9eed421c67
commit
92c40e2984
@ -11,6 +11,9 @@ namespace Bit.App.Pages
|
|||||||
{
|
{
|
||||||
private readonly Action<string> _callback;
|
private readonly Action<string> _callback;
|
||||||
|
|
||||||
|
private CancellationTokenSource _autofocusCts;
|
||||||
|
private Task _continuousAutofocusTask;
|
||||||
|
|
||||||
public ScanPage(Action<string> callback)
|
public ScanPage(Action<string> callback)
|
||||||
{
|
{
|
||||||
_callback = callback;
|
_callback = callback;
|
||||||
@ -27,7 +30,6 @@ namespace Bit.App.Pages
|
|||||||
ToolbarItems.RemoveAt(0);
|
ToolbarItems.RemoveAt(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private CancellationTokenSource _autofocusCts;
|
|
||||||
|
|
||||||
protected override void OnAppearing()
|
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
|
// 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
|
// https://github.com/Redth/ZXing.Net.Mobile/issues/414
|
||||||
_autofocusCts?.Cancel();
|
_autofocusCts?.Cancel();
|
||||||
_autofocusCts = new CancellationTokenSource();
|
_autofocusCts = new CancellationTokenSource(TimeSpan.FromMinutes(3));
|
||||||
|
|
||||||
var autofocusCts = _autofocusCts;
|
var autofocusCts = _autofocusCts;
|
||||||
|
_continuousAutofocusTask = Task.Run(async () =>
|
||||||
Task.Run(async () =>
|
|
||||||
{
|
|
||||||
await Task.Delay(TimeSpan.FromMinutes(3), autofocusCts.Token);
|
|
||||||
autofocusCts.Cancel();
|
|
||||||
});
|
|
||||||
|
|
||||||
Device.StartTimer(TimeSpan.FromSeconds(2), () =>
|
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (autofocusCts.IsCancellationRequested)
|
while (!autofocusCts.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
await Task.Delay(TimeSpan.FromSeconds(2), autofocusCts.Token);
|
||||||
|
Device.BeginInvokeOnMainThread(() =>
|
||||||
|
{
|
||||||
|
if (!autofocusCts.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_zxing.AutoFocus();
|
_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;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (TaskCanceledException) { }
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
#if !FDROID
|
||||||
|
Crashes.TrackError(ex);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}, autofocusCts.Token);
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnDisappearing()
|
protected override async void OnDisappearing()
|
||||||
{
|
{
|
||||||
_autofocusCts?.Cancel();
|
_autofocusCts?.Cancel();
|
||||||
|
|
||||||
|
await _continuousAutofocusTask;
|
||||||
_zxing.IsScanning = false;
|
_zxing.IsScanning = false;
|
||||||
|
|
||||||
base.OnDisappearing();
|
base.OnDisappearing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user