2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Collections.ObjectModel;
23 using Tizen.NUI.Binding;
25 namespace Tizen.NUI.Xaml
27 internal class VisualStateGroupList : IList<VisualStateGroup>
29 readonly IList<VisualStateGroup> _internalList;
31 void Validate(IList<VisualStateGroup> groups)
33 // If we have 1 group, no need to worry about duplicate group names
36 if (groups.GroupBy(vsg => vsg.Name).Any(g => g.Count() > 1))
38 throw new InvalidOperationException("VisualStateGroup Names must be unique");
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
45 if (groups.SelectMany(group => group.States).GroupBy(state => state.Name).Any(g => g.Count() > 1))
47 throw new InvalidOperationException("VisualState Names must be unique");
51 public VisualStateGroupList()
53 _internalList = new WatchAddList<VisualStateGroup>(Validate);
56 void ValidateOnStatesChanged(object sender, EventArgs eventArgs)
58 Validate(_internalList);
61 public IEnumerator<VisualStateGroup> GetEnumerator()
63 return _internalList.GetEnumerator();
66 IEnumerator IEnumerable.GetEnumerator()
68 return ((IEnumerable)_internalList).GetEnumerator();
71 public void Add(VisualStateGroup item)
73 _internalList.Add(item);
74 item.StatesChanged += ValidateOnStatesChanged;
79 foreach (var group in _internalList)
81 group.StatesChanged -= ValidateOnStatesChanged;
84 _internalList.Clear();
87 public bool Contains(VisualStateGroup item)
89 return _internalList.Contains(item);
92 public void CopyTo(VisualStateGroup[] array, int arrayIndex)
94 _internalList.CopyTo(array, arrayIndex);
97 public bool Remove(VisualStateGroup item)
99 item.StatesChanged -= ValidateOnStatesChanged;
100 return _internalList.Remove(item);
103 public int Count => _internalList.Count;
105 public bool IsReadOnly => false;
107 public int IndexOf(VisualStateGroup item)
109 return _internalList.IndexOf(item);
112 public void Insert(int index, VisualStateGroup item)
114 item.StatesChanged += ValidateOnStatesChanged;
115 _internalList.Insert(index, item);
118 public void RemoveAt(int index)
120 _internalList[index].StatesChanged -= ValidateOnStatesChanged;
121 _internalList.RemoveAt(index);
124 public VisualStateGroup this[int index]
126 get => _internalList[index];
127 set => _internalList[index] = value;