Implements view update on NotifyCollectionChanged in collectionView. (#2957)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Samples / Tizen.NUI.Samples / Samples / CollectionViewDemo / Group / CollectionViewLinearGroupSample.cs
1 using System;
2 using System.ComponentModel;
3 using System.Collections.Generic;
4 using System.Collections.ObjectModel;
5 using Tizen.NUI.BaseComponents;
6 using Tizen.NUI.Components;
7 using Tizen.NUI.Binding;
8
9 namespace Tizen.NUI.Samples
10 {
11     public class CollectionViewLinearGroupSample : IExample
12     {
13         CollectionView colView;
14         string selectedItem;
15         ItemSelectionMode selMode;
16         ObservableCollection<Album> albumSource;
17         Album insertDeleteGroup = new Album(999, "Insert / Delete Groups", new DateTime(1999, 12, 31));
18         Gallery insertMenu = new Gallery(999, "Insert item to first of 3rd Group");
19         Gallery deleteMenu = new Gallery(999, "Delete first item in 3rd Group");
20         Album moveGroup = new Album(999, "move Groups", new DateTime(1999, 12, 31));
21         Gallery moveMenu = new Gallery(999, "Move last item to first in 3rd group");
22
23         public void Activate()
24         {
25             Window window = NUIApplication.GetDefaultWindow();
26
27             albumSource = new AlbumViewModel();
28             // Add test menu options.
29             moveGroup.Add(moveMenu);
30             albumSource.Insert(0, moveGroup);
31             insertDeleteGroup.Add(insertMenu);
32             insertDeleteGroup.Add(deleteMenu);
33             albumSource.Insert(0, insertDeleteGroup);
34
35             selMode = ItemSelectionMode.SingleSelection;
36             DefaultTitleItem myTitle = new DefaultTitleItem();
37             //To Bind the Count property changes, need to create custom property for count.
38             myTitle.Text = "Linear Sample Group["+ albumSource.Count+"]";
39             //Set Width Specification as MatchParent to fit the Item width with parent View.
40             myTitle.WidthSpecification = LayoutParamPolicies.MatchParent;
41
42             colView = new CollectionView()
43             {
44                 ItemsSource = albumSource,
45                 ItemsLayouter = new LinearLayouter(),
46                 ItemTemplate = new DataTemplate(() =>
47                 {
48                     DefaultLinearItem item = new DefaultLinearItem();
49                     //Set Width Specification as MatchParent to fit the Item width with parent View.
50                     item.WidthSpecification = LayoutParamPolicies.MatchParent;
51                     //Decorate Label
52                     item.Label.SetBinding(TextLabel.TextProperty, "ViewLabel");
53                     item.Label.HorizontalAlignment = HorizontalAlignment.Begin;
54                     //Decorate Icon
55                     item.Icon.SetBinding(ImageView.ResourceUrlProperty, "ImageUrl");
56                     item.Icon.WidthSpecification = 80;
57                     item.Icon.HeightSpecification = 80;
58                     //Decorate Extra RadioButton.
59                     //[NOTE] This is sample of RadioButton usage in CollectionView.
60                     // RadioButton change their selection by IsSelectedProperty bindings with
61                     // SelectionChanged event with SingleSelection ItemSelectionMode of CollectionView.
62                     // be aware of there are no RadioButtonGroup. 
63                     item.Extra = new RadioButton();
64                     //FIXME : SetBinding in RadioButton crashed as Sensitive Property is disposed.
65                     //item.Extra.SetBinding(RadioButton.IsSelectedProperty, "Selected");
66                     item.Extra.WidthSpecification = 80;
67                     item.Extra.HeightSpecification = 80;
68
69                     return item;
70                 }),
71                 GroupHeaderTemplate = new DataTemplate(() =>
72                 {
73                     DefaultTitleItem group = new DefaultTitleItem();
74                     //Set Width Specification as MatchParent to fit the Item width with parent View.
75                     group.WidthSpecification = LayoutParamPolicies.MatchParent;
76
77                     group.Label.SetBinding(TextLabel.TextProperty, "Date");
78                     group.Label.HorizontalAlignment = HorizontalAlignment.Begin;
79
80                     return group;
81                 }),
82                 Header = myTitle,
83                 IsGrouped = true,
84                 ScrollingDirection = ScrollableBase.Direction.Vertical,
85                 WidthSpecification = LayoutParamPolicies.MatchParent,
86                 HeightSpecification = LayoutParamPolicies.MatchParent,
87                                 SelectionMode = selMode
88             };
89             colView.SelectionChanged += SelectionEvt;
90
91             window.Add(colView);
92
93         }
94
95         public void SelectionEvt(object sender, SelectionChangedEventArgs ev)
96         {
97             //Tizen.Log.Debug("NUI", "LSH :: SelectionEvt called");
98
99             //SingleSelection Only have 1 or nil object in the list.
100             foreach (object item in ev.PreviousSelection)
101             {
102                 if (item == null) break;
103                 if (item is Gallery unselItem)
104                 {
105                     unselItem.Selected = false;
106                     selectedItem = null;
107                     //Tizen.Log.Debug("NUI", "LSH :: Unselected: {0}", unselItem.ViewLabel);
108                 }
109                 else if (item is Album selAlbum)
110                 {
111                     selectedItem = selAlbum.Title;
112                     selAlbum.Selected = false;
113                     if (selAlbum == insertDeleteGroup)
114                     {
115                         albumSource.RemoveAt(2);
116                     }
117                 }
118                 
119             }
120             foreach (object item in ev.CurrentSelection)
121             {
122                 if (item == null) break;
123                 if (item is Gallery selItem)
124                 {
125                     selItem.Selected = true;
126                     selectedItem = selItem.Name;
127                     //Tizen.Log.Debug("NUI", "LSH :: Selected: {0}", selItem.ViewLabel);
128
129                     // Check test menu options.
130                     if (selItem == insertMenu)
131                     {
132                         // Insert new item to index 3.
133                         Random rand = new Random();
134                         int idx = rand.Next(1000);
135                         albumSource[2].Insert(0, new Gallery(idx, "Inserted Item"));
136                     }
137                     else if (selItem == deleteMenu)
138                     {
139                         // Remove item in index 3.
140                         albumSource[2].RemoveAt(0);
141                     }
142                     else if (selItem == moveMenu)
143                     {
144                         // Move last indexed item to index 3.
145                         albumSource[2].Move(albumSource[2].Count - 1, 0);                   
146                     }
147                 }
148                 else if (item is Album selAlbum)
149                 {
150                     selectedItem = selAlbum.Title;
151                     selAlbum.Selected = true;
152                     if (selAlbum == insertDeleteGroup)
153                     {
154                         Random rand = new Random();
155                         int groupIdx = rand.Next(1000);
156                         Album insertAlbum = new Album(groupIdx, "Inserted group", new DateTime(1999, 12, 31));
157                         int idx = rand.Next(1000);
158                         insertAlbum.Add(new Gallery(idx, "Inserted Item 1"));
159                         idx = rand.Next(1000);
160                         insertAlbum.Add(new Gallery(idx, "Inserted Item 2"));
161                         idx = rand.Next(1000);
162                         insertAlbum.Add(new Gallery(idx, "Inserted Item 3"));
163                         albumSource.Insert(2, insertAlbum);
164                     }
165                     else if (selAlbum == moveGroup)
166                     {
167                         albumSource.Move(albumSource.Count - 1, 2);
168
169                     }
170                 }
171             }
172             if (colView.Header != null && colView.Header is DefaultTitleItem)
173             {
174                 DefaultTitleItem title = (DefaultTitleItem)colView.Header;
175                 title.Text = "Linear Sample Count[" + albumSource.Count + (selectedItem != null ? "] Selected [" + selectedItem + "]" : "]");
176             }
177         }
178
179         public void Deactivate()
180         {
181             if (colView != null)
182             {
183                 colView.Dispose();
184             }
185         }
186     }
187 }