PM-3350 MAUI Migration Fix iOS Autofill extension

This commit is contained in:
Federico Maccaroni 2023-10-04 12:35:43 -03:00
parent 87eebda55f
commit 90649d1c8b
No known key found for this signature in database
GPG key ID: 5D233F8F2B034536
8 changed files with 77 additions and 88 deletions

View file

@ -10,9 +10,7 @@ namespace Bit.App
#if ANDROID
effects.Add<Effects.FabShadowEffect, Effects.FabShadowPlatformEffect>();
#else
effects.Add<Effects.NoEmojiKeyboardEffect, Bit.iOS.Core.Effects.NoEmojiKeyboardEffect>();
effects.Add<Effects.ScrollEnabledEffect, Effects.ScrollEnabledPlatformEffect>();
effects.Add<Effects.ScrollViewContentInsetAdjustmentBehaviorEffect, Bit.App.Effects.ScrollViewContentInsetAdjustmentBehaviorPlatformEffect>();
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();

View file

@ -12,7 +12,7 @@
BackgroundColor="#22000000"
Padding="0"
IsVisible="False">
<StackLayout
<VerticalStackLayout
x:Name="_accountListContainer"
VerticalOptions="Fill"
HorizontalOptions="FillAndExpand"
@ -58,5 +58,5 @@
<TapGestureRecognizer Tapped="FreeSpaceOverlay_Tapped" />
</BoxView.GestureRecognizers>
</BoxView>
</StackLayout>
</VerticalStackLayout>
</ContentView>

View file

@ -11,10 +11,14 @@ namespace Bit.Core;
public static class MauiProgram
{
public static MauiAppBuilder ConfigureMauiAppBuilder(Action<IEffectsBuilder> customEffectsBuilder, Action<IMauiHandlersCollection> customHandlers)
{
return ConfigureBaseMauiAppBuilder(customEffectsBuilder, customHandlers)
.UseMauiApp<Bit.App.App>();
}
public static MauiAppBuilder ConfigureBaseMauiAppBuilder(Action<IEffectsBuilder> customEffectsBuilder, Action<IMauiHandlersCollection> customHandlers)
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<Bit.App.App>()
.UseMauiCommunityToolkit()
.UseMauiCompatibility()
.UseBarcodeReader()

View file

@ -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<MauiContextSingleton> _instance = new Lazy<MauiContextSingleton>(() => 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<Microsoft.Maui.Controls.Application>();
// 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);
}
}
}

View file

@ -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<IDeviceActionService>("deviceActionService");
var messagingService = ServiceContainer.Resolve<IMessagingService>("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<IAccountsManager>("accountsManager");
_accountsManager.Init(() => appOptions, this);
iOSCoreHelpers.InitApp(this, Bit.Core.Constants.iOSAutoFillClearCiphersCacheKey,
_nfcSession, out _nfcDelegate, out _accountsManager);
}
private void InitAppIfNeeded()

View file

@ -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;
}

View file

@ -0,0 +1,15 @@
namespace Bit.iOS.Core.Utilities
{
public class MauiContextSingleton
{
private static readonly Lazy<MauiContextSingleton> _instance = new Lazy<MauiContextSingleton>(() => new MauiContextSingleton());
public static MauiContextSingleton Instance => _instance.Value;
private MauiContextSingleton() { }
public MauiContext? MauiContext { get; private set; }
public void Init(MauiContext mauiContext) => MauiContext = mauiContext;
}
}

View file

@ -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<Application>();
// 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<App.Effects.NoEmojiKeyboardEffect, Bit.iOS.Core.Effects.NoEmojiKeyboardEffect>();
effects.Add<App.Effects.ScrollEnabledEffect, App.Effects.ScrollEnabledPlatformEffect>();
effects.Add<App.Effects.ScrollViewContentInsetAdjustmentBehaviorEffect, Bit.App.Effects.ScrollViewContentInsetAdjustmentBehaviorPlatformEffect>();
}
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>(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();