From: YeongJong Lee Date: Thu, 29 Oct 2020 07:32:13 +0000 (+0900) Subject: [NUI] avoid duplicated elements in Selector (#2122) X-Git-Tag: accepted/tizen/unified/20210219.040944~313 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d0c101f074bcce710226d55ff9c81d451ce2d51e;p=platform%2Fcore%2Fcsapi%2Ftizenfx.git [NUI] avoid duplicated elements in Selector (#2122) Co-authored-by: Jiyun Yang --- diff --git a/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs b/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs index 4d92571..7c4b431 100755 --- a/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs +++ b/src/Tizen.NUI/src/public/BaseComponents/Style/Selector.cs @@ -37,22 +37,30 @@ namespace Tizen.NUI.BaseComponents /// The list for adding state-value pair. /// [EditorBrowsable(EditorBrowsableState.Never)] - public IList> StateValueList { get; private set; } = new List>(); + List> StateValueList { get; set; } = new List>(); /// - /// Adds the specified state and value to the . + /// Adds the specified state and value. /// /// The state. /// The value associated with state. [EditorBrowsable(EditorBrowsableState.Never)] - public void Add(ControlState state, T value) => StateValueList.Add(new StateValuePair(state, value)); + public void Add(ControlState state, T value) => Add(new StateValuePair(state, value)); /// - /// Adds the specified state and value to the . + /// Adds the specified state and value. /// /// [EditorBrowsable(EditorBrowsableState.Never)] - public void Add(StateValuePair stateValuePair) => StateValueList.Add(stateValuePair); + public void Add(StateValuePair stateValuePair) + { + // To prevent a state from having multiple values, remove existing state-value pair. + int index = StateValueList.FindIndex(x => x.State == stateValuePair.State); + if (index != -1) + StateValueList.RemoveAt(index); + + 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. @@ -100,7 +108,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Normal { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Normal).Value; + get => StateValueList.Find(x => x.State == ControlState.Normal).Value; set => Add(ControlState.Normal, value); } /// @@ -112,7 +120,7 @@ namespace Tizen.NUI.BaseComponents public T Pressed { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Pressed).Value; + get => StateValueList.Find(x => x.State == ControlState.Pressed).Value; set => Add(ControlState.Pressed, value); } /// @@ -123,7 +131,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Focused { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Focused).Value; + get => StateValueList.Find(x => x.State == ControlState.Focused).Value; set => Add(ControlState.Focused, value); } /// @@ -134,7 +142,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Selected { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Selected).Value; + get => StateValueList.Find(x => x.State == ControlState.Selected).Value; set => Add(ControlState.Selected, value); } /// @@ -146,7 +154,7 @@ namespace Tizen.NUI.BaseComponents public T Disabled { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Disabled).Value; + get => StateValueList.Find(x => x.State == ControlState.Disabled).Value; set => Add(ControlState.Disabled, value); } /// @@ -157,7 +165,7 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T DisabledFocused { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledFocused).Value; + get => StateValueList.Find(x => x.State == ControlState.DisabledFocused).Value; set => Add(ControlState.DisabledFocused, value); } /// @@ -167,7 +175,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 => ((List>)StateValueList).FindLast(x => x.State == ControlState.SelectedFocused).Value; + get => StateValueList.Find(x => x.State == ControlState.SelectedFocused).Value; set => Add(ControlState.SelectedFocused, value); } /// @@ -179,7 +187,7 @@ namespace Tizen.NUI.BaseComponents public T DisabledSelected { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.DisabledSelected).Value; + get => StateValueList.Find(x => x.State == ControlState.DisabledSelected).Value; set => Add(ControlState.DisabledSelected, value); } @@ -191,9 +199,16 @@ namespace Tizen.NUI.BaseComponents [EditorBrowsable(EditorBrowsableState.Never)] public T Other { - get => ((List>)StateValueList).FindLast(x => x.State == ControlState.Other).Value; + get => StateValueList.Find(x => x.State == ControlState.Other).Value; set => Add(ControlState.Other, value); } + + /// + /// Gets the number of elements. + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public int Count => StateValueList.Count; + /// /// Get value by State. /// @@ -212,7 +227,7 @@ namespace Tizen.NUI.BaseComponents result = default; - int index = ((List>)StateValueList).FindLastIndex(x => x.State == state); + int index = StateValueList.FindIndex(x => x.State == state); if (index >= 0) { result = StateValueList[index].Value; @@ -221,7 +236,7 @@ namespace Tizen.NUI.BaseComponents if (state.IsCombined) { - index = ((List>)StateValueList).FindLastIndex(x => state.Contains(x.State)); + index = StateValueList.FindIndex(x => state.Contains(x.State)); if (index >= 0) { result = StateValueList[index].Value; @@ -229,7 +244,7 @@ namespace Tizen.NUI.BaseComponents } } - index = ((List>)StateValueList).FindLastIndex(x => x.State == ControlState.Other); + index = StateValueList.FindIndex(x => x.State == ControlState.Other); if (index >= 0) { result = StateValueList[index].Value; @@ -240,7 +255,7 @@ namespace Tizen.NUI.BaseComponents } /// - /// Removes all elements from . + /// Removes all elements. /// [EditorBrowsable(EditorBrowsableState.Never)] public void Clear() @@ -286,12 +301,12 @@ namespace Tizen.NUI.BaseComponents if (cloneable) { All = (T)((ICloneable)other.All)?.Clone(); - StateValueList = ((List>)other.StateValueList).ConvertAll(m => new StateValuePair(m.State, (T)((ICloneable)m.Value)?.Clone())); + StateValueList = other.StateValueList.ConvertAll(m => new StateValuePair(m.State, (T)((ICloneable)m.Value)?.Clone())); } else { All = other.All; - StateValueList = ((List>)other.StateValueList).ConvertAll(m => m); + StateValueList = other.StateValueList.ConvertAll(m => m); } } @@ -299,6 +314,7 @@ namespace Tizen.NUI.BaseComponents { return StateValueList.Count > 1; } + } /// @@ -426,23 +442,4 @@ 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)); - } - } }