|
2 | 2 |
|
3 | 3 | #import "UAUnityMessageViewController.h" |
4 | 4 |
|
| 5 | +@interface UAUnityMessageViewController() <UAMessageCenterMessageViewDelegate> |
| 6 | +@property (nonatomic, strong) UADefaultMessageCenterMessageViewController *airshipMessageViewController; |
| 7 | +@end |
| 8 | + |
5 | 9 | @implementation UAUnityMessageViewController |
6 | 10 |
|
7 | | -- (void) viewDidLoad { |
8 | | - [super viewDidLoad]; |
9 | | - |
10 | | - UIBarButtonItem *done = [[UIBarButtonItem alloc] |
11 | | - initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
12 | | - target:self |
13 | | - action:@selector(dismissMessageViewController:)]; |
14 | | - |
15 | | - self.navigationItem.rightBarButtonItem = done; |
16 | | - |
17 | | - __weak UAUnityMessageViewController *weakSelf = self; |
18 | | - self.closeBlock = ^(BOOL animated) { |
19 | | - [weakSelf dismissViewControllerAnimated:animated completion:nil]; |
20 | | - }; |
| 11 | +- (instancetype)init { |
| 12 | + self = [super init]; |
| 13 | + |
| 14 | + if (self) { |
| 15 | + self.airshipMessageViewController = [[UADefaultMessageCenterMessageViewController alloc] initWithNibName:@"UADefaultMessageCenterMessageViewController" |
| 16 | + bundle:[UAMessageCenterResources bundle]]; |
| 17 | + self.airshipMessageViewController.delegate = self; |
| 18 | + |
| 19 | + UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
| 20 | + target:self |
| 21 | + action:@selector(inboxMessageDone:)]; |
| 22 | + |
| 23 | + self.airshipMessageViewController.navigationItem.leftBarButtonItem = done; |
| 24 | + |
| 25 | + self.viewControllers = @[self.airshipMessageViewController]; |
| 26 | + |
| 27 | + self.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; |
| 28 | + self.modalPresentationStyle = UIModalPresentationFullScreen; |
| 29 | + } |
| 30 | + |
| 31 | + return self; |
21 | 32 | } |
22 | 33 |
|
23 | | -- (void) dismissMessageViewController:(id)sender { |
| 34 | +- (void)inboxMessageDone:(id)sender { |
24 | 35 | [self dismissViewControllerAnimated:true completion:nil]; |
25 | 36 | } |
26 | 37 |
|
| 38 | +- (void)loadMessageForID:(NSString *)messageID { |
| 39 | + [self.airshipMessageViewController loadMessageForID:messageID]; |
| 40 | +} |
| 41 | + |
| 42 | +#pragma mark UAMessageCenterMessageViewDelegate |
| 43 | + |
| 44 | +- (void)messageClosed:(NSString *)messageID { |
| 45 | + [self dismissViewControllerAnimated:YES completion:nil]; |
| 46 | +} |
| 47 | + |
| 48 | +- (void)messageLoadStarted:(NSString *)messageID { |
| 49 | + // no-op |
| 50 | +} |
| 51 | + |
| 52 | +- (void)messageLoadSucceeded:(NSString *)messageID { |
| 53 | + // no-op |
| 54 | +} |
| 55 | + |
| 56 | +- (void)displayFailedToLoadAlertOnOK:(void (^)(void))okCompletion onRetry:(void (^)(void))retryCompletion { |
| 57 | + UIAlertController* alert = [UIAlertController alertControllerWithTitle:UAMessageCenterLocalizedString(@"ua_connection_error") |
| 58 | + message:UAMessageCenterLocalizedString(@"ua_mc_failed_to_load") |
| 59 | + preferredStyle:UIAlertControllerStyleAlert]; |
| 60 | + |
| 61 | + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:UAMessageCenterLocalizedString(@"ua_ok") |
| 62 | + style:UIAlertActionStyleDefault |
| 63 | + handler:^(UIAlertAction * action) { |
| 64 | + if (okCompletion) { |
| 65 | + okCompletion(); |
| 66 | + } |
| 67 | + }]; |
| 68 | + |
| 69 | + [alert addAction:defaultAction]; |
| 70 | + |
| 71 | + if (retryCompletion) { |
| 72 | + UIAlertAction *retryAction = [UIAlertAction actionWithTitle:UAMessageCenterLocalizedString(@"ua_retry_button") |
| 73 | + style:UIAlertActionStyleDefault |
| 74 | + handler:^(UIAlertAction * _Nonnull action) { |
| 75 | + if (retryCompletion) { |
| 76 | + retryCompletion(); |
| 77 | + } |
| 78 | + }]; |
| 79 | + |
| 80 | + [alert addAction:retryAction]; |
| 81 | + } |
| 82 | + |
| 83 | + [self presentViewController:alert animated:YES completion:nil]; |
| 84 | +} |
| 85 | + |
| 86 | +- (void)displayNoLongerAvailableAlertOnOK:(void (^)(void))okCompletion { |
| 87 | + UIAlertController* alert = [UIAlertController alertControllerWithTitle:UAMessageCenterLocalizedString(@"ua_content_error") |
| 88 | + message:UAMessageCenterLocalizedString(@"ua_mc_no_longer_available") |
| 89 | + preferredStyle:UIAlertControllerStyleAlert]; |
| 90 | + |
| 91 | + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:UAMessageCenterLocalizedString(@"ua_ok") |
| 92 | + style:UIAlertActionStyleDefault |
| 93 | + handler:^(UIAlertAction * action) { |
| 94 | + if (okCompletion) { |
| 95 | + okCompletion(); |
| 96 | + } |
| 97 | + }]; |
| 98 | + |
| 99 | + [alert addAction:defaultAction]; |
| 100 | + |
| 101 | + [self presentViewController:alert animated:YES completion:nil]; |
| 102 | +} |
| 103 | + |
| 104 | +- (void)messageLoadFailed:(NSString *)messageID error:(NSError *)error { |
| 105 | + UA_LTRACE(@"message load failed: %@", messageID); |
| 106 | + |
| 107 | + void (^retry)(void) = ^{ |
| 108 | + UA_WEAKIFY(self); |
| 109 | + [self displayFailedToLoadAlertOnOK:^{ |
| 110 | + UA_STRONGIFY(self) |
| 111 | + [self dismissViewControllerAnimated:true completion:nil]; |
| 112 | + } onRetry:^{ |
| 113 | + UA_STRONGIFY(self) |
| 114 | + [self loadMessageForID:messageID]; |
| 115 | + }]; |
| 116 | + }; |
| 117 | + |
| 118 | + void (^handleFailed)(void) = ^{ |
| 119 | + UA_WEAKIFY(self); |
| 120 | + [self displayFailedToLoadAlertOnOK:^{ |
| 121 | + UA_STRONGIFY(self) |
| 122 | + [self dismissViewControllerAnimated:true completion:nil]; |
| 123 | + } onRetry:nil]; |
| 124 | + }; |
| 125 | + |
| 126 | + void (^handleExpired)(void) = ^{ |
| 127 | + UA_WEAKIFY(self); |
| 128 | + [self displayNoLongerAvailableAlertOnOK:^{ |
| 129 | + UA_STRONGIFY(self) |
| 130 | + [self dismissViewControllerAnimated:true completion:nil]; |
| 131 | + }]; |
| 132 | + }; |
| 133 | + |
| 134 | + if ([error.domain isEqualToString:UAMessageCenterMessageLoadErrorDomain]) { |
| 135 | + if (error.code == UAMessageCenterMessageLoadErrorCodeFailureStatus) { |
| 136 | + // Encountered a failure status code |
| 137 | + NSUInteger status = [error.userInfo[UAMessageCenterMessageLoadErrorHTTPStatusKey] unsignedIntValue]; |
| 138 | + |
| 139 | + if (status >= 500) { |
| 140 | + retry(); |
| 141 | + } else if (status == 410) { |
| 142 | + // Gone: message has been permanently deleted from the backend. |
| 143 | + handleExpired(); |
| 144 | + } else { |
| 145 | + handleFailed(); |
| 146 | + } |
| 147 | + } else if (error.code == UAMessageCenterMessageLoadErrorCodeMessageExpired) { |
| 148 | + handleExpired(); |
| 149 | + } else { |
| 150 | + retry(); |
| 151 | + } |
| 152 | + } else { |
| 153 | + // Other errors |
| 154 | + retry(); |
| 155 | + } |
| 156 | +} |
| 157 | + |
27 | 158 | @end |
| 159 | + |
0 commit comments