[ios] Added osm authorization stub.

This commit is contained in:
Илья Гречухин 2015-12-28 17:36:51 +03:00 committed by Sergey Yershov
parent 3cf09b8d06
commit 5f3b8fb35e
11 changed files with 535 additions and 1 deletions

View file

@ -0,0 +1,13 @@
static NSString * const kOSMUsernameKey = @"OSMUsernameKey";
static NSString * const kOSMPasswordKey = @"OSMPasswordKey";
typedef NS_OPTIONS(NSUInteger, MWMAuthorizationButtonType)
{
MWMAuthorizationButtonTypeGoogle,
MWMAuthorizationButtonTypeFacebook,
MWMAuthorizationButtonTypeOSM
};
UIColor * MWMAuthorizationButtonBackgroundColor(MWMAuthorizationButtonType type);
void MWMAuthorizationConfigButton(UIButton * btn, MWMAuthorizationButtonType type);

View file

@ -0,0 +1,43 @@
#import "MWMAuthorizationCommon.h"
#import "UIButton+RuntimeAttributes.h"
UIColor * MWMAuthorizationButtonTextColor(MWMAuthorizationButtonType type)
{
switch (type)
{
case MWMAuthorizationButtonTypeGoogle:
return [UIColor blackColor];
case MWMAuthorizationButtonTypeFacebook:
case MWMAuthorizationButtonTypeOSM:
return [UIColor whiteColor];
}
return [UIColor clearColor];
}
UIColor * MWMAuthorizationButtonBackgroundColor(MWMAuthorizationButtonType type)
{
switch (type)
{
case MWMAuthorizationButtonTypeGoogle:
return [UIColor whiteColor];
case MWMAuthorizationButtonTypeFacebook:
return [UIColor colorWithRed:72. / 255. green:97. / 255. blue:163. / 255. alpha:1.];
case MWMAuthorizationButtonTypeOSM:
return [UIColor colorWithRed:3. / 255. green:122. / 255. blue:1. alpha:1.];
}
return [UIColor clearColor];
}
void MWMAuthorizationConfigButton(UIButton * btn, MWMAuthorizationButtonType type)
{
UIColor * txtCol = MWMAuthorizationButtonTextColor(type);
UIColor * bgCol = MWMAuthorizationButtonBackgroundColor(type);
[btn setTitleColor:txtCol forState:UIControlStateNormal];
[btn setTitleColor:bgCol forState:UIControlStateHighlighted];
[btn setBackgroundColor:bgCol forState:UIControlStateNormal];
[btn setBackgroundColor:[UIColor clearColor] forState:UIControlStateHighlighted];
btn.layer.borderColor = bgCol.CGColor;
}

View file

@ -0,0 +1,5 @@
#import "ViewController.h"
@interface MWMAuthorizationForgottenPasswordViewController : ViewController
@end

View file

@ -0,0 +1,72 @@
#import "MWMAuthorizationForgottenPasswordViewController.h"
#import "UIColor+MapsMeColor.h"
#import "UITextField+RuntimeAttributes.h"
@interface MWMAuthorizationForgottenPasswordViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField * emailTextField;
@property (weak, nonatomic) IBOutlet UIButton * resetPasswordButton;
@property (nonatomic) BOOL isCorrect;
@end
@implementation MWMAuthorizationForgottenPasswordViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = L(@"forgotten_password");
self.isCorrect = NO;
}
- (BOOL)shouldAutorotate
{
return NO;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSString * newString =
[textField.text stringByReplacingCharactersInRange:range withString:string];
self.isCorrect = [textField.validator validateString:newString];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self resetPassword];
return YES;
}
#pragma mark - Actions
- (IBAction)resetPassword
{
if (!self.resetPasswordButton.enabled)
return;
// TODO: Add password recovery
[self cancel];
}
- (IBAction)cancel
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Properties
- (void)setIsCorrect:(BOOL)isCorrect
{
_isCorrect = isCorrect;
self.resetPasswordButton.enabled = isCorrect;
CALayer * layer = self.resetPasswordButton.layer;
layer.borderColor = (isCorrect ? [UIColor buttonEnabledBlueText] : [UIColor clearColor]).CGColor;
}
@end

View file

@ -0,0 +1,5 @@
#import "ViewController.h"
@interface MWMAuthorizationLoginViewController : ViewController
@end

View file

@ -0,0 +1,79 @@
#import "Common.h"
#import "MapsAppDelegate.h"
#import "MWMAuthorizationCommon.h"
#import "MWMAuthorizationLoginViewController.h"
#import "UIColor+MapsMeColor.h"
@interface MWMAuthorizationLoginViewController ()
@property (weak, nonatomic) IBOutlet UIImageView * backgroundImage;
@property (weak, nonatomic) IBOutlet UIButton * loginGoogleButton;
@property (weak, nonatomic) IBOutlet UIButton * loginFacebookButton;
@property (weak, nonatomic) IBOutlet UIButton * loginOSMButton;
@property (weak, nonatomic) IBOutlet UIButton * signupButton;
@end
@implementation MWMAuthorizationLoginViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.backgroundImage.image = [UIImage imageWithColor:[UIColor primary]];
MWMAuthorizationConfigButton(self.loginGoogleButton, MWMAuthorizationButtonTypeGoogle);
MWMAuthorizationConfigButton(self.loginFacebookButton, MWMAuthorizationButtonTypeFacebook);
MWMAuthorizationConfigButton(self.loginOSMButton, MWMAuthorizationButtonTypeOSM);
[self checkConnection];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.title = L(@"login");
UINavigationBar * navBar = self.navigationController.navigationBar;
navBar.tintColor = [UIColor clearColor];
navBar.barTintColor = [UIColor clearColor];
navBar.shadowImage = [UIImage imageWithColor:[UIColor clearColor]];
navBar.shadowImage = [[UIImage alloc] init];
[navBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
navBar.translucent = YES;
}
- (void)viewWillDisappear:(BOOL)animated
{
UINavigationBar * navBar = self.navigationController.navigationBar;
[MapsAppDelegate.theApp customizeAppearanceForNavigationBar:navBar];
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (void)checkConnection
{
BOOL const isConnected = Platform::IsConnected();
self.loginGoogleButton.enabled = isConnected;
self.loginFacebookButton.enabled = isConnected;
self.signupButton.enabled = isConnected;
}
#pragma mark - Actions
- (IBAction)loginGoogle
{
// TODO: Add login
}
- (IBAction)loginFacebook
{
// TODO: Add login
}
- (IBAction)cancel
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end

View file

@ -0,0 +1,5 @@
#import "ViewController.h"
@interface MWMAuthorizationOSMLoginViewController : ViewController
@end

View file

@ -0,0 +1,149 @@
#import "MWMAuthorizationCommon.h"
#import "MWMAuthorizationOSMLoginViewController.h"
#import "UIColor+MapsMeColor.h"
#import "UITextField+RuntimeAttributes.h"
#include "editor/server_api.hpp"
#include "platform/platform.hpp"
typedef NS_OPTIONS(NSUInteger, MWMFieldCorrect)
{
MWMFieldCorrectNO = 0,
MWMFieldCorrectLogin = 1 << 0,
MWMFieldCorrectPassword = 1 << 1,
MWMFieldCorrectAll = MWMFieldCorrectLogin | MWMFieldCorrectPassword
};
@interface MWMAuthorizationOSMLoginViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField * loginTextField;
@property (weak, nonatomic) IBOutlet UITextField * passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton * loginButton;
@property (weak, nonatomic) IBOutlet UIButton * forgotButton;
@property (nonatomic) MWMFieldCorrect isCorrect;
@end
@implementation MWMAuthorizationOSMLoginViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = L(@"osm_login");
self.isCorrect = MWMFieldCorrectNO;
[self checkConnection];
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (void)checkConnection
{
self.forgotButton.enabled = Platform::IsConnected();
}
- (void)setLoginButtonEnabled:(BOOL)enabled
{
self.loginButton.enabled = enabled;
CALayer * layer = self.loginButton.layer;
layer.borderColor = (enabled ? [UIColor buttonEnabledBlueText] : [UIColor clearColor]).CGColor;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * newString =
[textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL const isValid = [textField.validator validateString:newString];
if ([textField isEqual:self.loginTextField])
{
if (isValid)
self.isCorrect |= MWMFieldCorrectLogin;
else
self.isCorrect &= ~MWMFieldCorrectLogin;
}
else if ([textField isEqual:self.passwordTextField])
{
if (isValid)
self.isCorrect |= MWMFieldCorrectPassword;
else
self.isCorrect &= ~MWMFieldCorrectPassword;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:self.loginTextField])
{
[self.passwordTextField becomeFirstResponder];
}
else if ([textField isEqual:self.passwordTextField])
{
[textField resignFirstResponder];
[self login];
}
return YES;
}
- (void)storeCredentials
{
NSUserDefaults * ud = [NSUserDefaults standardUserDefaults];
[ud setObject:self.loginTextField.text forKey:kOSMUsernameKey];
[ud setObject:self.passwordTextField.text forKey:kOSMPasswordKey];
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Actions
- (IBAction)login
{
if (!self.loginButton.enabled)
return;
if (Platform::IsConnected())
{
// TODO: Add async loader spinner
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
{
string username = self.loginTextField.text.UTF8String;
string password = self.passwordTextField.text.UTF8String;
BOOL const credentialsOK = osm::ServerApi06(username, password).CheckUserAndPassword();
dispatch_async(dispatch_get_main_queue(), ^
{
if (credentialsOK)
{
[self storeCredentials];
}
else
{
// TODO: Add alert "wrong username or password"
}
});
});
}
else
{
[self storeCredentials];
}
}
- (IBAction)cancel
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Properties
- (void)setIsCorrect:(MWMFieldCorrect)isCorrect
{
_isCorrect = isCorrect;
[self setLoginButtonEnabled:isCorrect == MWMFieldCorrectAll];
}
@end

View file

@ -0,0 +1,5 @@
#import "ViewController.h"
@interface MWMAuthorizationSignupViewController : ViewController
@end

View file

@ -0,0 +1,146 @@
#import "MWMAuthorizationCommon.h"
#import "MWMAuthorizationSignupViewController.h"
#import "UITextField+RuntimeAttributes.h"
typedef NS_OPTIONS(NSUInteger, MWMFieldCorrect)
{
MWMFieldCorrectNO = 0,
MWMFieldCorrectEmail = 1 << 0,
MWMFieldCorrectLogin = 1 << 1,
MWMFieldCorrectPassword = 1 << 2,
MWMFieldCorrectAll = MWMFieldCorrectEmail | MWMFieldCorrectLogin | MWMFieldCorrectPassword
};
@interface MWMAuthorizationSignupViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton * signupGoogleButton;
@property (weak, nonatomic) IBOutlet UIButton * signupFacebookButton;
@property (weak, nonatomic) IBOutlet UIButton * signupOSMButton;
@property (weak, nonatomic) IBOutlet UIButton * signupOSMSiteButton;
@property (weak, nonatomic) IBOutlet UITextField * emailTextField;
@property (weak, nonatomic) IBOutlet UITextField * loginTextField;
@property (weak, nonatomic) IBOutlet UITextField * passwordTextField;
@property (nonatomic) MWMFieldCorrect isCorrect;
@end
@implementation MWMAuthorizationSignupViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = L(@"sign_up");
MWMAuthorizationConfigButton(self.signupGoogleButton, MWMAuthorizationButtonTypeGoogle);
MWMAuthorizationConfigButton(self.signupFacebookButton, MWMAuthorizationButtonTypeFacebook);
MWMAuthorizationConfigButton(self.signupOSMButton, MWMAuthorizationButtonTypeOSM);
self.isCorrect = MWMFieldCorrectNO;
}
- (BOOL)shouldAutorotate
{
return NO;
}
- (void)setSignupOSMButtonEnabled:(BOOL)enabled
{
self.signupOSMButton.enabled = enabled;
CALayer * layer = self.signupOSMButton.layer;
layer.borderColor =
(enabled ? MWMAuthorizationButtonBackgroundColor(MWMAuthorizationButtonTypeOSM)
: [UIColor clearColor])
.CGColor;
}
#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * newString =
[textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL const isValid = [textField.validator validateString:newString];
if ([textField isEqual:self.emailTextField])
{
if (isValid)
self.isCorrect |= MWMFieldCorrectEmail;
else
self.isCorrect &= ~MWMFieldCorrectEmail;
}
if ([textField isEqual:self.loginTextField])
{
if (isValid)
self.isCorrect |= MWMFieldCorrectLogin;
else
self.isCorrect &= ~MWMFieldCorrectLogin;
}
else if ([textField isEqual:self.passwordTextField])
{
if (isValid)
self.isCorrect |= MWMFieldCorrectPassword;
else
self.isCorrect &= ~MWMFieldCorrectPassword;
}
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if ([textField isEqual:self.emailTextField])
{
[self.loginTextField becomeFirstResponder];
}
else if ([textField isEqual:self.loginTextField])
{
[self.passwordTextField becomeFirstResponder];
}
else if ([textField isEqual:self.passwordTextField])
{
[textField resignFirstResponder];
[self signupOSM];
}
return YES;
}
#pragma mark - Actions
- (IBAction)signupGoogle
{
// TODO: Add signup
}
- (IBAction)signupFacebook
{
// TODO: Add signup
}
- (IBAction)signupOSM
{
if (!self.signupOSMButton.enabled)
return;
// TODO: Add signup
}
- (IBAction)signupOSMSite
{
// TODO: Add signup
}
- (IBAction)cancel
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - Properties
- (void)setIsCorrect:(MWMFieldCorrect)isCorrect
{
_isCorrect = isCorrect;
[self setSignupOSMButtonEnabled:isCorrect == MWMFieldCorrectAll];
}
@end

View file

@ -77,7 +77,8 @@ extern NSDictionary * const deviceNames = @{@"x86_64" : @"Simulator",
@{@"Id" : @"Help", @"Title" : L(@"help"), @"Icon" : @"ic_settings_help"},
@{@"Id" : @"ReportBug", @"Title" : L(@"report_a_bug"), @"Icon" : @"ic_settings_feedback"}]},
@{@"Title" : @"",
@"Items" : @[@{@"Id" : @"Community", @"Title" : L(@"maps_me_community"), @"Icon" : @"ic_settings_community"},
@"Items" : @[@{@"Id" : @"Authorization", @"Title" : L(@"authorization"), @"Icon" : @"ic_settings_login"},
@{@"Id" : @"Community", @"Title" : L(@"maps_me_community"), @"Icon" : @"ic_settings_community"},
@{@"Id" : @"RateApp", @"Title" : L(@"rate_the_app"), @"Icon" : @"ic_settings_rate"}]},
@{@"Title" : @"",
@"Items" : @[@{@"Id" : @"About", @"Title" : L(@"about_menu_title"), @"Icon" : @"IconAbout"},
@ -135,6 +136,8 @@ extern NSDictionary * const deviceNames = @{@"x86_64" : @"Simulator",
NSString * itemId = self.items[indexPath.section][@"Items"][indexPath.row][@"Id"];
if ([itemId isEqualToString:@"About"])
[self about];
else if ([itemId isEqualToString:@"Authorization"])
[self authorization];
else if ([itemId isEqualToString:@"Community"])
[self community];
else if ([itemId isEqualToString:@"RateApp"])
@ -157,6 +160,15 @@ extern NSDictionary * const deviceNames = @{@"x86_64" : @"Simulator",
[self.navigationController pushViewController:vc animated:YES];
}
- (void)authorization
{
[[Statistics instance] logEvent:kStatSettingsOpenSection withParameters:@{kStatName : kStatAuthorization}];
UINavigationController * vc = [self.mainStoryboard instantiateViewControllerWithIdentifier:@"LoginNavigationController"];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.preferredContentSize = {520, 600};
[self.navigationController presentViewController:vc animated:YES completion:nil];
}
- (void)community
{
[[Statistics instance] logEvent:kStatSettingsOpenSection withParameters:@{kStatName : kStatSocial}];