mirror of
https://github.com/bitwarden/android.git
synced 2024-10-31 15:15:34 +03:00
This commit is contained in:
parent
3491c1aaeb
commit
76f1057951
1 changed files with 37 additions and 9 deletions
|
@ -1,5 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.AppCenter.Crashes;
|
||||||
using Xamarin.Forms;
|
using Xamarin.Forms;
|
||||||
|
|
||||||
namespace Bit.App.Pages
|
namespace Bit.App.Pages
|
||||||
|
@ -8,9 +11,6 @@ namespace Bit.App.Pages
|
||||||
{
|
{
|
||||||
private readonly Action<string> _callback;
|
private readonly Action<string> _callback;
|
||||||
|
|
||||||
private DateTime? _timerStarted = null;
|
|
||||||
private TimeSpan _timerMaxLength = TimeSpan.FromMinutes(3);
|
|
||||||
|
|
||||||
public ScanPage(Action<string> callback)
|
public ScanPage(Action<string> callback)
|
||||||
{
|
{
|
||||||
_callback = callback;
|
_callback = callback;
|
||||||
|
@ -27,26 +27,54 @@ namespace Bit.App.Pages
|
||||||
ToolbarItems.RemoveAt(0);
|
ToolbarItems.RemoveAt(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
private CancellationTokenSource _autofocusCts;
|
||||||
|
|
||||||
protected override void OnAppearing()
|
protected override void OnAppearing()
|
||||||
{
|
{
|
||||||
base.OnAppearing();
|
base.OnAppearing();
|
||||||
_zxing.IsScanning = true;
|
_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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_zxing.AutoFocus();
|
_zxing.AutoFocus();
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnDisappearing()
|
protected override void OnDisappearing()
|
||||||
{
|
{
|
||||||
_timerStarted = null;
|
_autofocusCts?.Cancel();
|
||||||
|
|
||||||
_zxing.IsScanning = false;
|
_zxing.IsScanning = false;
|
||||||
base.OnDisappearing();
|
base.OnDisappearing();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue