InvitationListViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. //
  2. // InvitationListViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 6/12/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 "InvitationListViewController.h"
  13. #import "InvitationAnswerPopupView.h"
  14. #import "LoginViewController.h"
  15. #import "ImageUtil.h"
  16. @implementation InvitationTitleViewCell
  17. - (void)awakeFromNib {
  18. self.selectionStyle = UITableViewCellSelectionStyleNone;
  19. self.backgroundColor = [UIColor clearColor];
  20. }
  21. @end
  22. @implementation InvitationTableViewCell
  23. - (void)awakeFromNib {
  24. self.selectionStyle = UITableViewCellSelectionStyleNone;
  25. self.backgroundColor = [UIColor clearColor];
  26. }
  27. @end
  28. @implementation InvitationActionCell
  29. - (void)awakeFromNib {
  30. self.selectionStyle = UITableViewCellSelectionStyleNone;
  31. self.backgroundColor = [UIColor clearColor];
  32. }
  33. @end
  34. @interface InvitationListViewController () <UITableViewDataSource, UITableViewDelegate> {
  35. NSMutableArray<InvitationModel> *_invitationList;
  36. UIImage *_bgCellImage1, *_bgCellImage2;
  37. InvitationAnswerPopupView *_apopup;
  38. }
  39. @end
  40. #pragma mark - Class Definition
  41. @implementation InvitationListViewController
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // Do any additional setup after loading the view.
  45. [self initUI];
  46. [self prepareViewDidLoad];
  47. }
  48. - (void)initUI {
  49. //set tableview option
  50. _tableView.delegate = self;
  51. _tableView.dataSource = self;
  52. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  53. _tableView.backgroundColor = [UIColor clearColor];
  54. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  55. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  56. _bgCellImage1 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  57. _bgCellImage2 = [ImageUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  58. }
  59. - (void)prepareViewDidLoad {
  60. [[JDFacade facade] updateLoginInfo:^{
  61. _invitationList = [JDFacade facade].loginUser.invitationList;
  62. dispatch_async(dispatch_get_main_queue(), ^{
  63. [_tableView reloadData];
  64. });
  65. }];
  66. }
  67. #pragma mark - Main Logic
  68. - (void)requestJoinInvitation:(InvitationModel *)invitation {
  69. //parameters
  70. NSDictionary *parameter = @{@"homegrp_id": invitation.homegrpId,
  71. @"authorization_reply": invitation.sdate,
  72. @"target_email": invitation.targetEmail,
  73. @"status_code": ksYES};
  74. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_STAT];
  75. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  76. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  77. return;
  78. }
  79. JDJSONModel *result = (JDJSONModel *) responseObject;
  80. if (result) {//API 성공 ,
  81. [_invitationList removeObject:invitation];
  82. NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"축하합니다!\n[%@] 멤버가 되었습니다.", @"축하합니다!\n[%@] 멤버가 되었습니다."), invitation.homegrpName];
  83. [[JDFacade facade] alert:msg completionHander:^{
  84. [self gotoNextScreenOnFlow];
  85. }];
  86. }
  87. } failure:^(id errorObject) {//답변이 틀린 경우,
  88. JDErrorModel *error = (JDErrorModel *)errorObject;
  89. if ([error.errorCode isEqualToString:@"400-102"]) {//답변이
  90. if ([error.errorCount integerValue] < 3) {//답변이 1,2회 틀린 경우,
  91. [[JDFacade facade] alert:error.errorMessage completionHander:^{
  92. [self popInvitationAnswer:invitation];
  93. }];
  94. } else {//답변이 3회 틀린 경우,
  95. [[JDFacade facade] alert:error.errorMessage completionHander:^{
  96. [_invitationList removeObject:invitation];
  97. if (_invitationList.count > 0) {//초대건수가 있으면, 다시 로드함.
  98. [_tableView reloadData];
  99. } else {//다음 플로우 진행
  100. [self gotoNextScreenOnFlow];
  101. }
  102. }];
  103. }
  104. } else {
  105. [[JDFacade facade] alert:error.errorMessage];
  106. }
  107. }];
  108. }
  109. - (void)requestRejectInvitation:(InvitationModel *)invitation {
  110. //parameters
  111. NSDictionary *parameter = @{@"homegrp_id": invitation.homegrpId,
  112. @"target_email": invitation.targetEmail,
  113. @"status_code": ksNO};
  114. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_STAT];
  115. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  116. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  117. return;
  118. }
  119. JDJSONModel *result = (JDJSONModel *) responseObject;
  120. if (result) {//API 성공 ,
  121. [[JDFacade facade] alert:NSLocalizedString(@"아쉽지만 다음에\n다시 초대할께요", @"아쉽지만 다음에\n다시 초대할께요") completionHander:^{
  122. [_invitationList removeObject:invitation];
  123. if (_invitationList.count > 0) {//초대건수가 있으면, 다시 로드함.
  124. [_tableView reloadData];
  125. } else {//다음 플로우 진행
  126. [self gotoNextScreenOnFlow];
  127. }
  128. }];
  129. }
  130. } failure:^(id errorObject) {
  131. JDErrorModel *error = (JDErrorModel *)errorObject;
  132. [[JDFacade facade] alert:error.errorMessage];
  133. }];
  134. }
  135. - (void)gotoNextScreenOnFlow {
  136. //다음 화면으로 진행 - FIXME :
  137. if ([self.presentingViewController isKindOfClass:[UINavigationController class]]) {
  138. [[JDFacade facade] gotoWishMenu:KNMenuIdDashboard];
  139. } else {
  140. LoginViewController *lvc = [[JDFacade facade] viewControllerOnPresentingViewController:self viewControllerClass:[LoginViewController class]];
  141. if ([lvc isKindOfClass:[LoginViewController class]]) {//로그인일 경우,
  142. [lvc actionAfterInvitaion];
  143. }
  144. }
  145. }
  146. #pragma mark - UITableView DataSource & Delegate
  147. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  148. return 3;
  149. }
  150. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  151. NSInteger count = 1;
  152. if (section == 1) {
  153. count = _invitationList.count;
  154. }
  155. return count;
  156. }
  157. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  158. CGFloat height = 0;
  159. if (indexPath.section == 0) {
  160. CGFloat width = IPHONE_WIDTH - 40;
  161. CustomLabel *lblDesc = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, width, 35)];
  162. lblDesc.font = [UIFont systemFontOfSize:kUIFontSize01];
  163. lblDesc.numberOfLines = 0;
  164. NSString *desc = [NSString stringWithFormat:NSLocalizedString(@"[%@]님을 기다리는 사람이 있습니다\n홈에 참여해서 함께 장치를 제어해보세요", @"[%@]님을 기다리는 사람이 있습니다\n홈에 참여해서 함께 장치를 제어해보세요"), [JDFacade facade].loginUser.nickname];
  165. CGFloat adjustHeight = [CommonUtil getSizeFromString:desc font:lblDesc.font width:width].height;
  166. adjustHeight = adjustHeight < 35 ? 0 : adjustHeight - 35;
  167. height = 160 + adjustHeight;
  168. } else if (indexPath.section == 2) {//action
  169. height = 100;
  170. } else if (indexPath.section == 1) {//calculate
  171. CGFloat width = IPHONE_WIDTH - 80 - 20;
  172. CustomLabel *lblTemp = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, width, 17)];
  173. lblTemp.font = [UIFont systemFontOfSize:kUIFontSize01];
  174. lblTemp.numberOfLines = 0;
  175. InvitationModel *invitation = _invitationList[indexPath.row];
  176. NSString *invitationMsg = [NSString stringWithFormat:NSLocalizedString(@"[%@]에 초대합니다", @"[%@]에 초대합니다"), invitation.homegrpName];
  177. CGFloat adjustHeight = [CommonUtil getSizeFromString:invitationMsg font:lblTemp.font width:width].height;
  178. adjustHeight = adjustHeight < 17 ? 0 : adjustHeight - 17;
  179. height = 150 + adjustHeight;
  180. }
  181. return height;
  182. }
  183. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  184. NSInteger section = indexPath.section;
  185. UITableViewCell *cell = nil;
  186. if (section == 0) {//title
  187. InvitationTitleViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"TitleCellIdentifier"];
  188. tcell.lblTitle.text = NSLocalizedString(@"초대받은 홈에 참여해보세요!", @"초대받은 홈에 참여해보세요!");
  189. tcell.lblDesc.text = [NSString stringWithFormat:NSLocalizedString(@"[%@]님을 기다리는 사람이 있습니다\n홈에 참여해서 함께 장치를 제어해보세요", @"[%@]님을 기다리는 사람이 있습니다\n홈에 참여해서 함께 장치를 제어해보세요"), [JDFacade facade].loginUser.nickname];
  190. cell = tcell;
  191. } else if (section == 2) {//action
  192. InvitationActionCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"ActionCellIdentifier"];
  193. [tcell.btnLater setTitle:NSLocalizedString(@"나중에 다시보기", @"나중에 다시보기") forState:UIControlStateNormal];
  194. if (![tcell.btnLater actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  195. [tcell.btnLater addTarget:self action:@selector(btnLaterTouched:) forControlEvents:UIControlEventTouchUpInside];
  196. }
  197. cell = tcell;
  198. } else if (section == 1) {//invitation
  199. InvitationModel *invitation = _invitationList[indexPath.row];
  200. InvitationTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  201. tcell.lblTitle.text = [NSString stringWithFormat:NSLocalizedString(@"초대자: %@", @"초대자: %@"), invitation.nickname];
  202. tcell.lblDesc.text = [NSString stringWithFormat:NSLocalizedString(@"[%@]에 초대합니다", @"[%@]에 초대합니다"), invitation.homegrpName];
  203. [tcell.btnJoin setTitle:NSLocalizedString(@"참여", @"참여") forState:UIControlStateNormal];
  204. [tcell.btnReject setTitle:NSLocalizedString(@"거절", @"거절") forState:UIControlStateNormal];
  205. tcell.btnJoin.value = invitation;
  206. tcell.btnReject.value = invitation;
  207. if (![tcell.btnJoin actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  208. [tcell.btnJoin addTarget:self action:@selector(btnJoinTouched:) forControlEvents:UIControlEventTouchUpInside];
  209. }
  210. if (![tcell.btnReject actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  211. [tcell.btnReject addTarget:self action:@selector(btnRejectTouched:) forControlEvents:UIControlEventTouchUpInside];
  212. }
  213. cell = tcell;
  214. //set background image
  215. if (indexPath.row % 2 == 1) {
  216. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  217. } else {
  218. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  219. }
  220. }
  221. return cell;
  222. }
  223. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  224. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  225. }
  226. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  227. // Remove seperator inset
  228. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  229. [cell setSeparatorInset:UIEdgeInsetsZero];
  230. }
  231. // Prevent the cell from inheriting the Table View's margin settings
  232. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  233. [cell setPreservesSuperviewLayoutMargins:NO];
  234. }
  235. // Explictly set your cell's layout margins
  236. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  237. [cell setLayoutMargins:UIEdgeInsetsZero];
  238. }
  239. }
  240. #pragma mark - UI Events
  241. - (void)popInvitationAnswer:(InvitationModel *)invitation {
  242. //pop
  243. if (!_apopup) {
  244. _apopup = [[InvitationAnswerPopupView alloc] initFromNib];
  245. }
  246. _apopup.invitation = invitation;
  247. [_apopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  248. if (buttonIndex == 0) {//OK
  249. invitation.sdate = _apopup.selectedDate;
  250. [self requestJoinInvitation:invitation];
  251. }
  252. }];
  253. }
  254. - (void)btnJoinTouched:(id)sender {
  255. CustomButton *btnJoin = (CustomButton *)sender;
  256. InvitationModel *invitation = btnJoin.value;
  257. [self popInvitationAnswer:invitation];
  258. }
  259. - (void)btnRejectTouched:(id)sender {
  260. CustomButton *btnReject = (CustomButton *)sender;
  261. InvitationModel *invitation = btnReject.value;
  262. [self requestRejectInvitation:invitation];
  263. }
  264. - (void)btnLaterTouched:(id)sender {
  265. [self gotoNextScreenOnFlow];
  266. }
  267. #pragma mark - MemoryWarning
  268. - (void)didReceiveMemoryWarning
  269. {
  270. [super didReceiveMemoryWarning];
  271. // Dispose of any resources that can be recreated.
  272. }
  273. @end