-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
BindableLayout.cs
401 lines (335 loc) · 12.5 KB
/
BindableLayout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#nullable disable
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.Maui.Controls.Internals;
namespace Microsoft.Maui.Controls
{
// TODO ezhart 2021-07-16 This interface is just here to give Layout and Compatibility.Layout common ground for BindableLayout
// once we have the IContainer changes in, we may be able to drop this in favor of simply Core.ILayout
// See also IndicatorView.cs
public interface IBindableLayout
{
public IList Children { get; }
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="Type[@FullName='Microsoft.Maui.Controls.BindableLayout']/Docs/*" />
public static class BindableLayout
{
/// <summary>Bindable property for attached property <c>ItemsSource</c>.</summary>
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.CreateAttached("ItemsSource", typeof(IEnumerable), typeof(IBindableLayout), default(IEnumerable),
propertyChanged: (b, o, n) => { GetBindableLayoutController(b).ItemsSource = (IEnumerable)n; });
/// <summary>Bindable property for attached property <c>ItemTemplate</c>.</summary>
public static readonly BindableProperty ItemTemplateProperty =
BindableProperty.CreateAttached("ItemTemplate", typeof(DataTemplate), typeof(IBindableLayout), default(DataTemplate),
propertyChanged: (b, o, n) => { GetBindableLayoutController(b).ItemTemplate = (DataTemplate)n; });
/// <summary>Bindable property for attached property <c>ItemTemplateSelector</c>.</summary>
public static readonly BindableProperty ItemTemplateSelectorProperty =
BindableProperty.CreateAttached("ItemTemplateSelector", typeof(DataTemplateSelector), typeof(IBindableLayout), default(DataTemplateSelector),
propertyChanged: (b, o, n) => { GetBindableLayoutController(b).ItemTemplateSelector = (DataTemplateSelector)n; });
static readonly BindableProperty BindableLayoutControllerProperty =
BindableProperty.CreateAttached("BindableLayoutController", typeof(BindableLayoutController), typeof(IBindableLayout), default(BindableLayoutController),
defaultValueCreator: (b) => new BindableLayoutController((IBindableLayout)b),
propertyChanged: (b, o, n) => OnControllerChanged(b, (BindableLayoutController)o, (BindableLayoutController)n));
/// <summary>Bindable property for attached property <c>EmptyView</c>.</summary>
public static readonly BindableProperty EmptyViewProperty =
BindableProperty.Create("EmptyView", typeof(object), typeof(IBindableLayout), null, propertyChanged: (b, o, n) => { GetBindableLayoutController(b).EmptyView = n; });
/// <summary>Bindable property for attached property <c>EmptyViewTemplate</c>.</summary>
public static readonly BindableProperty EmptyViewTemplateProperty =
BindableProperty.Create("EmptyViewTemplate", typeof(DataTemplate), typeof(IBindableLayout), null, propertyChanged: (b, o, n) => { GetBindableLayoutController(b).EmptyViewTemplate = (DataTemplate)n; });
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='SetItemsSource']/Docs/*" />
public static void SetItemsSource(BindableObject b, IEnumerable value)
{
b.SetValue(ItemsSourceProperty, value);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='GetItemsSource']/Docs/*" />
public static IEnumerable GetItemsSource(BindableObject b)
{
return (IEnumerable)b.GetValue(ItemsSourceProperty);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='SetItemTemplate']/Docs/*" />
public static void SetItemTemplate(BindableObject b, DataTemplate value)
{
b.SetValue(ItemTemplateProperty, value);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='GetItemTemplate']/Docs/*" />
public static DataTemplate GetItemTemplate(BindableObject b)
{
return (DataTemplate)b.GetValue(ItemTemplateProperty);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='SetItemTemplateSelector']/Docs/*" />
public static void SetItemTemplateSelector(BindableObject b, DataTemplateSelector value)
{
b.SetValue(ItemTemplateSelectorProperty, value);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='GetItemTemplateSelector']/Docs/*" />
public static DataTemplateSelector GetItemTemplateSelector(BindableObject b)
{
return (DataTemplateSelector)b.GetValue(ItemTemplateSelectorProperty);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='GetEmptyView']/Docs/*" />
public static object GetEmptyView(BindableObject b)
{
return b.GetValue(EmptyViewProperty);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='SetEmptyView']/Docs/*" />
public static void SetEmptyView(BindableObject b, object value)
{
b.SetValue(EmptyViewProperty, value);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='GetEmptyViewTemplate']/Docs/*" />
public static DataTemplate GetEmptyViewTemplate(BindableObject b)
{
return (DataTemplate)b.GetValue(EmptyViewTemplateProperty);
}
/// <include file="../../docs/Microsoft.Maui.Controls/BindableLayout.xml" path="//Member[@MemberName='SetEmptyViewTemplate']/Docs/*" />
public static void SetEmptyViewTemplate(BindableObject b, DataTemplate value)
{
b.SetValue(EmptyViewTemplateProperty, value);
}
internal static BindableLayoutController GetBindableLayoutController(BindableObject b)
{
return (BindableLayoutController)b.GetValue(BindableLayoutControllerProperty);
}
static void SetBindableLayoutController(BindableObject b, BindableLayoutController value)
{
b.SetValue(BindableLayoutControllerProperty, value);
}
static void OnControllerChanged(BindableObject b, BindableLayoutController oldC, BindableLayoutController newC)
{
if (oldC != null)
{
oldC.ItemsSource = null;
}
if (newC == null)
{
return;
}
newC.StartBatchUpdate();
newC.ItemsSource = GetItemsSource(b);
newC.ItemTemplate = GetItemTemplate(b);
newC.ItemTemplateSelector = GetItemTemplateSelector(b);
newC.EmptyView = GetEmptyView(b);
newC.EmptyViewTemplate = GetEmptyViewTemplate(b);
newC.EndBatchUpdate();
}
internal static void Add(this IBindableLayout layout, object item)
{
if (layout is Maui.ILayout mauiLayout && item is IView view)
{
mauiLayout.Add(view);
}
else
{
_ = layout.Children.Add(item);
}
}
internal static void Insert(this IBindableLayout layout, object item, int index)
{
if (layout is Maui.ILayout mauiLayout && item is IView view)
{
mauiLayout.Insert(index, view);
}
else
{
layout.Children.Insert(index, item);
}
}
internal static void Remove(this IBindableLayout layout, object item)
{
if (layout is Maui.ILayout mauiLayout && item is IView view)
{
_ = mauiLayout.Remove(view);
}
else
{
layout.Children.Remove(item);
}
}
internal static void RemoveAt(this IBindableLayout layout, int index)
{
if (layout is Maui.ILayout mauiLayout)
{
mauiLayout.RemoveAt(index);
}
else
{
layout.Children.RemoveAt(index);
}
}
internal static void Clear(this IBindableLayout layout)
{
if (layout is Maui.ILayout mauiLayout)
{
mauiLayout.Clear();
}
else
{
layout.Children.Clear();
}
}
}
class BindableLayoutController
{
readonly WeakReference<IBindableLayout> _layoutWeakReference;
readonly WeakNotifyCollectionChangedProxy _collectionChangedProxy = new();
readonly NotifyCollectionChangedEventHandler _collectionChangedEventHandler;
IEnumerable _itemsSource;
DataTemplate _itemTemplate;
DataTemplateSelector _itemTemplateSelector;
bool _isBatchUpdate;
object _emptyView;
DataTemplate _emptyViewTemplate;
View _currentEmptyView;
public IEnumerable ItemsSource { get => _itemsSource; set => SetItemsSource(value); }
public DataTemplate ItemTemplate { get => _itemTemplate; set => SetItemTemplate(value); }
public DataTemplateSelector ItemTemplateSelector { get => _itemTemplateSelector; set => SetItemTemplateSelector(value); }
public object EmptyView { get => _emptyView; set => SetEmptyView(value); }
public DataTemplate EmptyViewTemplate { get => _emptyViewTemplate; set => SetEmptyViewTemplate(value); }
public BindableLayoutController(IBindableLayout layout)
{
_layoutWeakReference = new WeakReference<IBindableLayout>(layout);
_collectionChangedEventHandler = ItemsSourceCollectionChanged;
}
~BindableLayoutController() => _collectionChangedProxy.Unsubscribe();
internal void StartBatchUpdate()
{
_isBatchUpdate = true;
}
internal void EndBatchUpdate()
{
_isBatchUpdate = false;
CreateChildren();
}
void SetItemsSource(IEnumerable itemsSource)
{
if (_itemsSource is INotifyCollectionChanged)
{
_collectionChangedProxy.Unsubscribe();
}
_itemsSource = itemsSource;
if (_itemsSource is INotifyCollectionChanged c)
{
_collectionChangedProxy.Subscribe(c, _collectionChangedEventHandler);
}
if (!_isBatchUpdate)
{
CreateChildren();
}
}
void SetItemTemplate(DataTemplate itemTemplate)
{
if (itemTemplate is DataTemplateSelector)
{
throw new NotSupportedException($"You are using an instance of {nameof(DataTemplateSelector)} to set the {nameof(BindableLayout)}.{BindableLayout.ItemTemplateProperty.PropertyName} property. Use {nameof(BindableLayout)}.{BindableLayout.ItemTemplateSelectorProperty.PropertyName} property instead to set an item template selector");
}
_itemTemplate = itemTemplate;
if (!_isBatchUpdate)
{
CreateChildren();
}
}
void SetItemTemplateSelector(DataTemplateSelector itemTemplateSelector)
{
_itemTemplateSelector = itemTemplateSelector;
if (!_isBatchUpdate)
{
CreateChildren();
}
}
void SetEmptyView(object emptyView)
{
_emptyView = emptyView;
_currentEmptyView = CreateEmptyView(_emptyView, _emptyViewTemplate);
if (!_isBatchUpdate)
{
CreateChildren();
}
}
void SetEmptyViewTemplate(DataTemplate emptyViewTemplate)
{
_emptyViewTemplate = emptyViewTemplate;
_currentEmptyView = CreateEmptyView(_emptyView, _emptyViewTemplate);
if (!_isBatchUpdate)
{
CreateChildren();
}
}
void CreateChildren()
{
if (!_layoutWeakReference.TryGetTarget(out IBindableLayout layout))
{
return;
}
layout.Clear();
UpdateEmptyView(layout);
if (_itemsSource == null)
return;
foreach (object item in _itemsSource)
{
layout.Add(CreateItemView(item, layout));
}
}
void UpdateEmptyView(IBindableLayout layout)
{
if (_currentEmptyView == null)
return;
if (!_itemsSource?.GetEnumerator().MoveNext() ?? true)
{
layout.Add(_currentEmptyView);
return;
}
layout.Remove(_currentEmptyView);
}
View CreateItemView(object item, IBindableLayout layout)
{
return CreateItemView(item, _itemTemplate ?? _itemTemplateSelector?.SelectTemplate(item, layout as BindableObject));
}
View CreateItemView(object item, DataTemplate dataTemplate)
{
if (dataTemplate != null)
{
var view = (View)dataTemplate.CreateContent();
view.BindingContext = item;
return view;
}
else
{
return new Label { Text = item?.ToString(), HorizontalTextAlignment = TextAlignment.Center };
}
}
View CreateEmptyView(object emptyView, DataTemplate dataTemplate)
{
if (!_layoutWeakReference.TryGetTarget(out IBindableLayout layout))
{
return null;
}
if (dataTemplate != null)
{
var view = (View)dataTemplate.CreateContent();
view.BindingContext = (layout as BindableObject).BindingContext;
return view;
}
if (emptyView is View emptyLayout)
{
return emptyLayout;
}
return new Label { Text = emptyView?.ToString(), HorizontalTextAlignment = TextAlignment.Center };
}
void ItemsSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (!_layoutWeakReference.TryGetTarget(out IBindableLayout layout))
{
return;
}
e.Apply(
insert: (item, index, _) => layout.Insert(CreateItemView(item, layout), index),
removeAt: (item, index) => layout.RemoveAt(index),
reset: CreateChildren);
// UpdateEmptyView is called from within CreateChildren, therefor skip it for Reset
if (e.Action != NotifyCollectionChangedAction.Reset)
UpdateEmptyView(layout);
}
}
}