2
2
using System . Collections . Generic ;
3
3
using System . Collections . ObjectModel ;
4
4
using System . Collections . Specialized ;
5
- using JetBrains . Annotations ;
6
5
using UnityEngine . UIElements ;
7
6
using UnityMvvmToolkit . Common . Interfaces ;
8
7
using UnityMvvmToolkit . Core ;
@@ -18,14 +17,17 @@ public abstract partial class BindableListView<TItemBindingContext> : ListView,
18
17
private VisualTreeAsset _itemTemplate ;
19
18
20
19
private PropertyBindingData _itemsSourceBindingData ;
21
- private IReadOnlyProperty < ObservableCollection < TItemBindingContext > > _itemsSource ;
20
+ private ObservableCollection < TItemBindingContext > _itemsSource ;
21
+ private IReadOnlyProperty < ObservableCollection < TItemBindingContext > > _itemsSourceProperty ;
22
22
23
23
private IObjectProvider _objectProvider ;
24
24
private List < VisualElement > _itemAssets ;
25
+ private Dictionary < int , TItemBindingContext > _activeItems ;
25
26
26
27
public virtual void Initialize ( )
27
28
{
28
29
_itemAssets = new List < VisualElement > ( ) ;
30
+ _activeItems = new Dictionary < int , TItemBindingContext > ( ) ;
29
31
}
30
32
31
33
public virtual void Dispose ( )
@@ -36,6 +38,7 @@ public virtual void Dispose()
36
38
}
37
39
38
40
_itemAssets . Clear ( ) ;
41
+ _activeItems . Clear ( ) ;
39
42
}
40
43
41
44
public virtual void SetBindingContext ( IBindingContext context , IObjectProvider objectProvider )
@@ -50,35 +53,38 @@ public virtual void SetBindingContext(IBindingContext context, IObjectProvider o
50
53
51
54
_objectProvider = objectProvider ;
52
55
53
- _itemsSource = objectProvider
56
+ _itemsSourceProperty = objectProvider
54
57
. RentReadOnlyProperty < ObservableCollection < TItemBindingContext > > ( context , _itemsSourceBindingData ) ;
55
- _itemsSource . Value . CollectionChanged += OnItemsCollectionChanged ;
58
+ _itemsSource = _itemsSourceProperty . Value ;
59
+ _itemsSource . CollectionChanged += OnItemsCollectionChanged ;
56
60
57
- itemsSource = _itemsSource . Value ;
61
+ itemsSource = _itemsSource ;
58
62
makeItem += OnMakeItem ;
59
63
bindItem += OnBindItem ;
60
64
unbindItem += OnUnbindItem ;
61
65
}
62
66
63
67
public virtual void ResetBindingContext ( IObjectProvider objectProvider )
64
68
{
65
- if ( _itemsSource is null )
69
+ if ( _itemsSourceProperty is null )
66
70
{
67
71
return ;
68
72
}
69
73
70
- _itemsSource . Value . CollectionChanged -= OnItemsCollectionChanged ;
74
+ _itemsSource . CollectionChanged -= OnItemsCollectionChanged ;
71
75
72
76
makeItem -= OnMakeItem ;
73
77
bindItem -= OnBindItem ;
74
78
unbindItem -= OnUnbindItem ;
75
79
itemsSource = Array . Empty < TItemBindingContext > ( ) ;
76
80
77
- objectProvider . ReturnReadOnlyProperty ( _itemsSource ) ;
81
+ objectProvider . ReturnReadOnlyProperty ( _itemsSourceProperty ) ;
78
82
79
- _itemsSource = null ;
80
83
_itemTemplate = null ;
81
84
_objectProvider = null ;
85
+
86
+ _itemsSource = null ;
87
+ _itemsSourceProperty = null ;
82
88
}
83
89
84
90
protected virtual VisualElement MakeItem ( VisualTreeAsset itemTemplate )
@@ -94,22 +100,22 @@ protected virtual void BindItem(VisualElement item, int index, TItemBindingConte
94
100
item . SetChildsBindingContext ( bindingContext , objectProvider ) ;
95
101
}
96
102
97
- protected virtual void UnbindItem ( VisualElement item , int index , [ CanBeNull ] TItemBindingContext bindingContext ,
103
+ protected virtual void UnbindItem ( VisualElement item , int index , TItemBindingContext bindingContext ,
98
104
IObjectProvider objectProvider )
99
105
{
100
106
item . ResetChildsBindingContext ( objectProvider ) ;
101
107
}
102
108
103
109
protected virtual void OnItemsCollectionChanged ( object sender , NotifyCollectionChangedEventArgs e )
104
110
{
105
- if ( e . Action == NotifyCollectionChangedAction . Remove )
111
+ #if UNITY_2021
112
+ if ( e . Action is NotifyCollectionChangedAction . Remove or NotifyCollectionChangedAction . Reset )
106
113
{
107
114
Rebuild ( ) ;
115
+ return ;
108
116
}
109
- else
110
- {
111
- RefreshItems ( ) ;
112
- }
117
+ #endif
118
+ RefreshItems ( ) ;
113
119
}
114
120
115
121
private VisualElement OnMakeItem ( )
@@ -123,18 +129,21 @@ private VisualElement OnMakeItem()
123
129
124
130
private void OnBindItem ( VisualElement item , int index )
125
131
{
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 ) ;
127
138
}
128
139
129
140
private void OnUnbindItem ( VisualElement item , int index )
130
141
{
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 ) )
136
145
{
137
- UnbindItem ( item , index , default , _objectProvider ) ;
146
+ UnbindItem ( item , index , itemBindingContext , _objectProvider ) ;
138
147
}
139
148
}
140
149
}
0 commit comments