-
Notifications
You must be signed in to change notification settings - Fork 3
/
CoreViewController.cs
440 lines (367 loc) · 17.1 KB
/
CoreViewController.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
using System;
using UIKit;
using System.IO;
using Newtonsoft.Json;
using Rock.Mobile.IO;
using FamilyManager.UI;
using CoreGraphics;
using Foundation;
using iOS;
using Rock.Mobile.PlatformSpecific.iOS.UI;
using Customization;
using Rock.Mobile.Network;
using Rock.Client;
using System.Linq;
namespace FamilyManager
{
public class CoreViewController : UINavigationController
{
ImageCropViewController ImageCropViewController { get; set; }
UIImage ImageCropperPendingImage { get; set; }
FirstRunViewController FirstRunViewController { get; set; }
LoginViewController LoginViewController { get; set; }
ContainerViewController ContainerViewController { get; set; }
bool ViewDidAppearCalled { get; set; }
DateTime LockTimer { get; set; }
UILabel ConnectingLabel { get; set; }
UIActivityIndicatorView ProgressIndicator { get; set; }
public CoreViewController( )
{
}
public override bool PrefersStatusBarHidden()
{
return true;
}
public override void ViewDidLoad()
{
base.ViewDidLoad();
ImageCropViewController = new ImageCropViewController( );
LoginViewController = new LoginViewController( this );
ContainerViewController = new ContainerViewController( this );
FirstRunViewController = new FirstRunViewController( this );
// load any current cached config
Config.Instance.LoadFromDevice( );
// color it with whatever we currently have.
if ( Config.Instance.VisualSettings.TopHeaderBGColor != null && Config.Instance.VisualSettings.BackgroundColor != null )
{
ColorNavBar( Config.Instance.VisualSettings.TopHeaderBGColor );
View.BackgroundColor = Theme.GetColor( Config.Instance.VisualSettings.BackgroundColor );
}
else
{
// if either of our colors are null, this is probably our first run, so just use black.
ColorNavBar( "#000000FF" );
View.BackgroundColor = Theme.GetColor( "#000000FF" );
}
// create a simple centered label (slightly above center, actually) that lets the user know we're trying to connect
ConnectingLabel = new UILabel();
ConnectingLabel.Layer.AnchorPoint = CGPoint.Empty;
ConnectingLabel.Font = ConnectingLabel.Font.WithSize( 32 );
ConnectingLabel.Text = "Connecting to Rock";
ConnectingLabel.TextColor = Theme.GetColor( "#6A6A6AFF" );
ConnectingLabel.SizeToFit( );
ConnectingLabel.Layer.Position = new CGPoint( ( View.Bounds.Width - ConnectingLabel.Bounds.Width ) / 2,
( (View.Bounds.Height - ConnectingLabel.Bounds.Height ) / 2) - ConnectingLabel.Bounds.Height );
View.AddSubview( ConnectingLabel );
// show a spinner so the user feels like there's activity happening
ProgressIndicator = new UIActivityIndicatorView();
ProgressIndicator.Layer.AnchorPoint = CGPoint.Empty;
ProgressIndicator.ActivityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge;
ProgressIndicator.Color = UIColor.Gray;
ProgressIndicator.StartAnimating( );
ProgressIndicator.Layer.Position = new CGPoint( ( View.Bounds.Width - ProgressIndicator.Bounds.Width ) / 2, ConnectingLabel.Frame.Bottom + 30 );
View.AddSubview( ProgressIndicator );
LockTimer = DateTime.Now;
}
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear(animated);
// iOS will call ViewDidAppear plenty of times,
// but we also have to wait until this is called to begin
// showing view controllers, so we have to have a check for the first call.
if ( ViewDidAppearCalled == false )
{
ViewDidAppearCalled = true;
// when deciding which ViewController to start with, check to see if we have a RockURL OR
// if the Config file needs to be updated. (Which would be true if this is the first run since an upgrade)
if ( string.IsNullOrWhiteSpace( Config.Instance.RockURL ) == true || Config.Instance.NeedsUpdate( ) == true )
{
// we need to know where Rock is, so prompt them for this.
PresentViewController( FirstRunViewController, false, null );
}
// we're good, so do the normal launch
else
{
Start( );
}
}
else
{
TryPresentImageCropper( );
}
}
public void SetupComplete( )
{
Start( );
}
public void LoginComplete( )
{
// with login complete, dismiss it and launch the main viewController
PopViewController( true );
}
void Start( )
{
RockApi.SetRockURL( Config.Instance.RockURL );
RockApi.SetAuthorizationKey( Config.Instance.RockAuthorizationKey );
// get the initial config (this is where we call the new Update)
Config.Instance.UpdateCurrentConfigurationDefinedValue(
delegate(bool result )
{
// failure or not, hide the connection UI
ProgressIndicator.Hidden = true;
ProgressIndicator.HidesWhenStopped = true;
ProgressIndicator.StopAnimating( );
ConnectingLabel.Hidden = true;
if( result )
{
// we're good to move on.
// first hide the FirstRun (in case it was showing)
if( FirstRunViewController != null )
{
FirstRunViewController.DismissViewController( true, null );
}
ApplyCurrentConfigSettings( );
// add the container, so that when we're done with the login it will already be there.
PushViewController( ContainerViewController, false );
DisplayLoginScreen( false );
}
else
{
// display an error and return them to the first run screen.
DisplayFirstRun( );
}
} );
}
void ApplyCurrentConfigSettings( )
{
// update our theme
ColorNavBar( Config.Instance.VisualSettings.TopHeaderBGColor );
View.BackgroundColor = Theme.GetColor( Config.Instance.VisualSettings.BackgroundColor );
// hook int othe Location service so that if AutoDetect is on, we can get the campus.
Location.Location.Instance.OnReadyDelegate = delegate( )
{
// if preferred, use the location service to detect the campus.
if( Config.Instance.AutoDetectCampus == true )
{
// feed the locations to track
Location.Location.Instance.BeginAddRegionsForTrack( );
foreach( Rock.Client.Campus campus in Config.Instance.Campuses )
{
// make sure it has a valid location
if( campus.Location != null &&
campus.Location.Latitude.HasValue &&
campus.Location.Longitude.HasValue )
{
Location.Location.Instance.AddRegionForTrack( campus.Name, campus.Location.Latitude.Value, campus.Location.Longitude.Value, 1000 );
}
}
// and done
Location.Location.Instance.CommitRegionsForTrack( );
}
};
}
public void RegionEntered( string region )
{
// find the campus based on the region string provided
int i;
for( i = 0; i < Config.Instance.Campuses.Count; i++ )
{
if( Config.Instance.Campuses[ i ].Name == region )
{
Config.Instance.SelectedCampusIndex = i;
break;
}
}
}
void ColorNavBar( string topHeaderBGColor )
{
// setup the style of the nav bar
NavigationBar.TintColor = Theme.GetColor( topHeaderBGColor );
// create a 1x1 image of the desired color and render it as an image
UIImage solidColor = new UIImage();
UIGraphics.BeginImageContext( new CGSize( 1, 1 ) );
CGContext context = UIGraphics.GetCurrentContext( );
context.SetFillColor( Theme.GetColor( topHeaderBGColor ).CGColor );
context.FillRect( new CGRect( 0, 0, 1, 1 ) );
solidColor = UIGraphics.GetImageFromCurrentImageContext( );
UIGraphics.EndImageContext( );
// now apply it as the background image, tiled.
NavigationBar.BarTintColor = UIColor.Clear;
NavigationBar.SetBackgroundImage( solidColor, UIBarMetrics.Default );
NavigationBar.Translucent = false;
}
public static void ApplyTheme( UIViewController viewController )
{
// set the title image for the bar
if ( FileCache.Instance.FileExists( Theme.LogoImageName ) == true )
{
MemoryStream imageStream = (MemoryStream)FileCache.Instance.LoadFile( Theme.LogoImageName );
NSData imageData = NSData.FromStream( imageStream );
viewController.NavigationItem.TitleView = new UIImageView( UIImage.LoadFromData( imageData, 2 ) );
}
// set the background image if it exists
if ( FileCache.Instance.FileExists( Theme.BackgroundImageName ) == true )
{
MemoryStream imageStream = (MemoryStream)FileCache.Instance.LoadFile( Theme.BackgroundImageName );
NSData imageData = NSData.FromStream( imageStream );
viewController.View.Layer.Contents = UIImage.LoadFromData( imageData ).CGImage;
}
// and set the background color either way.
viewController.View.BackgroundColor = Theme.GetColor( Config.Instance.VisualSettings.BackgroundColor );
}
public delegate void OnCaptureComplete( NSData dataStream );
OnCaptureComplete OnCaptureCompleteDelegate;
public void BeginPictureCapture( OnCaptureComplete onCaptureComplete )
{
OnCaptureCompleteDelegate = onCaptureComplete;
// only allow the camera if they HAVE one
if( Rock.Mobile.Media.PlatformCamera.Instance.IsAvailable( ) )
{
// launch the camera
string jpgFilename = System.IO.Path.Combine ( Environment.GetFolderPath(Environment.SpecialFolder.Personal), "cameraTemp.jpg" );
Rock.Mobile.Media.PlatformCamera.Instance.CaptureImage( jpgFilename, this, delegate(object s, Rock.Mobile.Media.PlatformCamera.CaptureImageEventArgs args)
{
// if the result is true, they either got a picture or pressed cancel
bool success = false;
if( args.Result == true )
{
// either way, no need for an error
success = true;
// if the image path is valid, they didn't cancel
if ( string.IsNullOrWhiteSpace( args.ImagePath ) == false )
{
// load the image for cropping
ImageCropperPendingImage = UIImage.FromFile( args.ImagePath );
}
}
if( success == false )
{
Rock.Mobile.Util.Debug.DisplayError( Strings.Camera_Error_Header, Strings.Camera_Error_Message );
}
});
}
else
{
// notify them they don't have a camera
Rock.Mobile.Util.Debug.DisplayError( Strings.Camera_None_Header, Strings.Camera_None_Message );
onCaptureComplete( null );
}
}
public void TryPresentImageCropper( )
{
// if the image cropper is pending, launch it now.
if ( ImageCropperPendingImage != null )
{
ImageCropViewController.Begin( ImageCropperPendingImage, 1.0f, HandleImageCropRequest );
PresentModalViewController( ImageCropViewController, true );
ImageCropperPendingImage = null;
}
}
object HandleImageCropRequest( ImageCropViewController.Request request, object context )
{
switch ( request )
{
case ImageCropViewController.Request.SupportedInterfaceOrientations:
{
return (object)(UIInterfaceOrientationMask.LandscapeLeft | UIInterfaceOrientationMask.LandscapeRight);
}
case ImageCropViewController.Request.ShouldAutorotate:
{
return (object)false;
}
case ImageCropViewController.Request.PreferStatusBarHidden:
{
return (object)PrefersStatusBarHidden( );
}
case ImageCropViewController.Request.IsDeviceLandscape:
{
return (object)true;
}
case ImageCropViewController.Request.CropperDone:
{
// if croppedImage is null, they simply cancelled
UIImage croppedImage = (UIImage)context;
if ( croppedImage != null )
{
NSData croppedImageData = croppedImage.AsJPEG( );
// notify the initial caller we're done.
OnCaptureCompleteDelegate( croppedImageData );
OnCaptureCompleteDelegate = null;
}
ImageCropViewController.DismissModalViewController( true );
break;
}
}
return null;
}
void RestoreFromBackground( )
{
// check to see if we should re-run the Start() process
TimeSpan delta = DateTime.Now - LockTimer;
if ( delta.TotalMinutes > Settings.General_AutoLockTime )
{
// if they're not on the firstRun controller, we need to first verify Rock
// is still where it should be, and second, force them to log back in.
if ( FirstRunViewController.Visible == false )
{
// see if Rock is present
ApplicationApi.IsRockAtURL( Config.Instance.RockURL,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription )
{
// if it IS, show the login controller (if we're not already)
if( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) )
{
DisplayLoginScreen( false );
}
// Rock wasn't found, so display the first run VC again
else
{
DisplayFirstRun( );
}
} );
}
}
}
public void DisplayFirstRun( )
{
ContainerViewController.RemoveFromParentViewController( );
LoginViewController.RemoveFromParentViewController( );
// display an error and return them to the first run screen.
Rock.Mobile.Util.Debug.DisplayError( Strings.General_StartUp_Error_Header, Strings.General_StartUp_Error_Message );
PresentViewController( FirstRunViewController, false, null );
}
public void DisplayLoginScreen( bool animated )
{
if ( LoginViewController.Visible == false )
{
PushViewController( LoginViewController, animated );
}
}
public void DidEnterBackground( )
{
FileCache.Instance.SaveCacheMap( );
Config.Instance.SaveToDevice( );
LockTimer = DateTime.Now;
}
public void OnResignActivation( )
{
FileCache.Instance.SaveCacheMap( );
Config.Instance.SaveToDevice( );
LockTimer = DateTime.Now;
}
public void OnActivated( )
{
RestoreFromBackground( );
}
}
}