HomeMemberAddViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. @implementation MemberInputTableViewCell
  18. - (void)awakeFromNib {
  19. self.backgroundColor = [UIColor clearColor];
  20. self.selectionStyle = UITableViewCellSelectionStyleNone;
  21. }
  22. @end
  23. @implementation MemberAddedTableViewCell
  24. - (void)awakeFromNib {
  25. self.backgroundColor = [UIColor clearColor];
  26. self.selectionStyle = UITableViewCellSelectionStyleNone;
  27. }
  28. @end
  29. @implementation MemberQuizTableViewCell
  30. - (void)awakeFromNib {
  31. self.backgroundColor = [UIColor clearColor];
  32. self.selectionStyle = UITableViewCellSelectionStyleNone;
  33. _lblQuizInfo.text = NSLocalizedString(@"우리만 아는 특별한 날을 물어보세요\n진짜 멤버라면 바로 맞출 수 있을거예요", @"우리만 아는 특별한 날을 물어보세요\n진짜 멤버라면 바로 맞출 수 있을거예요");
  34. _btnDate.date = [NSDate date];
  35. _btnDate.placeHolder = _btnDate.dateString;
  36. _txtQuestion.keyboardType = UIKeyboardTypeDefault;
  37. _txtQuestion.returnKeyType = UIReturnKeyDone;
  38. _txtQuestion.placeholder = NSLocalizedString(@"ex) 우리집 강아지 뽀삐의 생일은?", @"ex) 우리집 강아지 뽀삐의 생일은?");
  39. _txtQuestion.customTextFieldSuperview = CustomTextFieldSuperviewIsContentView;
  40. }
  41. @end
  42. @interface HomeMemberAddViewController () <UITableViewDataSource, UITableViewDelegate> {
  43. NSMutableArray<HomeMemberModel> *_inviteList;
  44. CustomTextField *_txtQuestion, *_txtTempEmail;
  45. DatePickerButton *_btnDate;
  46. }
  47. @end
  48. #pragma mark - Class Definition
  49. @implementation HomeMemberAddViewController
  50. - (void)viewDidLoad {
  51. [super viewDidLoad];
  52. _inviteList = (NSMutableArray<HomeMemberModel> *)[[NSMutableArray alloc] init];
  53. [self initUI];
  54. [self prepareViewDidLoad];
  55. }
  56. - (void)viewWillAppear:(BOOL)animated {
  57. [super viewWillAppear:animated];
  58. }
  59. - (void)initUI {
  60. //set tableview option
  61. _tableView.delegate = self;
  62. _tableView.dataSource = self;
  63. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  64. _tableView.backgroundColor = [UIColor clearColor];
  65. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  66. //Localization
  67. [_btnSend setTitle:NSLocalizedString(@"초대", @"초대") forState:UIControlStateNormal];
  68. [_btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  69. }
  70. - (void)prepareViewDidLoad {
  71. }
  72. #pragma mark - Main Logic
  73. - (void)requestInviteMembers {
  74. NSMutableArray *invitations = [[NSMutableArray alloc] init];
  75. for (HomeMemberModel *member in _inviteList) {
  76. [invitations addObject:[member toDictionary]];
  77. }
  78. //parameters
  79. NSDictionary *parameter = @{@"authorization_question": _txtQuestion.text,
  80. @"authorization_reply": _btnDate.dateString,
  81. @"invitation_list": invitations}; //TODO : dictionary;
  82. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_INVITE];
  83. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  84. [[JDFacade facade] toast:NSLocalizedString(@"초대장을 보냈습니다", @"초대장을 보냈습니다")];
  85. HomeMemberViewController *vc = [[JDFacade facade] viewControllerOnNaviationController:[HomeMemberViewController class]];
  86. if (vc) {
  87. [vc refreshHomeMemberList];
  88. }
  89. [self.navigationController popViewControllerAnimated:YES];
  90. } failure:^(id errorObject) {
  91. JDErrorModel *error = (JDErrorModel *)errorObject;
  92. [[JDFacade facade] alert:error.errorMessage];
  93. }];
  94. }
  95. #pragma mark - UITableView DataSource & Delegate
  96. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  97. return 3;
  98. }
  99. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  100. NSInteger count = 1;
  101. if (section == 0) {
  102. count = _inviteList.count;
  103. }
  104. return count;
  105. }
  106. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. CGFloat height = 50;
  108. if (indexPath.section == 2) {
  109. height = 200;
  110. }
  111. return height;
  112. }
  113. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  114. UITableViewCell *cell = nil;
  115. if (indexPath.section == 0) {//added
  116. MemberAddedTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"AddedCellIdentifier"];
  117. HomeMemberModel *member = _inviteList[indexPath.row];
  118. tcell.txtEmail.text = member.targetEmail;
  119. tcell.btnRemove.value = member;
  120. if (![tcell.btnRemove actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  121. [tcell.btnRemove addTarget:self action:@selector(btnRemoveTouched:) forControlEvents:UIControlEventTouchUpInside];
  122. }
  123. cell = tcell;
  124. } else if (indexPath.section == 1) {//input
  125. MemberInputTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"InputCellIdentifier"];
  126. _txtTempEmail = !_txtTempEmail ? tcell.txtEmail : _txtTempEmail;
  127. if (![tcell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  128. [tcell.btnAdd addTarget:self action:@selector(btnAddTouched:) forControlEvents:UIControlEventTouchUpInside];
  129. }
  130. cell = tcell;
  131. } else if (indexPath.section == 2) {//invite list
  132. MemberQuizTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"QuizCellIdentifier"];
  133. //init controls link
  134. _txtQuestion = !_txtQuestion ? tcell.txtQuestion : _txtQuestion;
  135. _btnDate = !_btnDate ? tcell.btnDate : _btnDate;
  136. // if (_txtQuestion.customTextFieldSuperview != CustomTextFieldSuperviewIsScrollView) {
  137. // _txtQuestion.customTextFieldSuperview = CustomTextFieldSuperviewIsScrollView;
  138. // }
  139. cell = tcell;
  140. }
  141. return cell;
  142. }
  143. - (void)btnAddTouched:(id)sender {
  144. if (![ValidateUtil validateTextfiled:_txtTempEmail type:ValidateTypeEmail title:NSLocalizedString(@"이메일", @"이메일")]) {
  145. return;
  146. }
  147. HomeMemberModel *member = [[HomeMemberModel alloc] init];
  148. member.targetEmail = _txtTempEmail.text;
  149. member.gradeCode = KNEET_MEMBER_SIMPLE;
  150. //api - 12.5
  151. NSDictionary *parameter = @{@"target_email" : _txtTempEmail.text};
  152. JDErrorModel *result = [[RequestHandler handler] sendSyncPostRequestAPIPath:API_POST_HOMEGROUP_MEMBER_VALIDATE parameters:parameter modelClass:[JDErrorModel class] showLoadingView:@(YES)];
  153. if (!result) {
  154. [_inviteList addObject:member];
  155. _txtTempEmail.text = ksEmptyString;
  156. [_tableView reloadData];
  157. } else {
  158. [[JDFacade facade] alert:result.errorMessage];
  159. }
  160. }
  161. - (void)btnRemoveTouched:(id)sender {
  162. CustomButton *btnRemove = (CustomButton *)sender;
  163. HomeMemberModel *member = btnRemove.value;
  164. [_inviteList removeObject:member];
  165. [_tableView reloadData];
  166. }
  167. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  168. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  169. }
  170. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  171. // Remove seperator inset
  172. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  173. [cell setSeparatorInset:UIEdgeInsetsZero];
  174. }
  175. // Prevent the cell from inheriting the Table View's margin settings
  176. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  177. [cell setPreservesSuperviewLayoutMargins:NO];
  178. }
  179. // Explictly set your cell's layout margins
  180. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  181. [cell setLayoutMargins:UIEdgeInsetsZero];
  182. }
  183. }
  184. #pragma mark - UI Events
  185. - (void)btnDeleteTouched:(id)sender {
  186. CustomButton *btn = (CustomButton *)sender;
  187. HomeMemberModel *member = btn.value;
  188. [_inviteList enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  189. [_inviteList removeObject:member];
  190. }];
  191. [_tableView reloadData];
  192. }
  193. - (IBAction)btnSendTouched:(id)sender {
  194. //1.validate
  195. if (_inviteList.count <= 0) {
  196. [[JDFacade facade] alert:NSLocalizedString(@"초대할 이메일을 입력하세요", @"초대할 이메일을 입력하세요")];
  197. return;
  198. }
  199. if (![ValidateUtil validateTextfiled:_txtQuestion type:ValidateTypeNull title:NSLocalizedString(@"퀴즈", @"퀴즈")]) {
  200. return;
  201. }
  202. [self requestInviteMembers];
  203. }
  204. - (IBAction)btnCancelTouched:(id)sender {
  205. [self dismissViewControllerAnimated:YES completion:nil];
  206. }
  207. #pragma mark - MemoryWarning
  208. - (void)didReceiveMemoryWarning
  209. {
  210. [super didReceiveMemoryWarning];
  211. // Dispose of any resources that can be recreated.
  212. }
  213. @end