[NUI] sync with https://github.com/nui-dali/NUITizenGallery
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / CollectionViewTest / CollectionViewTest5Page.xaml.cs
1 /*
2  * Copyright(c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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.ComponentModel;
20 using Tizen.NUI;
21 using Tizen.NUI.Binding;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Components;
24 using Tizen.NUI.Components.Extension;
25
26 namespace NUITizenGallery
27 {
28     public partial class CollectionViewTest5Page : ContentPage
29     {
30         TestItem item10;
31         ObservableCollection<TestItem> source;
32         void OnSelect10Clicked(object sender, ClickedEventArgs e)
33         {
34             ColView.SelectedItem = item10;
35         }
36
37         void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
38         {
39             TestItem SelectedItem = null;
40             string message;
41             foreach(TestItem item in e.CurrentSelection)
42             {
43                 Console.WriteLine("Selected Item {0}", item?.GetHashCode());
44                 SelectedItem = item;
45             }
46
47             if (source == null)
48             {
49                 message = "Source is NULL";
50             }
51             else
52             {
53                 int index = source.IndexOf(SelectedItem);
54                 message = index + " was Selected";
55             }
56
57             var btn = new Button() { Text = "Ok", };
58             btn.Clicked += (object s, ClickedEventArgs args) =>
59             {
60                 Navigator?.Pop();
61             };
62
63             Console.WriteLine(message);
64             DialogPage.ShowAlertDialog("Selected", message, btn);
65         }
66
67         public CollectionViewTest5Page()
68         {
69             InitializeComponent();
70             var MyModel = new TestSourceModel();
71             item10 = MyModel.TestSource[10];
72
73             ColView.ItemsSource = source = MyModel.TestSource;
74             ColView.ItemTemplate = new DataTemplate(() =>
75             {
76                 var item = new RecyclerViewItem()
77                 {
78                     HeightSpecification = LayoutParamPolicies.MatchParent,
79                     WidthSpecification = 200,
80                 };
81                 item.SetBinding(View.BackgroundColorProperty, "BgColor");
82                 var label = new TextLabel()
83                 {
84                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
85                     PivotPoint = Tizen.NUI.PivotPoint.Center,
86                     PositionUsesPivotPoint = true,
87                 };
88                 label.PixelSize = 30;
89                 label.SetBinding(TextLabel.TextProperty, "Index");
90                 item.Add(label);
91
92                 return item;
93             });
94         }
95
96         protected override void Dispose(DisposeTypes type)
97         {
98             if (Disposed)
99             {
100                 return;
101             }
102
103             if (type == DisposeTypes.Explicit)
104             {
105                 RemoveAllChildren(true);
106             }
107
108             base.Dispose(type);
109         }
110
111         private void RemoveAllChildren(bool dispose = false)
112         {
113             RecursiveRemoveChildren(this, dispose);
114         }
115
116         private void RecursiveRemoveChildren(View parent, bool dispose)
117         {
118             if (parent == null)
119             {
120                 return;
121             }
122
123             int maxChild = (int)parent.ChildCount;
124             for (int i = maxChild - 1; i >= 0; --i)
125             {
126                 View child = parent.GetChildAt((uint)i);
127                 if (child == null)
128                 {
129                     continue;
130                 }
131
132                 RecursiveRemoveChildren(child, dispose);
133                 parent.Remove(child);
134                 if (dispose)
135                 {
136                     child.Dispose();
137                 }
138             }
139         }
140     }
141 }