Support new features of Tizen.CircularUI (#188)
[platform/core/csapi/xsf.git] / src / XSF / Tizen.Wearable.CircularUI.Forms.Renderer / CirclePageRenderer.cs
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *     http://floralicense.org/license/
9  *
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.
15  */
16
17 using System;
18 using System.Collections.ObjectModel;
19 using System.Collections.Specialized;
20 using Tizen.Wearable.CircularUI.Forms;
21 using Xamarin.Forms;
22 using Xamarin.Forms.Platform.Tizen;
23 using XForms = Xamarin.Forms.Forms;
24 using XToolbarItem = Xamarin.Forms.ToolbarItem;
25
26 [assembly: ExportRenderer(typeof(CirclePage), typeof(Tizen.Wearable.CircularUI.Forms.Renderer.CirclePageRenderer))]
27 namespace Tizen.Wearable.CircularUI.Forms.Renderer
28 {
29         public class CirclePageRenderer : VisualElementRenderer<CirclePage>
30         {
31                 NativeCirclePage _circlePage = null;
32
33                 public CirclePageRenderer()
34                 {
35                         RegisterPropertyHandler(Xamarin.Forms.Page.BackgroundImageSourceProperty, UpdateBackgroundImage);
36                         RegisterPropertyHandler(CirclePage.ActionButtonProperty, UpdateActionButton);
37                         RegisterPropertyHandler(CirclePage.RotaryFocusObjectProperty, UpdateRotaryFocusObject);
38                 }
39
40                 public ElmSharp.Wearable.CircleSurface CircleSurface;
41
42                 public void UpdateRotaryFocusObject()
43                 {
44                         _circlePage.UpdateRotaryFocusObject(Element.RotaryFocusObject);
45                 }
46
47                 protected override void OnElementChanged(ElementChangedEventArgs<CirclePage> e)
48                 {
49                         if (_circlePage == null)
50                         {
51                                 _circlePage = NativeFactory.GetNativeControl(typeof(NativeCirclePage)) as NativeCirclePage;
52                                 CircleSurface = _circlePage.Surface;
53
54                                 if (Element.ToolbarItems.Count > 0)
55                                 {
56                                         Device.BeginInvokeOnMainThread(() =>
57                                         {
58                                                 _circlePage.SetVisibleMoreOption(true);
59
60                                                 foreach (var item in Element.ToolbarItems)
61                                                 {
62                                                         _circlePage.AddToolbarItem(item);
63                                                 }
64                                         });
65                                 }
66                                 SetNativeView(_circlePage);
67                         }
68                         if (e.NewElement != null)
69                         {
70                                 _circlePage.SetElement(e.NewElement);
71
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)
79                                 {
80                                         circleSurfaceItems.CollectionChanged += OnCircleSurfaceItemsChanged;
81
82                                         foreach (var item in circleSurfaceItems)
83                                         {
84                                                 _circlePage.AddCircleSurfaceItem(item);
85                                         }
86                                 }
87                         }
88                         if (e.OldElement != null)
89                         {
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;
98                         }
99                         base.OnElementChanged(e);
100                 }
101
102                 protected override void OnElementReady()
103                 {
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)
108                         {
109                                 OnPageAppearing(Element, EventArgs.Empty);
110                         }
111                 }
112
113                 protected override void UpdateBackgroundColor(bool initialize)
114                 {
115                         if (initialize && Element.BackgroundColor.IsDefault)
116                                 return;
117
118                         _circlePage.UpdateBackgroundColor(Element.BackgroundColor);
119                 }
120
121                 protected void UpdateBackgroundImage(bool initialize)
122                 {
123                         if (initialize && Element.BackgroundImageSource.IsNullOrEmpty())
124                                 return;
125
126                         _circlePage.UpdateBackgroundImage(Element.BackgroundImageSource);
127                 }
128
129                 protected override void Dispose(bool disposing)
130                 {
131                         if (Element != null)
132                         {
133                                 Element.Appearing -= OnPageAppearing;
134                                 Element.Disappearing -= OnPageDisappearing;
135
136                                 var toolbarItems = Element.ToolbarItems as ObservableCollection<XToolbarItem>;
137                                 if (toolbarItems != null)
138                                         toolbarItems.CollectionChanged -= OnToolbarItemChanged;
139
140                                 var circleSurfaceItems = Element.CircleSurfaceItems as ObservableCollection<ICircleSurfaceItem>;
141                                 if (circleSurfaceItems != null)
142                                         circleSurfaceItems.CollectionChanged -= OnCircleSurfaceItemsChanged;
143                         }
144
145                         if (_circlePage != null)
146                         {
147                                 _circlePage.Dispose(disposing);
148                         }
149
150                         base.Dispose(disposing);
151                 }
152
153                 void UpdateActionButton(bool initialize)
154                 {
155                         _circlePage.UpdateActionButton(Element.ActionButton);
156                 }
157
158                 void OnToolbarItemChanged(object sender, NotifyCollectionChangedEventArgs e)
159                 {
160                         _circlePage.SetVisibleMoreOption(Element.ToolbarItems.Count > 0);
161                         if (e.Action == NotifyCollectionChangedAction.Add ||
162                                 e.Action == NotifyCollectionChangedAction.Replace)
163                         {
164                                 foreach (XToolbarItem item in e.NewItems) _circlePage.AddToolbarItem(item);
165                         }
166                         if (e.Action == NotifyCollectionChangedAction.Remove ||
167                                 e.Action == NotifyCollectionChangedAction.Replace)
168                         {
169                                 foreach (XToolbarItem item in e.OldItems) _circlePage.RemoveToolbarITem(item);
170                         }
171                 }
172
173                 void OnPageDisappearing(object sender, EventArgs e)
174                 {
175                         _circlePage.DeactivateRotaryWidget();
176                 }
177
178                 void OnPageAppearing(object sender, EventArgs e)
179                 {
180                         _circlePage.ActivateRotaryWidget();
181                 }
182
183                 void OnCircleSurfaceItemsChanged(object sender, NotifyCollectionChangedEventArgs e)
184                 {
185                         if (e.Action == NotifyCollectionChangedAction.Add ||
186                                 e.Action == NotifyCollectionChangedAction.Replace)
187                         {
188                                 foreach (ICircleSurfaceItem item in e.NewItems)
189                                         _circlePage.AddCircleSurfaceItem(item);
190                         }
191                         if (e.Action == NotifyCollectionChangedAction.Remove ||
192                                 e.Action == NotifyCollectionChangedAction.Replace)
193                         {
194                                 foreach (ICircleSurfaceItem item in e.OldItems)
195                                         _circlePage.RemoveCircleSurfaceItem(item);
196                         }
197                 }
198         }
199 }