summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjh5.cho <jh5.cho@samsung.com>2017-07-13 06:02:51 (GMT)
committerKangho Hur <kangho.hur@samsung.com>2017-10-23 04:34:39 (GMT)
commit8a6ea57a7b6610d1d9c6abeec622e6f0df723d31 (patch)
tree5db2f71afb8dd97e5867ffe2f035ada9679943f6
parentb2a4787e51bb177ab8e05be138964d8d111a0cb4 (diff)
downloadxamarin-forms-8a6ea57a7b6610d1d9c6abeec622e6f0df723d31.zip
xamarin-forms-8a6ea57a7b6610d1d9c6abeec622e6f0df723d31.tar.gz
xamarin-forms-8a6ea57a7b6610d1d9c6abeec622e6f0df723d31.tar.bz2
[TabbedPage] Enable TabbedPage to add or remove tabs
TASK=TCAPI-2494 - Allow TabbedPage to add or remove tabs Change-Id: I38d39a1c34b8bcb1b301af89277ee270163d8a29
-rw-r--r--Xamarin.Forms.Platform.Tizen/Renderers/TabbedPageRenderer.cs134
1 files changed, 111 insertions, 23 deletions
diff --git a/Xamarin.Forms.Platform.Tizen/Renderers/TabbedPageRenderer.cs b/Xamarin.Forms.Platform.Tizen/Renderers/TabbedPageRenderer.cs
index 3af1b34..9d9283e 100644
--- a/Xamarin.Forms.Platform.Tizen/Renderers/TabbedPageRenderer.cs
+++ b/Xamarin.Forms.Platform.Tizen/Renderers/TabbedPageRenderer.cs
@@ -1,12 +1,12 @@
using System.Collections.Generic;
+using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
+using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
using ElmSharp;
using EToolbarItem = ElmSharp.ToolbarItem;
using EToolbarItemEventArgs = ElmSharp.ToolbarItemEventArgs;
using EColor = ElmSharp.Color;
-using Xamarin.Forms.PlatformConfiguration.TizenSpecific;
-using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.Tizen
{
@@ -17,10 +17,12 @@ namespace Xamarin.Forms.Platform.Tizen
Scroller _scroller;
Toolbar _toolbar;
Dictionary<EToolbarItem, Page> _itemToItemPage = new Dictionary<EToolbarItem, Page>();
+ List<EToolbarItem> _toolbarItemList = new List<EToolbarItem>();
+ bool _isResettingToolbarItems = false;
public TabbedPageRenderer()
{
- RegisterPropertyHandler(TabbedPage.TitleProperty, UpdateTitle);
+ RegisterPropertyHandler(Page.TitleProperty, UpdateTitle);
RegisterPropertyHandler("CurrentPage", CurrentPageChanged);
RegisterPropertyHandler(TabbedPage.BarBackgroundColorProperty, UpdateBarBackgroundColor);
}
@@ -97,6 +99,15 @@ namespace Xamarin.Forms.Platform.Tizen
UpdateTitle();
}
+ if (e.OldElement != null)
+ {
+ Element.PagesChanged -= OnElementPagesChanged;
+ }
+ if (e.NewElement != null)
+ {
+ Element.PagesChanged += OnElementPagesChanged;
+ }
+
base.OnElementChanged(e);
}
@@ -104,6 +115,7 @@ namespace Xamarin.Forms.Platform.Tizen
{
if (disposing)
{
+ Element.PagesChanged -= OnElementPagesChanged;
if (_outterLayout != null)
{
_outterLayout.Unrealize();
@@ -149,21 +161,15 @@ namespace Xamarin.Forms.Platform.Tizen
index++;
}
_innerBox.MinimumWidth = Element.Children.Count * bound.Width;
+ if (_toolbar.SelectedItem == null)
+ return;
int currentPage = MultiPage<Page>.GetIndex(_itemToItemPage[_toolbar.SelectedItem]);
_scroller.ScrollTo(currentPage, 0, true);
}
void OnItemPageScrolled(object sender, System.EventArgs e)
{
- Page newPage = Element.GetPageByIndex(_scroller.HorizontalPageIndex);
- foreach (var pair in _itemToItemPage)
- {
- if (pair.Value == newPage)
- {
- pair.Key.IsSelected = true;
- return;
- }
- }
+ _toolbarItemList[_scroller.HorizontalPageIndex].IsSelected = true;
}
void UpdateBarBackgroundColor()
@@ -205,33 +211,115 @@ namespace Xamarin.Forms.Platform.Tizen
}
}
- void FillToolbarAndContents()
+ void OnElementPagesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
- //add items to toolbar
- foreach (Page child in Element.Children)
+ switch (e.Action)
+ {
+ case NotifyCollectionChangedAction.Add:
+ AddToolbarItems(e);
+ break;
+ case NotifyCollectionChangedAction.Remove:
+ RemoveToolbarItems(e);
+ break;
+ default:
+ ResetToolbarItems();
+ break;
+ }
+ }
+
+ void AddToolbarItems(NotifyCollectionChangedEventArgs e)
+ {
+ int index = e.NewStartingIndex < 0 ? _toolbar.ItemsCount : e.NewStartingIndex;
+ foreach (Page item in e.NewItems)
+ {
+ AddToolbarItem(item, index++);
+ }
+ }
+
+ EToolbarItem AddToolbarItem(Page newItem, int index)
+ {
+ EToolbarItem toolbarItem;
+ if (index == 0)
{
- EToolbarItem toolbarItem = _toolbar.Append(child.Title, string.IsNullOrEmpty(child.Icon) ? null : ResourcePath.GetPath(child.Icon));
- if (Element.BarBackgroundColor != Color.Default)
+ toolbarItem = _toolbar.Prepend(newItem.Title, string.IsNullOrEmpty(newItem.Icon) ? null : ResourcePath.GetPath(newItem.Icon));
+ }
+ else
+ {
+ toolbarItem = _toolbar.InsertAfter(_toolbarItemList[index - 1], newItem.Title, string.IsNullOrEmpty(newItem.Icon) ? null : ResourcePath.GetPath(newItem.Icon));
+ }
+ _toolbarItemList.Insert(index, toolbarItem);
+ _itemToItemPage.Add(toolbarItem, newItem);
+
+ if (Element.BarBackgroundColor != Color.Default)
+ {
+ toolbarItem.SetPartColor("bg", _toolbar.BackgroundColor);
+ }
+
+ var childContent = Platform.GetOrCreateRenderer(newItem).NativeView;
+ _innerBox.PackEnd(childContent);
+
+ newItem.PropertyChanged += OnPageTitleChanged;
+
+ return toolbarItem;
+ }
+
+ void RemoveToolbarItems(NotifyCollectionChangedEventArgs e)
+ {
+ foreach (Page item in e.OldItems)
+ {
+ RemoveToolbarItem(item);
+ }
+ }
+
+ void RemoveToolbarItem(Page oldItem)
+ {
+ foreach (var pair in _itemToItemPage)
+ {
+ if (pair.Value == oldItem)
{
- toolbarItem.SetPartColor("bg", _toolbar.BackgroundColor);
+ pair.Value.PropertyChanged -= OnPageTitleChanged;
+ _toolbarItemList.Remove(pair.Key);
+ _itemToItemPage.Remove(pair.Key);
+ pair.Key.Delete();
+ return;
}
- _itemToItemPage.Add(toolbarItem, child);
+ }
+ }
+
+ void ResetToolbarItems()
+ {
+ _isResettingToolbarItems = true;
+ foreach(var pair in _itemToItemPage)
+ {
+ pair.Value.PropertyChanged -= OnPageTitleChanged;
+ pair.Key.Delete();
+ }
+ _itemToItemPage.Clear();
+ _toolbarItemList.Clear();
+
+ FillToolbarAndContents();
+ _isResettingToolbarItems = false;
+ }
- var childContent = Platform.GetOrCreateRenderer(child).NativeView;
- _innerBox.PackEnd(childContent);
+ void FillToolbarAndContents()
+ {
+ int index = 0;
+ //add items to toolbar
+ foreach (Page child in Element.Children)
+ {
+ EToolbarItem toolbarItem = AddToolbarItem(child, index++);
if (Element.CurrentPage == child)
{
//select item on the toolbar and fill content
toolbarItem.IsSelected = true;
}
- child.PropertyChanged += OnPageTitleChanged;
}
}
void OnToolbarItemSelected(object sender, EToolbarItemEventArgs e)
{
- if (_toolbar.SelectedItem == null)
+ if (_toolbar.SelectedItem == null || _isResettingToolbarItems)
return;
var oldPage = Element.CurrentPage;