-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathConfig.cs
567 lines (477 loc) · 21.8 KB
/
Config.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
using System;
using Rock.Mobile.Network;
using RestSharp;
using System.Net;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using Rock.Mobile.IO;
using FamilyManager.UI;
using Customization;
namespace FamilyManager
{
/// <summary>
/// App configuration class that manages app settings, downloaded rock values (marital status, campuses, etc.) and
/// the configurationTemplates, which define the appearance and certain preferences.
/// </summary>
public class Config
{
const int CURRENT_VERSION = 1;
const string FileName = "config.dat";
static Config _Instance = new Config( );
public static Config Instance { get { return _Instance; } }
public Config( )
{
FamilyAttributeDefines = new List<Rock.Client.Attribute>();
PersonAttributeDefines = new List<Rock.Client.Attribute>();
CurrentConfigurationTemplate = new Rock.Client.DefinedValue();
// default recording first visits to true.
RecordFirstVisit = true;
// default auto-detecting the campus to true.
AutoDetectCampus = true;
}
/// <summary>
/// This is the defined template we're currently using. It's basically
/// the wrapper within which a ConfigurationTemplate exists, and has meta data
/// like its Rock Database Id.
/// </summary>
[JsonProperty]
Rock.Client.DefinedValue CurrentConfigurationTemplate { get; set; }
/// <summary>
/// The extracted CurrentTemplate, taken out of the CurrentConfigurationTemplate above.
/// We store this for convenience
/// </summary>
/// <value>The current template.</value>
[JsonIgnore]
ConfigurationTemplate CurrentTemplate
{
get
{
return ConfigurationTemplate.Template( CurrentConfigurationTemplate );
}
}
/// <summary>
/// The family attributes above tell me which attributes to go get from Rock, and then
/// store here.
/// </summary>
public List<Rock.Client.Attribute> FamilyAttributeDefines { get; set; }
/// <summary>
/// The person attributes above tell me which attributes to go get from Rock, and then
/// store here.
/// </summary>
public List<Rock.Client.Attribute> PersonAttributeDefines { get; set; }
/// <summary>
/// The URL to reach Rock.
/// </summary>
/// <value>The rock UR.</value>
public string RockURL { get; set; }
// The key used for hitting end points on Rock. The user will still have to pass a login check.
public string RockAuthorizationKey { get; set; }
// Campuses available
[JsonProperty]
public int SelectedCampusIndex { get; set; }
[JsonProperty]
public List<Rock.Client.Campus> Campuses { get; protected set; }
public void SetCampuses( List<Rock.Client.Campus> campuses )
{
SelectedCampusIndex = 0;
Campuses = campuses;
}
[JsonProperty]
public int Version { get; set; }
[JsonProperty]
public bool AutoDetectCampus { get; set; }
[JsonProperty]
public List<Rock.Client.DefinedValue> MaritalStatus { get; set; }
[JsonProperty]
public List<Rock.Client.DefinedValue> SchoolGrades { get; set; }
[JsonProperty]
public Rock.Client.GroupTypeRole FamilyMemberChildGroupRole { get; set; }
[JsonProperty]
public Rock.Client.GroupTypeRole FamilyMemberAdultGroupRole { get; set; }
[JsonProperty]
public Rock.Client.Attribute FirstTimeVisitAttrib { get; set; }
[JsonProperty]
public Rock.Client.GroupTypeRole CanCheckInGroupRole { get; set; }
[JsonProperty]
public Rock.Client.GroupTypeRole AllowedCheckInByRole { get; set; }
[JsonProperty]
public List<Rock.Client.DefinedValue> ConfigurationTemplates { get; set; }
[JsonIgnore]
public Theme VisualSettings { get { return CurrentTemplate.VisualSettings; } }
// This simply determines whether the app should ask about recording first visits.
[JsonIgnore]
public bool FirstVisitPrompt { get { return CurrentTemplate.FirstVisitPrompt; } }
// This is the person alias ID for the currently logged in user.
[JsonIgnore]
public int CurrentPersonAliasId { get; set; }
// This is set based on the user's response to FirstVisitPrompt. The default is true.
[JsonIgnore]
public bool RecordFirstVisit { get; set; }
[JsonIgnore]
public string DefaultState { get { return CurrentTemplate.DefaultState; } }
[JsonIgnore]
public List<Dictionary<string, string>> FamilyAttributes { get { return CurrentTemplate.FamilyAttributes; } }
[JsonIgnore]
public List<Dictionary<string, string>> PersonAttributes { get { return CurrentTemplate.PersonAttributes; } }
[JsonIgnore]
public int ConfigurationTemplateId { get { return CurrentConfigurationTemplate.Id; } }
public bool LoadFromDevice( )
{
bool success = false;
// load the latest version
string filePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), FileName);
// if the file exists
if ( System.IO.File.Exists( filePath ) == true )
{
// read it
using ( StreamReader reader = new StreamReader( filePath ) )
{
string json = reader.ReadLine( );
try
{
// guard against the LaunchData changing and the user having old data.
_Instance = JsonConvert.DeserializeObject<Config>( json ) as Config;
success = true;
}
catch( Exception )
{
// if it fails for some reason, just create a new one
_Instance = new Config();
}
}
}
return success;
}
public void SaveToDevice( )
{
string filePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), FileName);
// open a stream
using (StreamWriter writer = new StreamWriter(filePath, false))
{
string json = JsonConvert.SerializeObject( _Instance );
writer.WriteLine( json );
}
}
public bool NeedsUpdate( )
{
if ( Version < CURRENT_VERSION )
{
return true;
}
return false;
}
public delegate void OnComplete( bool result );
string TempRockUrl { get; set; }
string TempAuthKey { get; set; }
public void TryBindToRockServer( string rockUrl, string authKey, OnComplete onComplete )
{
TempRockUrl = rockUrl;
TempAuthKey = authKey;
// this will let us use the url temporarily
RockApi.SetRockURL( rockUrl );
RockApi.SetAuthorizationKey( authKey );
// get the config templates
GetConfigTemplates(
// capute the result callback
delegate( bool result )
{
// restore the original URLs, so that they only update
// if Commit is called.
RockApi.SetRockURL( RockURL );
RockApi.SetAuthorizationKey( RockAuthorizationKey );
// now notify the original caller
onComplete( result );
} );
}
public List<Rock.Client.DefinedValue> TempConfigurationTemplates { get; set; }
void GetConfigTemplates( OnComplete onComplete )
{
ConfigurationTemplate.DownloadConfigurationTemplates(
delegate( List<Rock.Client.DefinedValue> model )
{
if ( model != null )
{
TempConfigurationTemplates = model;
GetCampuses( onComplete );
}
// Config Templates Failed
else
{
onComplete( false );
}
} );
}
public List<Rock.Client.Campus> TempCampuses { get; set; }
void GetCampuses( OnComplete onComplete )
{
// get the campuses
RockApi.Get_Campuses( "?$expand=Location",
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.Campus> model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true )
{
TempCampuses = model;
GetMaritalValues( onComplete );
}
// Campuses failed
else
{
onComplete( false );
}
} );
}
List<Rock.Client.DefinedValue> TempMaritalStatus { get; set; }
void GetMaritalValues( OnComplete onComplete )
{
ApplicationApi.GetDefinedValuesForDefinedType( Rock.Client.SystemGuid.DefinedType.PERSON_MARITAL_STATUS,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.DefinedValue> model )
{
if( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true )
{
TempMaritalStatus = model;
GetSchoolGrades( onComplete );
}
// Marital Status Failed
else
{
onComplete( false );
}
} );
}
List<Rock.Client.DefinedValue> TempSchoolGrades { get; set; }
void GetSchoolGrades( OnComplete onComplete )
{
ApplicationApi.GetDefinedValuesForDefinedType( Rock.Client.SystemGuid.DefinedType.SCHOOL_GRADES,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.DefinedValue> model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true )
{
TempSchoolGrades = model;
GetChildGroupTypeRole( onComplete );
}
// School Grades Failed
else
{
onComplete( false );
}
} );
}
Rock.Client.GroupTypeRole TempChildRole { get; set; }
void GetChildGroupTypeRole( OnComplete onComplete )
{
ApplicationApi.GetGroupTypeRoleForGuid( Rock.Client.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_CHILD,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.GroupTypeRole> model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true && model != null && model.Count > 0 )
{
TempChildRole = model[ 0 ];
GetAdultGroupTypeRole( onComplete );
}
// Child Role Failed
else
{
onComplete( false );
}
} );
}
Rock.Client.GroupTypeRole TempAdultRole { get; set; }
void GetAdultGroupTypeRole( OnComplete onComplete )
{
ApplicationApi.GetGroupTypeRoleForGuid( Rock.Client.SystemGuid.GroupRole.GROUPROLE_FAMILY_MEMBER_ADULT,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.GroupTypeRole> model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true && model != null && model.Count > 0 )
{
TempAdultRole = model[ 0 ];
GetFirstTimeVisitAttribValue( onComplete );
}
// Adult Role Failed
else
{
onComplete( false );
}
} );
}
Rock.Client.Attribute TempFirstTimeVisit { get; set; }
void GetFirstTimeVisitAttribValue( OnComplete onComplete )
{
ApplicationApi.GetAttributeForGuid( "655D6FBA-F8C0-4919-9E31-C1C936653555",
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, Rock.Client.Attribute model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true && model != null )
{
TempFirstTimeVisit = model;
GetCanCheckInGroupTypeRole( onComplete );
}
// Child Role Failed
else
{
onComplete( false );
}
} );
}
Rock.Client.GroupTypeRole TempCanCheckInRole { get; set; }
void GetCanCheckInGroupTypeRole( OnComplete onComplete )
{
ApplicationApi.GetGroupTypeRoleForGuid( Rock.Client.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_CAN_CHECK_IN,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.GroupTypeRole> model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true && model != null && model.Count > 0 )
{
TempCanCheckInRole = model[ 0 ];
GetAllowedToCheckInGroupTypeRole( onComplete );
}
// Can Check In Role Failed
else
{
onComplete( false );
}
} );
}
Rock.Client.GroupTypeRole TempAllowedCheckInByRole { get; set; }
void GetAllowedToCheckInGroupTypeRole( OnComplete onComplete )
{
ApplicationApi.GetGroupTypeRoleForGuid( Rock.Client.SystemGuid.GroupRole.GROUPROLE_KNOWN_RELATIONSHIPS_ALLOW_CHECK_IN_BY,
delegate(System.Net.HttpStatusCode statusCode, string statusDescription, List<Rock.Client.GroupTypeRole> model )
{
if ( Rock.Mobile.Network.Util.StatusInSuccessRange( statusCode ) == true && model != null && model.Count > 0 )
{
TempAllowedCheckInByRole = model[ 0 ];
// Note: add more stuff here if you need.
onComplete( true );
}
// Allowed Check In By Role Failed
else
{
onComplete( false );
}
} );
}
public void CommitRockSync( )
{
// Everything worked, so store all our values
RockURL = TempRockUrl;
RockAuthorizationKey = TempAuthKey;
MaritalStatus = TempMaritalStatus;
SchoolGrades = TempSchoolGrades;
FamilyMemberChildGroupRole = TempChildRole;
FamilyMemberAdultGroupRole = TempAdultRole;
FirstTimeVisitAttrib = TempFirstTimeVisit;
CanCheckInGroupRole = TempCanCheckInRole;
AllowedCheckInByRole = TempAllowedCheckInByRole;
ConfigurationTemplates = TempConfigurationTemplates;
SetCampuses( TempCampuses );
// permenantly update the api values.
RockApi.SetRockURL( RockURL );
RockApi.SetAuthorizationKey( RockAuthorizationKey );
// stamp the version as well.
Version = CURRENT_VERSION;
}
public delegate void ConfigurationSet( bool result );
public void SetConfigurationDefinedValue( Rock.Client.DefinedValue configDefinedValue, ConfigurationSet onConfigSet )
{
// extract the config template from the defined value
ConfigurationTemplate configTemplate = ConfigurationTemplate.Template( configDefinedValue );
configTemplate.VisualSettings.RemoveDownloadedImages( );
// first get the theme images
configTemplate.VisualSettings.DownloadImages( RockApi.BaseUrl,
delegate(bool imageResult )
{
if( imageResult == true )
{
// start by sorting our attributes so we get the defined values correctly
configTemplate.SortAttributeLists( );
// get the family attribute definitions
DownloadAttributeDefinitions( configTemplate.FamilyAttributes, delegate( List<Rock.Client.Attribute> familyAttribDefines )
{
if ( familyAttribDefines != null )
{
// finally, download the PERSON attribute definitions
DownloadAttributeDefinitions( configTemplate.PersonAttributes, delegate( List<Rock.Client.Attribute> personAttribDefines )
{
// it worked! we're done.
if ( personAttribDefines != null )
{
// it all worked, so store our values and save!
CurrentConfigurationTemplate = configDefinedValue;
_Instance.FamilyAttributeDefines = familyAttribDefines;
_Instance.PersonAttributeDefines = personAttribDefines;
// save to the device
SaveToDevice( );
}
// and either way, return the result
onConfigSet( personAttribDefines != null ? true : false );
} );
}
// failed to download family attributes
else
{
onConfigSet( false );
}
} );
}
// failed to get the images
else
{
onConfigSet( false );
}
} );
}
public void UpdateCurrentConfigurationDefinedValue( ConfigurationSet onConfigSet )
{
// attempt to update our current config.
ConfigurationTemplate.UpdateTemplate( CurrentConfigurationTemplate,
delegate(bool result, Rock.Client.DefinedValue templateDefinedValue )
{
// if we got something back, continue and try to take it.
// if result is true and templateDefinedValue is null, that's fine, it means there isn't a new template.
// if reslut is false, we'll return that and fail.
if ( result && templateDefinedValue != null )
{
// now invoke the SetConfig, which will make sure we get all the dependencies we need.
SetConfigurationDefinedValue( templateDefinedValue, onConfigSet );
}
else
{
onConfigSet( result );
}
} );
}
/// <summary>
/// Broke attribute definition downloading into its own function
/// for simplicity
/// </summary>
delegate void AttribDefinitionDownloaded( List<Rock.Client.Attribute> attribList );
void DownloadAttributeDefinitions( List<Dictionary<string, string>> attribList, AttribDefinitionDownloaded onCompletion )
{
// now build an array of the attributeIds we should request
int[] attributeIds = new int[ attribList.Count ];
for ( int i = 0; i < attribList.Count; i++ )
{
attributeIds[ i ] = int.Parse( attribList[ i ][ "attributeId" ] );
}
// and lastly, request them
if ( attribList.Count > 0 )
{
ApplicationApi.GetAttribute( attributeIds,
delegate(HttpStatusCode attribStatusCode, string attribStatusDescription, List<Rock.Client.Attribute> attribModel )
{
if ( attribModel != null && Rock.Mobile.Network.Util.StatusInSuccessRange( attribStatusCode ) )
{
Rock.Mobile.Util.Debug.WriteLine( "Got attrib" );
onCompletion( attribModel );
}
else
{
onCompletion( null );
}
} );
}
else
{
onCompletion( new List<Rock.Client.Attribute>( ) );
}
}
}
}