From 7b3c187a3955a5afe9c4451f2bfdb1bf70fcd1a8 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 24 Feb 2022 00:23:51 +0100 Subject: [PATCH] Interfaced handlers for Picker and ProgressBar (#4843) * interfaced PickerHandler * interfaced ProgressBarHandler --- .../src/Handlers/Picker/IPickerHandler.cs | 18 +++++++++++ .../Handlers/Picker/PickerHandler.Android.cs | 28 ++++++++--------- .../Handlers/Picker/PickerHandler.Standard.cs | 18 +++++------ .../Handlers/Picker/PickerHandler.Windows.cs | 31 ++++++++++--------- src/Core/src/Handlers/Picker/PickerHandler.cs | 28 ++++++++++++----- .../src/Handlers/Picker/PickerHandler.iOS.cs | 26 ++++++++-------- .../ProgressBar/IProgressBarHandler.cs | 18 +++++++++++ .../ProgressBar/ProgressBarHandler.Android.cs | 4 +-- .../ProgressBarHandler.Standard.cs | 4 +-- .../ProgressBar/ProgressBarHandler.Windows.cs | 9 ++++-- .../ProgressBar/ProgressBarHandler.cs | 26 +++++++++++++--- .../ProgressBar/ProgressBarHandler.iOS.cs | 4 +-- 12 files changed, 142 insertions(+), 72 deletions(-) create mode 100644 src/Core/src/Handlers/Picker/IPickerHandler.cs create mode 100644 src/Core/src/Handlers/ProgressBar/IProgressBarHandler.cs diff --git a/src/Core/src/Handlers/Picker/IPickerHandler.cs b/src/Core/src/Handlers/Picker/IPickerHandler.cs new file mode 100644 index 000000000000..a332d9fd5ada --- /dev/null +++ b/src/Core/src/Handlers/Picker/IPickerHandler.cs @@ -0,0 +1,18 @@ +#if __IOS__ || MACCATALYST +using PlatformView = Microsoft.Maui.Platform.MauiPicker; +#elif MONOANDROID +using PlatformView = Microsoft.Maui.Platform.MauiPicker; +#elif WINDOWS +using PlatformView = Microsoft.Maui.Platform.MauiComboBox; +#elif NETSTANDARD || (NET6_0 && !IOS && !ANDROID) +using PlatformView = System.Object; +#endif + +namespace Microsoft.Maui.Handlers +{ + public partial interface IPickerHandler : IViewHandler + { + new IPicker VirtualView { get; } + new PlatformView PlatformView { get; } + } +} \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.Android.cs b/src/Core/src/Handlers/Picker/PickerHandler.Android.cs index ceb54df6968c..2dbd5aa8d5e0 100644 --- a/src/Core/src/Handlers/Picker/PickerHandler.Android.cs +++ b/src/Core/src/Handlers/Picker/PickerHandler.Android.cs @@ -51,51 +51,51 @@ void SetupDefaults(MauiPicker platformView) } // This is a Android-specific mapping - public static void MapBackground(PickerHandler handler, IPicker picker) + public static void MapBackground(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateBackground(picker, s_defaultBackground); } - public static void MapReload(PickerHandler handler, IPicker picker, object? args) => handler.Reload(); + public static void MapReload(IPickerHandler handler, IPicker picker, object? args) => Reload(handler); - public static void MapTitle(PickerHandler handler, IPicker picker) + public static void MapTitle(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTitle(picker); } - public static void MapTitleColor(PickerHandler handler, IPicker picker) + public static void MapTitleColor(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTitleColor(picker, s_defaultTitleColors); } - public static void MapSelectedIndex(PickerHandler handler, IPicker picker) + public static void MapSelectedIndex(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateSelectedIndex(picker); } - public static void MapCharacterSpacing(PickerHandler handler, IPicker picker) + public static void MapCharacterSpacing(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateCharacterSpacing(picker); } - public static void MapFont(PickerHandler handler, IPicker picker) + public static void MapFont(IPickerHandler handler, IPicker picker) { var fontManager = handler.GetRequiredService(); handler.PlatformView?.UpdateFont(picker, fontManager); } - public static void MapHorizontalTextAlignment(PickerHandler handler, IPicker picker) + public static void MapHorizontalTextAlignment(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateHorizontalAlignment(picker.HorizontalTextAlignment); } - public static void MapTextColor(PickerHandler handler, IPicker picker) + public static void MapTextColor(IPickerHandler handler, IPicker picker) { handler.PlatformView.UpdateTextColor(picker, s_defaultTextColors); } - public static void MapVerticalTextAlignment(PickerHandler handler, IPicker picker) + public static void MapVerticalTextAlignment(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateVerticalAlignment(picker.VerticalTextAlignment); } @@ -168,15 +168,15 @@ void OnClick(object? sender, EventArgs e) void OnRowsCollectionChanged(object? sender, EventArgs e) { - Reload(); + Reload(this); } - void Reload() + static void Reload(IPickerHandler handler) { - if (VirtualView == null || PlatformView == null) + if (handler.VirtualView == null || handler.PlatformView == null) return; - PlatformView.UpdatePicker(VirtualView); + handler.PlatformView.UpdatePicker(handler.VirtualView); } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs b/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs index f512e7de4a8a..9cd9f216e459 100644 --- a/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs +++ b/src/Core/src/Handlers/Picker/PickerHandler.Standard.cs @@ -6,14 +6,14 @@ public partial class PickerHandler : ViewHandler { protected override object CreatePlatformView() => throw new NotImplementedException(); - public static void MapReload(PickerHandler handler, IPicker picker, object? args) { } - public static void MapTitle(PickerHandler handler, IPicker view) { } - public static void MapTitleColor(PickerHandler handler, IPicker view) { } - public static void MapSelectedIndex(PickerHandler handler, IPicker view) { } - public static void MapCharacterSpacing(PickerHandler handler, IPicker view) { } - public static void MapFont(PickerHandler handler, IPicker view) { } - public static void MapTextColor(PickerHandler handler, IPicker view) { } - public static void MapHorizontalTextAlignment(PickerHandler handler, IPicker view) { } - public static void MapVerticalTextAlignment(PickerHandler handler, IPicker view) { } + public static void MapReload(IPickerHandler handler, IPicker picker, object? args) { } + public static void MapTitle(IPickerHandler handler, IPicker view) { } + public static void MapTitleColor(IPickerHandler handler, IPicker view) { } + public static void MapSelectedIndex(IPickerHandler handler, IPicker view) { } + public static void MapCharacterSpacing(IPickerHandler handler, IPicker view) { } + public static void MapFont(IPickerHandler handler, IPicker view) { } + public static void MapTextColor(IPickerHandler handler, IPicker view) { } + public static void MapHorizontalTextAlignment(IPickerHandler handler, IPicker view) { } + public static void MapVerticalTextAlignment(IPickerHandler handler, IPicker view) { } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/Picker/PickerHandler.Windows.cs b/src/Core/src/Handlers/Picker/PickerHandler.Windows.cs index 1c977c6c8049..862a4abdb719 100644 --- a/src/Core/src/Handlers/Picker/PickerHandler.Windows.cs +++ b/src/Core/src/Handlers/Picker/PickerHandler.Windows.cs @@ -43,53 +43,56 @@ void SetupDefaults(MauiComboBox platformView) _defaultForeground = platformView.Foreground; } - void Reload() + static void Reload(IPickerHandler handler) { - if (VirtualView == null || PlatformView == null) + if (handler.VirtualView == null || handler.PlatformView == null) return; - PlatformView.ItemsSource = new ItemDelegateList(VirtualView); + handler.PlatformView.ItemsSource = new ItemDelegateList(handler.VirtualView); } - public static void MapReload(PickerHandler handler, IPicker picker, object? args) => handler.Reload(); + public static void MapReload(IPickerHandler handler, IPicker picker, object? args) => Reload(handler); - public static void MapTitle(PickerHandler handler, IPicker picker) + public static void MapTitle(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTitle(picker); } - public static void MapTitleColor(PickerHandler handler, IPicker picker) + public static void MapTitleColor(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTitle(picker); } - public static void MapSelectedIndex(PickerHandler handler, IPicker picker) + public static void MapSelectedIndex(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateSelectedIndex(picker); } - public static void MapCharacterSpacing(PickerHandler handler, IPicker picker) + public static void MapCharacterSpacing(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateCharacterSpacing(picker); } - public static void MapFont(PickerHandler handler, IPicker picker) + public static void MapFont(IPickerHandler handler, IPicker picker) { var fontManager = handler.GetRequiredService(); handler.PlatformView?.UpdateFont(picker, fontManager); } - public static void MapTextColor(PickerHandler handler, IPicker picker) + public static void MapTextColor(IPickerHandler handler, IPicker picker) { - handler.PlatformView?.UpdateTextColor(picker, handler._defaultForeground); + if (handler is PickerHandler platformHandler) + { + platformHandler.PlatformView?.UpdateTextColor(picker, platformHandler._defaultForeground); + } } - public static void MapHorizontalTextAlignment(PickerHandler handler, IPicker picker) + public static void MapHorizontalTextAlignment(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateHorizontalTextAlignment(picker); } - public static void MapVerticalTextAlignment(PickerHandler handler, IPicker picker) + public static void MapVerticalTextAlignment(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateVerticalTextAlignment(picker); } @@ -102,7 +105,7 @@ void OnControlSelectionChanged(object? sender, WSelectionChangedEventArgs e) void OnRowsCollectionChanged(object? sender, EventArgs e) { - Reload(); + Reload(this); } } } diff --git a/src/Core/src/Handlers/Picker/PickerHandler.cs b/src/Core/src/Handlers/Picker/PickerHandler.cs index d408509ef630..cc84c9bb1404 100644 --- a/src/Core/src/Handlers/Picker/PickerHandler.cs +++ b/src/Core/src/Handlers/Picker/PickerHandler.cs @@ -1,8 +1,18 @@ -namespace Microsoft.Maui.Handlers +#if __IOS__ || MACCATALYST +using PlatformView = Microsoft.Maui.Platform.MauiPicker; +#elif MONOANDROID +using PlatformView = Microsoft.Maui.Platform.MauiPicker; +#elif WINDOWS +using PlatformView = Microsoft.Maui.Platform.MauiComboBox; +#elif NETSTANDARD || (NET6_0 && !IOS && !ANDROID) +using PlatformView = System.Object; +#endif + +namespace Microsoft.Maui.Handlers { - public partial class PickerHandler + public partial class PickerHandler : IPickerHandler { - public static IPropertyMapper PickerMapper = new PropertyMapper(ViewMapper) + public static IPropertyMapper Mapper = new PropertyMapper(ViewMapper) { #if __ANDROID__ [nameof(IPicker.Background)] = MapBackground, @@ -17,7 +27,7 @@ public partial class PickerHandler [nameof(ITextAlignment.VerticalTextAlignment)] = MapVerticalTextAlignment }; - public static CommandMapper PickerCommandMapper = new(ViewCommandMapper) + public static CommandMapper CommandMapper = new(ViewCommandMapper) { ["Reload"] = MapReload }; @@ -25,18 +35,20 @@ public partial class PickerHandler static PickerHandler() { #if __IOS__ - PickerMapper.PrependToMapping(nameof(IView.FlowDirection), (h, __) => h.UpdateValue(nameof(ITextAlignment.HorizontalTextAlignment))); + Mapper.PrependToMapping(nameof(IView.FlowDirection), (h, __) => h.UpdateValue(nameof(ITextAlignment.HorizontalTextAlignment))); #endif } - public PickerHandler() : base(PickerMapper, PickerCommandMapper) + public PickerHandler() : base(Mapper, CommandMapper) { - } public PickerHandler(IPropertyMapper mapper) : base(mapper) { - } + + IPicker IPickerHandler.VirtualView => VirtualView; + + PlatformView IPickerHandler.PlatformView => PlatformView; } } diff --git a/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs b/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs index 1b05ee3d1376..d605fc82fbab 100644 --- a/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs +++ b/src/Core/src/Handlers/Picker/PickerHandler.iOS.cs @@ -87,54 +87,54 @@ protected override void DisconnectHandler(MauiPicker platformView) base.DisconnectHandler(platformView); } - void Reload() + static void Reload(IPickerHandler handler) { - if (VirtualView == null || PlatformView == null) + if (handler.VirtualView == null || handler.PlatformView == null) return; - PlatformView.UpdatePicker(VirtualView); + handler.PlatformView.UpdatePicker(handler.VirtualView); } - public static void MapReload(PickerHandler handler, IPicker picker, object? args) => handler.Reload(); + public static void MapReload(IPickerHandler handler, IPicker picker, object? args) => Reload(handler); - public static void MapTitle(PickerHandler handler, IPicker picker) + public static void MapTitle(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTitle(picker); } - public static void MapTitleColor(PickerHandler handler, IPicker picker) + public static void MapTitleColor(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTitleColor(picker); } - public static void MapSelectedIndex(PickerHandler handler, IPicker picker) + public static void MapSelectedIndex(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateSelectedIndex(picker); } - public static void MapCharacterSpacing(PickerHandler handler, IPicker picker) + public static void MapCharacterSpacing(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateCharacterSpacing(picker); } - public static void MapFont(PickerHandler handler, IPicker picker) + public static void MapFont(IPickerHandler handler, IPicker picker) { var fontManager = handler.GetRequiredService(); handler.PlatformView?.UpdateFont(picker, fontManager); } - public static void MapHorizontalTextAlignment(PickerHandler handler, IPicker picker) + public static void MapHorizontalTextAlignment(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateHorizontalTextAlignment(picker); } - public static void MapTextColor(PickerHandler handler, IPicker picker) + public static void MapTextColor(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateTextColor(picker); } - public static void MapVerticalTextAlignment(PickerHandler handler, IPicker picker) + public static void MapVerticalTextAlignment(IPickerHandler handler, IPicker picker) { handler.PlatformView?.UpdateVerticalTextAlignment(picker); } @@ -177,7 +177,7 @@ void OnEditing(object? sender, EventArgs eventArgs) void OnRowsCollectionChanged(object? sender, EventArgs e) { - Reload(); + Reload(this); } void UpdatePickerFromPickerSource(PickerSource pickerSource) diff --git a/src/Core/src/Handlers/ProgressBar/IProgressBarHandler.cs b/src/Core/src/Handlers/ProgressBar/IProgressBarHandler.cs new file mode 100644 index 000000000000..5819a63a03e3 --- /dev/null +++ b/src/Core/src/Handlers/ProgressBar/IProgressBarHandler.cs @@ -0,0 +1,18 @@ +#if __IOS__ || MACCATALYST +using PlatformView = UIKit.UIProgressView; +#elif MONOANDROID +using PlatformView = Android.Widget.ProgressBar; +#elif WINDOWS +using PlatformView = Microsoft.UI.Xaml.Controls.ProgressBar; +#elif NETSTANDARD || (NET6_0 && !IOS && !ANDROID) +using PlatformView = System.Object; +#endif + +namespace Microsoft.Maui.Handlers +{ + public partial interface IProgressBarHandler : IViewHandler + { + new IProgress VirtualView { get; } + new PlatformView PlatformView { get; } + } +} \ No newline at end of file diff --git a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Android.cs b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Android.cs index 6596d618c30f..900a0e54a290 100644 --- a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Android.cs +++ b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Android.cs @@ -14,12 +14,12 @@ protected override AndroidProgressBar CreatePlatformView() }; } - public static void MapProgress(ProgressBarHandler handler, IProgress progress) + public static void MapProgress(IProgressBarHandler handler, IProgress progress) { handler.PlatformView?.UpdateProgress(progress); } - public static void MapProgressColor(ProgressBarHandler handler, IProgress progress) + public static void MapProgressColor(IProgressBarHandler handler, IProgress progress) { handler.PlatformView?.UpdateProgressColor(progress); } diff --git a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Standard.cs b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Standard.cs index ea2eee431dc7..8d8e7edf4169 100644 --- a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Standard.cs +++ b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Standard.cs @@ -6,7 +6,7 @@ public partial class ProgressBarHandler : ViewHandler { protected override object CreatePlatformView() => throw new NotImplementedException(); - public static void MapProgress(ProgressBarHandler handler, IProgress progress) { } - public static void MapProgressColor(ProgressBarHandler handler, IProgress progress) { } + public static void MapProgress(IProgressBarHandler handler, IProgress progress) { } + public static void MapProgressColor(IProgressBarHandler handler, IProgress progress) { } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Windows.cs b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Windows.cs index e3375cde723d..aea8074dd45b 100644 --- a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Windows.cs +++ b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.Windows.cs @@ -19,14 +19,17 @@ void SetupDefaults(ProgressBar platformView) _foregroundDefault = platformView.GetForegroundCache(); } - public static void MapProgress(ProgressBarHandler handler, IProgress progress) + public static void MapProgress(IProgressBarHandler handler, IProgress progress) { handler.PlatformView?.UpdateProgress(progress); } - public static void MapProgressColor(ProgressBarHandler handler, IProgress progress) + public static void MapProgressColor(IProgressBarHandler handler, IProgress progress) { - handler.PlatformView?.UpdateProgressColor(progress, handler._foregroundDefault); + if (handler is ProgressBarHandler platformHandler) + { + platformHandler.PlatformView?.UpdateProgressColor(progress, platformHandler._foregroundDefault); + } } } } \ No newline at end of file diff --git a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.cs b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.cs index 6ee8b297b1ee..329a9e834742 100644 --- a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.cs +++ b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.cs @@ -1,22 +1,38 @@ #nullable enable +#if __IOS__ || MACCATALYST +using PlatformView = UIKit.UIProgressView; +#elif MONOANDROID +using PlatformView = Android.Widget.ProgressBar; +#elif WINDOWS +using PlatformView = Microsoft.UI.Xaml.Controls.ProgressBar; +#elif NETSTANDARD || (NET6_0 && !IOS && !ANDROID) +using PlatformView = System.Object; +#endif + namespace Microsoft.Maui.Handlers { - public partial class ProgressBarHandler + public partial class ProgressBarHandler : IProgressBarHandler { - public static IPropertyMapper ProgressMapper = new PropertyMapper(ViewHandler.ViewMapper) + public static IPropertyMapper Mapper = new PropertyMapper(ViewHandler.ViewMapper) { [nameof(IProgress.Progress)] = MapProgress, [nameof(IProgress.ProgressColor)] = MapProgressColor }; - public ProgressBarHandler() : base(ProgressMapper) + public static CommandMapper CommandMapper = new(ViewCommandMapper) { + }; + public ProgressBarHandler() : base(Mapper) + { } - public ProgressBarHandler(IPropertyMapper? mapper = null) : base(mapper ?? ProgressMapper) + public ProgressBarHandler(IPropertyMapper? mapper = null) : base(mapper ?? Mapper) { - } + + IProgress IProgressBarHandler.VirtualView => VirtualView; + + PlatformView IProgressBarHandler.PlatformView => PlatformView; } } \ No newline at end of file diff --git a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.iOS.cs b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.iOS.cs index a8c54d83741c..fc30d91f9958 100644 --- a/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.iOS.cs +++ b/src/Core/src/Handlers/ProgressBar/ProgressBarHandler.iOS.cs @@ -10,12 +10,12 @@ protected override UIProgressView CreatePlatformView() return new UIProgressView(UIProgressViewStyle.Default); } - public static void MapProgress(ProgressBarHandler handler, IProgress progress) + public static void MapProgress(IProgressBarHandler handler, IProgress progress) { handler.PlatformView?.UpdateProgress(progress); } - public static void MapProgressColor(ProgressBarHandler handler, IProgress progress) + public static void MapProgressColor(IProgressBarHandler handler, IProgress progress) { handler.PlatformView?.UpdateProgressColor(progress); }