forked from omz/AppSales-Mobile
/
AccountsViewController+ButtonActions.m
193 lines (171 loc) · 8.2 KB
/
AccountsViewController+ButtonActions.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
//
// AccountsViewController+ButtonActions.m
// AppSales
//
// Created by Nicolas Gomollon on 8/9/17.
//
//
#import "AccountsViewController+ButtonActions.h"
#import "ASProgressHUD.h"
@implementation AccountsViewController (AccountsViewController_ButtonActions)
- (void)getAccessTokenWithLogin:(NSDictionary *)loginInfo {
pressedAccountButton = AccountButtonTypeGetAccessToken;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
LoginManager *loginManager = [[LoginManager alloc] initWithLoginInfo:loginInfo];
loginManager.shouldDeleteCookies = NO;
loginManager.delegate = self;
[loginManager logIn];
});
}
- (void)generateAccessTokenWithLogin:(NSDictionary *)loginInfo {
pressedAccountButton = AccountButtonTypeGenerateAccessToken;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
LoginManager *loginManager = [[LoginManager alloc] initWithLoginInfo:loginInfo];
loginManager.shouldDeleteCookies = NO;
loginManager.delegate = self;
[loginManager logIn];
});
}
- (void)findVendorIDsWithLogin:(NSDictionary *)loginInfo {
if (vendors == nil) {
vendors = [[NSMutableDictionary alloc] init];
} else {
[vendors removeAllObjects];
}
pressedAccountButton = AccountButtonTypeSelectVendorID;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
LoginManager *loginManager = [[LoginManager alloc] initWithLoginInfo:loginInfo];
loginManager.shouldDeleteCookies = NO;
loginManager.delegate = self;
[loginManager logIn];
});
}
- (void)loginSucceeded {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul), ^{
@autoreleasepool {
switch (pressedAccountButton) {
case AccountButtonTypeGetAccessToken:
case AccountButtonTypeGenerateAccessToken: {
LoginManager *loginManager = [[LoginManager alloc] initWithLoginInfo:nil];
NSString *csrfToken = [loginManager generateCSRFToken];
if (csrfToken != nil) {
NSDictionary *resultDict = nil;
if (pressedAccountButton == AccountButtonTypeGenerateAccessToken) {
resultDict = [loginManager resetAccessKey:csrfToken];
} else {
resultDict = [loginManager getAccessKey:csrfToken];
}
if (resultDict != nil) {
// TODO: Save `expiryDate` and `createdDate` for the fetched access token, so as to let the user know when the token will expire.
NSString *accessToken = resultDict[@"accessKey"];
dispatch_async(dispatch_get_main_queue(), ^{
[self finishedFetchingAccessToken:accessToken];
});
} else {
[self performSelectorOnMainThread:@selector(failedToFetchAccessToken) withObject:nil waitUntilDone:YES];
}
} else {
[self performSelectorOnMainThread:@selector(failedToGenerateCSRFToken) withObject:nil waitUntilDone:YES];
}
break;
}
case AccountButtonTypeSelectVendorID: {
NSURL *userDetailURL = [NSURL URLWithString:[kITCBaseURL stringByAppendingString:kITCUserDetailAction]];
NSData *userDetailData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:userDetailURL] returningResponse:nil error:nil];
NSDictionary *userDetail = [NSJSONSerialization JSONObjectWithData:userDetailData options:0 error:nil];
NSString *contentProviderId = userDetail[@"data"][@"contentProviderId"];
if (contentProviderId.length > 0) {
NSURL *paymentVendorsURL = [NSURL URLWithString:[kITCBaseURL stringByAppendingFormat:kITCPaymentVendorsAction, contentProviderId]];
NSData *paymentVendorsData = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:paymentVendorsURL] returningResponse:nil error:nil];
NSDictionary *paymentVendors = [NSJSONSerialization JSONObjectWithData:paymentVendorsData options:0 error:nil];
NSArray *sapVendors = paymentVendors[@"data"];
if (sapVendors.count > 0) {
for (NSDictionary *vendor in sapVendors) {
NSNumber *vendorID = vendor[@"sapVendorNumber"];
NSString *vendorName = vendor[@"vendorName"];
vendors[vendorID.description] = vendorName;
}
[self performSelectorOnMainThread:@selector(finishedLoadingVendors) withObject:nil waitUntilDone:YES];
} else {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
}
} else {
[self performSelectorOnMainThread:@selector(failedToLoadVendorIDs) withObject:nil waitUntilDone:YES];
}
break;
}
}
}
});
}
- (void)loginFailed {
[ASProgressHUD hideHUDForView:self.currentViewController.navigationController.view animated:YES];
}
- (void)finishedFetchingAccessToken:(NSString *)accessToken {
FieldEditorViewController *vc = self.currentViewController;
[ASProgressHUD hideHUDForView:vc.navigationController.view animated:YES];
[vc.values setObject:accessToken forKey:kAccountAccessToken];
[vc.tableView reloadData];
}
- (void)failedToFetchAccessToken {
[ASProgressHUD hideHUDForView:self.currentViewController.navigationController.view animated:YES];
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"The access token could not be fetched automatically. Please check your username and password or enter your access token manually. You'll find it in the Sales and Trends module, under the Reports section, by clicking on the (?) beside About Reports on itunesconnect.apple.com.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil] show];
}
- (void)failedToGenerateCSRFToken {
[ASProgressHUD hideHUDForView:self.currentViewController.navigationController.view animated:YES];
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"Could not fetch CSRF token from server. Please try again later.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil] show];
}
- (void)finishedLoadingVendors {
switch (vendors.count) {
case 0: {
[self failedToLoadVendorIDs];
break;
}
case 1: {
[self finishedLoadingVendorID:vendors.allKeys.firstObject];
break;
}
default: {
[self chooseVendor];
break;
}
}
}
- (void)chooseVendor {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Select Your Primary Vendor", nil)
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
for (NSString *vendorID in vendors) {
NSString *vendorName = vendors[vendorID];
NSString *buttonTitle = [NSString stringWithFormat:@"%@ (%@)", vendorName, vendorID];
[alertController addAction:[UIAlertAction actionWithTitle:buttonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self finishedLoadingVendorID:vendorID];
}]];
}
[alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
[ASProgressHUD hideHUDForView:self.currentViewController.navigationController.view animated:YES];
}]];
[self.currentViewController presentViewController:alertController animated:YES completion:nil];
}
- (void)finishedLoadingVendorID:(NSString *)vendorID {
FieldEditorViewController *vc = self.currentViewController;
[ASProgressHUD hideHUDForView:vc.navigationController.view animated:YES];
[vc.values setObject:vendorID forKey:kAccountVendorID];
[vc.tableView reloadData];
}
- (void)failedToLoadVendorIDs {
[ASProgressHUD hideHUDForView:self.currentViewController.navigationController.view animated:YES];
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", nil) message:NSLocalizedString(@"The vendor ID could not be filled automatically. Please check your username and password or enter your vendor ID manually. You'll find it at the top of the Sales and Trends module on itunesconnect.apple.com.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"OK", nil) otherButtonTitles:nil] show];
}
- (FieldEditorViewController *)currentViewController {
FieldEditorViewController *vc = nil;
if (self.presentedViewController) {
// Adding new account.
vc = ((UINavigationController *)self.presentedViewController).viewControllers[0];
} else {
// Editing existing account.
vc = self.navigationController.viewControllers.lastObject;
}
return vc;
}
@end