2 * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Flora License, Version 1.1 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.ObjectModel;
19 using System.Collections.Specialized;
20 using Tizen.Wearable.CircularUI.Forms;
22 using Xamarin.Forms.Platform.Tizen;
23 using XForms = Xamarin.Forms.Forms;
24 using XToolbarItem = Xamarin.Forms.ToolbarItem;
26 [assembly: ExportRenderer(typeof(CirclePage), typeof(Tizen.Wearable.CircularUI.Forms.Renderer.CirclePageRenderer))]
27 namespace Tizen.Wearable.CircularUI.Forms.Renderer
29 public class CirclePageRenderer : VisualElementRenderer<CirclePage>
31 NativeCirclePage _circlePage = null;
33 public CirclePageRenderer()
35 RegisterPropertyHandler(Xamarin.Forms.Page.BackgroundImageSourceProperty, UpdateBackgroundImage);
36 RegisterPropertyHandler(CirclePage.ActionButtonProperty, UpdateActionButton);
37 RegisterPropertyHandler(CirclePage.RotaryFocusObjectProperty, UpdateRotaryFocusObject);
40 public ElmSharp.Wearable.CircleSurface CircleSurface;
42 public void UpdateRotaryFocusObject()
44 _circlePage.UpdateRotaryFocusObject(Element.RotaryFocusObject);
47 protected override void OnElementChanged(ElementChangedEventArgs<CirclePage> e)
49 if (_circlePage == null)
51 _circlePage = NativeFactory.GetNativeControl(typeof(NativeCirclePage)) as NativeCirclePage;
52 CircleSurface = _circlePage.Surface;
54 if (Element.ToolbarItems.Count > 0)
56 Device.BeginInvokeOnMainThread(() =>
58 _circlePage.SetVisibleMoreOption(true);
60 foreach (var item in Element.ToolbarItems)
62 _circlePage.AddToolbarItem(item);
66 SetNativeView(_circlePage);
68 if (e.NewElement != null)
70 _circlePage.SetElement(e.NewElement);
72 e.NewElement.Appearing += OnPageAppearing;
73 e.NewElement.Disappearing += OnPageDisappearing;
74 var toolbarItems = e.NewElement.ToolbarItems as ObservableCollection<XToolbarItem>;
75 if (toolbarItems != null)
76 toolbarItems.CollectionChanged += OnToolbarItemChanged;
77 var circleSurfaceItems = e.NewElement.CircleSurfaceItems as ObservableCollection<ICircleSurfaceItem>;
78 if (circleSurfaceItems != null)
80 circleSurfaceItems.CollectionChanged += OnCircleSurfaceItemsChanged;
82 foreach (var item in circleSurfaceItems)
84 _circlePage.AddCircleSurfaceItem(item);
88 if (e.OldElement != null)
90 e.OldElement.Appearing -= OnPageAppearing;
91 e.OldElement.Disappearing -= OnPageDisappearing;
92 var toolbarItems = e.NewElement.ToolbarItems as ObservableCollection<XToolbarItem>;
93 if (toolbarItems != null)
94 toolbarItems.CollectionChanged -= OnToolbarItemChanged;
95 var circleSurfaceItems = e.NewElement.CircleSurfaceItems as ObservableCollection<ICircleSurfaceItem>;
96 if (circleSurfaceItems != null)
97 circleSurfaceItems.CollectionChanged -= OnCircleSurfaceItemsChanged;
99 base.OnElementChanged(e);
102 protected override void OnElementReady()
104 base.OnElementReady();
105 // A Page created by with ContentTemplate of ShellContent, was appered before create a renderer
106 // So need to call OnPageAppearing if page already appeared
107 if (Element.Appeared)
109 OnPageAppearing(Element, EventArgs.Empty);
113 protected override void UpdateBackgroundColor(bool initialize)
115 if (initialize && Element.BackgroundColor.IsDefault)
118 _circlePage.UpdateBackgroundColor(Element.BackgroundColor);
121 protected void UpdateBackgroundImage(bool initialize)
123 if (initialize && Element.BackgroundImageSource.IsNullOrEmpty())
126 _circlePage.UpdateBackgroundImage(Element.BackgroundImageSource);
129 protected override void Dispose(bool disposing)
133 Element.Appearing -= OnPageAppearing;
134 Element.Disappearing -= OnPageDisappearing;
136 var toolbarItems = Element.ToolbarItems as ObservableCollection<XToolbarItem>;
137 if (toolbarItems != null)
138 toolbarItems.CollectionChanged -= OnToolbarItemChanged;
140 var circleSurfaceItems = Element.CircleSurfaceItems as ObservableCollection<ICircleSurfaceItem>;
141 if (circleSurfaceItems != null)
142 circleSurfaceItems.CollectionChanged -= OnCircleSurfaceItemsChanged;
145 if (_circlePage != null)
147 _circlePage.Dispose(disposing);
150 base.Dispose(disposing);
153 void UpdateActionButton(bool initialize)
155 _circlePage.UpdateActionButton(Element.ActionButton);
158 void OnToolbarItemChanged(object sender, NotifyCollectionChangedEventArgs e)
160 _circlePage.SetVisibleMoreOption(Element.ToolbarItems.Count > 0);
161 if (e.Action == NotifyCollectionChangedAction.Add ||
162 e.Action == NotifyCollectionChangedAction.Replace)
164 foreach (XToolbarItem item in e.NewItems) _circlePage.AddToolbarItem(item);
166 if (e.Action == NotifyCollectionChangedAction.Remove ||
167 e.Action == NotifyCollectionChangedAction.Replace)
169 foreach (XToolbarItem item in e.OldItems) _circlePage.RemoveToolbarITem(item);
173 void OnPageDisappearing(object sender, EventArgs e)
175 _circlePage.DeactivateRotaryWidget();
178 void OnPageAppearing(object sender, EventArgs e)
180 _circlePage.ActivateRotaryWidget();
183 void OnCircleSurfaceItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
185 if (e.Action == NotifyCollectionChangedAction.Add ||
186 e.Action == NotifyCollectionChangedAction.Replace)
188 foreach (ICircleSurfaceItem item in e.NewItems)
189 _circlePage.AddCircleSurfaceItem(item);
191 if (e.Action == NotifyCollectionChangedAction.Remove ||
192 e.Action == NotifyCollectionChangedAction.Replace)
194 foreach (ICircleSurfaceItem item in e.OldItems)
195 _circlePage.RemoveCircleSurfaceItem(item);