Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // SignUpViewController.m
- // House and Land
- //
- // Created by Sam Davies on 5/3/18.
- // Copyright © 2018 Digital Noir. All rights reserved.
- //
- #import "SignUpViewController.h"
- #import "DatabaseHelper.h"
- #import "User.h"
- #import "CustomAlertView.h"
- #import "ColorHelper.h"
- #import "StateSelectionView.h"
- #import "CustomLoadingView.h"
- @interface SignUpViewController ()
- @end
- @implementation SignUpViewController
- #pragma mark ViewController lifecycle methods
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.nameTextField.delegate = self;
- self.emailAddressTextField.delegate = self;
- self.passwordTextField.delegate = self;
- self.confirmPasswordTextField.delegate = self;
- [self.userDetailsView.layer setCornerRadius:5];
- [self.userDetailsView.layer setBorderWidth:1];
- [self.userDetailsView.layer setBorderColor:[[UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0] CGColor]];
- [self.stateView.layer setCornerRadius:5];
- [self.stateView.layer setBorderWidth:1];
- [self.stateView.layer setBorderColor:[[UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0] CGColor]];
- [self.signUpButton.layer setCornerRadius:5];
- [self.loginButton.layer setCornerRadius:5];
- }
- #pragma mark IBActions
- - (IBAction)backButtonPressed:(id)sender {
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (IBAction)loginButtonPressed:(id)sender {
- [self performSegueWithIdentifier:@"loginSegueIdentifier" sender:self];
- }
- - (IBAction)stateButtonPressed:(id)sender {
- StateSelectionView *stateSelectionView = [[StateSelectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
- [stateSelectionView setupStateSelectionViewInParentView:self SelectedValue:self.stateLabel.text];
- [self.view addSubview:stateSelectionView];
- }
- - (IBAction)signUpButtonPressed:(id)sender {
- [[CustomLoadingView new] setupLoadingViewWithTitle:@""];
- [self.view endEditing:YES];
- if ([self validateData]) {
- [[DatabaseHelper shareInstanceHelper] sendRegisterUserWithName:self.nameTextField.text Email:self.emailAddressTextField.text Password:self.passwordTextField.text ConfirmPassword:self.confirmPasswordTextField.text State:self.stateLabel.text NotificationStatus:@"1" CompletionHandler:^(NSDictionary *returnData) {
- dispatch_async(dispatch_get_main_queue(), ^{
- BOOL success = [[returnData allKeys] containsObject:@"success"];
- if (success) {
- NSString *token = [[returnData objectForKey:@"success"] objectForKey:@"token"];
- User *currentUser = [[DatabaseHelper shareInstanceHelper] getCurrentUser];
- [currentUser setSessionKey:token];
- [currentUser setUserEmail:self.emailAddressTextField.text];
- [currentUser setUserName:self.nameTextField.text];
- [currentUser setUserState:self.stateLabel.text];
- [[DatabaseHelper shareInstanceHelper] saveCurrentUserLocally:currentUser];
- }
- CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)];
- [alertView setupWithAlertDescription:success ? @"You registration has been successful and we've logged you in." : @"Error" CloseButton:NO Button1Text:@"THANKS" Button1CustomAction:YES];
- [alertView.firstButton addTarget:self action:@selector(signUpSuccessful) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:alertView];
- });
- }];
- }
- }
- #pragma mark private methods
- - (BOOL)validateData {
- if (![self validateName] || ![self validateEmail] || ![self validatePassword] || ![self validateConfirmPassword] || ![self validateState]) {
- return NO;
- }
- [self enableErrorValidationForTextField:nil];
- return YES;
- }
- - (void)showAlertWithDescription:(NSString *)description {
- CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
- [alertView setupWithAlertDescription:description CloseButton:NO Button1Text:@"THANKS" Button1CustomAction:NO];
- [self.view addSubview:alertView];
- }
- - (BOOL)validateName {
- NSString *charactersSetString = @"0123456789!@#$%^&*()-_=+";
- NSCharacterSet *onlyLetters = [NSCharacterSet characterSetWithCharactersInString:charactersSetString];
- BOOL nameIsValid = ![onlyLetters isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:self.nameTextField.text]];
- if ([[self.nameTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
- [self showAlertWithDescription:@"You must enter a name before you can register."];
- [self enableErrorValidationForTextField:self.nameTextField];
- return NO;
- } else if (!nameIsValid) {
- [self showAlertWithDescription:@"Invalid name."];
- [self enableErrorValidationForTextField:self.nameTextField];
- return NO;
- }
- return YES;
- }
- - (BOOL)validateEmail {
- NSString *regExPattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
- NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
- NSUInteger regExMatches = [regEx numberOfMatchesInString:self.emailAddressTextField.text options:0 range:NSMakeRange(0, [self.emailAddressTextField.text length])];
- if ([[self.emailAddressTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
- [self showAlertWithDescription:@"You must enter an email address before you can register."];
- [self enableErrorValidationForTextField:self.emailAddressTextField];
- return NO;
- } else if (regExMatches == 0) {
- [self showAlertWithDescription:@"Invalid email."];
- [self enableErrorValidationForTextField:self.emailAddressTextField];
- return NO;
- }
- return YES;
- }
- - (BOOL)validatePassword {
- if ([[self.passwordTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
- [self showAlertWithDescription:@"You must enter a password before you can register."];
- [self enableErrorValidationForTextField:self.passwordTextField];
- return NO;
- } else if (self.passwordTextField.text.length < 6 || self.passwordTextField.text.length > 32) {
- [self showAlertWithDescription:@"Invalid password."];
- [self enableErrorValidationForTextField:self.passwordTextField];
- return NO;
- }
- return YES;
- }
- - (BOOL)validateConfirmPassword {
- if (![self.confirmPasswordTextField.text isEqualToString:self.passwordTextField.text]) {
- [self showAlertWithDescription:@"The passwords that you enter must match."];
- [self enableErrorValidationForTextField:self.confirmPasswordTextField];
- return NO;
- }
- return YES;
- }
- - (BOOL)validateState {
- if ([self.stateLabel.text isEqualToString:@"State"]) {
- [self showAlertWithDescription:@"You must choose a state before you can register."];
- [self enableErrorValidationForTextField:self.stateLabel];
- return NO;
- }
- return YES;
- }
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- [textField resignFirstResponder];
- return YES;
- }
- - (void)enableErrorValidationForTextField:(UITextField*)textField {
- for (UIView *tempView in self.currentErrorValidationTextField.subviews) {
- if ([tempView isKindOfClass:[UIImageView class]]) {
- [tempView removeFromSuperview];
- }
- }
- [self.currentErrorValidationTextField setTextColor:[UIColor blackColor]];
- [self.currentErrorValidationTextField setTintColor:[UIColor blackColor]];
- self.currentErrorValidationTextField = textField;
- [self.currentErrorValidationTextField setTextColor:CUSTOM_COLOR_RED(1)];
- [self.currentErrorValidationTextField setTintColor:CUSTOM_COLOR_RED(1)];
- UIImageView *errorValidationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.currentErrorValidationTextField.frame.size.width-self.currentErrorValidationTextField.frame.size.height/2, self.currentErrorValidationTextField.frame.size.height/4, self.currentErrorValidationTextField.frame.size.height/2, self.currentErrorValidationTextField.frame.size.height/2)];
- [errorValidationImageView setImage:[UIImage imageNamed:@"validationErrorIcon"]];
- [errorValidationImageView setContentMode:UIViewContentModeScaleAspectFit];
- [self.currentErrorValidationTextField addSubview:errorValidationImageView];
- }
- - (void)signUpSuccessful {
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark public methods
- - (void)userSelectedState:(NSString*)state {
- [self.stateLabel setTextColor:CUSTOM_COLOR_BLUE(1)];
- [self.stateLabel setText:state];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement