Improve performance when ContextPopupItem changed 92/122092/4
authorSidharth Gupta <sid92.gupta@samsung.com>
Thu, 30 Mar 2017 02:10:29 +0000 (11:10 +0900)
committerSidharth Gupta <sid92.gupta@samsung.com>
Tue, 4 Apr 2017 10:14:55 +0000 (19:14 +0900)
Uses the faster ElmSharp SetPartText() and SetPartContent()
methods whenever possible.

Signed-off-by: Sidharth Gupta <sid92.gupta@samsung.com>
Change-Id: I14bbcb700485e94e4ba5a6c4f9acf7bb062fe343

Tizen.Xamarin.Forms.Extension.Renderer/ContextPopupImplementation.cs
Tizen.Xamarin.Forms.Extension/ContextPopup.cs [changed mode: 0755->0644]
Tizen.Xamarin.Forms.Extension/IContextPopup.cs

index 589a931..fe5da77 100644 (file)
@@ -164,6 +164,29 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
             }
         }
 
+        public bool HasLabelPart(ContextPopupItem item)
+        {
+            ElmSharp.ContextPopupItem nativeItem = _items[item];
+            return nativeItem.Text != null;
+        }
+
+        public bool HasIconPart(ContextPopupItem item)
+        {
+            ElmSharp.ContextPopupItem nativeItem = _items[item];
+            return nativeItem.Icon != null;
+        }
+
+        public void UpdateContextPopupItemLabel(ContextPopupItem item)
+        {
+            ElmSharp.ContextPopupItem nativeItem = _items[item];
+            nativeItem.SetPartText("default", item.Label);
+        }
+
+        public void UpdateContextPopupItemIcon(ContextPopupItem item)
+        {
+            AppendOrModifyItemWithIcon(item);
+        }
+
         public void Dispose()
         {
             Dispose(true);
@@ -233,7 +256,7 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
             }
             else
             {
-                nativeItem = AppendItemWithIcon(item);
+                nativeItem = AppendOrModifyItemWithIcon(item);
             }
 
             _items.Add(item, nativeItem);
@@ -253,21 +276,27 @@ namespace Tizen.Xamarin.Forms.Extension.Renderer
             };
         }
 
-        ElmSharp.ContextPopupItem AppendItemWithIcon(ContextPopupItem item)
+        ElmSharp.ContextPopupItem AppendOrModifyItemWithIcon(ContextPopupItem item)
         {
-            ElmSharp.ContextPopupItem nativeItem;
+            ElmSharp.ContextPopupItem nativeItem = null;
             Icon icon = new Icon(_popup);
             icon.StandardIconName = item.Icon;
             if (!string.IsNullOrEmpty(icon.StandardIconName))
             {
-                nativeItem = _popup.Append(item.Label, icon);
+                if (!_items.ContainsKey(item))
+                    nativeItem = _popup.Append(item.Label, icon);
+                else
+                    _items[item].SetPartContent("icon", icon);
             }
             else
             {
                 //Not a standard icon
                 XFPlatformTizen.Native.Image iconImage = new XFPlatformTizen.Native.Image(_popup);
                 var task = iconImage.LoadFromImageSourceAsync(item.Icon);
-                nativeItem = _popup.Append(item.Label, iconImage);
+                if (!_items.ContainsKey(item))
+                    nativeItem = _popup.Append(item.Label, iconImage);
+                else
+                    _items[item].SetPartContent("icon", iconImage);
             }
 
             return nativeItem;
old mode 100755 (executable)
new mode 100644 (file)
index ce0909b..795a8ba
@@ -209,13 +209,28 @@ namespace Tizen.Xamarin.Forms.Extension
 
         void ContextPopupItemPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
         {
-            //Clear completely and then add all items again because for example, if initially there was
-            //no icon, an icon cannot be added later using the native APIs. However, this scenario is
-            //possible in the Tizen.Xamarin.Forms.Extension APIs.
-            //And, the native APIs do not provide any method to "InsertAt" or "Replace", so we cannot
-            //just replace the item which had a property change.
-            _contextPopup.ClearItems();
-            _contextPopup.AddItems(_items);
+            var item = sender as ContextPopupItem;
+
+            if (e.PropertyName == nameof(ContextPopupItem.Label) && _contextPopup.HasLabelPart(item))
+            {
+                // If the native item already has a label
+                _contextPopup.UpdateContextPopupItemLabel(item);
+            }
+            else if (e.PropertyName == nameof(ContextPopupItem.Icon) && _contextPopup.HasIconPart(item))
+            {
+                // If the native item already has an icon
+                _contextPopup.UpdateContextPopupItemIcon(item);
+            }
+            else
+            {
+                // Clear completely and then add all items again. Because, for example, if initially there was
+                // no icon, an icon cannot be added later using the native APIs. However, this scenario is
+                // possible in the Tizen.Xamarin.Forms.Extension APIs.
+                // And, the native APIs do not provide any method to "InsertAt" or "Replace", so we cannot
+                // just replace the item which had a property change.
+                _contextPopup.ClearItems();
+                _contextPopup.AddItems(_items);
+            }
         }
 
         static void OnSelectedIndexChanged(BindableObject bindable, object oldValue, object newValue)
index 44e46ca..0b0388f 100644 (file)
@@ -1,21 +1,21 @@
-using System;
+using System;
 using System.Collections.Generic;
 using Xamarin.Forms;
 
 namespace Tizen.Xamarin.Forms.Extension
 {
     internal interface IContextPopup
-    {\r
-        event EventHandler SelectedIndexChanged;\r
-\r
-        event EventHandler Dismissed;\r
-\r
-        ContextPopupOrientation Orientation { get; set; }\r
-\r
-        ContextPopupItem SelectedItem { get; set; }\r
-\r
-        ContextPopupDirectionPriorities DirectionPriorities { get; set; }\r
-\r
+    {
+        event EventHandler SelectedIndexChanged;
+
+        event EventHandler Dismissed;
+
+        ContextPopupOrientation Orientation { get; set; }
+
+        ContextPopupItem SelectedItem { get; set; }
+
+        ContextPopupDirectionPriorities DirectionPriorities { get; set; }
+
         bool IsAutoHidingEnabled { get; set; }
 
         void AddItems(IEnumerable<ContextPopupItem> items);
@@ -29,5 +29,13 @@ namespace Tizen.Xamarin.Forms.Extension
         void Dismiss();
 
         bool TryGetContextPopupDirection(out ContextPopupDirection direction);
+
+        bool HasLabelPart(ContextPopupItem item);
+
+        bool HasIconPart(ContextPopupItem item);
+
+        void UpdateContextPopupItemLabel(ContextPopupItem item);
+
+        void UpdateContextPopupItemIcon(ContextPopupItem item);
     }
 }
\ No newline at end of file