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.Generic;
19 using System.Collections.ObjectModel;
21 namespace Tizen.NUI.Binding
23 internal class AttachedCollection<T> : ObservableCollection<T>, ICollection<T>, IAttachedObject where T : BindableObject, IAttachedObject
25 readonly List<WeakReference> _associatedObjects = new List<WeakReference>();
27 public AttachedCollection()
31 public AttachedCollection(IEnumerable<T> collection) : base(collection)
35 public AttachedCollection(IList<T> list) : base(list)
39 public void AttachTo(BindableObject bindable)
42 throw new ArgumentNullException(nameof(bindable));
43 OnAttachedTo(bindable);
46 public void DetachFrom(BindableObject bindable)
48 OnDetachingFrom(bindable);
51 protected override void ClearItems()
53 foreach (WeakReference weakbindable in _associatedObjects)
55 foreach (T item in this)
57 var bindable = weakbindable.Target as BindableObject;
60 item?.DetachFrom(bindable);
66 protected override void InsertItem(int index, T item)
68 base.InsertItem(index, item);
69 foreach (WeakReference weakbindable in _associatedObjects)
71 var bindable = weakbindable.Target as BindableObject;
74 item?.AttachTo(bindable);
78 protected virtual void OnAttachedTo(BindableObject bindable)
80 lock (_associatedObjects)
82 _associatedObjects.Add(new WeakReference(bindable));
84 foreach (T item in this)
85 item?.AttachTo(bindable);
88 protected virtual void OnDetachingFrom(BindableObject bindable)
90 foreach (T item in this)
91 item?.DetachFrom(bindable);
92 lock (_associatedObjects)
94 for (var i = 0; i < _associatedObjects.Count; i++)
96 object target = _associatedObjects[i].Target;
98 if (target == null || target == bindable)
100 _associatedObjects.RemoveAt(i);
107 protected override void RemoveItem(int index)
109 T item = this[index];
110 foreach (WeakReference weakbindable in _associatedObjects)
112 var bindable = weakbindable.Target as BindableObject;
113 if (bindable == null)
115 item?.DetachFrom(bindable);
118 base.RemoveItem(index);
121 protected override void SetItem(int index, T item)
124 foreach (WeakReference weakbindable in _associatedObjects)
126 var bindable = weakbindable.Target as BindableObject;
127 if (bindable == null)
129 old?.DetachFrom(bindable);
132 base.SetItem(index, item);
134 foreach (WeakReference weakbindable in _associatedObjects)
136 var bindable = weakbindable.Target as BindableObject;
137 if (bindable == null)
139 item?.AttachTo(bindable);