[NUI] integration from DevelNUI to master (#3309)
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / CollectionViewTest / CollectionViewTest9.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.Generic;
19 using System.Collections.ObjectModel;
20 using System.ComponentModel;
21 using Tizen.NUI;
22 using Tizen.NUI.Binding;
23 using Tizen.NUI.BaseComponents;
24 using Tizen.NUI.Components;
25 using Tizen.NUI.Components.Extension;
26
27 namespace NUITizenGallery
28 {
29     public partial class CollectionViewTest9 : ContentPage
30     {
31         void OnCheckClicked(object sender, ClickedEventArgs e)
32         {
33             CheckBox check = sender as CheckBox;
34             if (check == null) return;
35             if (check.BindingContext == null) return;
36             var item = check.BindingContext;
37             if (item is TestItem tItem)
38             {
39                 tItem.IsSelected = check.IsSelected;
40                 Console.WriteLine($"On Clicked {tItem.Index} : {tItem.IsSelected}");
41             }
42
43         }
44
45         void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
46         {
47             List<object> cur = new List<object>(e.CurrentSelection);
48
49             foreach(TestItem item in e.PreviousSelection)
50             {
51                 if (cur.Contains(item)) continue;
52                 item.IsSelected = false;
53                 Console.WriteLine($"On Selection {item.Index} : {item.IsSelected}");
54             }
55             foreach(TestItem item in cur)
56             {
57                 item.IsSelected = true;
58                 Console.WriteLine($"On Selection {item.Index} : {item.IsSelected}");
59             }
60         }
61
62         public CollectionViewTest9()
63         {
64             InitializeComponent();
65             BindingContext = new GroupTestSourceModel(5, 5);
66
67             ColView.ItemTemplate = new DataTemplate(() =>
68             {
69                 var item = new DefaultLinearItem()
70                 {
71                     WidthSpecification = LayoutParamPolicies.MatchParent,
72                 };
73                 item.Label.SetBinding(TextLabel.TextProperty, "Name");
74
75                 var icon = new View()
76                 {
77                     WidthSpecification = 60,
78                     HeightSpecification = 60
79                 };
80                 icon.SetBinding(BackgroundColorProperty, "BgColor");
81                 item.Icon = icon;
82
83                 var check = new CheckBox()
84                 {
85                     WidthSpecification = 60,
86                     HeightSpecification = 60,
87                 };
88                 check.SetBinding(Button.IsSelectedProperty, "IsSelected");
89                 check.Clicked += OnCheckClicked;
90                 item.Extra = check;
91
92                 return item;
93             });
94             ColView.GroupHeaderTemplate = new DataTemplate(() =>
95             {
96                 var header = new DefaultTitleItem()
97                 {
98                     WidthSpecification = LayoutParamPolicies.MatchParent,
99                 };
100                 header.Label.SetBinding(TextLabel.TextProperty, "GroupName");
101
102                 return header;
103             });
104         }
105
106         protected override void Dispose(DisposeTypes type)
107         {
108             if (Disposed)
109             {
110                 return;
111             }
112
113             if (type == DisposeTypes.Explicit)
114             {
115                 RemoveAllChildren(true);
116             }
117
118             base.Dispose(type);
119         }
120
121         private void RemoveAllChildren(bool dispose = false)
122         {
123             RecursiveRemoveChildren(this, dispose);
124         }
125
126         private void RecursiveRemoveChildren(View parent, bool dispose)
127         {
128             if (parent == null)
129             {
130                 return;
131             }
132
133             int maxChild = (int)parent.ChildCount;
134             for (int i = maxChild - 1; i >= 0; --i)
135             {
136                 View child = parent.GetChildAt((uint)i);
137                 if (child == null)
138                 {
139                     continue;
140                 }
141
142                 RecursiveRemoveChildren(child, dispose);
143                 parent.Remove(child);
144                 if (dispose)
145                 {
146                     child.Dispose();
147                 }
148             }
149         }
150     }
151 }