[iOS] ActivityIndicator should not disappear when used in a ViewCell (#495)
authoradrianknight89 <adrianknight89@outlook.com>
Thu, 6 Apr 2017 18:25:47 +0000 (13:25 -0500)
committerRui Marinho <me@ruimarinho.net>
Thu, 6 Apr 2017 18:25:47 +0000 (19:25 +0100)
* preserve activity indicator state

* changed to using listviewrenderer

* remove whitespace

* moved message to constructor

* add sample code

* remove message sending

* changes

* remove curly braces

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44980.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.iOS/Renderers/ActivityIndicatorRenderer.cs
Xamarin.Forms.Platform.iOS/Renderers/ListViewRenderer.cs

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44980.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla44980.cs
new file mode 100644 (file)
index 0000000..fc65b1f
--- /dev/null
@@ -0,0 +1,44 @@
+using System.Collections.Generic;
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+#if UITEST
+using Xamarin.UITest;
+using NUnit.Framework;
+#endif
+
+namespace Xamarin.Forms.Controls
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Bugzilla, 44980, "ActivityIndicator disappears when scrolling", PlatformAffected.iOS)]
+       public class Bugzilla44980 : TestContentPage
+       {
+               protected override void Init()
+               {
+                       var list = new List<string>();
+                       for (var i = 0; i < 100; i++)
+                               list.Add(i.ToString());
+
+                       Content = new CListView
+                       {
+                               ItemsSource = list,
+                               ItemTemplate = new DataTemplate(() =>
+                               {
+                                       var activityIndicator = new ActivityIndicator
+                                       {
+                                               IsRunning = true,
+                                               IsVisible = true
+                                       };
+                                       return new ViewCell { View = activityIndicator };
+                               })
+                       };
+               }
+       }
+
+       public class CListView : ListView
+       {
+               public CListView() : base(ListViewCachingStrategy.RecycleElement)
+               {
+               }
+       }
+}
\ No newline at end of file
index 84d146b..2eb5549 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla42832.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44044.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44338.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44980.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45067.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45027.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla45330.cs" />
index abd8f24..e77778c 100644 (file)
@@ -44,5 +44,11 @@ namespace Xamarin.Forms.Platform.iOS
                        else
                                Control.StopAnimating();
                }
+
+               internal void PreserveState()
+               {
+                       if (Control != null && !Control.IsAnimating && Element != null && Element.IsRunning)
+                               Control.StartAnimating();
+               }
        }
 }
\ No newline at end of file
index 99a1066..8581e26 100644 (file)
@@ -780,12 +780,13 @@ namespace Xamarin.Forms.Platform.iOS
 
                        public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
                        {
-                               UITableViewCell nativeCell = null;
+                               Cell cell;
+                               UITableViewCell nativeCell;
 
                                var cachingStrategy = Controller.CachingStrategy;
                                if (cachingStrategy == ListViewCachingStrategy.RetainElement)
                                {
-                                       var cell = GetCellForPath(indexPath);
+                                       cell = GetCellForPath(indexPath);
                                        nativeCell = CellTableViewCell.GetNativeCell(tableView, cell);
                                }
                                else if (cachingStrategy == ListViewCachingStrategy.RecycleElement)
@@ -794,13 +795,13 @@ namespace Xamarin.Forms.Platform.iOS
                                        nativeCell = tableView.DequeueReusableCell(ContextActionsCell.Key + id);
                                        if (nativeCell == null)
                                        {
-                                               var cell = GetCellForPath(indexPath);
+                                               cell = GetCellForPath(indexPath);
                                                nativeCell = CellTableViewCell.GetNativeCell(tableView, cell, true, id.ToString());
                                        }
                                        else
                                        {
                                                var templatedList = TemplatedItemsView.TemplatedItems.GetGroup(indexPath.Section);
-                                               var cell = (Cell)((INativeElementView)nativeCell).Element;
+                                               cell = (Cell)((INativeElementView)nativeCell).Element;
                                                ICellController controller = cell;
                                                controller.SendDisappearing();
                                                templatedList.UpdateContent(cell, indexPath.Row);
@@ -813,10 +814,10 @@ namespace Xamarin.Forms.Platform.iOS
                                var bgColor = tableView.IndexPathForSelectedRow != null && tableView.IndexPathForSelectedRow.Equals(indexPath) ? UIColor.Clear : DefaultBackgroundColor;
 
                                SetCellBackgroundColor(nativeCell, bgColor);
-
+                               PreserveActivityIndicatorState(cell);
                                return nativeCell;
                        }
-
+               
                        public override nfloat GetHeightForHeader(UITableView tableView, nint section)
                        {
                                if (List.IsGroupingEnabled)
@@ -1079,6 +1080,24 @@ namespace Xamarin.Forms.Platform.iOS
 
                                base.Dispose(disposing);
                        }
+
+                       void PreserveActivityIndicatorState(Element element)
+                       {
+                               if (element == null)
+                                       return;
+
+                               var activityIndicator = element as ActivityIndicator;
+                               if (activityIndicator != null)
+                               {
+                                       var renderer = Platform.GetRenderer(activityIndicator) as ActivityIndicatorRenderer;
+                                       renderer?.PreserveState();
+                               }
+                               else
+                               {
+                                       foreach (Element childElement in (element as IElementController).LogicalChildren)
+                                               PreserveActivityIndicatorState(childElement);
+                               }
+                       }
                }
        }