From 024c26de71ad7f6f3350337d800dc3f673bee078 Mon Sep 17 00:00:00 2001 From: Elie Bariche <33458222+ebariche@users.noreply.github.com> Date: Wed, 18 May 2022 06:54:32 -0400 Subject: [PATCH] fix: Temporarily restore LoadActions --- build/PackageDiffIgnore.xml | 102 ------------------ .../DependencyObjectGenerator.cs | 54 ++++++++++ src/Uno.UI/Controls/StatePresenter.iOS.cs | 24 +++++ 3 files changed, 78 insertions(+), 102 deletions(-) diff --git a/build/PackageDiffIgnore.xml b/build/PackageDiffIgnore.xml index a7c79bc393c7..063de9b9c9db 100644 --- a/build/PackageDiffIgnore.xml +++ b/build/PackageDiffIgnore.xml @@ -8787,108 +8787,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/SourceGenerators/Uno.UI.SourceGenerators/DependencyObject/DependencyObjectGenerator.cs b/src/SourceGenerators/Uno.UI.SourceGenerators/DependencyObject/DependencyObjectGenerator.cs index 8a0f7fd8a754..2db54e020376 100644 --- a/src/SourceGenerators/Uno.UI.SourceGenerators/DependencyObject/DependencyObjectGenerator.cs +++ b/src/SourceGenerators/Uno.UI.SourceGenerators/DependencyObject/DependencyObjectGenerator.cs @@ -325,6 +325,11 @@ private void WriteAndroidAttachedToWindow(INamedTypeSymbol typeSymbol, IndentedS if (isAndroidView || isAndroidActivity || isAndroidFragment) { + if (!isAndroidActivity && !isAndroidFragment) + { + WriteRegisterLoadActions(typeSymbol, builder); + } + builder.AppendLine($@" #if {hasOverridesAttachedToWindowAndroid} //Is Android view (that doesn't already override OnAttachedToWindow) @@ -334,11 +339,15 @@ private void WriteAndroidAttachedToWindow(INamedTypeSymbol typeSymbol, IndentedS protected override void OnNativeLoaded() {{ + _loadActions.ForEach(a => a.Item1()); + BinderAttachedToWindow(); }} protected override void OnNativeUnloaded() {{ + _loadActions.ForEach(a => a.Item2()); + BinderDetachedFromWindow(); }} #else //Not UnoViewGroup @@ -350,6 +359,7 @@ protected override void OnAttachedToWindow() OnLoading(); OnLoaded(); #endif + _loadActions.ForEach(a => a.Item1()); BinderAttachedToWindow(); }} @@ -357,6 +367,7 @@ protected override void OnAttachedToWindow() protected override void OnDetachedFromWindow() {{ base.OnDetachedFromWindow(); + _loadActions.ForEach(a => a.Item2()); #if {implementsIFrameworkElement} //Is IFrameworkElement OnUnloaded(); #endif @@ -394,6 +405,41 @@ private void BinderDetachedFromWindow() } } + private static void WriteRegisterLoadActions(INamedTypeSymbol typeSymbol, IndentedStringBuilder builder) + { + builder.AppendLine($@" + // A list of actions to be executed on Load and Unload + private List<(Action loaded, Action unloaded)> _loadActions = new List<(Action loaded, Action unloaded)>(2); + + /// + /// Registers actions to be executed when the control is Loaded and Unloaded. + /// + /// + /// + /// + /// The loaded action may be executed immediately if the control is already loaded. + public IDisposable RegisterLoadActions(Action loaded, Action unloaded) + {{ + var actions = (loaded, unloaded); + + _loadActions.Add(actions); + +#if __ANDROID__ + if(this.IsLoaded()) +#elif __IOS__ || __MACOS__ + if(Window != null) +#else +#error Unsupported platform +#endif + {{ + loaded(); + }} + + return Disposable.Create(() => _loadActions.Remove(actions)); + }} + "); + } + private void WriteAttachToWindow(INamedTypeSymbol typeSymbol, IndentedStringBuilder builder) { var hasOverridesAttachedToWindowiOS = typeSymbol.Is(_iosViewSymbol) && @@ -405,6 +451,8 @@ private void WriteAttachToWindow(INamedTypeSymbol typeSymbol, IndentedStringBuil if (hasOverridesAttachedToWindowiOS) { + WriteRegisterLoadActions(typeSymbol, builder); + builder.AppendLine($@" public override void MovedToWindow() {{ @@ -412,10 +460,12 @@ public override void MovedToWindow() if(Window != null) {{ + _loadActions.ForEach(a => a.loaded()); OnAttachedToWindowPartial(); }} else {{ + _loadActions.ForEach(a => a.unloaded()); OnDetachedFromWindowPartial(); }} }} @@ -448,6 +498,8 @@ private void WriteViewDidMoveToWindow(INamedTypeSymbol typeSymbol, IndentedStrin if (hasOverridesAttachedToWindowiOS) { + WriteRegisterLoadActions(typeSymbol, builder); + builder.AppendLine($@" public override void ViewDidMoveToWindow() {{ @@ -455,10 +507,12 @@ public override void ViewDidMoveToWindow() if(Window != null) {{ + _loadActions.ForEach(a => a.loaded()); OnAttachedToWindowPartial(); }} else {{ + _loadActions.ForEach(a => a.unloaded()); OnDetachedFromWindowPartial(); }} }} diff --git a/src/Uno.UI/Controls/StatePresenter.iOS.cs b/src/Uno.UI/Controls/StatePresenter.iOS.cs index cce2492503c9..8c641b3c5a4b 100644 --- a/src/Uno.UI/Controls/StatePresenter.iOS.cs +++ b/src/Uno.UI/Controls/StatePresenter.iOS.cs @@ -48,16 +48,40 @@ public StatePresenter() InitializeBinder(); } + #region RegisterLoadActions + + private List<(Action loaded, Action unloaded)> _loadActions = new List<(Action loaded, Action unloaded)>(2); + + public IDisposable RegisterLoadActions(Action loaded, Action unloaded) + { + var actions = (loaded, unloaded); + + _loadActions.Add(actions); + + if(Window != null) + { + loaded(); + } + + return Disposable.Create(() => _loadActions.Remove(actions)); + } + + #endregion + public override void MovedToWindow() { base.MovedToWindow(); if (this.Window != null) { + _loadActions.ForEach(a => a.loaded()); + Loaded?.Invoke(); } else { + _loadActions.ForEach(a => a.unloaded()); + Unloaded?.Invoke(); } }