-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Proposal - Replace internal use of MessagingCenter #3880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
de967b9
39867d8
6cb67bb
4a01020
c4e8f49
63a4020
b425af7
ca9f0d7
b5125da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |||||
| using Android.OS; | ||||||
| using Android.Views; | ||||||
| using Android.Views.Animations; | ||||||
| using CommunityToolkit.Mvvm.Messaging; | ||||||
| using Microsoft.Maui.Controls.Internals; | ||||||
| using Microsoft.Maui.Controls.Platform; | ||||||
| using Microsoft.Maui.Graphics; | ||||||
|
|
@@ -27,8 +28,6 @@ public class Platform : BindableObject, IPlatformLayout, INavigation | |||||
| internal static string PackageName { get; private set; } | ||||||
| internal static string GetPackageName() => PackageName; | ||||||
|
|
||||||
| internal const string CloseContextActionsSignalName = "Xamarin.CloseContextActions"; | ||||||
|
|
||||||
| internal static readonly BindableProperty RendererProperty = BindableProperty.CreateAttached("Renderer", typeof(IVisualElementRenderer), typeof(Platform), default(IVisualElementRenderer), | ||||||
| propertyChanged: (bindable, oldvalue, newvalue) => | ||||||
| { | ||||||
|
|
@@ -81,7 +80,7 @@ internal bool NavAnimationInProgress | |||||
| return; | ||||||
| _navAnimationInProgress = value; | ||||||
| if (value) | ||||||
| MessagingCenter.Send(this, CloseContextActionsSignalName); | ||||||
| WeakReferenceMessenger.Default.Send(new CloseContextActionsMessage()); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this could be simplied through the extension:
Suggested change
|
||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||||||||||||||
| using Android.Text; | ||||||||||||||||||
| using Android.Views; | ||||||||||||||||||
| using Android.Widget; | ||||||||||||||||||
| using CommunityToolkit.Mvvm.Messaging; | ||||||||||||||||||
| using Microsoft.Maui.Controls.Internals; | ||||||||||||||||||
| using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity; | ||||||||||||||||||
| using AppCompatAlertDialog = AndroidX.AppCompat.App.AlertDialog; | ||||||||||||||||||
|
|
@@ -50,20 +51,17 @@ internal sealed class PopupRequestHelper : IDisposable | |||||||||||||||||
| internal PopupRequestHelper(Activity context) | ||||||||||||||||||
| { | ||||||||||||||||||
| Activity = context; | ||||||||||||||||||
| MessagingCenter.Subscribe<Page, bool>(Activity, Page.BusySetSignalName, OnPageBusy); | ||||||||||||||||||
| MessagingCenter.Subscribe<Page, AlertArguments>(Activity, Page.AlertSignalName, OnAlertRequested); | ||||||||||||||||||
| MessagingCenter.Subscribe<Page, PromptArguments>(Activity, Page.PromptSignalName, OnPromptRequested); | ||||||||||||||||||
| MessagingCenter.Subscribe<Page, ActionSheetArguments>(Activity, Page.ActionSheetSignalName, OnActionSheetRequested); | ||||||||||||||||||
|
|
||||||||||||||||||
| WeakReferenceMessenger.Default.Register<Activity, PageBusyMessage>(Activity, (r, m) => OnPageBusy(m.Page, m.IsBusy)); | ||||||||||||||||||
| WeakReferenceMessenger.Default.Register<Activity, PageAlertMessage>(Activity, (r, m) => OnAlertRequested(m.Page, m.Arguments)); | ||||||||||||||||||
| WeakReferenceMessenger.Default.Register<Activity, PromptMessage>(Activity, (r, m) => OnPromptRequested(m.Page, m.Arguments)); | ||||||||||||||||||
| WeakReferenceMessenger.Default.Register<Activity, ActionSheetMessage>(Activity, (r, m) => OnActionSheetRequested(m.Page, m.Arguments)); | ||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: if we don't care about the strongly-typed recipient (it's not used in the handler), this can be simplified.
Suggested change
|
||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public Activity Activity { get; } | ||||||||||||||||||
|
|
||||||||||||||||||
| public void Dispose() | ||||||||||||||||||
| { | ||||||||||||||||||
| MessagingCenter.Unsubscribe<Page, bool>(Activity, Page.BusySetSignalName); | ||||||||||||||||||
| MessagingCenter.Unsubscribe<Page, AlertArguments>(Activity, Page.AlertSignalName); | ||||||||||||||||||
| MessagingCenter.Unsubscribe<Page, PromptArguments>(Activity, Page.PromptSignalName); | ||||||||||||||||||
| MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(Activity, Page.ActionSheetSignalName); | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| public void ResetBusyCount() | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |||||
| using Microsoft.Maui.Controls.Platform; | ||||||
| using WVisibility = Microsoft.UI.Xaml.Visibility; | ||||||
| using Microsoft.Extensions.Logging; | ||||||
| using CommunityToolkit.Mvvm.Messaging; | ||||||
|
|
||||||
| namespace Microsoft.Maui.Controls.Compatibility.Platform.UWP | ||||||
| { | ||||||
|
|
@@ -136,10 +137,10 @@ internal Platform(Microsoft.UI.Xaml.Window page) | |||||
|
|
||||||
| _container.SizeChanged += OnRendererSizeChanged; | ||||||
|
|
||||||
| MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) => | ||||||
| WeakReferenceMessenger.Default.Register<UI.Xaml.Window, PageBusyMessage>(page, (window, message) => | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The recipient is not used, we can drop the first type argument:
Suggested change
|
||||||
| { | ||||||
| Microsoft.UI.Xaml.Controls.ProgressBar indicator = GetBusyIndicator(); | ||||||
| indicator.Visibility = enabled ? WVisibility.Visible : WVisibility.Collapsed; | ||||||
| indicator.Visibility = message.IsBusy ? WVisibility.Visible : WVisibility.Collapsed; | ||||||
| }); | ||||||
|
|
||||||
| _toolbarTracker.CollectionChanged += OnToolbarItemsChanged; | ||||||
|
|
@@ -601,13 +602,16 @@ internal IToolbarProvider GetToolbarProvider() | |||||
|
|
||||||
| internal static void SubscribeAlertsAndActionSheets() | ||||||
| { | ||||||
| MessagingCenter.Subscribe<Page, AlertArguments>(Forms.MainWindow, Page.AlertSignalName, OnPageAlert); | ||||||
| MessagingCenter.Subscribe<Page, ActionSheetArguments>(Forms.MainWindow, Page.ActionSheetSignalName, OnPageActionSheet); | ||||||
| MessagingCenter.Subscribe<Page, PromptArguments>(Forms.MainWindow, Page.PromptSignalName, OnPagePrompt); | ||||||
| WeakReferenceMessenger.Default.Register<UI.Xaml.Window, PageAlertMessage>(Forms.MainWindow, OnPageAlert); | ||||||
| WeakReferenceMessenger.Default.Register<UI.Xaml.Window, PromptMessage>(Forms.MainWindow, OnPagePrompt); | ||||||
| WeakReferenceMessenger.Default.Register<UI.Xaml.Window, ActionSheetMessage>(Forms.MainWindow, OnPageActionSheet); | ||||||
| } | ||||||
|
|
||||||
| static void OnPageActionSheet(Page sender, ActionSheetArguments options) | ||||||
| static void OnPageActionSheet(UI.Xaml.Window window, ActionSheetMessage message) | ||||||
| { | ||||||
| var sender = message.Page; | ||||||
| var options = message.Arguments; | ||||||
|
|
||||||
| bool userDidSelect = false; | ||||||
|
|
||||||
| if (options.FlowDirection == FlowDirection.MatchParent) | ||||||
|
|
@@ -654,8 +658,11 @@ static void OnPageActionSheet(Page sender, ActionSheetArguments options) | |||||
| } | ||||||
| } | ||||||
|
|
||||||
| static async void OnPagePrompt(Page sender, PromptArguments options) | ||||||
| static async void OnPagePrompt(UI.Xaml.Window window, PromptMessage message) | ||||||
| { | ||||||
| var sender = message.Page; | ||||||
| var options = message.Arguments; | ||||||
|
|
||||||
| var promptDialog = new PromptDialog | ||||||
| { | ||||||
| Title = options.Title ?? string.Empty, | ||||||
|
|
@@ -693,8 +700,11 @@ static async Task<string> ShowPrompt(PromptDialog prompt) | |||||
| return null; | ||||||
| } | ||||||
|
|
||||||
| static async void OnPageAlert(Page sender, AlertArguments options) | ||||||
| static async void OnPageAlert(UI.Xaml.Window window, PageAlertMessage message) | ||||||
| { | ||||||
| var sender = message.Page; | ||||||
| var options = message.Arguments; | ||||||
|
|
||||||
| string content = options.Message ?? string.Empty; | ||||||
| string title = options.Title ?? string.Empty; | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading.Tasks; | ||
| using CommunityToolkit.Mvvm.Messaging; | ||
| using CoreGraphics; | ||
| using Foundation; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
@@ -655,30 +656,43 @@ internal static string ResolveMsAppDataUri(Uri uri) | |
| internal void SubscribeToAlertsAndActionSheets() | ||
| { | ||
| var busyCount = 0; | ||
| MessagingCenter.Subscribe(this, Page.BusySetSignalName, (Page sender, bool enabled) => | ||
|
|
||
| WeakReferenceMessenger.Default.Register<Platform, PageBusyMessage>(this, (platform, message) => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is introducing a closure to capture |
||
| { | ||
| var sender = message.Page; | ||
| var enabled = message.IsBusy; | ||
|
|
||
| if (!PageIsChildOfPlatform(sender)) | ||
| return; | ||
| busyCount = Math.Max(0, enabled ? busyCount + 1 : busyCount - 1); | ||
| UIApplication.SharedApplication.NetworkActivityIndicatorVisible = busyCount > 0; | ||
| }); | ||
|
|
||
| MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments arguments) => | ||
| WeakReferenceMessenger.Default.Register<Platform, PageAlertMessage>(this, (platform, message) => | ||
| { | ||
| var sender = message.Page; | ||
| var arguments = message.Arguments; | ||
|
|
||
| if (!PageIsChildOfPlatform(sender)) | ||
| return; | ||
| PresentAlert(arguments); | ||
| }); | ||
|
|
||
| MessagingCenter.Subscribe(this, Page.PromptSignalName, (Page sender, PromptArguments arguments) => | ||
| WeakReferenceMessenger.Default.Register<Platform, PromptMessage>(this, (platform, message) => | ||
| { | ||
| var sender = message.Page; | ||
| var arguments = message.Arguments; | ||
|
|
||
| if (!PageIsChildOfPlatform(sender)) | ||
| return; | ||
| PresentPrompt(arguments); | ||
| }); | ||
|
|
||
| MessagingCenter.Subscribe(this, Page.ActionSheetSignalName, (Page sender, ActionSheetArguments arguments) => | ||
| WeakReferenceMessenger.Default.Register<Platform, ActionSheetMessage>(this, (platform, message) => | ||
| { | ||
| var sender = message.Page; | ||
| var arguments = message.Arguments; | ||
|
|
||
| if (!PageIsChildOfPlatform(sender)) | ||
| return; | ||
|
|
||
|
|
@@ -695,10 +709,6 @@ static bool IsModalPresentedFullScreen(Page modal) | |
|
|
||
| internal void UnsubscribeFromAlertsAndActionsSheets() | ||
| { | ||
| MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(this, Page.ActionSheetSignalName); | ||
| MessagingCenter.Unsubscribe<Page, AlertArguments>(this, Page.AlertSignalName); | ||
| MessagingCenter.Unsubscribe<Page, PromptArguments>(this, Page.PromptSignalName); | ||
| MessagingCenter.Unsubscribe<Page, bool>(this, Page.BusySetSignalName); | ||
| } | ||
|
|
||
| internal void MarkForRemoval() | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,12 +17,12 @@ | |||||
| using PointF = CoreGraphics.CGPoint; | ||||||
| using RectangleF = CoreGraphics.CGRect; | ||||||
| using SizeF = CoreGraphics.CGSize; | ||||||
| using CommunityToolkit.Mvvm.Messaging; | ||||||
|
|
||||||
| namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS | ||||||
| { | ||||||
| public class NavigationRenderer : UINavigationController, IVisualElementRenderer, IEffectControlProvider | ||||||
| { | ||||||
| internal const string UpdateToolbarButtons = "Xamarin.UpdateToolbarButtons"; | ||||||
| bool _appeared; | ||||||
| bool _ignorePopCall; | ||||||
| bool _loaded; | ||||||
|
|
@@ -40,7 +40,7 @@ public class NavigationRenderer : UINavigationController, IVisualElementRenderer | |||||
| [Preserve(Conditional = true)] | ||||||
| public NavigationRenderer() : base(typeof(FormsNavigationBar), null) | ||||||
| { | ||||||
| MessagingCenter.Subscribe<IVisualElementRenderer>(this, UpdateToolbarButtons, sender => | ||||||
| WeakReferenceMessenger.Default.Register<UpdateToolBarButtonsMessage>(this, (receiver, message) => | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be ignoring the recipient too and introducing a closure:
Suggested change
Plus changes in the handler to invoke the instance methods on |
||||||
| { | ||||||
| if (!ViewControllers.Any()) | ||||||
| return; | ||||||
|
|
@@ -262,7 +262,7 @@ protected override void Dispose(bool disposing) | |||||
|
|
||||||
| if (disposing) | ||||||
| { | ||||||
| MessagingCenter.Unsubscribe<IVisualElementRenderer>(this, UpdateToolbarButtons); | ||||||
| WeakReferenceMessenger.Default.Unregister<UpdateToolBarButtonsMessage>(this); | ||||||
|
|
||||||
| foreach (var childViewController in ViewControllers) | ||||||
| childViewController.Dispose(); | ||||||
|
|
@@ -1711,4 +1711,6 @@ protected override void Dispose(bool disposing) | |||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| internal sealed class UpdateToolBarButtonsMessage { } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |||||
| using Microsoft.Maui.Graphics; | ||||||
| using ObjCRuntime; | ||||||
| using UIKit; | ||||||
| using CommunityToolkit.Mvvm.Messaging; | ||||||
|
|
||||||
| namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS | ||||||
| { | ||||||
|
|
@@ -391,7 +392,8 @@ public override void WillRotate(UIInterfaceOrientation toInterfaceOrientation, d | |||||
| } | ||||||
|
|
||||||
| FlyoutPage.UpdateFlyoutLayoutBehavior(); | ||||||
| MessagingCenter.Send<IVisualElementRenderer>(this, NavigationRenderer.UpdateToolbarButtons); | ||||||
|
|
||||||
| WeakReferenceMessenger.Default.Send(new UpdateToolBarButtonsMessage()); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this can be simplified:
Suggested change
Same for the other two occurrences below in this file. |
||||||
| } | ||||||
|
|
||||||
| base.WillRotate(toInterfaceOrientation, duration); | ||||||
|
|
@@ -449,7 +451,7 @@ void ClearControllers() | |||||
| void HandleFlyoutPropertyChanged(object sender, PropertyChangedEventArgs e) | ||||||
| { | ||||||
| if (e.PropertyName == Page.IconImageSourceProperty.PropertyName || e.PropertyName == Page.TitleProperty.PropertyName) | ||||||
| MessagingCenter.Send<IVisualElementRenderer>(this, NavigationRenderer.UpdateToolbarButtons); | ||||||
| WeakReferenceMessenger.Default.Send(new UpdateToolBarButtonsMessage()); | ||||||
| } | ||||||
|
|
||||||
| void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) | ||||||
|
|
@@ -470,7 +472,7 @@ void HandlePropertyChanged(object sender, PropertyChangedEventArgs e) | |||||
| else if (e.Is(Microsoft.Maui.Controls.FlyoutPage.FlyoutLayoutBehaviorProperty)) | ||||||
| UpdateFlyoutLayoutBehavior(base.View.Bounds.Size); | ||||||
|
|
||||||
| MessagingCenter.Send<IVisualElementRenderer>(this, NavigationRenderer.UpdateToolbarButtons); | ||||||
| WeakReferenceMessenger.Default.Send(new UpdateToolBarButtonsMessage()); | ||||||
| } | ||||||
|
|
||||||
| public override void ViewWillTransitionToSize(CGSize toSize, IUIViewControllerTransitionCoordinator coordinator) | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.