-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUAUnityMessageViewController.m
More file actions
159 lines (125 loc) · 5.89 KB
/
UAUnityMessageViewController.m
File metadata and controls
159 lines (125 loc) · 5.89 KB
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
/* Copyright Urban Airship and Contributors */
#import "UAUnityMessageViewController.h"
@interface UAUnityMessageViewController() <UAMessageCenterMessageViewDelegate>
@property (nonatomic, strong) UADefaultMessageCenterMessageViewController *airshipMessageViewController;
@end
@implementation UAUnityMessageViewController
- (instancetype)init {
self = [super init];
if (self) {
self.airshipMessageViewController = [[UADefaultMessageCenterMessageViewController alloc] initWithNibName:@"UADefaultMessageCenterMessageViewController"
bundle:[UAMessageCenterResources bundle]];
self.airshipMessageViewController.delegate = self;
UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(inboxMessageDone:)];
self.airshipMessageViewController.navigationItem.leftBarButtonItem = done;
self.viewControllers = @[self.airshipMessageViewController];
self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
self.modalPresentationStyle = UIModalPresentationFullScreen;
}
return self;
}
- (void)inboxMessageDone:(id)sender {
[self dismissViewControllerAnimated:true completion:nil];
}
- (void)loadMessageForID:(NSString *)messageID {
[self.airshipMessageViewController loadMessageForID:messageID];
}
#pragma mark UAMessageCenterMessageViewDelegate
- (void)messageClosed:(NSString *)messageID {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)messageLoadStarted:(NSString *)messageID {
// no-op
}
- (void)messageLoadSucceeded:(NSString *)messageID {
// no-op
}
- (void)displayFailedToLoadAlertOnOK:(void (^)(void))okCompletion onRetry:(void (^)(void))retryCompletion {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:UAMessageCenterLocalizedString(@"ua_connection_error")
message:UAMessageCenterLocalizedString(@"ua_mc_failed_to_load")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:UAMessageCenterLocalizedString(@"ua_ok")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (okCompletion) {
okCompletion();
}
}];
[alert addAction:defaultAction];
if (retryCompletion) {
UIAlertAction *retryAction = [UIAlertAction actionWithTitle:UAMessageCenterLocalizedString(@"ua_retry_button")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
if (retryCompletion) {
retryCompletion();
}
}];
[alert addAction:retryAction];
}
[self presentViewController:alert animated:YES completion:nil];
}
- (void)displayNoLongerAvailableAlertOnOK:(void (^)(void))okCompletion {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:UAMessageCenterLocalizedString(@"ua_content_error")
message:UAMessageCenterLocalizedString(@"ua_mc_no_longer_available")
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:UAMessageCenterLocalizedString(@"ua_ok")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
if (okCompletion) {
okCompletion();
}
}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
- (void)messageLoadFailed:(NSString *)messageID error:(NSError *)error {
UA_LTRACE(@"message load failed: %@", messageID);
void (^retry)(void) = ^{
UA_WEAKIFY(self);
[self displayFailedToLoadAlertOnOK:^{
UA_STRONGIFY(self)
[self dismissViewControllerAnimated:true completion:nil];
} onRetry:^{
UA_STRONGIFY(self)
[self loadMessageForID:messageID];
}];
};
void (^handleFailed)(void) = ^{
UA_WEAKIFY(self);
[self displayFailedToLoadAlertOnOK:^{
UA_STRONGIFY(self)
[self dismissViewControllerAnimated:true completion:nil];
} onRetry:nil];
};
void (^handleExpired)(void) = ^{
UA_WEAKIFY(self);
[self displayNoLongerAvailableAlertOnOK:^{
UA_STRONGIFY(self)
[self dismissViewControllerAnimated:true completion:nil];
}];
};
if ([error.domain isEqualToString:UAMessageCenterMessageLoadErrorDomain]) {
if (error.code == UAMessageCenterMessageLoadErrorCodeFailureStatus) {
// Encountered a failure status code
NSUInteger status = [error.userInfo[UAMessageCenterMessageLoadErrorHTTPStatusKey] unsignedIntValue];
if (status >= 500) {
retry();
} else if (status == 410) {
// Gone: message has been permanently deleted from the backend.
handleExpired();
} else {
handleFailed();
}
} else if (error.code == UAMessageCenterMessageLoadErrorCodeMessageExpired) {
handleExpired();
} else {
retry();
}
} else {
// Other errors
retry();
}
}
@end