mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-08-02 00:17:02 +00:00
302 lines
11 KiB
Objective-C
302 lines
11 KiB
Objective-C
//
|
|
// BITAuthenticationViewController.m
|
|
// HockeySDK
|
|
//
|
|
// Created by Stephan Diederich on 08.08.13.
|
|
//
|
|
//
|
|
|
|
#import "BITAuthenticationViewController.h"
|
|
#import "BITAuthenticator_Private.h"
|
|
#import "HockeySDKPrivate.h"
|
|
#import "HockeySDK.h"
|
|
#import "BITHockeyAppClient.h"
|
|
|
|
@interface BITAuthenticationViewController ()<UITextFieldDelegate> {
|
|
UIStatusBarStyle _statusBarStyle;
|
|
}
|
|
|
|
@property (nonatomic, copy) NSString *email;
|
|
@property (nonatomic, copy) NSString *password;
|
|
|
|
@end
|
|
|
|
@implementation BITAuthenticationViewController
|
|
|
|
- (instancetype) initWithDelegate:(id<BITAuthenticationViewControllerDelegate>)delegate {
|
|
self = [super initWithStyle:UITableViewStyleGrouped];
|
|
if (self) {
|
|
self.title = BITHockeyLocalizedString(@"HockeyAuthenticatorViewControllerTitle");
|
|
_delegate = delegate;
|
|
_showsSkipButton = YES;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - view lifecycle
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
|
|
[self.tableView setScrollEnabled:NO];
|
|
|
|
[self updateWebLoginButton];
|
|
}
|
|
|
|
- (void)viewWillAppear:(BOOL)animated {
|
|
[super viewWillAppear:animated];
|
|
|
|
_statusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
|
|
[[UIApplication sharedApplication] setStatusBarStyle:(self.navigationController.navigationBar.barStyle == UIBarStyleDefault) ? UIStatusBarStyleDefault : UIStatusBarStyleBlackOpaque];
|
|
|
|
[self updateSkipButton];
|
|
|
|
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
|
|
target:self
|
|
action:@selector(saveAction:)];
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = [self allRequiredFieldsEntered];
|
|
}
|
|
|
|
- (void)viewWillDisappear:(BOOL)animated {
|
|
[super viewWillDisappear:animated];
|
|
|
|
[[UIApplication sharedApplication] setStatusBarStyle:_statusBarStyle];
|
|
}
|
|
|
|
#pragma mark - Property overrides
|
|
- (void)setShowsSkipButton:(BOOL)showsSkipButton {
|
|
if(_showsSkipButton != showsSkipButton) {
|
|
_showsSkipButton = showsSkipButton;
|
|
[self updateSkipButton];
|
|
}
|
|
}
|
|
|
|
- (void) updateSkipButton {
|
|
if(self.showsSkipButton) {
|
|
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:BITHockeyLocalizedString(@"Skip")
|
|
style:UIBarButtonItemStyleBordered
|
|
target:self
|
|
action:@selector(dismissAction:)];
|
|
} else {
|
|
self.navigationItem.leftBarButtonItem = nil;
|
|
}
|
|
}
|
|
|
|
- (void)setShowsLoginViaWebButton:(BOOL)showsLoginViaWebButton {
|
|
if(_showsLoginViaWebButton != showsLoginViaWebButton) {
|
|
_showsLoginViaWebButton = showsLoginViaWebButton;
|
|
if(self.isViewLoaded) {
|
|
[self.tableView reloadData];
|
|
[self updateWebLoginButton];
|
|
}
|
|
}
|
|
}
|
|
|
|
- (void) updateWebLoginButton {
|
|
if(self.showsLoginViaWebButton) {
|
|
static const CGFloat kFooterHeight = 60.f;
|
|
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,
|
|
CGRectGetWidth(self.tableView.bounds),
|
|
kFooterHeight)];
|
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
|
|
[button setTitle:BITHockeyLocalizedString(@"Show login page") forState:UIControlStateNormal];
|
|
CGSize buttonSize = [button sizeThatFits:CGSizeMake(CGRectGetWidth(self.tableView.bounds),
|
|
kFooterHeight)];
|
|
button.frame = CGRectMake(floorf((CGRectGetWidth(containerView.bounds) - buttonSize.width) / 2.f),
|
|
floorf((kFooterHeight - buttonSize.height) / 2.f),
|
|
buttonSize.width,
|
|
buttonSize.height);
|
|
button.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
|
|
[containerView addSubview:button];
|
|
[button addTarget:self
|
|
action:@selector(handleWebLoginButton:)
|
|
forControlEvents:UIControlEventTouchUpInside];
|
|
self.tableView.tableFooterView = containerView;
|
|
} else {
|
|
self.tableView.tableFooterView = nil;
|
|
}
|
|
}
|
|
|
|
- (IBAction) handleWebLoginButton:(id)sender {
|
|
[self.delegate authenticationViewControllerDidTapWebButton:self];
|
|
}
|
|
#pragma mark - UIViewController Rotation
|
|
|
|
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation {
|
|
return YES;
|
|
}
|
|
|
|
#pragma mark - Private methods
|
|
- (BOOL)allRequiredFieldsEntered {
|
|
if (self.requirePassword && [self.password length] == 0)
|
|
return NO;
|
|
|
|
if (![self.email length] || !BITValidateEmail(self.email))
|
|
return NO;
|
|
|
|
return YES;
|
|
}
|
|
|
|
#pragma mark - Table view data source
|
|
|
|
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
|
|
return 1;
|
|
}
|
|
|
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
|
|
if(self.showsLoginViaWebButton) {
|
|
return 0;
|
|
} else {
|
|
NSInteger rows = 1;
|
|
|
|
if ([self requirePassword]) rows ++;
|
|
|
|
return rows;
|
|
}
|
|
}
|
|
|
|
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
|
|
if (section == 0) {
|
|
if(self.showsLoginViaWebButton) {
|
|
return BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerWebLoginDescription");
|
|
} else if(self.requirePassword) {
|
|
return BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerDataEmailAndPasswordDescription");
|
|
} else {
|
|
return BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerDataEmailDescription");
|
|
}
|
|
}
|
|
|
|
return nil;
|
|
}
|
|
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
|
|
static NSString *CellIdentifier = @"InputCell";
|
|
|
|
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
|
|
if (cell == nil) {
|
|
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
|
|
|
|
cell.accessoryType = UITableViewCellAccessoryNone;
|
|
cell.selectionStyle = UITableViewCellSelectionStyleNone;
|
|
cell.backgroundColor = [UIColor whiteColor];
|
|
|
|
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, self.view.frame.size.width - 110 - 35, 30)];
|
|
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
|
|
textField.autoresizingMask = UIViewAutoresizingFlexibleWidth;
|
|
}
|
|
textField.adjustsFontSizeToFitWidth = YES;
|
|
textField.textColor = [UIColor blackColor];
|
|
textField.backgroundColor = [UIColor lightGrayColor];
|
|
|
|
if (0 == [indexPath row]) {
|
|
textField.placeholder = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerEmailPlaceholder");
|
|
textField.text = self.email;
|
|
|
|
textField.keyboardType = UIKeyboardTypeEmailAddress;
|
|
if ([self requirePassword])
|
|
textField.returnKeyType = UIReturnKeyNext;
|
|
else
|
|
textField.returnKeyType = UIReturnKeyDone;
|
|
|
|
[textField addTarget:self action:@selector(userEmailEntered:) forControlEvents:UIControlEventEditingChanged];
|
|
[textField becomeFirstResponder];
|
|
} else {
|
|
textField.placeholder = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerPasswordPlaceholder");
|
|
textField.text = self.password;
|
|
|
|
textField.keyboardType = UIKeyboardTypeDefault;
|
|
textField.returnKeyType = UIReturnKeyDone;
|
|
textField.secureTextEntry = YES;
|
|
[textField addTarget:self action:@selector(userPasswordEntered:) forControlEvents:UIControlEventEditingChanged];
|
|
}
|
|
|
|
textField.backgroundColor = [UIColor whiteColor];
|
|
textField.autocorrectionType = UITextAutocorrectionTypeNo;
|
|
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
|
|
textField.textAlignment = kBITTextLabelAlignmentLeft;
|
|
textField.delegate = self;
|
|
textField.tag = indexPath.row;
|
|
|
|
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
|
|
[textField setEnabled: YES];
|
|
|
|
[cell addSubview:textField];
|
|
}
|
|
|
|
if (0 == [indexPath row]) {
|
|
cell.textLabel.text = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerEmailDescription");
|
|
} else {
|
|
cell.textLabel.text = BITHockeyLocalizedString(@"HockeyAuthenticationViewControllerPasswordDescription");
|
|
}
|
|
|
|
return cell;
|
|
}
|
|
|
|
|
|
- (void)userEmailEntered:(id)sender {
|
|
self.email = [(UITextField *)sender text];
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = [self allRequiredFieldsEntered];
|
|
}
|
|
|
|
- (void)userPasswordEntered:(id)sender {
|
|
self.password = [(UITextField *)sender text];
|
|
|
|
self.navigationItem.rightBarButtonItem.enabled = [self allRequiredFieldsEntered];
|
|
}
|
|
|
|
#pragma mark - UITextFieldDelegate
|
|
|
|
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
|
|
NSInteger nextTag = textField.tag + 1;
|
|
|
|
UIResponder* nextResponder = [self.view viewWithTag:nextTag];
|
|
if (nextResponder) {
|
|
[nextResponder becomeFirstResponder];
|
|
} else {
|
|
if ([self allRequiredFieldsEntered]) {
|
|
if ([textField isFirstResponder])
|
|
[textField resignFirstResponder];
|
|
|
|
[self saveAction:nil];
|
|
}
|
|
}
|
|
return NO;
|
|
}
|
|
|
|
#pragma mark - Actions
|
|
- (void)dismissAction:(id)sender {
|
|
[self.delegate authenticationViewControllerDidSkip:self];
|
|
}
|
|
|
|
- (void)saveAction:(id)sender {
|
|
[self setLoginUIEnabled:NO];
|
|
|
|
__weak typeof(self) weakSelf = self;
|
|
[self.delegate authenticationViewController:self
|
|
handleAuthenticationWithEmail:self.email
|
|
password:self.password
|
|
completion:^(BOOL succeeded, NSError *error) {
|
|
if(succeeded) {
|
|
//controller should dismiss us shortly..
|
|
} else {
|
|
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil
|
|
message:error.localizedDescription
|
|
delegate:nil
|
|
cancelButtonTitle:BITHockeyLocalizedString(@"OK")
|
|
otherButtonTitles:nil];
|
|
[alertView show];
|
|
typeof(self) strongSelf = weakSelf;
|
|
[strongSelf setLoginUIEnabled:YES];
|
|
}
|
|
}];
|
|
}
|
|
|
|
- (void) setLoginUIEnabled:(BOOL) enabled {
|
|
self.navigationItem.rightBarButtonItem.enabled = enabled;
|
|
self.tableView.userInteractionEnabled = enabled;
|
|
}
|
|
|
|
@end
|