HomeMemberAddViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. //
  2. // HomeMemberAddViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 6/15/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "RequestHandler.h"
  9. #import "LoginModel.h"
  10. #import "CustomButton.h"
  11. #import "CustomLabel.h"
  12. #import "DatePickerButton.h"
  13. #import "CustomTextField.h"
  14. #import "ValidateUtil.h"
  15. #import "HomeMemberAddViewController.h"
  16. #import "HomeMemberViewController.h"
  17. #import "CustomTableView.h"
  18. #define kiHeaderCellHeight 100
  19. #define kiAddCellHeight 61
  20. #define kiInputCellHeight 80
  21. #define kiQuizCellHeight 430
  22. /**
  23. - Sub Text Color : kUITextColor04
  24. - 제목 Text Color : kUITextColor01
  25. Input Text Color
  26. - 기본 : kUITextColor01
  27. - hint : kUITextColor03
  28. - 달력 아이콘 Text Color : kUITextColor01
  29. **/
  30. @implementation MemberHeaderTableViewCell
  31. - (void)awakeFromNib {
  32. self.backgroundColor = [UIColor clearColor];
  33. self.selectionStyle = UITableViewCellSelectionStyleNone;
  34. }
  35. @end
  36. @implementation MemberInputTableViewCell
  37. - (void)awakeFromNib {
  38. self.backgroundColor = [UIColor clearColor];
  39. self.selectionStyle = UITableViewCellSelectionStyleNone;
  40. }
  41. @end
  42. @implementation MemberAddedTableViewCell
  43. - (void)awakeFromNib {
  44. self.backgroundColor = [UIColor clearColor];
  45. self.selectionStyle = UITableViewCellSelectionStyleNone;
  46. }
  47. @end
  48. @implementation MemberQuizTableViewCell
  49. - (void)awakeFromNib {
  50. self.backgroundColor = [UIColor clearColor];
  51. self.selectionStyle = UITableViewCellSelectionStyleNone;
  52. _btnDate.date = [NSDate date];
  53. // _btnDate.placeHolder = _btnDate.dateString;
  54. _txtQuestion.keyboardType = UIKeyboardTypeDefault;
  55. _txtQuestion.returnKeyType = UIReturnKeyDone;
  56. _txtQuestion.placeholder = @"예) 아빠 생일은?";
  57. _txtQuestion.customTextFieldSuperview = CustomTextFieldSuperviewIsContentView;
  58. }
  59. @end
  60. @interface HomeMemberAddViewController () <UITableViewDataSource, UITableViewDelegate, CustomTextFieldDelegate> {
  61. NSMutableArray<HomeMemberModel> *_inviteList;
  62. CustomTextField *_txtQuestion, *_txtTempEmail;
  63. DatePickerButton *_btnDate;
  64. }
  65. @end
  66. #pragma mark - Class Definition
  67. @implementation HomeMemberAddViewController
  68. - (void)viewDidLoad {
  69. [super viewDidLoad];
  70. _inviteList = (NSMutableArray<HomeMemberModel> *)[[NSMutableArray alloc] init];
  71. [self initUI];
  72. [self prepareViewDidLoad];
  73. }
  74. - (void)viewWillAppear:(BOOL)animated {
  75. [super viewWillAppear:animated];
  76. }
  77. - (void)initUI {
  78. [self initTableViewAsDefaultStyle:_tableView];
  79. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"common_button_left_bg"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  80. [self.btnCancel setBackgroundImage:[UIImage imageNamed:@"common_button_left_bg_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  81. [self.btnSend setBackgroundImage:[UIImage imageNamed:@"common_button_right_bg"] forState:UIControlStateNormal capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  82. [self.btnSend setBackgroundImage:[UIImage imageNamed:@"common_button_right_bg_press"] forState:UIControlStateHighlighted capInsets:UIEdgeInsetsMake(kfTopInset,kfRightInset,kfBottomInset,kfLeftInset)];
  83. //Localization
  84. [_btnSend setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  85. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  86. }
  87. - (void)prepareViewDidLoad {
  88. }
  89. #pragma mark - Main Logic
  90. - (void)requestInviteMembers {
  91. NSMutableArray *invitations = [[NSMutableArray alloc] init];
  92. for (HomeMemberModel *member in _inviteList) {
  93. [invitations addObject:[member toDictionary]];
  94. }
  95. //parameters
  96. NSDictionary *parameter = @{@"authorization_question": _txtQuestion.text,
  97. @"authorization_reply": _btnDate.dateString,
  98. @"invitation_list": invitations};
  99. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_INVITE];
  100. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  101. [self dismissViewControllerAnimated:YES completion:^{
  102. [[JDFacade facade] toast:NSLocalizedString(@"초대장을 보냈습니다", @"초대장을 보냈습니다")];
  103. }];
  104. } failure:^(id errorObject) {
  105. JDErrorModel *error = (JDErrorModel *)errorObject;
  106. [[JDFacade facade] alert:error.errorMessage];
  107. }];
  108. }
  109. #pragma mark - UITableView DataSource & Delegate
  110. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  111. return 4;
  112. }
  113. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  114. NSInteger count = 1;
  115. if (section == 2) {
  116. count = _inviteList.count;
  117. }
  118. return count;
  119. }
  120. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  121. CGFloat height = 0;
  122. if (indexPath.section == 0) {
  123. height = kiHeaderCellHeight;
  124. } else if (indexPath.section == 1) {
  125. height = kiInputCellHeight;
  126. } else if (indexPath.section == 2) {
  127. height = kiAddCellHeight;
  128. } else if (indexPath.section == 3) {
  129. height = kiQuizCellHeight;
  130. }
  131. return height;
  132. }
  133. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  134. UITableViewCell *cell = nil;
  135. if (indexPath.section == 0) { // title
  136. MemberHeaderTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  137. cell = tcell;
  138. } else if (indexPath.section == 1) {// input
  139. MemberInputTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"InputCellIdentifier"];
  140. _txtTempEmail = !_txtTempEmail ? tcell.txtEmail : _txtTempEmail;
  141. _txtTempEmail.delegate = self;
  142. if (![tcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  143. [tcell.btnAdd addTarget:self action:@selector(btnAddTouched:) forControlEvents:UIControlEventTouchUpInside];
  144. }
  145. cell = tcell;
  146. } else if (indexPath.section == 2) {// added
  147. MemberAddedTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"AddedCellIdentifier"];
  148. HomeMemberModel *member = _inviteList[indexPath.row];
  149. tcell.txtEmail.text = member.targetEmail;
  150. tcell.btnRemove.value = member;
  151. if (![tcell.btnRemove actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  152. [tcell.btnRemove addTarget:self action:@selector(btnRemoveTouched:) forControlEvents:UIControlEventTouchUpInside];
  153. }
  154. cell = tcell;
  155. } else if (indexPath.section == 3) {// Quiz
  156. MemberQuizTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"QuizCellIdentifier"];
  157. //init controls link
  158. _txtQuestion = !_txtQuestion ? tcell.txtQuestion : _txtQuestion;
  159. _btnDate = !_btnDate ? tcell.btnDate : _btnDate;
  160. // if (_txtQuestion.customTextFieldSuperview != CustomTextFieldSuperviewIsScrollView) {
  161. // _txtQuestion.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  162. // }
  163. cell = tcell;
  164. }
  165. return cell;
  166. }
  167. - (void)btnAddTouched:(id)sender {
  168. if (![ValidateUtil validateTextfiled:_txtTempEmail type:ValidateTypeEmail title:NSLocalizedString(@"이메일", @"이메일")]) {
  169. return;
  170. }
  171. HomeMemberModel *member = [[HomeMemberModel alloc] init];
  172. member.targetEmail = _txtTempEmail.text;
  173. member.gradeCode = KNEET_MEMBER_SIMPLE;
  174. //api - 12.5
  175. NSDictionary *parameter = @{@"target_email" : _txtTempEmail.text};
  176. JDErrorModel *result = [[RequestHandler handler] sendSyncPostRequestAPIPath:API_POST_HOMEGROUP_MEMBER_VALIDATE parameters:parameter modelClass:[JDErrorModel class] showLoadingView:YES];
  177. if (!result) {
  178. [_inviteList addObject:member];
  179. _txtTempEmail.text = ksEmptyString;
  180. [_tableView reloadData];
  181. } else {
  182. [[JDFacade facade] alert:result.errorMessage];
  183. }
  184. }
  185. - (void)btnRemoveTouched:(id)sender {
  186. CustomButton *btnRemove = (CustomButton *)sender;
  187. HomeMemberModel *member = btnRemove.value;
  188. [_inviteList removeObject:member];
  189. [_tableView reloadData];
  190. }
  191. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  192. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  193. }
  194. #pragma mark - UI Events
  195. - (void)btnDeleteTouched:(id)sender {
  196. CustomButton *btn = (CustomButton *)sender;
  197. HomeMemberModel *member = btn.value;
  198. [_inviteList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  199. [_inviteList removeObject:member];
  200. }];
  201. [_tableView reloadData];
  202. }
  203. - (IBAction)btnSendTouched:(id)sender {
  204. //1.validate
  205. if (_inviteList.count <= 0) {
  206. [[JDFacade facade] alert:NSLocalizedString(@"초대할 이메일을 입력하세요", @"초대할 이메일을 입력하세요")];
  207. return;
  208. }
  209. if (![ValidateUtil validateTextfiled:_txtQuestion type:ValidateTypeNull title:NSLocalizedString(@"퀴즈", @"퀴즈")]) {
  210. return;
  211. }
  212. if (_txtQuestion.text.length > 60) {
  213. [[JDFacade facade] alert:@"퀴즈는 최대 60자까지 입력할 수 있습니다"];
  214. return;
  215. }
  216. if ([_btnDate.dateString isEmptyString] || [[_btnDate titleForState:UIControlStateNormal] isEqualToString:_btnDate.placeHolder]) {
  217. [[JDFacade facade] alert:@"정답 날짜를 입력하세요"];
  218. return;
  219. }
  220. [self requestInviteMembers];
  221. }
  222. - (IBAction)btnCancelTouched:(id)sender {
  223. [self dismissViewControllerAnimated:YES completion:nil];
  224. }
  225. - (IBAction)btnQuizSelectTouched:(id)sender {
  226. }
  227. #pragma mark - UITextField Delegate
  228. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  229. if ([textField isEqual:_txtTempEmail]) {
  230. [self btnAddTouched:nil];
  231. }
  232. return YES;
  233. }
  234. #pragma mark - MemoryWarning
  235. - (void)didReceiveMemoryWarning
  236. {
  237. [super didReceiveMemoryWarning];
  238. // Dispose of any resources that can be recreated.
  239. }
  240. @end