-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSearchFamilesViewController.cs
382 lines (306 loc) · 14.6 KB
/
SearchFamilesViewController.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
using System;
using UIKit;
using System.IO;
using Foundation;
using Rock.Mobile.PlatformSpecific.iOS.UI;
using CoreGraphics;
using Rock.Mobile.Util.Strings;
using Rock.Mobile.IO;
using FamilyManager.UI;
using Rock.Mobile.PlatformSpecific.Util;
using System.Collections.Generic;
using iOS;
using Customization;
using Rock.Mobile.PlatformSpecific.iOS.Graphics;
using System.Linq;
using Rock.Mobile.Network;
using Rock.Mobile.Util;
namespace FamilyManager
{
public class SearchFamiliesViewController : UIViewController
{
/// <summary>
/// Definition for a cell that can be used to display family search results
/// </summary>
class FamilySearchResultCell : UITableViewCell
{
public static string Identifier = "FamilySearchResultCell";
public FamilySearchResultView FamilyView { get; set; }
public FamilySearchResultCell( UITableViewCellStyle style, string cellIdentifier ) : base( style, cellIdentifier )
{
BackgroundColor = UIColor.Clear;
FamilyView = new FamilySearchResultView( );
AddSubview( FamilyView );
}
}
public class TableSource : UITableViewSource
{
SearchFamiliesViewController Parent { get; set; }
List<nfloat> CellHeights { get; set; }
static nfloat FooterCellHeight = 100;
/// <summary>
/// Definition for the source table that backs the tableView
/// </summary>
public TableSource ( SearchFamiliesViewController parent )
{
Parent = parent;
// create a list for the cell heights, since they're variable
CellHeights = new List<nfloat>( );
}
public void FamiliesUpdated( UITableView parentTable )
{
CellHeights = new List<nfloat>();
}
public override nint RowsInSection (UITableView tableview, nint section)
{
// IF there's at least one family, add one extra row for padding. Otherwise don't show anything.
return Parent.Families.Count > 0 ? Parent.Families.Count + 1 : 1;
}
public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
{
tableView.DeselectRow( indexPath, true );
if ( indexPath.Row < Parent.Families.Count )
{
Parent.RowClicked( indexPath.Row );
}
}
public override nfloat GetHeightForRow(UITableView tableView, NSIndexPath indexPath)
{
return CalcRowHeight( tableView, indexPath.Row );
}
public override nfloat EstimatedHeight(UITableView tableView, NSIndexPath indexPath)
{
return CalcRowHeight( tableView, indexPath.Row );
}
nfloat CalcRowHeight( UITableView tableView, int rowIndex )
{
if ( rowIndex < CellHeights.Count )
{
return CellHeights[ rowIndex ];
}
else
{
return FooterCellHeight;
}
}
public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
FamilySearchResultCell cell = tableView.DequeueReusableCell( FamilySearchResultCell.Identifier ) as FamilySearchResultCell;
// if there are no cells to reuse, create a new one
if (cell == null)
{
cell = new FamilySearchResultCell( UITableViewCellStyle.Default, FamilySearchResultCell.Identifier );
// configure the cell colors
cell.SelectionStyle = UITableViewCellSelectionStyle.None;
}
nfloat cellWidth = tableView.Bounds.Width * .98f;
if ( Parent.Families.Count > 0 )
{
// is this an actual family row? (as opposed to our bottom dummy row)
if ( indexPath.Row < Parent.Families.Count )
{
cell.FamilyView.FormatCell( cellWidth, Parent.Families[ indexPath.Row ] );
cell.Bounds = cell.FamilyView.Bounds;
// if the row is beyond what we've stored, add it.
if ( indexPath.Row >= CellHeights.Count )
{
CellHeights.Add( cell.Bounds.Height );
}
else
{
// otherwise replace the existing entry
CellHeights[ indexPath.Row ] = cell.Bounds.Height;
}
}
else
{
// this is the bottom dummy cell, so just give it a little padding.
cell.FamilyView.Container.Hidden = true;
}
}
// either they've never searched, or it's a failed search
else
{
if ( Parent.DidSearchFail == true )
{
cell.FamilyView.FormatCell( cellWidth, Strings.Search_NoResults_Title, Strings.Search_NoResults_Suggestions, "", Strings.Search_NoResults_Suggestion1, Strings.Search_NoResults_Suggestion2 );
// if the row is beyond what we've stored, add it.
if ( indexPath.Row >= CellHeights.Count )
{
CellHeights.Add( cell.Bounds.Height );
}
else
{
// otherwise replace the existing entry
CellHeights[ indexPath.Row ] = cell.Bounds.Height;
}
}
else
{
cell.FamilyView.Container.Hidden = true;
}
}
return cell;
}
}
Dynamic_UITextField SearchField { get; set; }
UIButton SearchButton { get; set; }
UIButton ClearButton { get; set; }
UIButton AddFamilyButton { get; set; }
UITableView TableView { get; set; }
UIBlockerView BlockerView { get; set; }
ContainerViewController Parent { get; set; }
public List<Rock.Client.Family> Families { get; set; }
public bool DidSearchFail { get; set; }
public SearchFamiliesViewController( ContainerViewController parent ) : base( )
{
Parent = parent;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
View.Layer.Contents = Parent.View.Layer.Contents;
Families = new List<Rock.Client.Family>();
// setup the blocker view
BlockerView = new UIBlockerView( View, View.Bounds.ToRectF( ) );
// setup the search field
SearchField = new Dynamic_UITextField( this, View, Strings.General_Search, false, false );
View.AddSubview( SearchField );
SearchField.GetTextField( ).AutocapitalizationType = UITextAutocapitalizationType.None;
SearchField.GetTextField( ).AutocorrectionType = UITextAutocorrectionType.No;
SearchField.GetTextField( ).ClearButtonMode = UITextFieldViewMode.Always;
SearchField.GetTextField( ).ShouldReturn += delegate(UITextField textField)
{
PerformSearch( );
return true;
};
DidSearchFail = false;
// setup the search button
SearchButton = UIButton.FromType( UIButtonType.System );
SearchButton.Layer.AnchorPoint = CGPoint.Empty;
SearchButton.SetTitle( Strings.General_Search, UIControlState.Normal );
Theme.StyleButton( SearchButton, Config.Instance.VisualSettings.PrimaryButtonStyle );
SearchButton.Font = FontManager.GetFont( Settings.General_RegularFont, Config.Instance.VisualSettings.SmallFontSize );
View.AddSubview( SearchButton );
SearchButton.TouchUpInside += (object sender, EventArgs e ) =>
{
PerformSearch( );
};
// setup the refresh button
ClearButton = UIButton.FromType( UIButtonType.System );
ClearButton.Layer.AnchorPoint = CGPoint.Empty;
ClearButton.SetTitle( Strings.General_Clear, UIControlState.Normal );
Theme.StyleButton( ClearButton, Config.Instance.VisualSettings.DefaultButtonStyle );
View.AddSubview( ClearButton );
ClearButton.TouchUpInside += (object sender, EventArgs e ) =>
{
Families = new List<Rock.Client.Family>( );
DidSearchFail = false;
// reload data
((TableSource)TableView.Source).FamiliesUpdated( TableView );
TableView.ReloadData( );
};
// setup the add family button
AddFamilyButton = UIButton.FromType( UIButtonType.System );
AddFamilyButton.Layer.AnchorPoint = CGPoint.Empty;
AddFamilyButton.SetTitle( Strings.General_AddFamily, UIControlState.Normal );
AddFamilyButton.Font = FontManager.GetFont( Settings.General_RegularFont, Config.Instance.VisualSettings.SmallFontSize );
Theme.StyleButton( AddFamilyButton, Config.Instance.VisualSettings.DefaultButtonStyle );
View.AddSubview( AddFamilyButton );
AddFamilyButton.TouchUpInside += (object sender, EventArgs e ) =>
{
Parent.PresentNewFamilyPage( );
};
// setup the list
TableView = new UITableView( );
TableView.Layer.AnchorPoint = CGPoint.Empty;
TableView.BackgroundColor = UIColor.Clear;
TableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
View.AddSubview( TableView );
TableView.Source = new TableSource( this );
}
public void PerformSearch( )
{
if( SearchField.GetCurrentValue( ).Length > Settings.General_MinSearchLength )
{
// put the list at the top, so that when we refresh it, it correctly resizes each row
TableView.SetContentOffset( CGPoint.Empty, true );
SearchField.ResignFirstResponder( );
BlockerView.BringToFront( );
BlockerView.Show(
delegate
{
// search for the family
RockApi.Get_Groups_FamiliesByPersonNameSearch( SearchField.GetCurrentValue( ),
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.Family> model)
{
if( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true && model != null && model.Count > 0 )
{
Families = model;
}
else
{
// error (or no results)
Families = new List<Rock.Client.Family>( );
DidSearchFail = true;
}
// reload data
((TableSource)TableView.Source).FamiliesUpdated( TableView );
TableView.ReloadData( );
BlockerView.Hide( );
});
});
}
}
public override void ViewDidLayoutSubviews()
{
base.ViewDidLayoutSubviews();
// set the search field and refresh button, cause those are known
SearchField.Layer.Position = new CGPoint( 10, 25 );
SearchField.ViewDidLayoutSubviews( new CGRect( 0, 0, View.Bounds.Width * 0.3375f, View.Bounds.Height ) );
// position the clear button
ClearButton.SizeToFit( );
ClearButton.Bounds = new CGRect( 0, 0, ClearButton.Bounds.Width * 2, SearchButton.Bounds.Height );
ClearButton.Layer.Position = new CGPoint( SearchField.Frame.Right - ClearButton.Bounds.Width, SearchField.Frame.Bottom + 10 );
// now set the search button, which will use remaining width
SearchButton.Layer.Position = new CGPoint( 10, SearchField.Frame.Bottom + 10 );
SearchButton.Bounds = new CGRect( 0, 0, SearchField.Bounds.Width, 0 );
SearchButton.SizeToFit( );
SearchButton.Frame = new CGRect( 10, SearchField.Frame.Bottom + 10, SearchField.Bounds.Width - ClearButton.Bounds.Width - 2, SearchButton.Bounds.Height );
// position the 'add family' button
AddFamilyButton.Bounds = new CGRect( 0, 0, SearchField.Bounds.Width, SearchButton.Bounds.Height );
AddFamilyButton.Layer.Position = new CGPoint( SearchButton.Frame.Left, SearchButton.Frame.Bottom + 10 );
// setup the table view
nfloat tableXPos = SearchField.Frame.Right + 10;
TableView.Frame = new CGRect( tableXPos, SearchField.Layer.Position.Y, View.Bounds.Width - tableXPos, View.Bounds.Height );
TableView.SetNeedsLayout( );
BlockerView.SetBounds( View.Bounds.ToRectF( ) );
}
public override void ViewWillAppear( bool animated )
{
base.ViewWillAppear( animated );
TableView.ReloadData( );
}
public override void TouchesEnded(NSSet touches, UIEvent evt)
{
base.TouchesEnded(touches, evt);
SearchField.ResignFirstResponder( );
}
public void TryUpdateFamily( Rock.Client.Family family )
{
// see if the family exists
Rock.Client.Family currFamily = Families.Where( f => f.Id == family.Id ).SingleOrDefault( );
// if it does, get its index and replace it.
if ( currFamily != null )
{
int currIndex = Families.IndexOf( currFamily );
Families[ currIndex ] = family;
}
}
public void RowClicked( int index )
{
// notify our parent it should present the family page.
Parent.PresentFamilyPage( Families[ index ] );
}
}
}