[NUI] Add license, delete unnecessary code (#2679)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / Interactivity / AttachedCollection.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.Generic;
19 using System.Collections.ObjectModel;
20
21 namespace Tizen.NUI.Binding
22 {
23     internal class AttachedCollection<T> : ObservableCollection<T>, ICollection<T>, IAttachedObject where T : BindableObject, IAttachedObject
24     {
25         readonly List<WeakReference> _associatedObjects = new List<WeakReference>();
26
27         public AttachedCollection()
28         {
29         }
30
31         public AttachedCollection(IEnumerable<T> collection) : base(collection)
32         {
33         }
34
35         public AttachedCollection(IList<T> list) : base(list)
36         {
37         }
38
39         public void AttachTo(BindableObject bindable)
40         {
41             if (bindable == null)
42                 throw new ArgumentNullException(nameof(bindable));
43             OnAttachedTo(bindable);
44         }
45
46         public void DetachFrom(BindableObject bindable)
47         {
48             OnDetachingFrom(bindable);
49         }
50
51         protected override void ClearItems()
52         {
53             foreach (WeakReference weakbindable in _associatedObjects)
54             {
55                 foreach (T item in this)
56                 {
57                     var bindable = weakbindable.Target as BindableObject;
58                     if (bindable == null)
59                         continue;
60                     item?.DetachFrom(bindable);
61                 }
62             }
63             base.ClearItems();
64         }
65
66         protected override void InsertItem(int index, T item)
67         {
68             base.InsertItem(index, item);
69             foreach (WeakReference weakbindable in _associatedObjects)
70             {
71                 var bindable = weakbindable.Target as BindableObject;
72                 if (bindable == null)
73                     continue;
74                 item?.AttachTo(bindable);
75             }
76         }
77
78         protected virtual void OnAttachedTo(BindableObject bindable)
79         {
80             lock (_associatedObjects)
81             {
82                 _associatedObjects.Add(new WeakReference(bindable));
83             }
84             foreach (T item in this)
85                 item?.AttachTo(bindable);
86         }
87
88         protected virtual void OnDetachingFrom(BindableObject bindable)
89         {
90             foreach (T item in this)
91                 item?.DetachFrom(bindable);
92             lock (_associatedObjects)
93             {
94                 for (var i = 0; i < _associatedObjects.Count; i++)
95                 {
96                     object target = _associatedObjects[i].Target;
97
98                     if (target == null || target == bindable)
99                     {
100                         _associatedObjects.RemoveAt(i);
101                         i--;
102                     }
103                 }
104             }
105         }
106
107         protected override void RemoveItem(int index)
108         {
109             T item = this[index];
110             foreach (WeakReference weakbindable in _associatedObjects)
111             {
112                 var bindable = weakbindable.Target as BindableObject;
113                 if (bindable == null)
114                     continue;
115                 item?.DetachFrom(bindable);
116             }
117
118             base.RemoveItem(index);
119         }
120
121         protected override void SetItem(int index, T item)
122         {
123             T old = this[index];
124             foreach (WeakReference weakbindable in _associatedObjects)
125             {
126                 var bindable = weakbindable.Target as BindableObject;
127                 if (bindable == null)
128                     continue;
129                 old?.DetachFrom(bindable);
130             }
131
132             base.SetItem(index, item);
133
134             foreach (WeakReference weakbindable in _associatedObjects)
135             {
136                 var bindable = weakbindable.Target as BindableObject;
137                 if (bindable == null)
138                     continue;
139                 item?.AttachTo(bindable);
140             }
141         }
142     }
143 }