From 6ffb3136d4dbfaec99b2c21daf6dc4f1be28cb81 Mon Sep 17 00:00:00 2001 From: Matt Portune <59324545+mportune-bw@users.noreply.github.com> Date: Thu, 5 Mar 2020 16:18:04 -0500 Subject: [PATCH] Workaround for older bug in Xamarin.Forms by waiting for app to resume before attempting to set Application.Current.MainPage (#757) --- src/App/App.xaml.cs | 25 +++++++++++++++++++ .../Pages/Accounts/TwoFactorPageViewModel.cs | 1 + 2 files changed, 26 insertions(+) diff --git a/src/App/App.xaml.cs b/src/App/App.xaml.cs index 65abf171b..b5f66da1c 100644 --- a/src/App/App.xaml.cs +++ b/src/App/App.xaml.cs @@ -39,6 +39,8 @@ namespace Bit.App private readonly IDeviceActionService _deviceActionService; private readonly AppOptions _appOptions; + private static bool _isResumed; + public App(AppOptions appOptions) { _appOptions = appOptions ?? new AppOptions(); @@ -150,6 +152,27 @@ namespace Bit.App } }); } + + // Workaround for https://github.com/xamarin/Xamarin.Forms/issues/7478 + // Fixed in last Xamarin.Forms 4.4.0.x - remove this hack after updating + public static void WaitForResume() + { + var checkFrequencyInMillis = 100; + var maxTimeInMillis = 5000; + + var count = 0; + while(!_isResumed) + { + Task.Delay(checkFrequencyInMillis).Wait(); + count += checkFrequencyInMillis; + + // don't let this run forever + if(count >= maxTimeInMillis) + { + break; + } + } + } protected async override void OnStart() { @@ -172,6 +195,7 @@ namespace Bit.App protected async override void OnSleep() { System.Diagnostics.Debug.WriteLine("XF App: OnSleep"); + _isResumed = false; if(Device.RuntimePlatform == Device.Android) { var isLocked = await _lockService.IsLockedAsync(); @@ -187,6 +211,7 @@ namespace Bit.App protected override void OnResume() { System.Diagnostics.Debug.WriteLine("XF App: OnResume"); + _isResumed = true; if(Device.RuntimePlatform == Device.Android) { ResumedAsync(); diff --git a/src/App/Pages/Accounts/TwoFactorPageViewModel.cs b/src/App/Pages/Accounts/TwoFactorPageViewModel.cs index c38351f7e..2e5171d85 100644 --- a/src/App/Pages/Accounts/TwoFactorPageViewModel.cs +++ b/src/App/Pages/Accounts/TwoFactorPageViewModel.cs @@ -141,6 +141,7 @@ namespace Bit.App.Pages page.DuoWebView.RegisterAction(sig => { Token = sig; + App.WaitForResume(); Device.BeginInvokeOnMainThread(async () => await SubmitAsync()); }); break;