[Xaml] Modify by HQ's feedback
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / VisualState / VisualStateGroupList.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;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
21 using System.Linq;
22 using Tizen.NUI;
23 using Tizen.NUI.Binding;
24
25 namespace Tizen.NUI.Xaml
26 {
27     internal class VisualStateGroupList : IList<VisualStateGroup>
28     {
29         readonly IList<VisualStateGroup> _internalList;
30
31         void Validate(IList<VisualStateGroup> groups)
32         {
33             // If we have 1 group, no need to worry about duplicate group names
34             if (groups.Count > 1)
35             {
36                 if (groups.GroupBy(vsg => vsg.Name).Any(g => g.Count() > 1))
37                 {
38                     throw new InvalidOperationException("VisualStateGroup Names must be unique");
39                 }
40             }
41
42             // State names must be unique within this group list, so pull in all 
43             // the states in all the groups, group them by name, and see if we have
44             // and duplicates
45             if (groups.SelectMany(group => group.States).GroupBy(state => state.Name).Any(g => g.Count() > 1))
46             {
47                 throw new InvalidOperationException("VisualState Names must be unique");
48             }
49         }
50
51         public VisualStateGroupList()
52         {
53             _internalList = new WatchAddList<VisualStateGroup>(Validate);
54         }
55
56         void ValidateOnStatesChanged(object sender, EventArgs eventArgs)
57         {
58             Validate(_internalList);
59         }
60
61         public IEnumerator<VisualStateGroup> GetEnumerator()
62         {
63             return _internalList.GetEnumerator();
64         }
65
66         IEnumerator IEnumerable.GetEnumerator()
67         {
68             return ((IEnumerable)_internalList).GetEnumerator();
69         }
70
71         public void Add(VisualStateGroup item)
72         {
73             _internalList.Add(item);
74             item.StatesChanged += ValidateOnStatesChanged;
75         }
76
77         public void Clear()
78         {
79             foreach (var group in _internalList)
80             {
81                 group.StatesChanged -= ValidateOnStatesChanged;
82             }
83
84             _internalList.Clear();
85         }
86
87         public bool Contains(VisualStateGroup item)
88         {
89             return _internalList.Contains(item);
90         }
91
92         public void CopyTo(VisualStateGroup[] array, int arrayIndex)
93         {
94             _internalList.CopyTo(array, arrayIndex);
95         }
96
97         public bool Remove(VisualStateGroup item)
98         {
99             item.StatesChanged -= ValidateOnStatesChanged;
100             return _internalList.Remove(item);
101         }
102
103         public int Count => _internalList.Count;
104
105         public bool IsReadOnly => false;
106
107         public int IndexOf(VisualStateGroup item)
108         {
109             return _internalList.IndexOf(item);
110         }
111
112         public void Insert(int index, VisualStateGroup item)
113         {
114             item.StatesChanged += ValidateOnStatesChanged;
115             _internalList.Insert(index, item);
116         }
117
118         public void RemoveAt(int index)
119         {
120             _internalList[index].StatesChanged -= ValidateOnStatesChanged;
121             _internalList.RemoveAt(index);
122         }
123
124         public VisualStateGroup this[int index]
125         {
126             get => _internalList[index];
127             set => _internalList[index] = value;
128         }
129     }
130 }