From 2c046a01003665aa78ca8f069c215ba1420c76cc Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 1 Dec 2021 17:43:49 +0300 Subject: Present login error correctly on iPad/Mac --- Juick/API.h | 2 ++ Juick/API.m | 4 +++- Juick/Helpers/User+UIView.h | 2 +- Juick/Helpers/User+UIView.m | 13 ++++++------- Juick/ViewControllers/LoginViewController.m | 5 +++++ Juick/ViewControllers/MessagesViewController.m | 2 +- Juick/ViewControllers/NewPostViewController.m | 4 ++-- 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Juick/API.h b/Juick/API.h index de771c4..ad1433b 100644 --- a/Juick/API.h +++ b/Juick/API.h @@ -9,6 +9,8 @@ #import "Message.h" #import "DeviceRegistration.h" +extern NSString * const JuickErrorDomain; + @protocol JuickAuthorizationDelegate -(void) unauthorized; @end diff --git a/Juick/API.m b/Juick/API.m index ce8915a..4c3edc2 100644 --- a/Juick/API.m +++ b/Juick/API.m @@ -9,6 +9,8 @@ #import "Chat.h" #import "API.h" +NSString * const JuickErrorDomain = @"JuickErrorDomain"; + @interface API () @property(nonatomic, strong) NSURL *baseURL; @property(nonatomic, strong) NSURLSession *urlSession; @@ -207,7 +209,7 @@ NSInteger statusCode = ((NSHTTPURLResponse *)response).statusCode; if (!response || statusCode != 200) { NSURL *url = request.URL; - NSError *err = [NSError errorWithDomain:@"JuickErrorDomain" + NSError *err = [NSError errorWithDomain:JuickErrorDomain code:statusCode userInfo:@{@"url": url.absoluteString}]; callback(nil, err); diff --git a/Juick/Helpers/User+UIView.h b/Juick/Helpers/User+UIView.h index d2f75ce..dcd7047 100644 --- a/Juick/Helpers/User+UIView.h +++ b/Juick/Helpers/User+UIView.h @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface User (UIView) -+ (void) throwUnableToLogin:(UIViewController *)view error:(NSError *)error path:(NSString *)path params:(NSDictionary * _Nullable)params; ++ (void) throwUnableToLogin:(UIViewController *)parentViewController sourceView:(UIView *)sourceView error:(NSError *)error path:(NSString *)path params:(NSDictionary * _Nullable)params; @end diff --git a/Juick/Helpers/User+UIView.m b/Juick/Helpers/User+UIView.m index 4fb0bc3..2235cef 100644 --- a/Juick/Helpers/User+UIView.m +++ b/Juick/Helpers/User+UIView.m @@ -10,19 +10,18 @@ @implementation User (UIView) -+(void) throwUnableToLogin:(UIViewController *)view error:(NSError *)error path:(NSString *)path params:(NSDictionary *)params { ++(void) throwUnableToLogin:(UIViewController *)parentViewController sourceView:(UIView *)sourceView error:(NSError *)error path:(NSString *)path params:(NSDictionary *)params { UIAlertController *alert = [UIAlertController new]; NSString *title = error.userInfo[@"url"] ? error.userInfo[@"url"] : @"Something went wrong"; [alert setTitle:title]; [alert setMessage:[NSString stringWithFormat:@"err: %@, path: %@, params: %@", error.localizedDescription, path, params]]; [alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:nil]]; - UIPopoverPresentationController *popover = [alert popoverPresentationController]; - if (popover) { - popover.sourceView = view.view; - popover.sourceRect = CGRectMake(CGRectGetMidX(view.view.bounds), CGRectGetMidY(view.view.bounds), 0, 0); - popover.permittedArrowDirections = UIPopoverArrowDirectionDown; + UIPopoverPresentationController *popopverPresenter = [alert popoverPresentationController]; + if (popopverPresenter) { + popopverPresenter.sourceView = sourceView; + popopverPresenter.sourceRect = CGRectMake(CGRectGetMidX(sourceView.bounds), CGRectGetMidY(sourceView.bounds), 0, 0); } - [view presentViewController:alert animated:YES completion:nil]; + [parentViewController presentViewController:alert animated:YES completion:nil]; } @end diff --git a/Juick/ViewControllers/LoginViewController.m b/Juick/ViewControllers/LoginViewController.m index a0e9b97..dd80215 100644 --- a/Juick/ViewControllers/LoginViewController.m +++ b/Juick/ViewControllers/LoginViewController.m @@ -8,6 +8,7 @@ #import "LoginViewController.h" #import "User.h" +#import "User+UIView.h" NSString * const UserChangedNotificationName = @"UserSignedIn"; @@ -57,6 +58,10 @@ NSString * const UserChangedNotificationName = @"UserSignedIn"; [[NSNotificationCenter defaultCenter] postNotificationName:UserChangedNotificationName object:nil]; [[AppDelegate shared] registerForRemoteNotifications]; [self.navigationController dismissViewControllerAnimated:YES completion:nil]; + } else { + [[NSOperationQueue mainQueue] addOperationWithBlock:^{ + [User throwUnableToLogin:self sourceView:self.passwordField error:[NSError errorWithDomain:JuickErrorDomain code:403 userInfo:@{}] path:@"/login" params:nil]; + }]; } }]; } diff --git a/Juick/ViewControllers/MessagesViewController.m b/Juick/ViewControllers/MessagesViewController.m index cd4d740..0fb04ac 100644 --- a/Juick/ViewControllers/MessagesViewController.m +++ b/Juick/ViewControllers/MessagesViewController.m @@ -49,7 +49,7 @@ NSString* const messageCellIdentifier = @"messageCell"; } self.dataLoading = NO; if (err) { - [User throwUnableToLogin:self error:err path:self.path params:self.params]; + [User throwUnableToLogin:self sourceView:self.navigationItem.titleView error:err path:self.path params:self.params]; return; } NSArray *newMsgs = next; diff --git a/Juick/ViewControllers/NewPostViewController.m b/Juick/ViewControllers/NewPostViewController.m index 1b655f6..760dd84 100644 --- a/Juick/ViewControllers/NewPostViewController.m +++ b/Juick/ViewControllers/NewPostViewController.m @@ -57,7 +57,7 @@ NSString * const ReplyPostedNotificationName = @"ReplyPosted"; }]; } else { self.navigationController.navigationItem.rightBarButtonItem.enabled = YES; - [User throwUnableToLogin:self error:err path:@"postMessage" params:nil]; + [User throwUnableToLogin:self sourceView:self.navigationItem.rightBarButtonItem error:err path:@"postMessage" params:nil]; } }]; } else { @@ -69,7 +69,7 @@ NSString * const ReplyPostedNotificationName = @"ReplyPosted"; }]; } else { self.navigationItem.rightBarButtonItem.enabled = YES; - [User throwUnableToLogin:self error:err path:@"postreply" params:nil]; + [User throwUnableToLogin:self sourceView:self.navigationItem.rightBarButtonItem error:err path:@"postreply" params:nil]; } }]; } -- cgit v1.2.3