Advertisement
Fonix

Untitled

Jan 6th, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  PMRegisterViewController.m
  3. //  Panacea
  4. //
  5. //  Created by Jacques Questiaux on 2013/11/13.
  6. //  Copyright (c) 2013 Cobiinteractive. All rights reserved.
  7. //
  8.  
  9. #import "PMRegisterViewController.h"
  10. #import "PMSettings.h"
  11. #import "PMCountrySelectViewController.h"
  12.  
  13. @interface PMRegisterViewController ()
  14.  
  15. @end
  16.  
  17. @implementation PMRegisterViewController
  18.  
  19. @synthesize phoneNumberTextField;
  20. @synthesize verificationCodeTextField;
  21. @synthesize registerButton;
  22. @synthesize countryButton;
  23. @synthesize countryCode;
  24.  
  25. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  26. {
  27.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  28.     if (self) {
  29.         // Custom initialization
  30.     }
  31.     return self;
  32. }
  33.  
  34. - (void)viewDidLoad
  35. {
  36.     [super viewDidLoad];
  37.     // Do any additional setup after loading the view.
  38.    
  39.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleRegisterResponse:) name:@"REGISTER" object:nil];
  40.    
  41.     [self.view endEditing:YES];
  42. }
  43.  
  44. - (void) viewWillAppear:(BOOL)animated {
  45.    
  46.     [self setCountryButton];
  47. }
  48.  
  49. - (void) setCountryButton {
  50.    
  51.     NSString *code = [NSString stringWithFormat:@"+%@", [PMSettings getCountryCode]];
  52.     countryCode.text = code;
  53.     [countryButton setTitle:[PMSettings getCountry] forState:UIControlStateNormal];
  54. }
  55.  
  56. - (void)didReceiveMemoryWarning
  57. {
  58.     [super didReceiveMemoryWarning];
  59.     // Dispose of any resources that can be recreated.
  60. }
  61.  
  62. - (void) verificationState {
  63.     phoneNumberTextField.enabled = false;
  64.     verificationCodeTextField.enabled = true;
  65.     [registerButton setTitle:@"Verify" forState:UIControlStateNormal];
  66. }
  67.  
  68. - (void) phoneNumberState {
  69.     phoneNumberTextField.enabled = true;
  70.     verificationCodeTextField.enabled = false;
  71.     [registerButton setTitle:@"Send PIN via SMS" forState:UIControlStateNormal];
  72. }
  73.  
  74. - (IBAction)registerButtonPressed:(id)sender {
  75.  
  76.     if(phoneNumberTextField.enabled){
  77.         //combine area code with the number
  78.         NSString *phoneNumber = [[PMSettings getCountryCode] stringByAppendingString:phoneNumberTextField.text];
  79.        
  80.         [[Panacea sharedInstance] registerPhoneNumber:phoneNumber WithTag:self];
  81.         [phoneNumberTextField resignFirstResponder];
  82.         phoneNumberTextField.enabled = false;
  83.     }
  84.     else {
  85.         [[Panacea sharedInstance] registerVerification:verificationCodeTextField.text WithTag:self];
  86.         [verificationCodeTextField resignFirstResponder];
  87.         verificationCodeTextField.enabled = false;
  88.     }
  89. }
  90.  
  91. - (IBAction)countryButtonPressed:(id)sender {
  92.    
  93.     UIStoryboard *inboxStoryBoard = [Panacea getPanaceaStoryBoard];
  94.    
  95.     UINavigationController *nav = (UINavigationController*)[inboxStoryBoard instantiateViewControllerWithIdentifier:@"PMCountryNavigationController"];
  96.    
  97.     PMCountrySelectViewController *csvc = (PMCountrySelectViewController *)[inboxStoryBoard instantiateViewControllerWithIdentifier:@"PMCountrySelectViewController"];
  98.    
  99.     csvc.previousViewController = self;
  100.    
  101.     [nav setViewControllers:@[csvc]];
  102.     if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  103.         nav.modalPresentationStyle = UIModalPresentationFormSheet;
  104.    
  105.     [self presentViewController:nav animated:YES completion:nil];
  106. }
  107.  
  108. #pragma mark - WebService Response
  109.  
  110. - (void) handleRegisterResponse: (NSNotification*) notification {
  111.    
  112.     id tag = [notification.userInfo objectForKey:@"tag"];
  113.    
  114.     if(tag != self)
  115.         return;
  116.    
  117.     NSString *result = [notification.userInfo objectForKey:@"result"];
  118.    
  119.     NSLog(@"result: %@", result);
  120.    
  121.     if([result isEqualToString:@"REQUEST_PHONE_NUMBER"]){
  122.  
  123.         [self phoneNumberState];
  124.     }
  125.     else if ([result isEqualToString:@"REQUIRE_VERIFICATION_CODE"]){
  126.  
  127.         [self verificationState];
  128.     }
  129.     else if ([result isEqualToString:@"REGISTER_SUCCESS"]){
  130.        
  131.         [self performSegueWithIdentifier:@"registered" sender:self];
  132.     }
  133.     else if ([result isEqualToString:@"INVALID_VERIFICATION_CODE"]){
  134.        
  135.         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Verification Code" message:@"The Verification Code you submitted is invalid, please try again" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
  136.        
  137.         [alert show];
  138.        
  139.         [self verificationState];
  140.     }
  141. }
  142.  
  143.  
  144. #pragma mark - UITextField Delegate
  145.  
  146. - (BOOL) textFieldShouldReturn:(UITextField *)textField {
  147.    
  148.     [textField resignFirstResponder];
  149.    
  150.     return YES;
  151. }
  152.  
  153. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement