22using System . Collections . Generic ;
33using System . Collections . ObjectModel ;
44using System . Collections . Specialized ;
5- using JetBrains . Annotations ;
65using UnityEngine . UIElements ;
76using UnityMvvmToolkit . Common . Interfaces ;
87using UnityMvvmToolkit . Core ;
@@ -18,14 +17,17 @@ public abstract partial class BindableListView<TItemBindingContext> : ListView,
1817 private VisualTreeAsset _itemTemplate ;
1918
2019 private PropertyBindingData _itemsSourceBindingData ;
21- private IReadOnlyProperty < ObservableCollection < TItemBindingContext > > _itemsSource ;
20+ private ObservableCollection < TItemBindingContext > _itemsSource ;
21+ private IReadOnlyProperty < ObservableCollection < TItemBindingContext > > _itemsSourceProperty ;
2222
2323 private IObjectProvider _objectProvider ;
2424 private List < VisualElement > _itemAssets ;
25+ private Dictionary < int , TItemBindingContext > _activeItems ;
2526
2627 public virtual void Initialize ( )
2728 {
2829 _itemAssets = new List < VisualElement > ( ) ;
30+ _activeItems = new Dictionary < int , TItemBindingContext > ( ) ;
2931 }
3032
3133 public virtual void Dispose ( )
@@ -36,6 +38,7 @@ public virtual void Dispose()
3638 }
3739
3840 _itemAssets . Clear ( ) ;
41+ _activeItems . Clear ( ) ;
3942 }
4043
4144 public virtual void SetBindingContext ( IBindingContext context , IObjectProvider objectProvider )
@@ -50,35 +53,38 @@ public virtual void SetBindingContext(IBindingContext context, IObjectProvider o
5053
5154 _objectProvider = objectProvider ;
5255
53- _itemsSource = objectProvider
56+ _itemsSourceProperty = objectProvider
5457 . RentReadOnlyProperty < ObservableCollection < TItemBindingContext > > ( context , _itemsSourceBindingData ) ;
55- _itemsSource . Value . CollectionChanged += OnItemsCollectionChanged ;
58+ _itemsSource = _itemsSourceProperty . Value ;
59+ _itemsSource . CollectionChanged += OnItemsCollectionChanged ;
5660
57- itemsSource = _itemsSource . Value ;
61+ itemsSource = _itemsSource ;
5862 makeItem += OnMakeItem ;
5963 bindItem += OnBindItem ;
6064 unbindItem += OnUnbindItem ;
6165 }
6266
6367 public virtual void ResetBindingContext ( IObjectProvider objectProvider )
6468 {
65- if ( _itemsSource is null )
69+ if ( _itemsSourceProperty is null )
6670 {
6771 return ;
6872 }
6973
70- _itemsSource . Value . CollectionChanged -= OnItemsCollectionChanged ;
74+ _itemsSource . CollectionChanged -= OnItemsCollectionChanged ;
7175
7276 makeItem -= OnMakeItem ;
7377 bindItem -= OnBindItem ;
7478 unbindItem -= OnUnbindItem ;
7579 itemsSource = Array . Empty < TItemBindingContext > ( ) ;
7680
77- objectProvider . ReturnReadOnlyProperty ( _itemsSource ) ;
81+ objectProvider . ReturnReadOnlyProperty ( _itemsSourceProperty ) ;
7882
79- _itemsSource = null ;
8083 _itemTemplate = null ;
8184 _objectProvider = null ;
85+
86+ _itemsSource = null ;
87+ _itemsSourceProperty = null ;
8288 }
8389
8490 protected virtual VisualElement MakeItem ( VisualTreeAsset itemTemplate )
@@ -94,22 +100,22 @@ protected virtual void BindItem(VisualElement item, int index, TItemBindingConte
94100 item . SetChildsBindingContext ( bindingContext , objectProvider ) ;
95101 }
96102
97- protected virtual void UnbindItem ( VisualElement item , int index , [ CanBeNull ] TItemBindingContext bindingContext ,
103+ protected virtual void UnbindItem ( VisualElement item , int index , TItemBindingContext bindingContext ,
98104 IObjectProvider objectProvider )
99105 {
100106 item . ResetChildsBindingContext ( objectProvider ) ;
101107 }
102108
103109 protected virtual void OnItemsCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
104110 {
105- if ( e . Action == NotifyCollectionChangedAction . Remove )
111+ #if UNITY_2021
112+ if ( e . Action is NotifyCollectionChangedAction . Remove or NotifyCollectionChangedAction . Reset )
106113 {
107114 Rebuild ( ) ;
115+ return ;
108116 }
109- else
110- {
111- RefreshItems ( ) ;
112- }
117+ #endif
118+ RefreshItems ( ) ;
113119 }
114120
115121 private VisualElement OnMakeItem ( )
@@ -123,18 +129,21 @@ private VisualElement OnMakeItem()
123129
124130 private void OnBindItem ( VisualElement item , int index )
125131 {
126- BindItem ( item , index , _itemsSource . Value [ index ] , _objectProvider ) ;
132+ var itemId = item . GetHashCode ( ) ;
133+ var itemBindingContext = _itemsSource [ index ] ;
134+
135+ _activeItems . Add ( itemId , itemBindingContext ) ;
136+
137+ BindItem ( item , index , itemBindingContext , _objectProvider ) ;
127138 }
128139
129140 private void OnUnbindItem ( VisualElement item , int index )
130141 {
131- if ( index >= 0 && index < itemsSource . Count )
132- {
133- UnbindItem ( item , index , _itemsSource . Value [ index ] , _objectProvider ) ;
134- }
135- else
142+ var itemId = item . GetHashCode ( ) ;
143+
144+ if ( _activeItems . Remove ( itemId , out var itemBindingContext ) )
136145 {
137- UnbindItem ( item , index , default , _objectProvider ) ;
146+ UnbindItem ( item , index , itemBindingContext , _objectProvider ) ;
138147 }
139148 }
140149 }
0 commit comments