[NUI] Pass SelectionChangedEventArgs by default as SelectionChangedCommandParameter.
[platform/core/csapi/tizenfx.git] / test / NUITizenGallery / Examples / CollectionViewTest / Animals.cs
1 /*
2  * Copyright(c) 2022 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
18 using System;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.ComponentModel;
22 using System.Runtime.CompilerServices;
23 using Tizen.NUI;
24 using Tizen.NUI.Components;
25 using Tizen.NUI.Binding;
26
27 public class Animal : INotifyPropertyChanged
28 {
29     private string _name;
30     private string _scientificName;
31     private string _imageUrl = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "/images/animals/";
32
33     public event PropertyChangedEventHandler PropertyChanged;
34     private void OnPropertyChanged([CallerMemberName] string propertyName="") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
35
36
37     public Animal(string name, string scientificName)
38     {
39         _name = name;
40         _scientificName = scientificName;
41     }
42
43     public string Name
44     {
45         get => _name;
46         set
47         {
48             _name = value;
49             OnPropertyChanged();
50         }
51     }
52
53     public string ScientificName
54     {
55         get => _scientificName;
56         set
57         {
58             _scientificName = value;
59             OnPropertyChanged();
60         }
61     }
62
63     public string ImagePath
64     {
65         get
66         {
67             string filename = _name.Replace(" ", "");
68             filename = filename.ToLower() + ".jpg";
69             return _imageUrl + filename;
70         }
71     }
72 }
73
74
75 public class Animals : INotifyPropertyChanged
76 {
77     (string Name,string ScientificName)[] namePool = {
78     ("Bald Eagle", "Haliaeetus leucocephalus"),
79     ("Bear", "Ursidae"),
80     ("Cat", "Felis catus"),
81     ("Chicken", "Gallus gallus domesticus"),
82     ("Cow", "Bos taurus"),
83     ("Deer", "Cervidae"),
84     ("Dog", "Canis lupus familiaris"),
85     ("Duck", "Anatidae"),
86     ("Elephant", "Elephantidae"),
87     ("Emperor Penguin", "Aptenodytes forsteri"),
88     ("Giraffe", "Giraffa"),
89     ("Horse", "Equus ferus"),
90     ("Leopard", "Panthera pardus"),
91     ("Lion", "Panthera leo"),
92     ("Panda", "Ailuropoda melanoleuca"),
93     ("Peacock", "Pavo cristatus"),
94     ("Pig", "Sus scrofa domesticus"),
95     ("Pigeon", "Columba livia"),
96     ("Red Fox", "Vulpes vulpes"),
97     ("Seagull", "Larus canus"),
98     ("Squirrel", "Sciurus vulgaris"),
99     ("Tiger", "Panthera tigris"),
100     ("Wolf", "Canis lupus"),
101     ("Zebra", "Hippotigris"),
102     };
103
104     public event PropertyChangedEventHandler PropertyChanged;
105     private void OnPropertyChanged([CallerMemberName] string propertyName="") { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); }
106
107     public ObservableCollection<Animal> Source {get; private set; } = new ObservableCollection<Animal>();
108
109     public Animals()
110     {
111         for (int i = 0; i < namePool.Length; i++)
112             Source.Add(new Animal(namePool[i].Name, namePool[i].ScientificName));
113
114         SelectedAnimalChangedCommand = new Command<SelectionChangedEventArgs>((param) =>
115         {
116             if (param == null) return;
117
118             Animal animal = null;
119             // Single Selection Only have 1 or nil object in the list.
120             foreach (object item in param.PreviousSelection)
121             {
122                 animal = item as Animal;
123                 if (animal == null) break;
124
125                 Console.WriteLine($"Previous selected item {animal.Name}");
126             }
127             foreach (object item in param.CurrentSelection)
128             {
129                 animal = item as Animal;
130                 if (animal == null) break;
131
132                 Console.WriteLine($"Current selected item {animal.Name}");
133         }       });
134     }
135
136     public Command<SelectionChangedEventArgs> SelectedAnimalChangedCommand { get; }
137 }