StartHomeViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. //
  2. // StartHomeViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 4/23/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "CustomLabel.h"
  11. #import "CustomButton.h"
  12. #import "CustomTextField.h"
  13. #import "StartHomeViewController.h"
  14. #import "ValidateUtil.h"
  15. #import "CustomAlertView.h"
  16. @interface StartHomeViewController () <CustomTextFieldDelegate> {
  17. }
  18. @end
  19. #pragma mark - Class Definition
  20. @implementation StartHomeViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. // Do any additional setup after loading the view.
  24. [self initUI];
  25. [self prepareViewDidLoad];
  26. }
  27. - (void)viewDidAppear:(BOOL)animated {
  28. [super viewDidAppear:animated];
  29. _txtHomeName.delegate = self;
  30. _txtHomeName.keyboardType = UIKeyboardTypeDefault;
  31. _txtHomeName.returnKeyType = UIReturnKeyDone;
  32. _txtHomeName.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  33. }
  34. - (void)initUI {
  35. if (_canGoBack) {
  36. _btnNext.hidden = YES;
  37. _btnSNext.hidden = _btnCancel.hidden = NO;
  38. }
  39. //Localization
  40. _lblTitle.text = NSLocalizedString(@"시작합니다", @"시작합니다");
  41. _txtHomeName.placeholder = NSLocalizedString(@"홈 이름", @"홈 이름");
  42. [_btnNext setTitle:NSLocalizedString(@"다음", @"다음") forState:UIControlStateNormal];
  43. [_btnSNext setTitle:NSLocalizedString(@"다음", @"다음") forState:UIControlStateNormal];
  44. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  45. }
  46. - (void)prepareViewDidLoad {
  47. _lblHomeDesc.text = [NSString stringWithFormat:NSLocalizedString(@"%@님의 새로운 홈에 이름을 붙여주세요", @"%@님의 새로운 홈에 이름을 붙여주세요"), [JDFacade facade].loginUser.nickname];
  48. }
  49. #pragma mark - Main Logic
  50. - (void)requestRegisterHomeGroup {
  51. //parameters
  52. NSDictionary *parameter = @{@"homegrp_name": _txtHomeName.text};
  53. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP];
  54. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[LoginModel class] completion:^(id responseObject) {
  55. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  56. return;
  57. }
  58. LoginModel *result = (LoginModel *) responseObject;
  59. if (result) {//API 성공 ,
  60. //TODO : home group id
  61. [[JDFacade facade] toast:NSLocalizedString(@"등록되었습니다", @"등록되었습니다")];
  62. [JDFacade facade].tmpHomegrpName = _txtHomeName.text;
  63. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeLocationViewController" storyboardName:@"SignUp"];
  64. [[JDFacade facade] presentViewControllerByPush:vc pvc:self];
  65. }
  66. } failure:^(id errorObject) {
  67. JDErrorModel *error = (JDErrorModel *)errorObject;
  68. [[JDFacade facade] alert:error.errorMessage];
  69. }];
  70. }
  71. #pragma mark - UI Events
  72. - (IBAction)btnCreateHomeTouched:(id)sender {
  73. [self.view endEditing:YES];
  74. //validate
  75. #ifdef DEBUG
  76. if ([_txtHomeName.text isEmptyString]) {
  77. _txtHomeName.text = @"테스트홈";
  78. }
  79. #else
  80. if (![ValidateUtil validateTextfiled:_txtHomeName type:ValidateTypeNull title:NSLocalizedString(@"홈 이름", @"홈 이름")]) {
  81. return;
  82. }
  83. #endif
  84. [JDFacade facade].tmpHomegrpName = _txtHomeName.text;
  85. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeLocationViewController" storyboardName:@"SignUp"];
  86. UINavigationController *nc = [[UINavigationController alloc] init];
  87. nc.viewControllers = @[vc];
  88. [[JDFacade facade] presentViewControllerByPush:nc pvc:self];
  89. }
  90. - (IBAction)btnCancelTouched:(id)sender {
  91. if ([JDFacade facade].loginUser.homegrpList.count == 0) {//현재 홈이 없을 경우,
  92. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:NSLocalizedString(@"알림", @"알림") message:NSLocalizedString(@"홈 만들기를 취소하면 서비스를 더 이상 이용할 수 없습니다\n취소하고 로그아웃 할까요?", @"홈 만들기를 취소하면 서비스를 더 이상 이용할 수 없습니다\n취소하고 로그아웃 할까요?") delegate:nil OKButtonTitle:NSLocalizedString(@"확인", @"확인") cancelButtonTitle:NSLocalizedString(@"취소", @"취소")];
  93. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  94. if (buttonIndex == 0) {//OK
  95. [[JDFacade facade] logout];
  96. return;
  97. }
  98. }];
  99. } else {
  100. [[JDFacade facade] dismissViewControllerByPush:self];
  101. }
  102. }
  103. #pragma mark - CustomTextField
  104. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  105. [self btnCreateHomeTouched:nil];
  106. return YES;
  107. }
  108. #pragma mark - MemoryWarning
  109. - (void)didReceiveMemoryWarning
  110. {
  111. [super didReceiveMemoryWarning];
  112. // Dispose of any resources that can be recreated.
  113. }
  114. @end