HomeMemberAddViewController.m 9.2 KB

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