forked from dotnet/maui
/
ShellSearchResultsRenderer.cs
235 lines (192 loc) · 6.37 KB
/
ShellSearchResultsRenderer.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
#nullable disable
using System;
using System.Collections.Specialized;
using Foundation;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Extensions.Logging;
using ObjCRuntime;
using UIKit;
namespace Microsoft.Maui.Controls.Platform.Compatibility
{
public class ShellSearchResultsRenderer : UITableViewController, IShellSearchResultsRenderer
{
#region IShellSearchResultsRenderer
SearchHandler IShellSearchResultsRenderer.SearchHandler
{
get { return SearchHandler; }
set
{
SearchHandler = value;
OnSearchHandlerSet();
}
}
UIViewController IShellSearchResultsRenderer.ViewController => this;
#endregion IShellSearchResultsRenderer
readonly IShellContext _context;
DataTemplate _defaultTemplate;
// If data templates were horses, this is a donkey
DataTemplate DefaultTemplate
{
get
{
return _defaultTemplate ?? (_defaultTemplate = new DataTemplate(() =>
{
var label = new Label();
if (RuntimeFeature.IsShellSearchResultsRendererDisplayMemberNameSupported)
{
label.SetBinding(Label.TextProperty, SearchHandler.DisplayMemberName ?? ".");
}
else
{
if (SearchHandler.DisplayMemberName is not null)
{
Application.Current?.FindMauiContext()?.CreateLogger<ShellSearchResultsRenderer>().LogWarning(TrimmerConstants.SearchHandlerDisplayMemberNameNotSupportedWarning);
}
label.SetBinding(Label.TextProperty, TypedBinding.ForSingleNestingLevel(string.Empty, static (object o) => o));
}
label.HorizontalTextAlignment = TextAlignment.Center;
label.VerticalTextAlignment = TextAlignment.Center;
return label;
}));
}
}
public event EventHandler<object> ItemSelected;
public ShellSearchResultsRenderer(IShellContext context)
{
_context = context;
}
protected UITableViewRowAnimation DeleteRowsAnimation { get; set; } = UITableViewRowAnimation.Automatic;
protected UITableViewRowAnimation InsertRowsAnimation { get; set; } = UITableViewRowAnimation.Automatic;
protected UITableViewRowAnimation ReloadRowsAnimation { get; set; } = UITableViewRowAnimation.Automatic;
protected UITableViewRowAnimation ReloadSectionsAnimation { get; set; } = UITableViewRowAnimation.Automatic;
private ISearchHandlerController SearchController => SearchHandler;
private SearchHandler SearchHandler { get; set; }
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
if (SearchHandler != null)
{
var listProxy = (INotifyCollectionChanged)SearchController.ListProxy;
if (listProxy != null)
listProxy.CollectionChanged -= OnProxyCollectionChanged;
SearchController.ListProxyChanged -= OnListProxyChanged;
}
SearchHandler = null;
}
}
public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
{
var proxy = SearchController.ListProxy;
int row = indexPath.Row;
var context = proxy[row];
var template = SearchHandler.ItemTemplate;
if (template == null)
{
template = DefaultTemplate;
}
var cellId = ((IDataTemplateController)template.SelectDataTemplate(context, _context.Shell)).IdString;
var cell = (UIContainerCell)tableView.DequeueReusableCell(cellId);
if (cell == null)
{
var view = (View)template.CreateContent(context, _context.Shell);
view.BindingContext = context;
view.Parent = _context.Shell;
cell = new UIContainerCell(cellId, view);
}
else
{
cell.View.BindingContext = context;
}
return cell;
}
public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
{
var item = SearchController.ListProxy[indexPath.Row];
ItemSelected?.Invoke(this, item);
}
public override nint NumberOfSections(UITableView tableView)
{
return 1;
}
public override nint RowsInSection(UITableView tableView, nint section)
{
if (SearchController.ListProxy == null)
return 0;
return SearchController.ListProxy.Count;
}
NSIndexPath[] GetPaths(int section, int index, int count)
{
var paths = new NSIndexPath[count];
for (var i = 0; i < paths.Length; i++)
paths[i] = NSIndexPath.FromRowSection(index + i, section);
return paths;
}
void OnListProxyChanged(object sender, ListProxyChangedEventArgs e)
{
if (e.OldList != null)
{
((INotifyCollectionChanged)e.OldList).CollectionChanged -= OnProxyCollectionChanged;
}
// Full reset
TableView.ReloadData();
if (e.NewList != null)
{
((INotifyCollectionChanged)e.NewList).CollectionChanged += OnProxyCollectionChanged;
}
}
void OnProxyCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
int section = 0;
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
if (e.NewStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
TableView.BeginUpdates();
TableView.InsertRows(GetPaths(section, e.NewStartingIndex, e.NewItems.Count), InsertRowsAnimation);
TableView.EndUpdates();
break;
case NotifyCollectionChangedAction.Remove:
if (e.OldStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
TableView.BeginUpdates();
TableView.DeleteRows(GetPaths(section, e.OldStartingIndex, e.OldItems.Count), DeleteRowsAnimation);
TableView.EndUpdates();
break;
case NotifyCollectionChangedAction.Move:
if (e.OldStartingIndex == -1 || e.NewStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
TableView.BeginUpdates();
for (var i = 0; i < e.OldItems.Count; i++)
{
var oldIndex = e.OldStartingIndex;
var newIndex = e.NewStartingIndex;
if (e.NewStartingIndex < e.OldStartingIndex)
{
oldIndex += i;
newIndex += i;
}
TableView.MoveRow(NSIndexPath.FromRowSection(oldIndex, section), NSIndexPath.FromRowSection(newIndex, section));
}
TableView.EndUpdates();
break;
case NotifyCollectionChangedAction.Replace:
if (e.OldStartingIndex == -1)
goto case NotifyCollectionChangedAction.Reset;
TableView.BeginUpdates();
TableView.ReloadRows(GetPaths(section, e.OldStartingIndex, e.OldItems.Count), ReloadRowsAnimation);
TableView.EndUpdates();
break;
case NotifyCollectionChangedAction.Reset:
TableView.ReloadData();
return;
}
}
void OnSearchHandlerSet()
{
SearchController.ListProxyChanged += OnListProxyChanged;
}
}
}