InvitationListViewController.m 14 KB

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