From ce9503fa0ca0a130d251051b08e70d33290f74e8 Mon Sep 17 00:00:00 2001 From: Dinis Vieira Date: Sun, 15 Oct 2023 22:06:26 +0100 Subject: [PATCH] PM-3349 PM-3350 Added (migrated) CustomNavigationHandler (which should partially fix the AvatarIcon in the NavBar in iOS) Added (migrated) CustomContentPageHandler (which should mostly place the AvatarIcon in the navBar in the correct place for iOS) Added Task.Delay (workaround) to allow the Avatar to load in iOS on the LoginPage Added workaround for iOS bug with the toolbar size (more info in comment in AvatarImageSource.cs) Went through the AccountViewCell MAUI-Migration comments. (and deleted/added more comments as needed) Migrated some Device calls to DeviceInfo and MainThread Added (migrated) CustomTabbedHandler (for managing the iOS TabBar) --- .../AccountViewCell/AccountViewCell.xaml | 13 +-- src/Core/Controls/AvatarImageSource.cs | 8 ++ src/Core/Pages/Accounts/LockPage.xaml.cs | 22 +++-- .../Handlers/CustomContentPageHandler.cs | 80 +++++++++++++++++ .../Handlers/CustomNavigationHandler.cs | 82 +++++++++++++++++ src/iOS.Core/Handlers/CustomTabbedHandler.cs | 90 +++++++++++++++++++ src/iOS.Core/Utilities/iOSCoreHelpers.cs | 3 + 7 files changed, 274 insertions(+), 24 deletions(-) create mode 100644 src/iOS.Core/Handlers/CustomContentPageHandler.cs create mode 100644 src/iOS.Core/Handlers/CustomNavigationHandler.cs create mode 100644 src/iOS.Core/Handlers/CustomTabbedHandler.cs diff --git a/src/Core/Controls/AccountViewCell/AccountViewCell.xaml b/src/Core/Controls/AccountViewCell/AccountViewCell.xaml index 80fef3615..34ce0ab3c 100644 --- a/src/Core/Controls/AccountViewCell/AccountViewCell.xaml +++ b/src/Core/Controls/AccountViewCell/AccountViewCell.xaml @@ -8,7 +8,7 @@ xmlns:core="clr-namespace:Bit.Core" x:Name="_accountView" x:DataType="controls:AccountViewCellViewModel"> - - - Device.BeginInvokeOnMainThread(async () => await UnlockedAsync()); + _vm.UnlockedAction = () => MainThread.BeginInvokeOnMainThread(async () => await UnlockedAsync()); - // TODO Xamarin.Forms.Device.RuntimePlatform is no longer supported. Use Microsoft.Maui.Devices.DeviceInfo.Platform instead. For more details see https://learn.microsoft.com/en-us/dotnet/maui/migration/forms-projects#device-changes - if (Device.RuntimePlatform == Device.iOS) + if (DeviceInfo.Platform == DevicePlatform.iOS) { ToolbarItems.Add(_moreItem); } @@ -75,7 +70,7 @@ namespace Bit.App.Pages { if (message.Command == Constants.ClearSensitiveFields) { - Device.BeginInvokeOnMainThread(_vm.ResetPinPasswordFields); + MainThread.BeginInvokeOnMainThread(_vm.ResetPinPasswordFields); } }); if (_appeared) @@ -86,6 +81,9 @@ namespace Bit.App.Pages _appeared = true; _mainContent.Content = _mainLayout; + //Workaround: This delay allows the Avatar to correctly load on iOS. The cause of this issue is also likely connected with the race conditions issue when using loading modals in iOS + await Task.Delay(50); + _accountAvatar?.OnAppearing(); _vm.AvatarImageSource = await GetAvatarImageSourceAsync(); @@ -110,7 +108,7 @@ namespace Bit.App.Pages var tasks = Task.Run(async () => { await Task.Delay(500); - Device.BeginInvokeOnMainThread(async () => await _vm.PromptBiometricAsync()); + MainThread.BeginInvokeOnMainThread(async () => await _vm.PromptBiometricAsync()); }); } } @@ -118,7 +116,7 @@ namespace Bit.App.Pages private void PerformFocusSecretEntry(int? cursorPosition) { - Device.BeginInvokeOnMainThread(() => + MainThread.BeginInvokeOnMainThread(() => { SecretEntry.Focus(); if (cursorPosition.HasValue) @@ -153,7 +151,7 @@ namespace Bit.App.Pages var tasks = Task.Run(async () => { await Task.Delay(50); - Device.BeginInvokeOnMainThread(async () => await _vm.SubmitAsync()); + MainThread.BeginInvokeOnMainThread(async () => await _vm.SubmitAsync()); }); } } diff --git a/src/iOS.Core/Handlers/CustomContentPageHandler.cs b/src/iOS.Core/Handlers/CustomContentPageHandler.cs new file mode 100644 index 000000000..f1e5dd962 --- /dev/null +++ b/src/iOS.Core/Handlers/CustomContentPageHandler.cs @@ -0,0 +1,80 @@ +using System.Reflection; +using Foundation; +using Microsoft.Maui.Handlers; +using UIKit; +using ContentView = Microsoft.Maui.Platform.ContentView; + +namespace Bit.iOS.Core.Handlers +{ + public partial class CustomContentPageHandler : PageHandler + { + private Page? _page; + + protected override void ConnectHandler(ContentView platformView) + { + if (VirtualView is Page page) + { + _page = page; + _page.Loaded += Page_Loaded; + } + + base.ConnectHandler(platformView); + } + + private void Page_Loaded(object? sender, EventArgs e) + { + //Workaround: We can't use DisconnectHandler to dispose as we would have to call it manually from "outside" this class. So we unregister the event and set the page to null here. (it's very unlikely it would be called anyway) + if (_page != null) + { + _page.Loaded -= Page_Loaded; + _page = null; + + var navController = ViewController?.NavigationController; + if (navController?.NavigationBar != null) + { + CustomizeNavBar(navController); + } + } + } + + private void CustomizeNavBar(UINavigationController navigationController) + { + // Hide bottom line under nav bar + var navBar = navigationController.NavigationBar; + navBar.SetValueForKey(NSObject.FromObject(true), new Foundation.NSString("hidesShadow")); + + var navigationItem = navigationController.TopViewController.NavigationItem; + var leftNativeButtons = (navigationItem.LeftBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); + var rightNativeButtons = (navigationItem.RightBarButtonItems ?? new UIBarButtonItem[] { }).ToList(); + var newLeftButtons = new List(); + var newRightButtons = new List(); + foreach (var nativeItem in rightNativeButtons) + { + // Use reflection to get Xamarin private field "_item" + var field = nativeItem.GetType().GetField("_item", BindingFlags.NonPublic | BindingFlags.Instance); + if (field == null) + { + return; + } + if (!(field.GetValue(nativeItem) is ToolbarItem info)) + { + return; + } + if (info.Priority < 0) + { + newLeftButtons.Add(nativeItem); + } + else + { + newRightButtons.Add(nativeItem); + } + } + foreach (var nativeItem in leftNativeButtons) + { + newLeftButtons.Add(nativeItem); + } + navigationItem.RightBarButtonItems = newRightButtons.ToArray(); + navigationItem.LeftBarButtonItems = newLeftButtons.ToArray(); + } + } +} \ No newline at end of file diff --git a/src/iOS.Core/Handlers/CustomNavigationHandler.cs b/src/iOS.Core/Handlers/CustomNavigationHandler.cs new file mode 100644 index 000000000..cc2919480 --- /dev/null +++ b/src/iOS.Core/Handlers/CustomNavigationHandler.cs @@ -0,0 +1,82 @@ +using Bit.App.Controls; +using CoreFoundation; +using System.ComponentModel; +using UIKit; + +namespace Bit.iOS.Core.Handlers +{ + //This is a Compatibility verion of the NavigationRenderer. Eventually we should see if there's a better way to implement this behavior. + public class CustomNavigationHandler : Microsoft.Maui.Controls.Handlers.Compatibility.NavigationRenderer + { + public override void PushViewController(UIViewController viewController, bool animated) + { + base.PushViewController(viewController, animated); + + var currentPage = (Element as NavigationPage)?.CurrentPage; + if (currentPage == null) + { + return; + } + var toolbarItems = currentPage.ToolbarItems; + if (!toolbarItems.Any()) + { + return; + } + + // In order to get the correct index we need to do the same as XF and reverse the toolbar items list + // https://github.com/xamarin/Xamarin.Forms/blob/8f765bd87a2968bef9c86122d88c9c47be9196d2/Xamarin.Forms.Platform.iOS/Renderers/NavigationRenderer.cs#L1432 + toolbarItems = toolbarItems.Where(t => t.Order != ToolbarItemOrder.Secondary) + .Reverse() + .ToList(); + + var uiBarButtonItems = TopViewController.NavigationItem.RightBarButtonItems; + if (uiBarButtonItems == null) + { + return; + } + + foreach (ExtendedToolbarItem toolbarItem in toolbarItems.Where(t => t is ExtendedToolbarItem eti + && + eti.UseOriginalImage)) + { + var index = toolbarItems.IndexOf(toolbarItem); + if (index < 0 || index >= uiBarButtonItems.Length) + { + continue; + } + + // HACK: this is awful but I can't find another way to properly prevent memory leaks from + // subscribing on the PropertyChanged event; there are several private places where Xamarin Forms + // disposes objects that are not accessible from here so I think this should cover the (un)subscription + // but we need to remember to call the internal methods of ExtendedToolbarItem on the lifecycle of the Page + toolbarItem.OnAppearingAction = () => toolbarItem.PropertyChanged += ToolbarItem_PropertyChanged; + toolbarItem.OnDisappearingAction = () => toolbarItem.PropertyChanged -= ToolbarItem_PropertyChanged; + + // HACK: XF PimaryToolbarItem is sealed so we can't override it, and also it doesn't provide any + // direct way to replace it with our custom one (we can but we need to rewrite several parts of the NavigationRenderer) + // So I think this is the easiest soolution for now to set UIImageRenderingMode.AlwaysOriginal + // on the toolbar item image + void ToolbarItem_PropertyChanged(object s, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(ExtendedToolbarItem.IconImageSource)) + { + var uiBarButtonItem = uiBarButtonItems[index]; + + DispatchQueue.MainQueue.DispatchAsync(() => + { + try + { + uiBarButtonItem.Image = uiBarButtonItem.Image?.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); + } + catch (ObjectDisposedException) + { + // Do nothing, we can't access the proper place to properly dispose this, so here + // we can just catch and ignore the exception. This should only happen when logging out a user. + } + }); + } + }; + } + } + } +} \ No newline at end of file diff --git a/src/iOS.Core/Handlers/CustomTabbedHandler.cs b/src/iOS.Core/Handlers/CustomTabbedHandler.cs new file mode 100644 index 000000000..aa3a6f4ad --- /dev/null +++ b/src/iOS.Core/Handlers/CustomTabbedHandler.cs @@ -0,0 +1,90 @@ +using Bit.App.Abstractions; +using Bit.App.Pages; +using Bit.App.Utilities; +using Bit.Core.Abstractions; +using Bit.Core.Utilities; +using Bit.iOS.Core.Utilities; +using Microsoft.Maui.Controls.Handlers.Compatibility; +using Microsoft.Maui.Controls.Platform; +using UIKit; + +namespace Bit.iOS.Core.Handlers +{ + public partial class CustomTabbedHandler : TabbedRenderer + { + private IBroadcasterService _broadcasterService; + private UITabBarItem _previousSelectedItem; + + public CustomTabbedHandler() + { + _broadcasterService = ServiceContainer.Resolve("broadcasterService"); + _broadcasterService.Subscribe(nameof(CustomTabbedHandler), (message) => + { + if (message.Command is ThemeManager.UPDATED_THEME_MESSAGE_KEY) + { + MainThread.BeginInvokeOnMainThread(() => + { + iOSCoreHelpers.AppearanceAdjustments(); + UpdateTabBarAppearance(); + }); + } + }); + } + + protected override void OnElementChanged(VisualElementChangedEventArgs e) + { + base.OnElementChanged(e); + TabBar.Translucent = false; + TabBar.Opaque = true; + UpdateTabBarAppearance(); + } + + public override void ViewDidAppear(bool animated) + { + base.ViewDidAppear(animated); + + if(TabBar?.Items != null) + { + if (SelectedIndex < TabBar.Items.Length) + { + _previousSelectedItem = TabBar.Items[SelectedIndex]; + } + } + } + + public override void ItemSelected(UITabBar tabbar, UITabBarItem item) + { + if (_previousSelectedItem == item && Element is TabsPage tabsPage) + { + tabsPage.OnPageReselected(); + } + _previousSelectedItem = item; + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _broadcasterService.Unsubscribe(nameof(CustomTabbedHandler)); + } + base.Dispose(disposing); + } + + private void UpdateTabBarAppearance() + { + // https://developer.apple.com/forums/thread/682420 + var deviceActionService = ServiceContainer.Resolve("deviceActionService"); + if (deviceActionService.SystemMajorVersion() >= 15) + { + var appearance = new UITabBarAppearance(); + appearance.ConfigureWithOpaqueBackground(); + appearance.BackgroundColor = ThemeHelpers.TabBarBackgroundColor; + appearance.StackedLayoutAppearance.Normal.IconColor = ThemeHelpers.TabBarItemColor; + appearance.StackedLayoutAppearance.Normal.TitleTextAttributes = + new UIStringAttributes { ForegroundColor = ThemeHelpers.TabBarItemColor }; + TabBar.StandardAppearance = appearance; + TabBar.ScrollEdgeAppearance = TabBar.StandardAppearance; + } + } + } +} diff --git a/src/iOS.Core/Utilities/iOSCoreHelpers.cs b/src/iOS.Core/Utilities/iOSCoreHelpers.cs index 8567a9452..dcce807c0 100644 --- a/src/iOS.Core/Utilities/iOSCoreHelpers.cs +++ b/src/iOS.Core/Utilities/iOSCoreHelpers.cs @@ -45,6 +45,9 @@ namespace Bit.iOS.Core.Utilities public static void ConfigureMAUIHandlers(IMauiHandlersCollection handlers) { + handlers.AddHandler(typeof(TabsPage), typeof(Handlers.CustomTabbedHandler)); + handlers.AddHandler(typeof(NavigationPage), typeof(Handlers.CustomNavigationHandler)); + handlers.AddHandler(typeof(ContentPage), typeof(Handlers.CustomContentPageHandler)); Handlers.ButtonHandlerMappings.Setup(); Handlers.DatePickerHandlerMappings.Setup(); Handlers.EditorHandlerMappings.Setup();