HomeHubInitViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // HomeHubInitViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 10/23/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "ValidateUtil.h"
  9. #import "CustomTextField.h"
  10. #import "RequestHandler.h"
  11. #import "CustomLabel.h"
  12. #import "CustomButton.h"
  13. #import "HomeHubInitViewController.h"
  14. #import "DeviceModel.h"
  15. @interface HomeHubInitViewController() <CustomTextFieldDelegate> {
  16. }
  17. @end
  18. #pragma mark - Class Definition
  19. @implementation HomeHubInitViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. [self initUI];
  24. [self prepareViewDidLoad];
  25. }
  26. - (void)viewDidAppear:(BOOL)animated {
  27. [super viewDidAppear:animated];
  28. [UIView animateWithDuration:kfAnimationDur animations:^{
  29. _maskView.alpha = 0.7;
  30. } completion:^(BOOL finished) {
  31. _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
  32. [UIView animateWithDuration:kfAnimationDur animations:^{
  33. [self.view layoutIfNeeded];
  34. }];
  35. }];
  36. }
  37. - (void)initUI {
  38. _txtSecureCode.secureTextEntry = YES;
  39. _txtSecureCode.returnKeyType = UIReturnKeyDone;
  40. _txtSecureCodeConfirm.delegate = self;
  41. _txtSecureCodeConfirm.secureTextEntry = YES;
  42. _txtSecureCodeConfirm.returnKeyType = UIReturnKeyDone;
  43. _txtSecureCode.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  44. _txtSecureCodeConfirm.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  45. _txtSecureCode.placeholder = NSLocalizedString(@"보안키", @"보안키");
  46. _txtSecureCodeConfirm.placeholder = NSLocalizedString(@"보안키 확인", @"보안키 확인");
  47. //Localization
  48. [_btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  49. }
  50. - (void)prepareViewDidLoad {
  51. }
  52. #pragma mark - Main Logic
  53. - (void)requestRegisterSecureCode {
  54. //parameters
  55. NSDictionary *parameter = @{@"activation_code" : _activationCode,
  56. @"activation_password" : _activationPassword,
  57. @"user_device_password": _txtSecureCode.text};
  58. NSString *path = [NSString stringWithFormat:API_POST_PARTNER_QR];
  59. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[DeviceModel class] completion:^(id responseObject) {
  60. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  61. return;
  62. }
  63. DeviceModel *device = (DeviceModel *)responseObject;
  64. if (device) {
  65. [JDFacade facade].loginUser.homehubDeviceId = device.deviceId;
  66. }
  67. [[JDFacade facade] updateLoginInfo:^{
  68. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"WelcomeViewController" storyboardName:@"Main"];
  69. [self presentViewController:vc animated:YES completion:nil];
  70. }];
  71. } failure:^(id errorObject) {
  72. JDErrorModel *error = (JDErrorModel *)errorObject;
  73. [[JDFacade facade] alert:error.errorMessage];
  74. }];
  75. }
  76. #pragma mark - UI Events
  77. - (IBAction)btnConfirmTouched:(id)sender {
  78. //1.validate
  79. if (![ValidateUtil validateTextfiled:_txtSecureCode type:ValidateTypeNull title:NSLocalizedString(@"보안키", @"보안키")]) {
  80. return;
  81. }
  82. if (![ValidateUtil validateTextfiled:_txtSecureCodeConfirm type:ValidateTypeNull title:NSLocalizedString(@"보안키 확인", @"보안키 확인")]) {
  83. return;
  84. }
  85. if (![_txtSecureCode.text isEqualToString:_txtSecureCodeConfirm.text]) {
  86. [[JDFacade facade] alert:NSLocalizedString(@"보안키와 보안키확인이 다릅니다\n다시 입력해주세요", @"보안키와 보안키확인이 다릅니다\n다시 입력해주세요")];
  87. return;
  88. }
  89. [self requestRegisterSecureCode];
  90. }
  91. - (IBAction)btnCloseTouched:(id)sender {
  92. [[JDFacade facade] confirmTitle:@"연결취소" message:@"홈허브 연결을 취소할까요?" completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  93. if (buttonIndex == 0) {//OK
  94. [[JDFacade facade] dismissModalStack:YES completion:nil];
  95. }
  96. }];
  97. }
  98. #pragma mark - CustomTextField
  99. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  100. if ([textField isEqual:_txtSecureCodeConfirm]) {
  101. [self btnConfirmTouched:nil];
  102. }
  103. return YES;
  104. }
  105. #pragma mark - MemoryWarning
  106. - (void)didReceiveMemoryWarning
  107. {
  108. [super didReceiveMemoryWarning];
  109. // Dispose of any resources that can be recreated.
  110. }
  111. @end