diff --git a/src/App/MauiProgram.cs b/src/App/MauiProgram.cs index 708884c73..e66ce346c 100644 --- a/src/App/MauiProgram.cs +++ b/src/App/MauiProgram.cs @@ -10,9 +10,7 @@ namespace Bit.App #if ANDROID effects.Add(); #else - effects.Add(); - effects.Add(); - effects.Add(); + iOS.Core.Utilities.iOSCoreHelpers.ConfigureMAUIEffects(effects); #endif }, handlers => @@ -32,15 +30,7 @@ namespace Bit.App handlers.AddHandler(typeof(TabbedPage), typeof(Bit.App.Handlers.CustomTabbedPageHandler)); #else - iOS.Core.Handlers.ButtonHandlerMappings.Setup(); - iOS.Core.Handlers.DatePickerHandlerMappings.Setup(); - iOS.Core.Handlers.EditorHandlerMappings.Setup(); - iOS.Core.Handlers.EntryHandlerMappings.Setup(); - //iOS.Core.Handlers.LabelHandlerMappings.Setup(); - iOS.Core.Handlers.PickerHandlerMappings.Setup(); - iOS.Core.Handlers.SearchBarHandlerMappings.Setup(); - iOS.Core.Handlers.StepperHandlerMappings.Setup(); - iOS.Core.Handlers.TimePickerHandlerMappings.Setup(); + iOS.Core.Utilities.iOSCoreHelpers.ConfigureMAUIHandlers(handlers); #endif } ).Build(); diff --git a/src/Core/Controls/AccountSwitchingOverlay/AccountSwitchingOverlayView.xaml b/src/Core/Controls/AccountSwitchingOverlay/AccountSwitchingOverlayView.xaml index 8dbdced3a..926fc876e 100644 --- a/src/Core/Controls/AccountSwitchingOverlay/AccountSwitchingOverlayView.xaml +++ b/src/Core/Controls/AccountSwitchingOverlay/AccountSwitchingOverlayView.xaml @@ -12,7 +12,7 @@ BackgroundColor="#22000000" Padding="0" IsVisible="False"> - - + diff --git a/src/Core/MauiProgram.cs b/src/Core/MauiProgram.cs index ac52f30b8..161af22b8 100644 --- a/src/Core/MauiProgram.cs +++ b/src/Core/MauiProgram.cs @@ -11,10 +11,14 @@ namespace Bit.Core; public static class MauiProgram { public static MauiAppBuilder ConfigureMauiAppBuilder(Action customEffectsBuilder, Action customHandlers) + { + return ConfigureBaseMauiAppBuilder(customEffectsBuilder, customHandlers) + .UseMauiApp(); + } + public static MauiAppBuilder ConfigureBaseMauiAppBuilder(Action customEffectsBuilder, Action customHandlers) { var builder = MauiApp.CreateBuilder(); builder - .UseMauiApp() .UseMauiCommunityToolkit() .UseMauiCompatibility() .UseBarcodeReader() diff --git a/src/iOS.Autofill/AppDelegate.cs b/src/iOS.Autofill/AppDelegate.cs index 8fbdf9fe6..e27c57ae6 100644 --- a/src/iOS.Autofill/AppDelegate.cs +++ b/src/iOS.Autofill/AppDelegate.cs @@ -1,23 +1,8 @@ -using System; -using Foundation; -using Microsoft.Maui; -using Microsoft.Maui.Embedding; -using Microsoft.Maui.Hosting; +using Foundation; using UIKit; namespace Bit.iOS.Autofill { - public class MauiContextSingleton - { - private static Lazy _instance = new Lazy(() => new MauiContextSingleton()); - - private MauiContextSingleton() { } - - public static MauiContextSingleton Instance = _instance.Value; - - public MauiContext MauiContext { get; set; } - } - [Register("AppDelegate")] public partial class AppDelegate : UIApplicationDelegate { @@ -41,17 +26,5 @@ namespace Bit.iOS.Autofill public override void WillTerminate(UIApplication application) { } - - public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) - { - var builder = MauiApp.CreateBuilder(); - builder.UseMauiEmbedding(); - // Register the Window - builder.Services.Add(new Microsoft.Extensions.DependencyInjection.ServiceDescriptor(typeof(UIWindow), Window)); - var mauiApp = builder.Build(); - MauiContextSingleton.Instance.MauiContext = new MauiContext(mauiApp.Services); - - return base.FinishedLaunching(application, launchOptions); - } } } diff --git a/src/iOS.Autofill/CredentialProviderViewController.cs b/src/iOS.Autofill/CredentialProviderViewController.cs index d40127d7c..a0cbd8711 100644 --- a/src/iOS.Autofill/CredentialProviderViewController.cs +++ b/src/iOS.Autofill/CredentialProviderViewController.cs @@ -17,7 +17,6 @@ using CoreFoundation; using CoreNFC; using Foundation; using Microsoft.Maui.Controls; -using Microsoft.Maui.Controls.Compatibility; using Microsoft.Maui.Platform; using UIKit; @@ -64,8 +63,6 @@ namespace Bit.iOS.Autofill { try { - Console.WriteLine("[Bitwarden] PrepareCredentialList"); - InitAppIfNeeded(); _context.ServiceIdentifiers = serviceIdentifiers; if (serviceIdentifiers.Length > 0) @@ -354,7 +351,7 @@ namespace Bit.iOS.Autofill { if (await IsLocked() || await _stateService.Value.GetPasswordRepromptAutofillAsync()) { - DispatchQueue.MainQueue.DispatchAsync(() => PerformSegue("lockPasswordSegue", this)); + DispatchQueue.MainQueue.DispatchAsync(() => PerformSegue("lockPasswordSegue", this)); } else { @@ -397,28 +394,8 @@ namespace Bit.iOS.Autofill private void InitApp() { - if (ServiceContainer.RegisteredServices.Count > 0) - { - ServiceContainer.Reset(); - } - iOSCoreHelpers.RegisterLocalServices(); - var deviceActionService = ServiceContainer.Resolve("deviceActionService"); - var messagingService = ServiceContainer.Resolve("messagingService"); - ServiceContainer.Init(deviceActionService.DeviceUserAgent, - Bit.Core.Constants.iOSAutoFillClearCiphersCacheKey, Bit.Core.Constants.iOSAllClearCipherCacheKeys); - iOSCoreHelpers.InitLogger(); - iOSCoreHelpers.RegisterFinallyBeforeBootstrap(); - iOSCoreHelpers.Bootstrap(); - var appOptions = new AppOptions { IosExtension = true }; - var app = new App.App(appOptions); - ThemeManager.SetTheme(app.Resources); - iOSCoreHelpers.AppearanceAdjustments(); - _nfcDelegate = new Core.NFCReaderDelegate((success, message) => - messagingService.Send("gotYubiKeyOTP", message)); - iOSCoreHelpers.SubscribeBroadcastReceiver(this, _nfcSession, _nfcDelegate); - - _accountsManager = ServiceContainer.Resolve("accountsManager"); - _accountsManager.Init(() => appOptions, this); + iOSCoreHelpers.InitApp(this, Bit.Core.Constants.iOSAutoFillClearCiphersCacheKey, + _nfcSession, out _nfcDelegate, out _accountsManager); } private void InitAppIfNeeded() diff --git a/src/iOS.Core/Utilities/AccountSwitchingOverlayHelper.cs b/src/iOS.Core/Utilities/AccountSwitchingOverlayHelper.cs index b688c7fa4..e4dafacdc 100644 --- a/src/iOS.Core/Utilities/AccountSwitchingOverlayHelper.cs +++ b/src/iOS.Core/Utilities/AccountSwitchingOverlayHelper.cs @@ -1,8 +1,7 @@ -using System; -using System.Threading.Tasks; -using Bit.App.Controls; +using Bit.App.Controls; using Bit.Core.Abstractions; using Bit.Core.Utilities; +using Microsoft.Maui.Platform; using UIKit; namespace Bit.iOS.Core.Utilities @@ -72,17 +71,18 @@ namespace Bit.iOS.Core.Utilities //renderer.SetElementSize(new Size(containerView.Frame.Size.Width, containerView.Frame.Size.Height)); //var view = renderer.NativeView; - //view.TranslatesAutoresizingMaskIntoConstraints = false; + var view = overlay.ToPlatform(MauiContextSingleton.Instance.MauiContext); + view.TranslatesAutoresizingMaskIntoConstraints = false; - //containerView.AddSubview(view); - //containerView.AddConstraints(new NSLayoutConstraint[] - //{ - // NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, view, NSLayoutAttribute.Trailing, 1f, 0f), - // NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, view, NSLayoutAttribute.Leading, 1f, 0f), - // NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, view, NSLayoutAttribute.Top, 1f, 0f), - // NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, view, NSLayoutAttribute.Bottom, 1f, 0f) - //}); - //containerView.Hidden = true; + containerView.AddSubview(view); + containerView.AddConstraints(new NSLayoutConstraint[] + { + NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Trailing, NSLayoutRelation.Equal, view, NSLayoutAttribute.Trailing, 1f, 0f), + NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Leading, NSLayoutRelation.Equal, view, NSLayoutAttribute.Leading, 1f, 0f), + NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Top, NSLayoutRelation.Equal, view, NSLayoutAttribute.Top, 1f, 0f), + NSLayoutConstraint.Create(containerView, NSLayoutAttribute.Bottom, NSLayoutRelation.Equal, view, NSLayoutAttribute.Bottom, 1f, 0f) + }); + containerView.Hidden = true; return overlay; } diff --git a/src/iOS.Core/Utilities/MauiContextSingleton.cs b/src/iOS.Core/Utilities/MauiContextSingleton.cs new file mode 100644 index 000000000..6930d30a8 --- /dev/null +++ b/src/iOS.Core/Utilities/MauiContextSingleton.cs @@ -0,0 +1,15 @@ +namespace Bit.iOS.Core.Utilities +{ + public class MauiContextSingleton + { + private static readonly Lazy _instance = new Lazy(() => new MauiContextSingleton()); + + public static MauiContextSingleton Instance => _instance.Value; + + private MauiContextSingleton() { } + + public MauiContext? MauiContext { get; private set; } + + public void Init(MauiContext mauiContext) => MauiContext = mauiContext; + } +} diff --git a/src/iOS.Core/Utilities/iOSCoreHelpers.cs b/src/iOS.Core/Utilities/iOSCoreHelpers.cs index b561b4d73..0596b4c30 100644 --- a/src/iOS.Core/Utilities/iOSCoreHelpers.cs +++ b/src/iOS.Core/Utilities/iOSCoreHelpers.cs @@ -1,21 +1,18 @@ -using System; -using System.IO; -using System.Threading.Tasks; -using Bit.App.Abstractions; +using Bit.App.Abstractions; using Bit.App.Controls; using Bit.App.Models; using Bit.App.Pages; -using Bit.Core.Resources.Localization; using Bit.App.Services; using Bit.App.Utilities; using Bit.App.Utilities.AccountManagement; using Bit.Core.Abstractions; -using Bit.Core.Enums; +using Bit.Core.Resources.Localization; using Bit.Core.Services; using Bit.Core.Utilities; using Bit.iOS.Core.Services; using CoreNFC; using Foundation; +using Microsoft.Maui.Embedding; using UIKit; namespace Bit.iOS.Core.Utilities @@ -28,6 +25,37 @@ namespace Bit.iOS.Core.Utilities public static string AppGroupId = "group.com.8bit.bitwarden"; public static string AccessGroup = "LTZ2PFU5D6.com.8bit.bitwarden"; + public static void SetupMaui() + { + var builder = Bit.Core.MauiProgram.ConfigureBaseMauiAppBuilder(ConfigureMAUIEffects, ConfigureMAUIHandlers) + .UseMauiEmbedding(); + // Register the Window + builder.Services.Add(new ServiceDescriptor(typeof(UIWindow), UIApplication.SharedApplication.KeyWindow)); + var mauiApp = builder.Build(); + + MauiContextSingleton.Instance.Init(new MauiContext(mauiApp.Services)); + } + + public static void ConfigureMAUIEffects(IEffectsBuilder effects) + { + effects.Add(); + effects.Add(); + effects.Add(); + } + + public static void ConfigureMAUIHandlers(IMauiHandlersCollection handlers) + { + Handlers.ButtonHandlerMappings.Setup(); + Handlers.DatePickerHandlerMappings.Setup(); + Handlers.EditorHandlerMappings.Setup(); + Handlers.EntryHandlerMappings.Setup(); + //Handlers.LabelHandlerMappings.Setup(); + Handlers.PickerHandlerMappings.Setup(); + Handlers.SearchBarHandlerMappings.Setup(); + Handlers.StepperHandlerMappings.Setup(); + Handlers.TimePickerHandlerMappings.Setup(); + } + public static void InitApp(T rootController, string clearCipherCacheKey, NFCNdefReaderSession nfcSession, @@ -35,6 +63,8 @@ namespace Bit.iOS.Core.Utilities out IAccountsManager accountsManager) where T : UIViewController, IAccountsManagerHost { + SetupMaui(); + if (ServiceContainer.RegisteredServices.Count > 0) { ServiceContainer.Reset();