106 lines
3.1 KiB
Objective-C
106 lines
3.1 KiB
Objective-C
//
|
|
// AuthorizationViewController.m
|
|
// HotPocket (iOS)
|
|
//
|
|
// Created by Tomek Wójcik on 25/09/2025.
|
|
//
|
|
|
|
#import "AuthorizationViewController.h"
|
|
|
|
#import "AppDelegate.h"
|
|
#import "AuthorizationProgressViewController.h"
|
|
#import "HPAuthFlow.h"
|
|
#import "HPCredentialsHelper.h"
|
|
#import "MainViewController.h"
|
|
#import "NSURL+HotPocketExtensions.h"
|
|
|
|
@interface AuthorizationViewController (AuthorizationViewControllerPrivate)
|
|
|
|
#pragma mark - Private interface
|
|
|
|
@end
|
|
|
|
@implementation AuthorizationViewController
|
|
|
|
#pragma mark - View lifecycle
|
|
|
|
-(void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
|
|
self.invalidURLWarningView = [[UIImageView alloc] initWithImage:[UIImage systemImageNamed:@"exclamationmark.circle.fill"]];
|
|
self.invalidURLWarningView.contentMode = UIViewContentModeScaleAspectFit;
|
|
self.invalidURLWarningView.frame = CGRectMake(0, 0, 16, 16);
|
|
self.invalidURLWarningView.tintColor = [UIColor colorNamed:@"WarningColor"];
|
|
[self.view addSubview:self.invalidURLWarningView];
|
|
}
|
|
|
|
-(void)viewWillAppear:(BOOL)animated {
|
|
[super viewWillAppear:animated];
|
|
|
|
self.instanceURLField.rightView = self.invalidURLWarningView;
|
|
self.instanceURLField.rightViewMode = UITextFieldViewModeNever;
|
|
|
|
[self.instanceURLField addTarget:self action:@selector(onInstanceURLFieldChanged:) forControlEvents:UIControlEventEditingChanged];
|
|
}
|
|
|
|
#pragma mark - Actions
|
|
|
|
-(IBAction)doStartAuthorizationFlow:(id)sender {
|
|
#ifdef DEBUG
|
|
NSLog(@"-[AuthorizationViewController doStartAuthorizationFlow:] instanceURL=`%@`", self.instanceURLField.text);
|
|
#endif
|
|
if (!self.instanceURLField.text) {
|
|
// ???
|
|
return;
|
|
}
|
|
|
|
NSURL *instanceURL = [NSURL URLWithString:self.instanceURLField.text];
|
|
if (instanceURL.isUsableInHotPocket == NO) {
|
|
// ???
|
|
return;
|
|
}
|
|
|
|
UIApplication *application = [UIApplication sharedApplication];
|
|
AppDelegate *appDeleate = [application delegate];
|
|
appDeleate.authFlow.baseURL = instanceURL;
|
|
|
|
NSURL *authURL = [appDeleate.authFlow start];
|
|
if (authURL == nil) {
|
|
// ???
|
|
return;
|
|
}
|
|
|
|
if ([application canOpenURL:authURL] == YES) {
|
|
[application openURL:authURL options:@{} completionHandler:^(BOOL result) {
|
|
if (result == YES) {
|
|
AuthorizationProgressViewController *authorizationProgressViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"AuthorizationProgressViewController"];
|
|
[self.navigationController pushViewController:authorizationProgressViewController animated:YES];
|
|
}
|
|
}];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Event handlers
|
|
|
|
-(void)onInstanceURLFieldChanged:(UITextField *)sender {
|
|
sender.rightViewMode = UITextFieldViewModeNever;
|
|
|
|
if (!sender.text || [@"" isEqualToString:sender.text]) {
|
|
sender.rightViewMode = UITextFieldViewModeAlways;
|
|
return;
|
|
}
|
|
|
|
NSURL *url = [NSURL URLWithString:sender.text];
|
|
if (url == nil) {
|
|
sender.rightViewMode = UITextFieldViewModeAlways;
|
|
return;
|
|
}
|
|
|
|
if (url.isUsableInHotPocket == NO) {
|
|
sender.rightViewMode = UITextFieldViewModeAlways;
|
|
return;
|
|
}
|
|
}
|
|
|
|
@end
|