From a91cd6c156d97521263da26e9cb162aa42281c97 Mon Sep 17 00:00:00 2001 From: YeongJong Lee Date: Wed, 16 Sep 2020 16:44:00 +0900 Subject: [PATCH] [NUI] remove StateValueCollection from Selector (#1975) State-Value pair is now added to Selector.StateValueList without duplicate check. You need to use Selector.StateValueList when custom state-value pair is added to Selector. and it is now able to add custom state and pre-defined state in the same initializer. Before: ``` Selector textSelector = new Selector() { Normal = "Defalut", { ControlState.Pressed, "Pressed!" }, // build error { ControlState.Focused, "Focused!" } // build error }; ``` After: ``` Selector textSelector = new Selector() { Normal = "Default!", StateValueList = { { ControlState.Pressed, "Pressed!" }, { ControlState.Focused, "Focused!" } } }; ``` Also, this patch fixes a bunch of CA2227(Collection properties should be read only) warnings. Co-authored-by: dongsug-song <35130733+dongsug-song@users.noreply.github.com> --- .../src/public/BaseComponents/Style/Selector.cs | 90 +++++++++++++------ .../BaseComponents/Style/StateValueCollection.cs | 100 --------------------- 2 files changed, 64 insertions(+), 126 deletions(-) delete mode 100644 src/Tizen.NUI/src/public/BaseComponents/Style/StateValueCollection.cs diff --git a/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs b/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs index ebf5b1f..8b155fe 100755 --- a/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs @@ -15,6 +15,7 @@ * */ using System; +using System.Collections.Generic; using System.ComponentModel; using Tizen.NUI.Binding; using Tizen.NUI.Components; @@ -27,10 +28,31 @@ namespace Tizen.NUI.BaseComponents /// 6 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. [EditorBrowsable(EditorBrowsableState.Never)] - public class Selector : StateValueCollection + public class Selector { private readonly bool cloneable = typeof(T).IsAssignableFrom(typeof(ICloneable)); + /// + /// The list for adding state-value pair. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public IList> StateValueList { get; private set; } = new List>(); + + /// + /// Adds the specified state and value to the . + /// + /// The state. + /// The value associated with state. + [EditorBrowsable(EditorBrowsableState.Never)] + public void Add(ControlState state, T value) => StateValueList.Add(new StateValuePair(state, value)); + + /// + /// Adds the specified state and value to the . + /// + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public void Add(StateValuePair stateValuePair) => StateValueList.Add(stateValuePair); + /// 6 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. public static implicit operator Selector(T value) @@ -78,7 +100,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Normal { - get => Find(x => x.State == ControlState.Normal).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Normal).Value; set => Add(ControlState.Normal, value); } /// @@ -89,7 +111,8 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Pressed { - get => Find(x => x.State == ControlState.Pressed).Value; + + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Pressed).Value; set => Add(ControlState.Pressed, value); } /// @@ -100,7 +123,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Focused { - get => Find(x => x.State == ControlState.Focused).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Focused).Value; set => Add(ControlState.Focused, value); } /// @@ -111,7 +134,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Selected { - get => Find(x => x.State == ControlState.Selected).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Selected).Value; set => Add(ControlState.Selected, value); } /// @@ -122,7 +145,8 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Disabled { - get => Find(x => x.State == ControlState.Disabled).Value; + + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Disabled).Value; set => Add(ControlState.Disabled, value); } /// @@ -133,7 +157,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T DisabledFocused { - get => Find(x => x.State == ControlState.DisabledFocused).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledFocused).Value; set => Add(ControlState.DisabledFocused, value); } /// @@ -143,7 +167,7 @@ namespace Tizen.NUI.BaseComponents /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. public T SelectedFocused { - get => Find(x => x.State == ControlState.SelectedFocused).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.SelectedFocused).Value; set => Add(ControlState.SelectedFocused, value); } /// @@ -154,7 +178,8 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T DisabledSelected { - get => Find(x => x.State == ControlState.DisabledSelected).Value; + + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledSelected).Value; set => Add(ControlState.DisabledSelected, value); } @@ -166,7 +191,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Other { - get => Find(x => x.State == ControlState.Other).Value; + get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Other).Value; set => Add(ControlState.Other, value); } /// @@ -187,7 +212,7 @@ namespace Tizen.NUI.BaseComponents result = default; - int index = StateValueList.FindIndex(x => x.State == state); + int index = ((List>)StateValueList).FindLastIndex(x => x.State == state); if (index >= 0) { result = StateValueList[index].Value; @@ -196,7 +221,7 @@ namespace Tizen.NUI.BaseComponents if (state.IsCombined) { - index = StateValueList.FindIndex(x => state.Contains(x.State)); + index = ((List>)StateValueList).FindLastIndex(x => state.Contains(x.State)); if (index >= 0) { result = StateValueList[index].Value; @@ -204,7 +229,7 @@ namespace Tizen.NUI.BaseComponents } } - index = StateValueList.FindIndex(x => x.State == ControlState.Other); + index = ((List>)StateValueList).FindLastIndex(x => x.State == ControlState.Other); if (index >= 0) { result = StateValueList[index].Value; @@ -214,12 +239,14 @@ namespace Tizen.NUI.BaseComponents return false; } - /// + /// + /// Removes all elements from + /// [EditorBrowsable(EditorBrowsableState.Never)] - public override void Clear() + public void Clear() { All = default; - base.Clear(); + StateValueList.Clear(); } /// @@ -256,23 +283,15 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public void Clone(Selector other) { - Clear(); - if (cloneable) { All = (T)((ICloneable)other.All)?.Clone(); - foreach (var item in other.StateValueList) - { - AddWithoutCheck(new StateValuePair(item.State, (T)((ICloneable)item.Value)?.Clone())); - } + StateValueList = ((List>)other.StateValueList).ConvertAll(m => new StateValuePair(m.State, (T)((ICloneable)m.Value)?.Clone())); } else { All = other.All; - foreach (var item in other.StateValueList) - { - AddWithoutCheck(item); - } + StateValueList = ((List>)other.StateValueList).ConvertAll(m => m); } } @@ -407,4 +426,23 @@ namespace Tizen.NUI.BaseComponents } } } + + /// + /// Extension class for . + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public static class SelectorExtensions + { + /// + /// Adds the specified state and value to the . + /// + /// The list for adding state-value pair. + /// The state. + /// The value associated with state. + [EditorBrowsable(EditorBrowsableState.Never)] + public static void Add(this IList> list, ControlState state, T value) + { + list.Add(new StateValuePair(state, value)); + } + } } diff --git a/src/Tizen.NUI/src/public/BaseComponents/Style/StateValueCollection.cs b/src/Tizen.NUI/src/public/BaseComponents/Style/StateValueCollection.cs deleted file mode 100644 index eb9376a..0000000 --- a/src/Tizen.NUI/src/public/BaseComponents/Style/StateValueCollection.cs +++ /dev/null @@ -1,100 +0,0 @@ -/* - * Copyright(c) 2020 Samsung Electronics Co., Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.ComponentModel; - -namespace Tizen.NUI.BaseComponents -{ - /// - /// The StateValueCollection class, which is related by , it is abstract class for . - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public abstract class StateValueCollection : ICollection> - { - - [EditorBrowsable(EditorBrowsableState.Never)] - internal List> StateValueList { get; } = new List>(); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public int Count => StateValueList.Count; - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public bool IsReadOnly => ((ICollection>)StateValueList).IsReadOnly; - - /// - /// Add a with state and value. - /// - /// The state. - /// The value associated with state. - [EditorBrowsable(EditorBrowsableState.Never)] - public void Add(ControlState state, T value) => Add(new StateValuePair(state, value)); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public void Add(StateValuePair item) - { - // To prevent a state from having multiple values, remove existing state-value pair. - int index = StateValueList.FindIndex(x => x.State == item.State); - if (index != -1) - StateValueList.RemoveAt(index); - - StateValueList.Add(item); - } - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public virtual void Clear() => StateValueList.Clear(); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public bool Contains(StateValuePair item) => StateValueList.Contains(item); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public void CopyTo(StateValuePair[] array, int arrayIndex) => StateValueList.CopyTo(array, arrayIndex); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public bool Remove(StateValuePair item) => StateValueList.Remove(item); - - /// - [EditorBrowsable(EditorBrowsableState.Never)] - public IEnumerator> GetEnumerator() => StateValueList.GetEnumerator(); - - IEnumerator IEnumerable.GetEnumerator() => StateValueList.GetEnumerator(); - - /// - /// Searches for a StateValuePair that matches the conditions defined by the specified - /// predicate, and returns the first occurrence within the entire - /// - /// The delegate that defines the conditions of the element to search for. - /// The first element that matches the conditions defined by the specified predicate, - /// if found; otherwise, the default value for type . - public StateValuePair Find(Predicate> match) => StateValueList.Find(match); - - /// - /// Add a without duplication check. - /// - /// The StateValuePair item to add. - [EditorBrowsable(EditorBrowsableState.Never)] - internal void AddWithoutCheck(StateValuePair item) => StateValueList.Add(item); - } -} \ No newline at end of file -- 2.7.4