From 2d0dfbf756fb1bbb962b92b1ee123e2251d48690 Mon Sep 17 00:00:00 2001 From: Mike Rowley Date: Fri, 25 Mar 2022 11:44:37 -0600 Subject: [PATCH 1/8] Fix for Bug #934 (IconTintColorEffect does not work when the image is in Shared project as EmbeddeResource) --- .../IconTintColorEffectRouter.ios.cs | 61 ++++++++++++++----- 1 file changed, 46 insertions(+), 15 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 18d1bd516..29a02715d 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using System.Threading.Tasks; using UIKit; using Xamarin.CommunityToolkit.Effects; using Xamarin.Forms; @@ -11,6 +12,8 @@ namespace Xamarin.CommunityToolkit.iOS.Effects { public class IconTintColorEffectRouter : PlatformEffect { + private int _retryCount = 0; + protected override void OnAttached() => ApplyTintColor(); @@ -22,8 +25,8 @@ protected override void OnElementPropertyChanged(PropertyChangedEventArgs args) base.OnElementPropertyChanged(args); if (!args.PropertyName.Equals(IconTintColorEffect.TintColorProperty.PropertyName) && - !args.PropertyName.Equals(Image.SourceProperty.PropertyName) && - !args.PropertyName.Equals(ImageButton.SourceProperty.PropertyName)) + !args.PropertyName.Equals(Image.SourceProperty.PropertyName) && + !args.PropertyName.Equals(ImageButton.SourceProperty.PropertyName)) return; ApplyTintColor(); @@ -61,31 +64,59 @@ void ClearTintColor() var originalImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); button.SetImage(originalImage, UIControlState.Normal); } + break; } } - void SetUIImageViewTintColor(UIImageView imageView, Color color) + async void SetUIImageViewTintColor(UIImageView imageView, Color color) { if (imageView.Image == null) - return; - - imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); - imageView.TintColor = color.ToUIColor(); + { + if (_retryCount < 4) + { + _retryCount++; + await Task.Delay(100); + SetUIImageViewTintColor(imageView, color); + } + else + { + return; + } + } + else + { + imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); + imageView.TintColor = color.ToUIColor(); + _retryCount = 0; + } } - void SetUIButtonTintColor(UIButton button, Color color) + async void SetUIButtonTintColor(UIButton button, Color color) { if (button.CurrentImage == null) - return; - - var templatedImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); + { + if (_retryCount < 4) + { + _retryCount++; + await Task.Delay(100); + SetUIButtonTintColor(button, color); + } + else + { + return; + } + } + else + { + var templatedImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); - button.SetImage(null, UIControlState.Normal); + button.SetImage(null, UIControlState.Normal); - button.TintColor = color.ToUIColor(); - button.ImageView.TintColor = color.ToUIColor(); - button.SetImage(templatedImage, UIControlState.Normal); + button.TintColor = color.ToUIColor(); + button.ImageView.TintColor = color.ToUIColor(); + button.SetImage(templatedImage, UIControlState.Normal); + } } } } \ No newline at end of file From bc17c5ab813fe845ea8963f4f773fa31c10d4c3a Mon Sep 17 00:00:00 2001 From: Mike Rowley Date: Sat, 26 Mar 2022 10:36:01 -0600 Subject: [PATCH 2/8] Revised Fix for #934 --- .../IconTintColorEffectRouter.ios.cs | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 29a02715d..465708efb 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -1,5 +1,5 @@ -using System.ComponentModel; -using System.Threading.Tasks; +using System; +using System.ComponentModel; using UIKit; using Xamarin.CommunityToolkit.Effects; using Xamarin.Forms; @@ -12,8 +12,6 @@ namespace Xamarin.CommunityToolkit.iOS.Effects { public class IconTintColorEffectRouter : PlatformEffect { - private int _retryCount = 0; - protected override void OnAttached() => ApplyTintColor(); @@ -69,43 +67,45 @@ void ClearTintColor() } } - async void SetUIImageViewTintColor(UIImageView imageView, Color color) + void SetUIImageViewTintColor(UIImageView imageView, Color color) { if (imageView.Image == null) { - if (_retryCount < 4) - { - _retryCount++; - await Task.Delay(100); - SetUIImageViewTintColor(imageView, color); - } - else + Element.PropertyChanged += (_, e) => { - return; - } + if (e.PropertyName == Image.IsLoadingProperty.PropertyName) + { + var b = Element as Xamarin.Forms.Image ?? throw new NullReferenceException(); + + if (!b.IsLoading) + { + SetUIImageViewTintColor(imageView, color); + } + } + }; } else { imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate); imageView.TintColor = color.ToUIColor(); - _retryCount = 0; } } - async void SetUIButtonTintColor(UIButton button, Color color) + void SetUIButtonTintColor(UIButton button, Color color) { - if (button.CurrentImage == null) + if (button.ImageView.Image == null) { - if (_retryCount < 4) - { - _retryCount++; - await Task.Delay(100); - SetUIButtonTintColor(button, color); - } - else + Element.PropertyChanged += (_, e) => { - return; - } + if (e.PropertyName == ImageButton.IsLoadingProperty.PropertyName) + { + var b = Element as Xamarin.Forms.ImageButton ?? throw new NullReferenceException(); + if (!b.IsLoading) + { + SetUIButtonTintColor(button, color); + } + } + }; } else { From df00d13e6858873b8d211badee6ef7f8a5bffa23 Mon Sep 17 00:00:00 2001 From: Mike Rowley Date: Sat, 26 Mar 2022 15:48:24 -0600 Subject: [PATCH 3/8] Update src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs Co-authored-by: Pedro Jesus --- .../Effects/IconTintColor/IconTintColorEffectRouter.ios.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 465708efb..0603a1b9f 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -75,9 +75,9 @@ void SetUIImageViewTintColor(UIImageView imageView, Color color) { if (e.PropertyName == Image.IsLoadingProperty.PropertyName) { - var b = Element as Xamarin.Forms.Image ?? throw new NullReferenceException(); + var element = Element as Xamarin.Forms.Image ?? throw new NullReferenceException(); - if (!b.IsLoading) + if (!element.IsLoading) { SetUIImageViewTintColor(imageView, color); } From 13f0e20cc4e58c20b539d90c85c1cc1bbd64354e Mon Sep 17 00:00:00 2001 From: Mike Rowley Date: Sat, 26 Mar 2022 15:48:31 -0600 Subject: [PATCH 4/8] Update src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs Co-authored-by: Pedro Jesus --- .../Effects/IconTintColor/IconTintColorEffectRouter.ios.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 0603a1b9f..2055b81da 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -99,8 +99,8 @@ void SetUIButtonTintColor(UIButton button, Color color) { if (e.PropertyName == ImageButton.IsLoadingProperty.PropertyName) { - var b = Element as Xamarin.Forms.ImageButton ?? throw new NullReferenceException(); - if (!b.IsLoading) + var element = Element as Xamarin.Forms.ImageButton ?? throw new NullReferenceException(); + if (!element.IsLoading) { SetUIButtonTintColor(button, color); } From 7600df3fa035e4931e4d8142ec62c67474ba7a56 Mon Sep 17 00:00:00 2001 From: Mike Rowley Date: Sat, 26 Mar 2022 16:18:48 -0600 Subject: [PATCH 5/8] Revised Fix #2 for Bug #934 --- .../IconTintColorEffectRouter.ios.cs | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 2055b81da..723f2ffa6 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -54,11 +54,16 @@ void ClearTintColor() { case UIImageView imageView: if (imageView.Image != null) + { + Element.PropertyChanged -= ImageViewTintColorPropertyChanged; imageView.Image = imageView.Image.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); + } + break; case UIButton button: if (button.CurrentImage != null) { + Element.PropertyChanged -= ButtonTintColorPropertyChanged; var originalImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal); button.SetImage(originalImage, UIControlState.Normal); } @@ -71,18 +76,7 @@ void SetUIImageViewTintColor(UIImageView imageView, Color color) { if (imageView.Image == null) { - Element.PropertyChanged += (_, e) => - { - if (e.PropertyName == Image.IsLoadingProperty.PropertyName) - { - var element = Element as Xamarin.Forms.Image ?? throw new NullReferenceException(); - - if (!element.IsLoading) - { - SetUIImageViewTintColor(imageView, color); - } - } - }; + Element.PropertyChanged += ImageViewTintColorPropertyChanged; } else { @@ -91,21 +85,24 @@ void SetUIImageViewTintColor(UIImageView imageView, Color color) } } + void ImageViewTintColorPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == Image.IsLoadingProperty.PropertyName) + { + var b = Element as Xamarin.Forms.Image ?? throw new NullReferenceException(); + + if (!b.IsLoading) + { + ApplyTintColor(); + } + } + } + void SetUIButtonTintColor(UIButton button, Color color) { if (button.ImageView.Image == null) { - Element.PropertyChanged += (_, e) => - { - if (e.PropertyName == ImageButton.IsLoadingProperty.PropertyName) - { - var element = Element as Xamarin.Forms.ImageButton ?? throw new NullReferenceException(); - if (!element.IsLoading) - { - SetUIButtonTintColor(button, color); - } - } - }; + Element.PropertyChanged += ButtonTintColorPropertyChanged; } else { @@ -118,5 +115,17 @@ void SetUIButtonTintColor(UIButton button, Color color) button.SetImage(templatedImage, UIControlState.Normal); } } + + void ButtonTintColorPropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == ImageButton.IsLoadingProperty.PropertyName) + { + var b = Element as Xamarin.Forms.ImageButton ?? throw new NullReferenceException(); + if (!b.IsLoading) + { + ApplyTintColor(); + } + } + } } } \ No newline at end of file From 2786303eb7b9bf2f215c0bba4fd93747d9f89dfb Mon Sep 17 00:00:00 2001 From: pictos Date: Sat, 26 Mar 2022 23:08:03 -0300 Subject: [PATCH 6/8] updated the sample --- .../Pages/Effects/IconTintColorEffectPage.xaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/XCT.Sample/Pages/Effects/IconTintColorEffectPage.xaml b/samples/XCT.Sample/Pages/Effects/IconTintColorEffectPage.xaml index 9d09cb54b..a3a983cf7 100644 --- a/samples/XCT.Sample/Pages/Effects/IconTintColorEffectPage.xaml +++ b/samples/XCT.Sample/Pages/Effects/IconTintColorEffectPage.xaml @@ -37,16 +37,16 @@ HorizontalTextAlignment="Center" /> @@ -59,18 +59,18 @@ Grid.Column="1"/> From f657078ecad094782407a784920e14972a3c8941 Mon Sep 17 00:00:00 2001 From: pictos Date: Sat, 26 Mar 2022 23:08:14 -0300 Subject: [PATCH 7/8] improved variable name --- .../IconTintColor/IconTintColorEffectRouter.ios.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 723f2ffa6..272c18448 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -89,9 +89,9 @@ void ImageViewTintColorPropertyChanged(object sender, PropertyChangedEventArgs e { if (e.PropertyName == Image.IsLoadingProperty.PropertyName) { - var b = Element as Xamarin.Forms.Image ?? throw new NullReferenceException(); + var element = (Image)Element; - if (!b.IsLoading) + if (!element.IsLoading) { ApplyTintColor(); } @@ -120,8 +120,8 @@ void ButtonTintColorPropertyChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == ImageButton.IsLoadingProperty.PropertyName) { - var b = Element as Xamarin.Forms.ImageButton ?? throw new NullReferenceException(); - if (!b.IsLoading) + var element = (ImageButton)Element; + if (!element.IsLoading) { ApplyTintColor(); } From 2d7022b9083bebdf9c50c974d4e9000824ff0cb9 Mon Sep 17 00:00:00 2001 From: pictos Date: Sat, 26 Mar 2022 23:08:30 -0300 Subject: [PATCH 8/8] changed the null check object --- .../Effects/IconTintColor/IconTintColorEffectRouter.ios.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs index 272c18448..5d05b6fe0 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/Effects/IconTintColor/IconTintColorEffectRouter.ios.cs @@ -61,7 +61,7 @@ void ClearTintColor() break; case UIButton button: - if (button.CurrentImage != null) + if (button.ImageView.Image != null) { Element.PropertyChanged -= ButtonTintColorPropertyChanged; var originalImage = button.CurrentImage.ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);