From 7f4bbafe3cd5177d91ae31c88c37611559ce2871 Mon Sep 17 00:00:00 2001 From: Federico Maccaroni Date: Thu, 30 Nov 2023 18:23:54 -0300 Subject: [PATCH] PM-3350 iOS applied workaround on the iOS Autofill and Share extension to maui embed the navigation page with its content page in the Window --- src/Core/Pages/Accounts/HomePage.xaml.cs | 14 --- .../CredentialProviderViewController.cs | 94 +++++-------------- src/iOS.Core/Handlers/CustomWindowHandler.cs | 4 +- src/iOS.Core/Utilities/iOSCoreHelpers.cs | 2 +- .../LoadingViewController.cs | 4 + 5 files changed, 33 insertions(+), 85 deletions(-) diff --git a/src/Core/Pages/Accounts/HomePage.xaml.cs b/src/Core/Pages/Accounts/HomePage.xaml.cs index 906dd8cd4..03f8c8da4 100644 --- a/src/Core/Pages/Accounts/HomePage.xaml.cs +++ b/src/Core/Pages/Accounts/HomePage.xaml.cs @@ -79,20 +79,6 @@ namespace Bit.App.Pages } } - //protected override void OnHandlerChanged() - //{ - // base.OnHandlerChanged(); - - // if (!_appOptions.IosExtension) - // { - // return; - // } - - // var window = new Window(); - // window.ToHandler(this.Handler.MauiContext); - // window.Page = this; - //} - protected override void OnNavigatingFrom(NavigatingFromEventArgs args) { base.OnNavigatingFrom(args); diff --git a/src/iOS.Autofill/CredentialProviderViewController.cs b/src/iOS.Autofill/CredentialProviderViewController.cs index ca46b5a24..352762264 100644 --- a/src/iOS.Autofill/CredentialProviderViewController.cs +++ b/src/iOS.Autofill/CredentialProviderViewController.cs @@ -22,30 +22,6 @@ using UIKit; namespace Bit.iOS.Autofill { - public partial class NavigationPageFix : NavigationPage - { - public NavigationPageFix() - { - } - - public NavigationPageFix(Page root) : base(root) - { - } - - protected override void OnHandlerChanged() - { - base.OnHandlerChanged(); - - var window = new Window(this); - - //window.Page = this; - window.ToHandler(this.Handler.MauiContext); - - //window.Page = this; - //window.ToHandler(MauiContextSingleton.Instance.MauiContext); - } - } - public partial class CredentialProviderViewController : ASCredentialProviderViewController, IAccountsManagerHost { private Context _context; @@ -164,10 +140,8 @@ namespace Bit.iOS.Autofill } catch (Exception ex) { - UIPasteboard.General.String = ex.ToString(); - _labelErr.Text = ex.ToString(); - //LoggerHelper.LogEvenIfCantBeResolved(ex); - //throw; + LoggerHelper.LogEvenIfCantBeResolved(ex); + throw; } } @@ -287,6 +261,7 @@ namespace Bit.iOS.Autofill PerformSegue("setupSegue", this); return; } + if (_context.ServiceIdentifiers == null || _context.ServiceIdentifiers.Length == 0) { PerformSegue("loginSearchSegue", this); @@ -432,6 +407,19 @@ namespace Bit.iOS.Autofill } } + private void NavigateToPage(ContentPage page) + { + var navigationPage = new NavigationPage(page); + + var window = new Window(navigationPage); + window.ToHandler(MauiContextSingleton.Instance.MauiContext); + + var uiController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); + uiController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; + + PresentViewController(uiController, true, null); + } + private void LaunchHomePage() { try @@ -450,10 +438,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => CompleteRequest(); } - var navigationPage = new NavigationPageFix(homePage); - var loginController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginController, true, null); + NavigateToPage(homePage); LogoutIfAuthed(); } @@ -476,10 +461,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(environmentPage); - var loginController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginController, true, null); + NavigateToPage(environmentPage); } private void LaunchRegisterFlow() @@ -494,10 +476,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(registerPage); - var loginController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginController, true, null); + NavigateToPage(registerPage); } private void LaunchLoginFlow(string email = null) @@ -517,10 +496,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(loginPage); - var loginController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginController, true, null); + NavigateToPage(loginPage); LogoutIfAuthed(); } @@ -540,10 +516,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(loginWithDevicePage); - var loginController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginController, true, null); + NavigateToPage(loginWithDevicePage); LogoutIfAuthed(); } @@ -564,10 +537,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(loginPage); - var loginController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginController, true, null); + NavigateToPage(loginPage); LogoutIfAuthed(); } @@ -594,10 +564,7 @@ namespace Bit.iOS.Autofill vm.UpdateTempPasswordAction = () => DismissViewController(false, () => LaunchUpdateTempPasswordFlow()); } - var navigationPage = new NavigationPageFix(twoFactorPage); - var twoFactorController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - twoFactorController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(twoFactorController, true, null); + NavigateToPage(twoFactorPage); } private void LaunchSetPasswordFlow() @@ -613,10 +580,7 @@ namespace Bit.iOS.Autofill vm.CloseAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(setPasswordPage); - var setPasswordController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - setPasswordController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(setPasswordController, true, null); + NavigateToPage(setPasswordPage); } private void LaunchUpdateTempPasswordFlow() @@ -631,10 +595,7 @@ namespace Bit.iOS.Autofill vm.LogOutAction = () => DismissViewController(false, () => LaunchHomePage()); } - var navigationPage = new NavigationPageFix(updateTempPasswordPage); - var updateTempPasswordController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - updateTempPasswordController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(updateTempPasswordController, true, null); + NavigateToPage(updateTempPasswordPage); } private void LaunchDeviceApprovalOptionsFlow() @@ -650,10 +611,7 @@ namespace Bit.iOS.Autofill vm.LogInWithDeviceAction = () => DismissViewController(false, () => LaunchLoginWithDevice(AuthRequestType.AuthenticateAndUnlock, vm.Email, true)); } - var navigationPage = new NavigationPageFix(loginApproveDevicePage); - var loginApproveDeviceController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); - loginApproveDeviceController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; - PresentViewController(loginApproveDeviceController, true, null); + NavigateToPage(loginApproveDevicePage); } public Task SetPreviousPageInfoAsync() => Task.CompletedTask; diff --git a/src/iOS.Core/Handlers/CustomWindowHandler.cs b/src/iOS.Core/Handlers/CustomWindowHandler.cs index a6e8ac7f8..6927c7a9b 100644 --- a/src/iOS.Core/Handlers/CustomWindowHandler.cs +++ b/src/iOS.Core/Handlers/CustomWindowHandler.cs @@ -16,8 +16,8 @@ namespace Bit.iOS.Core.Handlers protected override UIWindow CreatePlatformElement() { // Haven't tested - return UIApplication.SharedApplication.Delegate.GetWindow(); - //return Platform.GetCurrentUIViewController().View.Window; + //return UIApplication.SharedApplication.Delegate.GetWindow(); + return Platform.GetCurrentUIViewController().View.Window; } } } diff --git a/src/iOS.Core/Utilities/iOSCoreHelpers.cs b/src/iOS.Core/Utilities/iOSCoreHelpers.cs index 60a46fa30..dd2efa399 100644 --- a/src/iOS.Core/Utilities/iOSCoreHelpers.cs +++ b/src/iOS.Core/Utilities/iOSCoreHelpers.cs @@ -35,7 +35,7 @@ namespace Bit.iOS.Core.Utilities }) .UseMauiEmbedding(); // Register the Window - builder.Services.Add(new ServiceDescriptor(typeof(UIWindow), UIApplication.SharedApplication.KeyWindow)); + builder.Services.Add(new ServiceDescriptor(typeof(UIWindow), _ => UIApplication.SharedApplication.KeyWindow, ServiceLifetime.Singleton)); var mauiApp = builder.Build(); MauiContextSingleton.Instance.Init(new MauiContext(mauiApp.Services)); diff --git a/src/iOS.ShareExtension/LoadingViewController.cs b/src/iOS.ShareExtension/LoadingViewController.cs index 8406ccb6e..9ec181254 100644 --- a/src/iOS.ShareExtension/LoadingViewController.cs +++ b/src/iOS.ShareExtension/LoadingViewController.cs @@ -200,6 +200,10 @@ namespace Bit.iOS.ShareExtension private void NavigateToPage(ContentPage page) { var navigationPage = new NavigationPage(page); + + var window = new Window(navigationPage); + window.ToHandler(MauiContextSingleton.Instance.MauiContext); + _currentModalController = navigationPage.ToUIViewController(MauiContextSingleton.Instance.MauiContext); _currentModalController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen; _presentingOnNavigationPage = true;