Add ScriptUI to support XAML file (#320)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Interactivity / MultiCondition.cs
1 using System.Collections.Generic;
2
3 namespace Tizen.NUI.Binding
4 {
5     internal sealed class MultiCondition : Condition
6     {
7         readonly BindableProperty _aggregatedStateProperty;
8
9         public MultiCondition()
10         {
11             _aggregatedStateProperty = BindableProperty.CreateAttached("AggregatedState", typeof(bool), typeof(MultiCondition), false, propertyChanged: OnAggregatedStatePropertyChanged);
12             Conditions = new TriggerBase.SealedList<Condition>();
13         }
14
15         public IList<Condition> Conditions { get; }
16
17         internal override bool GetState(BindableObject bindable)
18         {
19             return (bool)bindable.GetValue(_aggregatedStateProperty);
20         }
21
22         internal override void OnSealed()
23         {
24             ((TriggerBase.SealedList<Condition>)Conditions).IsReadOnly = true;
25             foreach (Condition condition in Conditions)
26                 condition.ConditionChanged = OnConditionChanged;
27         }
28
29         internal override void SetUp(BindableObject bindable)
30         {
31             foreach (Condition condition in Conditions)
32                 condition.SetUp(bindable);
33         }
34
35         internal override void TearDown(BindableObject bindable)
36         {
37             foreach (Condition condition in Conditions)
38                 condition.TearDown(bindable);
39         }
40
41         void OnAggregatedStatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
42         {
43             if ((bool)oldValue == (bool)newValue)
44                 return;
45
46             ConditionChanged?.Invoke(bindable, (bool)oldValue, (bool)newValue);
47         }
48
49         void OnConditionChanged(BindableObject bindable, bool oldValue, bool newValue)
50         {
51             var oldState = (bool)bindable.GetValue(_aggregatedStateProperty);
52             var newState = true;
53             foreach (Condition condition in Conditions)
54             {
55                 if (!condition.GetState(bindable))
56                 {
57                     newState = false;
58                     break;
59                 }
60             }
61             if (newState != oldState)
62                 bindable.SetValue(_aggregatedStateProperty, newState);
63         }
64     }
65 }