-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTPPSettingsAccountDetailViewController.m
1460 lines (1290 loc) · 60.9 KB
/
TPPSettingsAccountDetailViewController.m
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@import LocalAuthentication;
@import CoreLocation;
@import MessageUI;
@import PureLayout;
#import "TPPCatalogNavigationController.h"
#import "TPPConfiguration.h"
#import "TPPLinearView.h"
#import "TPPOPDS.h"
#import "TPPRootTabBarController.h"
#import "TPPSettingsAccountDetailViewController.h"
#import "TPPSettingsEULAViewController.h"
#import "TPPXML.h"
#import "UIFont+TPPSystemFontOverride.h"
#import "UIView+TPPViewAdditions.h"
#import "Palace-Swift.h"
#if defined(FEATURE_DRM_CONNECTOR)
#import <ADEPT/ADEPT.h>
#endif
typedef NS_ENUM(NSInteger, CellKind) {
CellKindAdvancedSettings,
CellKindAgeCheck,
CellKindBarcodeImage,
CellKindBarcode,
CellKindPIN,
CellKindLogInSignOut,
CellKindRegistration,
CellKindSyncButton,
CellKindAbout,
CellKindPrivacyPolicy,
CellKindContentLicense,
CellReportIssue,
CellKindPasswordReset
};
@interface TPPSettingsAccountDetailViewController () <TPPSignInOutBusinessLogicUIDelegate>
// view state
@property (nonatomic) BOOL loggingInAfterBarcodeScan;
@property (nonatomic) BOOL hiddenPIN;
// UI
@property (nonatomic) UIImageView *barcodeImageView;
@property (nonatomic) UILabel *barcodeTextLabel;
@property (nonatomic) UILabel *barcodeImageLabel;
@property (nonatomic) NSLayoutConstraint *barcodeHeightConstraint;
@property (nonatomic) NSLayoutConstraint *barcodeTextHeightConstraint;
@property (nonatomic) NSLayoutConstraint *barcodeTextLabelSpaceConstraint;
@property (nonatomic) NSLayoutConstraint *barcodeLabelSpaceConstraint;
@property (nonatomic) float userBrightnessSetting;
@property (nonatomic) NSMutableArray *tableData;
@property (nonatomic) UIButton *PINShowHideButton;
@property (nonatomic) UIButton *barcodeScanButton;
@property (nonatomic) UITableViewCell *logInSignOutCell;
@property (nonatomic) UITableViewCell *ageCheckCell;
@property (nonatomic) UISwitch *syncSwitch;
@property (nonatomic) UIView *accountInfoHeaderView;
@property (nonatomic) UIView *accountInfoFooterView;
@property (nonatomic) UIView *syncFooterView;
@property (nonatomic) UIActivityIndicatorView *juvenileActivityView;
// account state
@property TPPUserAccountFrontEndValidation *frontEndValidator;
@property (nonatomic) TPPSignInBusinessLogic *businessLogic;
@end
static const NSInteger sLinearViewTag = 1111;
static const CGFloat sVerticalMarginPadding = 2.0;
// table view sections indeces
static const NSInteger sSection0AccountInfo = 0;
static const NSInteger sSection1Sync = 1;
// Constraint constants
static const CGFloat sConstantZero = 0.0;
static const CGFloat sConstantSpacing = 12.0;
@implementation TPPSettingsAccountDetailViewController
/*
For NYPL, this field can accept any of the following:
- a username
- a 14-digit NYPL-issued barcode
- a 16-digit NYC ID issued by the city of New York to its residents. Patrons
can set up the NYC ID as a NYPL barcode even if they already have a NYPL card.
All of these types of authentication can be used with the PIN to sign in.
- Note: A patron can have multiple barcodes, because patrons may lose
their library card and get a new one with a different barcode.
Authenticating with any of those barcodes should work.
*/
@synthesize usernameTextField;
@synthesize PINTextField;
@synthesize forceEditability;
#pragma mark - NYPLSignInOutBusinessLogicUIDelegate properties
- (NSString *)context
{
return @"Settings Tab";
}
- (NSString *)username
{
return self.usernameTextField.text;
}
- (NSString *)pin
{
return self.PINTextField.text;
}
#pragma mark - Computed variables
- (NSString *)selectedAccountId
{
return self.businessLogic.libraryAccountID;
}
- (nullable Account *)selectedAccount
{
return self.businessLogic.libraryAccount;
}
- (TPPUserAccount *)selectedUserAccount
{
return self.businessLogic.userAccount;
}
#pragma mark - NSObject
// Overriding superclass's designated initializer
- (instancetype)initWithStyle:(__unused UITableViewStyle)style
{
NSString *libraryID = [[AccountsManager shared] currentAccountId];
NSAssert(libraryID, @"Tried to initialize NYPLSettingsAccountDetailViewController with the current library ID but that appears to be nil. A release build will continue with an empty library ID but this will likely produce unexpected behavior.");
return [self initWithLibraryAccountID:libraryID ?: @""];
}
- (instancetype)initWithLibraryAccountID:(NSString *)libraryUUID
{
self = [super initWithStyle:UITableViewStyleGrouped];
if(!self) return nil;
id<TPPDRMAuthorizing> drmAuthorizer = nil;
#if defined(FEATURE_DRM_CONNECTOR)
if ([AdobeCertificate.defaultCertificate hasExpired] == NO) {
drmAuthorizer = [NYPLADEPT sharedInstance];
}
#endif
self.businessLogic = [[TPPSignInBusinessLogic alloc]
initWithLibraryAccountID:libraryUUID
libraryAccountsProvider:AccountsManager.shared
urlSettingsProvider: TPPSettings.shared
bookRegistry:[TPPBookRegistry shared]
bookDownloadsCenter:[MyBooksDownloadCenter shared]
userAccountProvider:[TPPUserAccount class]
uiDelegate:self
drmAuthorizer:drmAuthorizer];
self.title = NSLocalizedString(@"Account", nil);
self.frontEndValidator = [[TPPUserAccountFrontEndValidation alloc]
initWithAccount:self.selectedAccount
businessLogic:self.businessLogic
inputProvider:self];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(accountDidChange)
name:NSNotification.TPPUserAccountDidChange
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(willResignActive)
name:UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(willEnterForeground)
name:UIApplicationWillEnterForegroundNotification
object:nil];
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - UIViewController + Views Preparation
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [TPPConfiguration backgroundColor];
self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
[self setupHeaderView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:@"LocationAuthorizationDidChange" object:nil];
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleMedium];
activityIndicator.center = CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2);
[self.view addSubview:activityIndicator];
[activityIndicator startAnimating];
__weak TPPSettingsAccountDetailViewController *weakSelf = self;
if (self.businessLogic.libraryAccount.details) {
dispatch_async(dispatch_get_main_queue(), ^{
[activityIndicator removeFromSuperview];
[weakSelf setupViews];
weakSelf.hiddenPIN = YES;
[weakSelf accountDidChange];
[weakSelf updateShowHidePINState];
});
} else {
[self.businessLogic ensureAuthenticationDocumentIsLoaded:^(BOOL success) {
if (success) {
dispatch_async(dispatch_get_main_queue(), ^{
[activityIndicator removeFromSuperview];
[weakSelf setupViews];
weakSelf.hiddenPIN = YES;
[weakSelf accountDidChange];
[weakSelf updateShowHidePINState];
});
} else {
dispatch_async(dispatch_get_main_queue(), ^{
[activityIndicator removeFromSuperview];
[weakSelf displayErrorMessage:NSLocalizedString(@"Please check your connection and try again.", nil)];
});
}
}];
}
}
- (void)appWillEnterForeground {
[self.tableView reloadData];
}
- (void)reloadData {
[self.tableView reloadData];
}
- (void)displayErrorMessage:(NSString *)errorMessage
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = errorMessage;
[label sizeToFit];
[self.view addSubview:label];
[label centerInSuperviewWithOffset:self.tableView.contentOffset];
}
- (void)setupViews
{
self.usernameTextField = [[UITextField alloc] initWithFrame:CGRectZero];
self.usernameTextField.delegate = self.frontEndValidator;
self.usernameTextField.placeholder =
self.businessLogic.selectedAuthentication.patronIDLabel ?: NSLocalizedString(@"Barcode or Username", nil);
switch (self.businessLogic.selectedAuthentication.patronIDKeyboard) {
case LoginKeyboardStandard:
case LoginKeyboardNone:
self.usernameTextField.keyboardType = UIKeyboardTypeASCIICapable;
break;
case LoginKeyboardEmail:
self.usernameTextField.keyboardType = UIKeyboardTypeEmailAddress;
break;
case LoginKeyboardNumeric:
self.usernameTextField.keyboardType = UIKeyboardTypeNumberPad;
break;
}
self.usernameTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.usernameTextField.autocorrectionType = UITextAutocorrectionTypeNo;
[self.usernameTextField
addTarget:self
action:@selector(textFieldsDidChange)
forControlEvents:UIControlEventEditingChanged];
self.barcodeScanButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.barcodeScanButton setImage:[UIImage imageNamed:@"CameraIcon"] forState:UIControlStateNormal];
[self.barcodeScanButton addTarget:self action:@selector(scanLibraryCard)
forControlEvents:UIControlEventTouchUpInside];
self.PINTextField = [[UITextField alloc] initWithFrame:CGRectZero];
self.PINTextField.placeholder = self.businessLogic.selectedAuthentication.pinLabel ?: NSLocalizedString(@"PIN", nil);
switch (self.businessLogic.selectedAuthentication.pinKeyboard) {
case LoginKeyboardStandard:
case LoginKeyboardNone:
self.PINTextField.keyboardType = UIKeyboardTypeASCIICapable;
break;
case LoginKeyboardEmail:
self.PINTextField.keyboardType = UIKeyboardTypeEmailAddress;
break;
case LoginKeyboardNumeric:
self.PINTextField.keyboardType = UIKeyboardTypeNumberPad;
break;
}
self.PINTextField.secureTextEntry = YES;
self.PINTextField.delegate = self.frontEndValidator;
[self.PINTextField
addTarget:self
action:@selector(textFieldsDidChange)
forControlEvents:UIControlEventEditingChanged];
self.PINShowHideButton = [UIButton buttonWithType:UIButtonTypeSystem];
[self.PINShowHideButton setTitle:NSLocalizedString(@"Show", nil) forState:UIControlStateNormal];
[self.PINShowHideButton sizeToFit];
[self.PINShowHideButton addTarget:self action:@selector(PINShowHideSelected)
forControlEvents:UIControlEventTouchUpInside];
self.PINTextField.rightView = self.PINShowHideButton;
self.PINTextField.rightViewMode = UITextFieldViewModeAlways;
[self setupTableData];
self.syncSwitch = [[UISwitch alloc] initWithFrame:CGRectZero];
}
- (NSArray *) cellsForAuthMethod:(AccountDetailsAuthentication *)authenticationMethod {
NSArray *authCells;
if (authenticationMethod.isOauth) {
// if authentication method is Oauth, just insert login/logout button, it will decide what to do by itself
authCells = @[@(CellKindLogInSignOut)];
} else if (authenticationMethod.isSaml && self.businessLogic.isSignedIn) {
// if authentication method is SAML and user is already logged, the only possible action is to logout
// add login/logout button, it will detect by itself that it should be log out in this case
authCells = @[@(CellKindLogInSignOut)];
} else if (authenticationMethod.isSaml) {
// if authentication method is SAML and previous case wasn't fullfilled, make a list of all possible IDPs to login
NSMutableArray *multipleCells = @[].mutableCopy;
for (OPDS2SamlIDP *idp in authenticationMethod.samlIdps) {
TPPSamlIdpCellType *idpCell = [[TPPSamlIdpCellType alloc] initWithIdp:idp];
[multipleCells addObject:idpCell];
}
authCells = multipleCells;
} else if (authenticationMethod.pinKeyboard != LoginKeyboardNone) {
// if authentication method has an information about pin keyboard, the login method is requires a pin
authCells = @[@(CellKindBarcode), @(CellKindPIN), @(CellKindLogInSignOut)];
} else {
// if all other cases failed, it means that server expects just a barcode, with a blank pin
self.PINTextField.text = @"";
authCells = @[@(CellKindBarcode), @(CellKindLogInSignOut)];
}
return authCells;
}
- (NSArray *) accountInfoSection {
NSMutableArray *workingSection = @[].mutableCopy;
if (self.businessLogic.selectedAuthentication.needsAgeCheck) {
workingSection = @[@(CellKindAgeCheck)].mutableCopy;
} else if (!self.businessLogic.selectedAuthentication.needsAuth) {
// no authentication needed, empty section
} else if (self.businessLogic.selectedAuthentication && self.businessLogic.isSignedIn) {
// user already logged in
// show only the selected auth method
[workingSection addObjectsFromArray:[self cellsForAuthMethod:self.businessLogic.selectedAuthentication]];
} else if (!self.businessLogic.isSignedIn && self.businessLogic.userAccount.needsAuth) {
// user needs to sign in
if (self.businessLogic.isSamlPossible) {
// TODO: SIMPLY-2884 add an information header that authentication is required
NSString *libraryInfo = [NSString stringWithFormat:@"Log in to %@ required to download books.", self.businessLogic.libraryAccount.name];
[workingSection addObject:[[TPPInfoHeaderCellType alloc] initWithInformation:libraryInfo]];
}
if (self.businessLogic.libraryAccount.details.auths.count > 1 && !self.businessLogic.libraryAccount.details.defaultAuth.isToken) {
// multiple authentication methods
for (AccountDetailsAuthentication *authenticationMethod in self.businessLogic.libraryAccount.details.auths) {
// show all possible login methods
TPPAuthMethodCellType *autheticationCell = [[TPPAuthMethodCellType alloc] initWithAuthenticationMethod:authenticationMethod];
[workingSection addObject:autheticationCell];
if (authenticationMethod.methodDescription == self.businessLogic.selectedAuthentication.methodDescription) {
// selected method, unfold
[workingSection addObjectsFromArray:[self cellsForAuthMethod:authenticationMethod]];
}
}
} else if (self.businessLogic.libraryAccount.details.auths.count == 1) {
// only 1 authentication method
// no method header needed
[workingSection addObjectsFromArray:[self cellsForAuthMethod:self.businessLogic.libraryAccount.details.auths[0]]];
} else if (self.businessLogic.selectedAuthentication) {
// only 1 authentication method
// no method header needed
[workingSection addObjectsFromArray:[self cellsForAuthMethod:self.businessLogic.selectedAuthentication]];
}
if (self.businessLogic.canResetPassword) {
[workingSection addObject:@(CellKindPasswordReset)];
}
} else {
[workingSection addObjectsFromArray:[self cellsForAuthMethod:self.businessLogic.selectedAuthentication]];
}
if ([self.businessLogic librarySupportsBarcodeDisplay]) {
[workingSection insertObject:@(CellKindBarcodeImage) atIndex:0];
}
return workingSection;
}
- (void)setupTableData
{
NSArray *section0AcctInfo = [self accountInfoSection];
NSMutableArray *section2About = [[NSMutableArray alloc] init];
if ([self.selectedAccount.details getLicenseURL:URLTypePrivacyPolicy]) {
[section2About addObject:@(CellKindPrivacyPolicy)];
}
if ([self.selectedAccount.details getLicenseURL:URLTypeContentLicenses]) {
[section2About addObject:@(CellKindContentLicense)];
}
NSMutableArray *section1Sync = [[NSMutableArray alloc] init];
if ([self.businessLogic shouldShowSyncButton]) {
[section1Sync addObject:@(CellKindSyncButton)];
[section2About addObject:@(CellKindAdvancedSettings)];
}
if ([self.businessLogic registrationIsPossible]) {
self.tableData = @[section0AcctInfo, @[@(CellKindRegistration)], section1Sync].mutableCopy;
} else {
self.tableData = @[section0AcctInfo, section1Sync].mutableCopy;
}
if (self.selectedAccount.hasSupportOption) {
[self.tableData addObject:@[@(CellReportIssue)]];
}
[self.tableData addObject:section2About];
// compute final tableview contents, adding all non-empty sections
NSMutableArray *finalTableContents = [[NSMutableArray alloc] init];
for (NSArray *section in self.tableData) {
if ([section count] != 0) {
[finalTableContents addObject:section];
}
}
self.tableData = finalTableContents;
[self.tableView reloadData];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// The new credentials are not yet saved after signup or after scanning. As such,
// reloading the table would lose the values in the barcode and PIN fields.
if (self.businessLogic.isLoggingInAfterSignUp || self.loggingInAfterBarcodeScan) {
return;
} else {
self.hiddenPIN = YES;
[self accountDidChange];
[self updateShowHidePINState];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
if (self.userBrightnessSetting && [[UIScreen mainScreen] brightness] != self.userBrightnessSetting) {
[[UIScreen mainScreen] setBrightness:self.userBrightnessSetting];
}
}
- (void)viewWillTransitionToSize:(__unused CGSize)size
withTransitionCoordinator:(__unused id<UIViewControllerTransitionCoordinator>)coordinator
{
[self.tableView reloadData];
}
/**
* Update Library Card value
*
*@param username user name or library card value
*/
- (void)setUserName:(nonnull NSString *)username
{
usernameTextField.text = username;
[PINTextField becomeFirstResponder];
}
#pragma mark - Account SignOut
- (void)logOut
{
UIAlertController *alert = [self.businessLogic logOutOrWarn];
if (alert) {
[self presentViewController:alert animated:YES completion:nil];
}
}
- (void)showLogoutAlertWithError:(NSError *)error responseCode:(NSInteger)code
{
NSString *title; NSString *message;
if (code == 401) {
title = @"Unexpected Credentials";
message = @"Your username or password may have changed since the last time you logged in.\n\nIf you believe this is an error, please contact your library.";
} else if (error) {
title = @"SettingsAccountViewControllerLogoutFailed";
message = error.localizedDescription;
} else {
title = @"SettingsAccountViewControllerLogoutFailed";
message = NSLocalizedString(@"An unknown error occurred while trying to sign out.", nil);
}
[self presentViewController:[TPPAlertUtils alertWithTitle:title message:message]
animated:YES
completion:nil];
}
#pragma mark - UITableViewDataSource / UITableViewDelegate + related methods
- (void)tableView:(__attribute__((unused)) UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *const)indexPath
{
NSArray *sectionArray = (NSArray *)self.tableData[indexPath.section];
if ([sectionArray[indexPath.row] isKindOfClass:[TPPAuthMethodCellType class]]) {
TPPAuthMethodCellType *methodCell = sectionArray[indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
self.businessLogic.selectedIDP = nil;
self.businessLogic.selectedAuthentication = methodCell.authenticationMethod;
[self setupTableData];
return;
} else if ([sectionArray[indexPath.row] isKindOfClass:[TPPSamlIdpCellType class]]) {
TPPSamlIdpCellType *idpCell = sectionArray[indexPath.row];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
self.businessLogic.selectedIDP = idpCell.idp;
[self.businessLogic logIn];
return;
} else if ([sectionArray[indexPath.row] isKindOfClass:[TPPInfoHeaderCellType class]]) {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
return;
}
CellKind cellKind = (CellKind)[sectionArray[indexPath.row] intValue];
switch(cellKind) {
case CellKindAgeCheck: {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (!TPPSettings.shared.userPresentedAgeCheck) {
__weak TPPSettingsAccountDetailViewController *weakSelf = self;
[[[AccountsManager shared] ageCheck] verifyCurrentAccountAgeRequirementWithUserAccountProvider:self.businessLogic.userAccount
currentLibraryAccountProvider:self.businessLogic
completion:^(BOOL aboveAgeLimit) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
cell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: @"CheckedCircle"]];
weakSelf.selectedAccount.details.userAboveAgeLimit = aboveAgeLimit;
if (!aboveAgeLimit) {
[[MyBooksDownloadCenter shared] reset:weakSelf.selectedAccountId];
[[TPPBookRegistry shared] reset:weakSelf.selectedAccountId];
}
TPPCatalogNavigationController *catalog = (TPPCatalogNavigationController*)[TPPRootTabBarController sharedController].viewControllers[0];
[catalog popToRootViewControllerAnimated:NO];
[catalog updateFeedAndRegistryOnAccountChange];
}];
}];
}
break;
}
case CellKindBarcode:
[self.usernameTextField becomeFirstResponder];
break;
case CellKindPIN:
[self.PINTextField becomeFirstResponder];
break;
case CellKindLogInSignOut: {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *logoutString;
if([self.selectedUserAccount hasCredentials]) {
if ([self.businessLogic shouldShowSyncButton] && !self.syncSwitch.on) {
logoutString = NSLocalizedString(@"If you sign out without enabling Sync, your books and any saved bookmarks will be removed.", nil);
} else {
logoutString = NSLocalizedString(@"If you sign out, your books and any saved bookmarks will be removed.", nil);
}
UIAlertController *const alertController =
(UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad &&
(self.traitCollection.horizontalSizeClass != UIUserInterfaceSizeClassCompact))
? [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Sign out", nil)
message:logoutString
preferredStyle:UIAlertControllerStyleAlert]
: [UIAlertController alertControllerWithTitle:logoutString
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
alertController.popoverPresentationController.sourceRect = self.view.bounds;
alertController.popoverPresentationController.sourceView = self.view;
[alertController addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"Sign out", @"Title for sign out action")
style:UIAlertActionStyleDestructive
handler:^(__attribute__((unused)) UIAlertAction *action) {
[self logOut];
}]];
[alertController addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"Cancel", nil)
style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:^{
alertController.view.tintColor = [TPPConfiguration mainColor];
}];
} else {
[self.businessLogic logIn];
}
break;
}
case CellKindRegistration: {
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
}
case CellKindSyncButton: {
break;
}
case CellKindAdvancedSettings: {
TPPSettingsAdvancedViewController *vc = [[TPPSettingsAdvancedViewController alloc] initWithAccount:self.selectedAccountId];
[self.navigationController pushViewController:vc animated:YES];
break;
}
case CellKindBarcodeImage: {
[self.tableView beginUpdates];
// Collapse barcode by adjusting certain constraints
if (self.barcodeImageView.bounds.size.height > sConstantZero) {
self.barcodeHeightConstraint.constant = sConstantZero;
self.barcodeTextHeightConstraint.constant = sConstantZero;
self.barcodeTextLabelSpaceConstraint.constant = sConstantZero;
self.barcodeLabelSpaceConstraint.constant = sConstantZero;
self.barcodeImageLabel.text = NSLocalizedString(@"Show Barcode", nil);
[[UIScreen mainScreen] setBrightness:self.userBrightnessSetting];
} else {
self.barcodeHeightConstraint.constant = 100.0;
self.barcodeTextHeightConstraint.constant = 30.0;
self.barcodeTextLabelSpaceConstraint.constant = -sConstantSpacing;
self.barcodeLabelSpaceConstraint.constant = -sConstantSpacing;
self.barcodeImageLabel.text = NSLocalizedString(@"Hide Barcode", nil);
self.userBrightnessSetting = [[UIScreen mainScreen] brightness];
[[UIScreen mainScreen] setBrightness:1.0];
}
[self.tableView endUpdates];
break;
}
case CellReportIssue: {
if (self.selectedAccount.supportEmail) {
[[ProblemReportEmail sharedInstance]
beginComposingTo:self.selectedAccount.supportEmail.rawValue
presentingViewController:self
book:nil];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
break;
} else {
BundledHTMLViewController *webController = [[BundledHTMLViewController alloc] initWithFileURL:AccountsManager.sharedInstance.currentAccount.supportURL title:AccountsManager.shared.currentAccount.name];
webController.hidesBottomBarWhenPushed = true;
[self.navigationController pushViewController:webController animated:YES];
break;
}
}
case CellKindAbout: {
RemoteHTMLViewController *vc = [[RemoteHTMLViewController alloc]
initWithURL:[self.selectedAccount.details getLicenseURL:URLTypeAcknowledgements]
title:NSLocalizedString(@"About", nil)
failureMessage:NSLocalizedString(@"The page could not load due to a connection error.", nil)];
[self.navigationController pushViewController:vc animated:YES];
break;
}
case CellKindPrivacyPolicy: {
RemoteHTMLViewController *vc = [[RemoteHTMLViewController alloc]
initWithURL:[self.selectedAccount.details getLicenseURL:URLTypePrivacyPolicy]
title:NSLocalizedString(@"Privacy Policy", nil)
failureMessage:NSLocalizedString(@"The page could not load due to a connection error.", nil)];
[self.navigationController pushViewController:vc animated:YES];
break;
}
case CellKindContentLicense: {
RemoteHTMLViewController *vc = [[RemoteHTMLViewController alloc]
initWithURL:[self.selectedAccount.details getLicenseURL:URLTypeContentLicenses]
title:NSLocalizedString(@"Content Licenses", nil)
failureMessage:NSLocalizedString(@"The page could not load due to a connection error.", nil)];
[self.navigationController pushViewController:vc animated:YES];
break;
}
case CellKindPasswordReset:
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.businessLogic resetPassword];
break;
}
}
- (void)didSelectRegularSignupOnCell:(UITableViewCell *)cell
{
[cell setUserInteractionEnabled:NO];
__weak __auto_type weakSelf = self;
[self.businessLogic startRegularCardCreationWithCompletion:^(UINavigationController * _Nullable navVC, NSError * _Nullable error) {
[cell setUserInteractionEnabled:YES];
if (error) {
UIAlertController *alert = [TPPAlertUtils alertWithTitle:NSLocalizedString(@"Error", "Alert title") error:error];
[TPPAlertUtils presentFromViewControllerOrNilWithAlertController:alert
viewController:nil
animated:YES
completion:nil];
[self.tableView reloadData];
return;
}
[TPPMainThreadRun asyncIfNeeded:^{
navVC.navigationBar.topItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil)
style:UIBarButtonItemStylePlain
target:weakSelf
action:@selector(didSelectBackForSignUp)];
navVC.modalPresentationStyle = UIModalPresentationFormSheet;
[weakSelf presentViewController:navVC animated:YES completion:nil];
}];
}];
}
- (void)didSelectBackForSignUp
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (UITableViewCell *)tableView:(__attribute__((unused)) UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *const)indexPath
{
NSArray *sectionArray = (NSArray *)self.tableData[indexPath.section];
if ([sectionArray[indexPath.row] isKindOfClass:[TPPAuthMethodCellType class]]) {
TPPAuthMethodCellType *methodCell = sectionArray[indexPath.row];
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = methodCell.authenticationMethod.methodDescription;
return cell;
} else if ([sectionArray[indexPath.row] isKindOfClass:[TPPSamlIdpCellType class]]) {
TPPSamlIdpCellType *idpCell = sectionArray[indexPath.row];
TPPSamlIDPCell *cell = [[TPPSamlIDPCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.idpName.text = idpCell.idp.displayName;
return cell;
} else if ([sectionArray[indexPath.row] isKindOfClass:[TPPInfoHeaderCellType class]]) {
TPPInfoHeaderCellType *infoCell = sectionArray[indexPath.row];
TPPLibraryDescriptionCell *cell = [[TPPLibraryDescriptionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
cell.descriptionLabel.text = infoCell.information;
return cell;
}
CellKind cellKind = (CellKind)[sectionArray[indexPath.row] intValue];
switch(cellKind) {
case CellKindBarcode: {
UITableViewCell *const cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
{
self.usernameTextField.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
[cell.contentView addSubview:self.usernameTextField];
self.usernameTextField.preservesSuperviewLayoutMargins = YES;
[self.usernameTextField autoPinEdgeToSuperviewMargin:ALEdgeRight];
[self.usernameTextField autoPinEdgeToSuperviewMargin:ALEdgeLeft];
[self.usernameTextField autoConstrainAttribute:ALAttributeTop toAttribute:ALAttributeMarginTop
ofView:[self.usernameTextField superview]
withOffset:sVerticalMarginPadding];
[self.usernameTextField autoConstrainAttribute:ALAttributeBottom toAttribute:ALAttributeMarginBottom
ofView:[self.usernameTextField superview]
withOffset:-sVerticalMarginPadding];
if (self.businessLogic.selectedAuthentication.supportsBarcodeScanner) {
[cell.contentView addSubview:self.barcodeScanButton];
CGFloat rightMargin = cell.layoutMargins.right;
self.barcodeScanButton.contentEdgeInsets = UIEdgeInsetsMake(0, rightMargin * 2, 0, rightMargin);
[self.barcodeScanButton autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeLeading];
if (!self.usernameTextField.enabled) {
self.barcodeScanButton.hidden = YES;
}
}
}
return cell;
}
case CellKindBarcodeImage:{
UITableViewCell *const cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
if (![self.businessLogic librarySupportsBarcodeDisplay]) {
TPPLOG(@"A nonvalid library was attempting to create a barcode image.");
} else {
TPPBarcode *barcode = [[TPPBarcode alloc] initWithLibrary:self.selectedAccount.name];
UIImage *barcodeImage = [barcode imageFromString:self.selectedUserAccount.authorizationIdentifier];
if (barcodeImage) {
self.barcodeImageView = [[UIImageView alloc] initWithImage:barcodeImage];
self.barcodeImageLabel = [[UILabel alloc] init];
self.barcodeTextLabel = [[UILabel alloc] init];
self.barcodeTextLabel.text = self.selectedUserAccount.authorizationIdentifier;
self.barcodeTextLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
self.barcodeTextLabel.textAlignment = NSTextAlignmentCenter;
self.barcodeImageLabel.text = NSLocalizedString(@"Show Barcode", nil);
self.barcodeImageLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
self.barcodeImageLabel.textColor = [TPPConfiguration mainColor];
[cell.contentView addSubview:self.barcodeImageView];
[cell.contentView addSubview:self.barcodeTextLabel];
[cell.contentView addSubview:self.barcodeImageLabel];
[self.barcodeTextLabel autoAlignAxisToSuperviewAxis:ALAxisVertical];
[self.barcodeTextLabel autoSetDimension:ALDimensionWidth toSize:self.tableView.bounds.size.width];
[self.barcodeImageView autoAlignAxisToSuperviewAxis:ALAxisVertical];
[self.barcodeImageView autoSetDimension:ALDimensionWidth toSize:self.tableView.bounds.size.width];
[NSLayoutConstraint autoSetPriority:UILayoutPriorityRequired forConstraints:^{
// Hidden to start
self.barcodeHeightConstraint = [self.barcodeImageView autoSetDimension:ALDimensionHeight toSize:0];
self.barcodeTextHeightConstraint = [self.barcodeTextLabel autoSetDimension:ALDimensionHeight toSize:0];
self.barcodeLabelSpaceConstraint = [self.barcodeImageView autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.barcodeTextLabel withOffset:0];
self.barcodeTextLabelSpaceConstraint = [self.barcodeTextLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeTop ofView:self.barcodeImageLabel withOffset:0];
}];
[self.barcodeImageView autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:sConstantSpacing];
[self.barcodeImageLabel autoAlignAxisToSuperviewAxis:ALAxisVertical];
[NSLayoutConstraint autoSetPriority:UILayoutPriorityDefaultHigh forConstraints:^{
[self.barcodeImageLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10.0];
}];
}
}
return cell;
}
case CellKindPIN: {
UITableViewCell *const cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
{
self.PINTextField.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
[cell.contentView addSubview:self.PINTextField];
self.PINTextField.preservesSuperviewLayoutMargins = YES;
[self.PINTextField autoPinEdgeToSuperviewMargin:ALEdgeRight];
[self.PINTextField autoPinEdgeToSuperviewMargin:ALEdgeLeft];
[self.PINTextField autoConstrainAttribute:ALAttributeTop toAttribute:ALAttributeMarginTop
ofView:[self.PINTextField superview]
withOffset:2.0];
[self.PINTextField autoConstrainAttribute:ALAttributeBottom toAttribute:ALAttributeMarginBottom
ofView:[self.PINTextField superview]
withOffset:-2.0];
}
return cell;
}
case CellKindLogInSignOut: {
if(!self.logInSignOutCell) {
self.logInSignOutCell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
self.logInSignOutCell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
}
[self updateLoginLogoutCellAppearance];
return self.logInSignOutCell;
}
case CellKindRegistration: {
RegistrationCell *cell = [RegistrationCell new];
[cell configureWithTitle:nil body:nil buttonTitle:nil buttonAction:^{
[self didSelectRegularSignupOnCell:cell];
}];
return cell;
}
case CellKindAgeCheck: {
self.ageCheckCell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
UIImageView *accessoryView = TPPSettings.shared.userPresentedAgeCheck ? [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CheckedCircle"]] : nil;
accessoryView.image = [accessoryView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
accessoryView.tintColor = [UIColor systemGreenColor];
self.ageCheckCell.accessoryView = accessoryView;
self.ageCheckCell.selectionStyle = TPPSettings.shared.userPresentedAgeCheck ? UITableViewCellSelectionStyleNone : UITableViewCellSelectionStyleDefault;
self.ageCheckCell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
self.ageCheckCell.textLabel.text = NSLocalizedString(@"Age Verification",
@"Statement that confirms if a user completed the age verification");
return self.ageCheckCell;
}
case CellKindSyncButton: {
UITableViewCell *const cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
self.syncSwitch.on = self.selectedAccount.details.syncPermissionGranted;
self.syncSwitch.enabled = true;
cell.accessoryView = self.syncSwitch;
[self.syncSwitch addTarget:self action:@selector(syncSwitchChanged:) forControlEvents:UIControlEventValueChanged];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = NSLocalizedString(@"Sync Bookmarks",
@"Title for switch to turn on or off syncing.");
return cell;
}
case CellReportIssue: {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = NSLocalizedString(@"Report an Issue", nil);
return cell;
}
case CellKindAbout: {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = [NSString stringWithFormat:@"About %@",self.selectedAccount.name];
cell.hidden = ([self.selectedAccount.details getLicenseURL:URLTypeAcknowledgements]) ? NO : YES;
return cell;
}
case CellKindPrivacyPolicy: {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = NSLocalizedString(@"Privacy Policy", nil);
cell.hidden = ([self.selectedAccount.details getLicenseURL:URLTypePrivacyPolicy]) ? NO : YES;
return cell;
}
case CellKindContentLicense: {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = NSLocalizedString(@"Content Licenses", nil);
cell.hidden = ([self.selectedAccount.details getLicenseURL:URLTypeContentLicenses]) ? NO : YES;
return cell;
}
case CellKindAdvancedSettings: {
UITableViewCell *cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont customFontForTextStyle:UIFontTextStyleBody];
cell.textLabel.text = NSLocalizedString(@"Advanced", nil);
return cell;
}
case CellKindPasswordReset:
return [self createPasswordResetCell];
}
}
- (UITableViewCell *)createPasswordResetCell {
UITableViewCell *cell = [[UITableViewCell alloc] init];
cell.textLabel.text = NSLocalizedString(@"Forgot your password?", "Password Reset");
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(__attribute__((unused)) UITableView *)tableView
{
return self.businessLogic.isAuthenticationDocumentLoading ? 0 : self.tableData.count;
}
- (NSInteger)tableView:(__attribute__((unused)) UITableView *)tableView
numberOfRowsInSection:(NSInteger const)section
{
if (section > (int)self.tableData.count - 1) {
return 0;
} else {
return [(NSArray *)self.tableData[section] count];
}
}
- (CGFloat)tableView:(__unused UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section == sSection0AccountInfo) {
return UITableViewAutomaticDimension;
}
return 0;
}
- (CGFloat)tableView:(__unused UITableView *)tableView heightForFooterInSection:(__unused NSInteger)section
{
if ((section == sSection0AccountInfo && [self.businessLogic shouldShowEULALink]) ||
(section == sSection1Sync && [self.businessLogic shouldShowSyncButton])) {
return UITableViewAutomaticDimension;