|
| 1 | +using DIPS.Mobile.UI.Components.BottomSheets.Header; |
| 2 | +using DIPS.Mobile.UI.Resources.LocalizedStrings.LocalizedStrings; |
| 3 | +using Microsoft.Maui.Platform; |
| 4 | +using UIKit; |
| 5 | +using Colors = DIPS.Mobile.UI.Resources.Colors.Colors; |
| 6 | + |
| 7 | +namespace DIPS.Mobile.UI.Components.BottomSheets.iOS; |
| 8 | + |
| 9 | +internal class BottomSheetNavigationBarHelper |
| 10 | +{ |
| 11 | + private readonly BottomSheet m_bottomSheet; |
| 12 | + private readonly UINavigationItem m_navigationItem; |
| 13 | + private readonly WeakReference<UINavigationController?> m_weakNavigationController; |
| 14 | + |
| 15 | + public BottomSheetNavigationBarHelper(BottomSheet bottomSheet, UINavigationItem navigationItem, UINavigationController? navigationController) |
| 16 | + { |
| 17 | + m_bottomSheet = bottomSheet; |
| 18 | + m_navigationItem = navigationItem; |
| 19 | + m_weakNavigationController = new WeakReference<UINavigationController?>(navigationController); |
| 20 | + |
| 21 | + if (bottomSheet.BottomSheetHeaderBehavior is not null) |
| 22 | + { |
| 23 | + bottomSheet.BottomSheetHeaderBehavior.PropertyChanged += OnHeaderBehaviorPropertyChanged; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public void Configure() |
| 28 | + { |
| 29 | + m_navigationItem.Title = m_bottomSheet.Title; |
| 30 | + |
| 31 | + var appearance = new UINavigationBarAppearance(); |
| 32 | + appearance.ConfigureWithOpaqueBackground(); |
| 33 | + appearance.BackgroundColor = Colors.GetColor(ColorName.color_surface_default).ToPlatform(); |
| 34 | + appearance.ShadowColor = UIColor.Clear; |
| 35 | + appearance.TitleTextAttributes = new UIStringAttributes |
| 36 | + { |
| 37 | + ForegroundColor = Colors.GetColor(ColorName.color_text_default).ToPlatform() |
| 38 | + }; |
| 39 | + m_navigationItem.StandardAppearance = appearance; |
| 40 | + m_navigationItem.ScrollEdgeAppearance = appearance; |
| 41 | + |
| 42 | + UpdateBarItems(); |
| 43 | + } |
| 44 | + |
| 45 | + public void UpdateTitle() |
| 46 | + { |
| 47 | + m_navigationItem.Title = m_bottomSheet.Title; |
| 48 | + } |
| 49 | + |
| 50 | + private void OnHeaderBehaviorPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e) |
| 51 | + { |
| 52 | + if (e.PropertyName is nameof(BottomSheetHeaderBehavior.IsBackButtonVisible) |
| 53 | + or nameof(BottomSheetHeaderBehavior.IsCloseButtonVisible) |
| 54 | + or nameof(BottomSheetHeaderBehavior.IsVisible) |
| 55 | + or nameof(BottomSheetHeaderBehavior.TitleAndBackButtonContainerCommand) |
| 56 | + or nameof(BottomSheetHeaderBehavior.CloseButtonCommand)) |
| 57 | + { |
| 58 | + MainThread.BeginInvokeOnMainThread(UpdateBarItems); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private void UpdateBarItems() |
| 63 | + { |
| 64 | + var headerBehavior = m_bottomSheet.BottomSheetHeaderBehavior; |
| 65 | + m_weakNavigationController.TryGetTarget(out var navigationController); |
| 66 | + |
| 67 | + if (headerBehavior is not null && !headerBehavior.IsVisible) |
| 68 | + { |
| 69 | + navigationController?.SetNavigationBarHidden(true, false); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + navigationController?.SetNavigationBarHidden(false, false); |
| 74 | + |
| 75 | + // Close button (right) |
| 76 | + if (headerBehavior?.IsCloseButtonVisible ?? true) |
| 77 | + { |
| 78 | + var closeButton = new UIBarButtonItem( UIBarButtonSystemItem.Close, (_, _) => OnCloseButtonTapped()); |
| 79 | + closeButton.AccessibilityLabel = DUILocalizedStrings.Close; |
| 80 | + m_navigationItem.RightBarButtonItem = closeButton; |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + m_navigationItem.RightBarButtonItem = null; |
| 85 | + } |
| 86 | + |
| 87 | + // Back button (left) |
| 88 | + if (headerBehavior?.IsBackButtonVisible ?? false) |
| 89 | + { |
| 90 | + var backImage = UIImage.GetSystemImage("chevron.left"); |
| 91 | + var backButton = new UIBarButtonItem(backImage, UIBarButtonItemStyle.Plain, (_, _) => |
| 92 | + { |
| 93 | + m_bottomSheet.BottomSheetHeaderBehavior?.TitleAndBackButtonContainerCommand?.Execute(null); |
| 94 | + }); |
| 95 | + backButton.AccessibilityLabel = DUILocalizedStrings.Back; |
| 96 | + m_navigationItem.LeftBarButtonItem = backButton; |
| 97 | + } |
| 98 | + else |
| 99 | + { |
| 100 | + m_navigationItem.LeftBarButtonItem = null; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private void OnCloseButtonTapped() |
| 105 | + { |
| 106 | + if (m_bottomSheet.BottomSheetHeaderBehavior?.CloseButtonCommand is not null) |
| 107 | + { |
| 108 | + // Pass the close action as parameter — the consumer decides if/when to invoke it |
| 109 | + m_bottomSheet.BottomSheetHeaderBehavior.CloseButtonCommand.Execute((Action)(() => m_bottomSheet.Close())); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + m_bottomSheet.Close(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + public void Dispose() |
| 118 | + { |
| 119 | + if (m_bottomSheet.BottomSheetHeaderBehavior is not null) |
| 120 | + { |
| 121 | + m_bottomSheet.BottomSheetHeaderBehavior.PropertyChanged -= OnHeaderBehaviorPropertyChanged; |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments