This repository was archived by the owner on Jul 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathDialogViewController.cs
More file actions
342 lines (284 loc) · 10.9 KB
/
DialogViewController.cs
File metadata and controls
342 lines (284 loc) · 10.9 KB
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
//
// DialogViewController.cs: drives MonoTouch.Dialog
//
// Author:
// Miguel de Icaza
//
// Code to support pull-to-refresh based on Martin Bowling's TweetTableView
// which is based in turn in EGOTableViewPullRefresh code which was created
// by Devin Doty and is Copyrighted 2009 enormego and released under the
// MIT X11 license
//
using System;
using UIKit;
using CoreGraphics;
using System.Collections.Generic;
using Foundation;
using CodeHub.iOS.DialogElements;
using System.Linq;
namespace CodeHub.iOS.ViewControllers
{
/// <summary>
/// The DialogViewController is the main entry point to use MonoTouch.Dialog,
/// it provides a simplified API to the UITableViewController.
/// </summary>
public class DialogViewController : TableViewController
{
private readonly Lazy<RootElement> _rootElement;
private UISearchBar _searchBar;
bool pushing;
public RootElement Root => _rootElement.Value;
public bool EnableSearch { get; set; }
public string SearchPlaceholder { get; set; }
public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
{
base.DidRotate (fromInterfaceOrientation);
ReloadData ();
}
Section [] originalSections;
Element [][] originalElements;
/// <summary>
/// Allows caller to programatically activate the search bar and start the search process
/// </summary>
public void StartSearch ()
{
if (originalSections != null)
return;
_searchBar.BecomeFirstResponder ();
CreateOriginals(Root);
}
private void CreateOriginals(RootElement root)
{
originalSections = root.Sections.ToArray ();
originalElements = new Element [originalSections.Length][];
for (int i = 0; i < originalSections.Length; i++)
originalElements [i] = originalSections [i].Elements.ToArray ();
}
/// <summary>
/// Allows the caller to programatically stop searching.
/// </summary>
public virtual void FinishSearch ()
{
if (originalSections == null)
return;
_searchBar.Text = "";
Root.Reset(originalSections);
originalSections = null;
originalElements = null;
_searchBar.ResignFirstResponder ();
ReloadData ();
}
public void PerformFilter (string text)
{
if (originalSections == null)
return;
var newSections = new List<Section> ();
for (int sidx = 0; sidx < originalSections.Length; sidx++){
Section newSection = null;
var section = originalSections [sidx];
Element [] elements = originalElements [sidx];
for (int eidx = 0; eidx < elements.Length; eidx++){
if (elements [eidx].Matches (text)){
if (newSection == null){
newSection = new Section (section.Header, section.Footer){
FooterView = section.FooterView,
HeaderView = section.HeaderView
};
newSections.Add (newSection);
}
newSection.Add (elements [eidx]);
}
}
}
Root.Reset(newSections);
ReloadData ();
}
public virtual void SearchButtonClicked (string text)
{
_searchBar.ResignFirstResponder();
}
protected class SearchDelegate : UISearchBarDelegate {
readonly WeakReference<DialogViewController> container;
public SearchDelegate (DialogViewController container)
{
this.container = new WeakReference<DialogViewController>(container);
}
public override void OnEditingStarted (UISearchBar searchBar)
{
searchBar.ShowsCancelButton = true;
container.Get()?.StartSearch ();
}
public override void OnEditingStopped (UISearchBar searchBar)
{
searchBar.ShowsCancelButton = false;
//container.FinishSearch ();
}
public override void TextChanged (UISearchBar searchBar, string searchText)
{
container.Get()?.PerformFilter (searchText ?? "");
}
public override void CancelButtonClicked (UISearchBar searchBar)
{
var r = container.Get();
searchBar.ShowsCancelButton = false;
if (r != null)
{
r._searchBar.Text = "";
r.FinishSearch();
}
searchBar.ResignFirstResponder ();
}
public override void SearchButtonClicked (UISearchBar searchBar)
{
container.Get()?.SearchButtonClicked (searchBar.Text);
}
}
protected virtual void DidScroll(CGPoint p)
{
}
public class Source : UITableViewSource {
private readonly WeakReference<DialogViewController> _container;
public RootElement Root => _container.Get()?.Root;
public DialogViewController Container
{
get { return _container.Get(); }
}
public Source (DialogViewController container)
{
_container = new WeakReference<DialogViewController>(container);
}
public override nint RowsInSection (UITableView tableview, nint section)
{
var s = Root?[(int)section];
var count = s?.Elements.Count;
return count ?? 0;
}
public override nint NumberOfSections (UITableView tableView)
{
return Root?.Count ?? 0;
}
public override string TitleForHeader (UITableView tableView, nint section)
{
return Root?[(int)section]?.Header;
}
public override string TitleForFooter (UITableView tableView, nint section)
{
return Root?[(int)section]?.Footer;
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
var section = Root?[indexPath.Section];
var element = section?[indexPath.Row];
return element?.GetCell (tableView);
}
public override void RowDeselected (UITableView tableView, NSIndexPath indexPath)
{
_container.Get()?.Deselected (indexPath);
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
_container.Get()?.Selected (indexPath);
}
public override UIView GetViewForHeader (UITableView tableView, nint sectionIdx)
{
var section = Root?[(int)sectionIdx];
return section?.HeaderView;
}
public override nfloat GetHeightForHeader (UITableView tableView, nint sectionIdx)
{
var section = Root?[(int)sectionIdx];
return section?.HeaderView?.Frame.Height ?? -1;
}
public override UIView GetViewForFooter (UITableView tableView, nint sectionIdx)
{
var section = Root?[(int)sectionIdx];
return section?.FooterView;
}
public override nfloat GetHeightForFooter (UITableView tableView, nint sectionIdx)
{
var section = Root?[(int)sectionIdx];
return section?.FooterView?.Frame.Height ?? -1;
}
public override void Scrolled (UIScrollView scrollView)
{
_container.Get()?.DidScroll(Root.TableView.ContentOffset);
}
public override nfloat GetHeightForRow (UITableView tableView, NSIndexPath indexPath)
{
var section = Root?[indexPath.Section];
var element = section?[indexPath.Row];
var sizable = element as IElementSizing;
return sizable?.GetHeight(tableView, indexPath) ?? tableView.RowHeight;
}
}
protected virtual IUISearchBarDelegate CreateSearchDelegate()
{
return new SearchDelegate(this);
}
void SetupSearch ()
{
if (EnableSearch){
_searchBar = new UISearchBar (new CGRect (0, 0, TableView.Bounds.Width, 44)) {
Delegate = CreateSearchDelegate()
};
if (SearchPlaceholder != null)
_searchBar.Placeholder = this.SearchPlaceholder;
TableView.TableHeaderView = _searchBar;
} else {
// Does not work with current Monotouch, will work with 3.0
// tableView.TableHeaderView = null;
}
}
public virtual void Deselected (NSIndexPath indexPath)
{
var section = Root[indexPath.Section];
var element = section[indexPath.Row];
element.Deselected (TableView, indexPath);
}
public virtual void Selected (NSIndexPath indexPath)
{
var section = Root[indexPath.Section];
var element = section[indexPath.Row];
element.Selected (TableView, indexPath);
}
public virtual Source CreateSizingSource()
{
return new Source (this);
}
public override void LoadView ()
{
base.LoadView();
SetupSearch ();
TableView.Source = CreateSizingSource();
}
public override void ViewWillAppear (bool animated)
{
base.ViewWillAppear (animated);
NavigationItem.HidesBackButton = !pushing;
TableView.ReloadData ();
}
public bool Pushing {
get {
return pushing;
}
set {
pushing = value;
if (NavigationItem != null)
NavigationItem.HidesBackButton = !pushing;
}
}
public void ReloadData ()
{
TableView.ReloadData();
}
public DialogViewController (UITableViewStyle style, bool pushing = true)
: base (style)
{
_rootElement = new Lazy<RootElement>(() => new RootElement(TableView));
EdgesForExtendedLayout = UIRectEdge.None;
SearchPlaceholder = "Search";
NavigationItem.BackBarButtonItem = new UIBarButtonItem { Title = "" };
this.pushing = pushing;
}
}
}