Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  SignUpViewController.m
  3. //  House and Land
  4. //
  5. //  Created by Sam Davies on 5/3/18.
  6. //  Copyright © 2018 Digital Noir. All rights reserved.
  7. //
  8.  
  9. #import "SignUpViewController.h"
  10. #import "DatabaseHelper.h"
  11. #import "User.h"
  12. #import "CustomAlertView.h"
  13. #import "ColorHelper.h"
  14. #import "StateSelectionView.h"
  15. #import "CustomLoadingView.h"
  16.  
  17. @interface SignUpViewController ()
  18.  
  19. @end
  20.  
  21. @implementation SignUpViewController
  22.  
  23. #pragma mark ViewController lifecycle methods
  24.  
  25. - (void)viewDidLoad {
  26.     [super viewDidLoad];
  27.    
  28.     self.nameTextField.delegate = self;
  29.     self.emailAddressTextField.delegate = self;
  30.     self.passwordTextField.delegate = self;
  31.     self.confirmPasswordTextField.delegate = self;
  32.    
  33.     [self.userDetailsView.layer setCornerRadius:5];
  34.     [self.userDetailsView.layer setBorderWidth:1];
  35.     [self.userDetailsView.layer setBorderColor:[[UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0] CGColor]];
  36.    
  37.     [self.stateView.layer setCornerRadius:5];
  38.     [self.stateView.layer setBorderWidth:1];
  39.     [self.stateView.layer setBorderColor:[[UIColor colorWithRed:241.0/255.0 green:241.0/255.0 blue:241.0/255.0 alpha:1.0] CGColor]];
  40.    
  41.     [self.signUpButton.layer setCornerRadius:5];
  42.     [self.loginButton.layer setCornerRadius:5];
  43. }
  44.  
  45. #pragma mark IBActions
  46.  
  47. - (IBAction)backButtonPressed:(id)sender {
  48.     [self.navigationController popViewControllerAnimated:YES];
  49. }
  50.  
  51. - (IBAction)loginButtonPressed:(id)sender {
  52.     [self performSegueWithIdentifier:@"loginSegueIdentifier" sender:self];
  53. }
  54.  
  55. - (IBAction)stateButtonPressed:(id)sender {
  56.    
  57.     StateSelectionView *stateSelectionView = [[StateSelectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  58.     [stateSelectionView setupStateSelectionViewInParentView:self SelectedValue:self.stateLabel.text];
  59.     [self.view addSubview:stateSelectionView];
  60. }
  61.  
  62. - (IBAction)signUpButtonPressed:(id)sender {
  63.    
  64.     [[CustomLoadingView new] setupLoadingViewWithTitle:@""];
  65.    
  66.     [self.view endEditing:YES];
  67.    
  68.     if ([self validateData]) {
  69.         [[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) {
  70.            
  71.             dispatch_async(dispatch_get_main_queue(), ^{
  72.                
  73.                 BOOL success = [[returnData allKeys] containsObject:@"success"];
  74.                 if (success) {
  75.                    
  76.                     NSString *token = [[returnData objectForKey:@"success"] objectForKey:@"token"];
  77.                    
  78.                     User *currentUser = [[DatabaseHelper shareInstanceHelper] getCurrentUser];
  79.                     [currentUser setSessionKey:token];
  80.                     [currentUser setUserEmail:self.emailAddressTextField.text];
  81.                     [currentUser setUserName:self.nameTextField.text];
  82.                     [currentUser setUserState:self.stateLabel.text];
  83.                    
  84.                     [[DatabaseHelper shareInstanceHelper] saveCurrentUserLocally:currentUser];
  85.                 }
  86.                
  87.                 CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height)];
  88.                 [alertView setupWithAlertDescription:success ? @"You registration has been successful and we've logged you in." : @"Error" CloseButton:NO Button1Text:@"THANKS" Button1CustomAction:YES];
  89.                 [alertView.firstButton addTarget:self action:@selector(signUpSuccessful) forControlEvents:UIControlEventTouchUpInside];
  90.                 [self.view addSubview:alertView];
  91.             });
  92.         }];
  93.     }
  94. }
  95.  
  96. #pragma mark private methods
  97.  
  98. - (BOOL)validateData {
  99.    
  100.     if (![self validateName] || ![self validateEmail] || ![self validatePassword] || ![self validateConfirmPassword] || ![self validateState]) {
  101.         return NO;
  102.     }
  103.    
  104.     [self enableErrorValidationForTextField:nil];
  105.    
  106.     return YES;
  107. }
  108.  
  109. - (void)showAlertWithDescription:(NSString *)description {
  110.    
  111.     CustomAlertView *alertView = [[CustomAlertView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
  112.     [alertView setupWithAlertDescription:description CloseButton:NO Button1Text:@"THANKS" Button1CustomAction:NO];
  113.     [self.view addSubview:alertView];
  114. }
  115.  
  116. - (BOOL)validateName {
  117.     NSString *charactersSetString = @"0123456789!@#$%^&*()-_=+";
  118.     NSCharacterSet *onlyLetters = [NSCharacterSet characterSetWithCharactersInString:charactersSetString];
  119.    
  120.     BOOL nameIsValid = ![onlyLetters isSupersetOfSet:[NSCharacterSet characterSetWithCharactersInString:self.nameTextField.text]];
  121.    
  122.     if ([[self.nameTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
  123.        
  124.         [self showAlertWithDescription:@"You must enter a name before you can register."];
  125.         [self enableErrorValidationForTextField:self.nameTextField];
  126.        
  127.         return NO;
  128.     } else if (!nameIsValid) {
  129.        
  130.         [self showAlertWithDescription:@"Invalid name."];
  131.         [self enableErrorValidationForTextField:self.nameTextField];
  132.        
  133.         return NO;
  134.     }
  135.    
  136.     return YES;
  137. }
  138.  
  139. - (BOOL)validateEmail {
  140.    
  141.     NSString *regExPattern = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  142.    
  143.     NSRegularExpression *regEx = [[NSRegularExpression alloc] initWithPattern:regExPattern options:NSRegularExpressionCaseInsensitive error:nil];
  144.     NSUInteger regExMatches = [regEx numberOfMatchesInString:self.emailAddressTextField.text options:0 range:NSMakeRange(0, [self.emailAddressTextField.text length])];
  145.    
  146.    
  147.     if ([[self.emailAddressTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
  148.        
  149.         [self showAlertWithDescription:@"You must enter an email address before you can register."];
  150.         [self enableErrorValidationForTextField:self.emailAddressTextField];
  151.        
  152.         return NO;
  153.     } else if (regExMatches == 0) {
  154.        
  155.         [self showAlertWithDescription:@"Invalid email."];
  156.         [self enableErrorValidationForTextField:self.emailAddressTextField];
  157.        
  158.         return NO;
  159.     }
  160.    
  161.     return YES;
  162. }
  163.  
  164. - (BOOL)validatePassword {
  165.    
  166.     if ([[self.passwordTextField.text stringByReplacingOccurrencesOfString:@" " withString:@""] isEqualToString:@""]) {
  167.      
  168.         [self showAlertWithDescription:@"You must enter a password before you can register."];
  169.         [self enableErrorValidationForTextField:self.passwordTextField];
  170.        
  171.         return NO;
  172.     } else if (self.passwordTextField.text.length < 6 || self.passwordTextField.text.length > 32) {
  173.        
  174.         [self showAlertWithDescription:@"Invalid password."];
  175.         [self enableErrorValidationForTextField:self.passwordTextField];
  176.        
  177.         return NO;
  178.     }
  179.    
  180.     return YES;
  181. }
  182.  
  183. - (BOOL)validateConfirmPassword {
  184.    
  185.     if (![self.confirmPasswordTextField.text isEqualToString:self.passwordTextField.text]) {
  186.        
  187.         [self showAlertWithDescription:@"The passwords that you enter must match."];
  188.         [self enableErrorValidationForTextField:self.confirmPasswordTextField];
  189.        
  190.         return NO;
  191.     }
  192.    
  193.     return YES;
  194. }
  195.  
  196. - (BOOL)validateState {
  197.    
  198.     if ([self.stateLabel.text isEqualToString:@"State"]) {
  199.        
  200.         [self showAlertWithDescription:@"You must choose a state before you can register."];
  201.         [self enableErrorValidationForTextField:self.stateLabel];
  202.        
  203.         return NO;
  204.     }
  205.    
  206.     return YES;
  207. }
  208.  
  209. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  210.     [textField resignFirstResponder];
  211.     return YES;
  212. }
  213.  
  214. - (void)enableErrorValidationForTextField:(UITextField*)textField {
  215.    
  216.     for (UIView *tempView in self.currentErrorValidationTextField.subviews) {
  217.         if ([tempView isKindOfClass:[UIImageView class]]) {
  218.             [tempView removeFromSuperview];
  219.         }
  220.     }
  221.    
  222.     [self.currentErrorValidationTextField setTextColor:[UIColor blackColor]];
  223.     [self.currentErrorValidationTextField setTintColor:[UIColor blackColor]];
  224.     self.currentErrorValidationTextField = textField;
  225.     [self.currentErrorValidationTextField setTextColor:CUSTOM_COLOR_RED(1)];
  226.     [self.currentErrorValidationTextField setTintColor:CUSTOM_COLOR_RED(1)];
  227.    
  228.     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)];
  229.     [errorValidationImageView setImage:[UIImage imageNamed:@"validationErrorIcon"]];
  230.     [errorValidationImageView setContentMode:UIViewContentModeScaleAspectFit];
  231.    
  232.     [self.currentErrorValidationTextField addSubview:errorValidationImageView];
  233. }
  234.  
  235. - (void)signUpSuccessful {
  236.     [self.navigationController popViewControllerAnimated:YES];
  237. }
  238.  
  239. #pragma mark public methods
  240.  
  241. - (void)userSelectedState:(NSString*)state {
  242.    
  243.     [self.stateLabel setTextColor:CUSTOM_COLOR_BLUE(1)];
  244.     [self.stateLabel setText:state];
  245. }
  246.  
  247.  
  248. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement