InvitationListViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 "CustomTableView.h"
  11. #import "CustomButton.h"
  12. #import "CustomLabel.h"
  13. #import "ImageUtil.h"
  14. #import "UIImageView+WebCache.h"
  15. #import "CustomImageView.h"
  16. #import "CustomAlertView.h"
  17. #import "InvitationAnswerPopupView.h"
  18. #import "LoginViewController.h"
  19. #import "InvitationListViewController.h"
  20. #import "InvitationAnswerViewController.h"
  21. #import "UIButton+WebCache.h"
  22. @implementation InvitationTableViewCell
  23. - (void)awakeFromNib {
  24. self.selectionStyle = UITableViewCellSelectionStyleNone;
  25. self.backgroundColor = [UIColor clearColor];
  26. }
  27. - (void)didMoveToSuperview {
  28. _lblJoin.text = NSLocalizedString(@"참여", @"참여");
  29. _lblReject.text = NSLocalizedString(@"거절", @"거절");
  30. [_lblJoin setUnderLine:_lblJoin.text];
  31. [_lblReject setUnderLine:_lblReject.text];
  32. }
  33. @end
  34. @interface InvitationListViewController () <UITableViewDataSource, UITableViewDelegate> {
  35. NSMutableArray<InvitationModel> *_invitationList;
  36. }
  37. @end
  38. #pragma mark - Class Definition
  39. @implementation InvitationListViewController
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. // Do any additional setup after loading the view.
  43. [self initUI];
  44. [self prepareViewDidLoad];
  45. }
  46. - (void)initUI {
  47. [self initTableViewAsDefaultStyle:_tableView];
  48. }
  49. - (void)prepareViewDidLoad {
  50. // [[JDFacade facade] updateLoginInfo:^{
  51. // _invitationList = [JDFacade facade].loginUser.invitationList;
  52. //
  53. // dispatch_async(dispatch_get_main_queue(), ^{
  54. // [_tableView reloadData];
  55. // });
  56. // }];
  57. [self requestInvitationList];
  58. }
  59. - (void)updateTitle {
  60. _lblTitle.text = [NSString stringWithFormat:@"초대알림 %zd", _invitationList.count];
  61. [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _invitationList.count]];
  62. }
  63. #pragma mark - Main Logic
  64. - (void)requestInvitationList {
  65. //parameters
  66. NSString *path = [NSString stringWithFormat:API_GET_HOMEGROUP_MEMBER_INVITATIONS];
  67. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[InvitationListModel class] completion:^(id responseObject) {
  68. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  69. return;
  70. }
  71. InvitationListModel *fetchedInvitationList = (InvitationListModel *) responseObject;
  72. if (fetchedInvitationList && fetchedInvitationList.list) {//API 성공 ,
  73. _invitationList = [fetchedInvitationList.list mutableCopy];
  74. }
  75. if (_invitationList && !_invitationList.count) {
  76. [self btnCloseTouched:nil];
  77. return;
  78. }
  79. [_tableView reloadData];
  80. [self updateTitle];
  81. } failure:^(id errorObject) {
  82. JDErrorModel *error = (JDErrorModel *)errorObject;
  83. [[JDFacade facade] alert:error.errorMessage];
  84. }];
  85. }
  86. - (void)requestRejectInvitation:(InvitationModel *)invitation {
  87. //parameters
  88. NSDictionary *parameter = @{@"homegrp_id": invitation.homegrpId,
  89. @"status_code": ksNO};
  90. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_INVITATIONS];
  91. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  92. [_invitationList removeObject:invitation];
  93. if (_invitationList.count > 0) {//초대건수가 있으면, 다시 로드함.
  94. [_tableView reloadData];
  95. } else {//다음 플로우 진행
  96. [[JDFacade facade] alertTitle:@"홈 초대 거절" message:@"모든 초대를 거절했습니다" completionHander:^{
  97. [self btnCloseTouched:nil];
  98. }];
  99. }
  100. } failure:^(id errorObject) {
  101. JDErrorModel *error = (JDErrorModel *)errorObject;
  102. [[JDFacade facade] alert:error.errorMessage];
  103. }];
  104. }
  105. #pragma mark - UITableView DataSource & Delegate
  106. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  107. return 1;
  108. }
  109. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  110. NSInteger count = _invitationList.count;
  111. return count;
  112. }
  113. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  114. CGFloat height = 0;
  115. CGFloat width = IPHONE_WIDTH - 80 - 20;
  116. CustomLabel *lblTemp = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, width, 17)];
  117. lblTemp.font = [UIFont systemFontOfSize:kUIFontSize01];
  118. lblTemp.numberOfLines = 0;
  119. InvitationModel *invitation = _invitationList[indexPath.row];
  120. NSString *fromNickname = invitation.nickname;
  121. CGFloat adjustHeight = [CommonUtil getSizeFromString:fromNickname font:lblTemp.font width:width].height;
  122. adjustHeight = adjustHeight < 20 ? 0 : adjustHeight - 20;
  123. height = 86 + adjustHeight;
  124. return height;
  125. }
  126. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  127. InvitationModel *invitation = _invitationList[indexPath.row];
  128. InvitationTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"InvitationCellIdentifier"];
  129. tcell.lblFromNickname.text = invitation.nickname;
  130. [tcell.btnProfile sd_setBackgroundImageWithURL:[NSURL URLWithString:invitation.imageFileName] forState:UIControlStateNormal placeholderImage:nil options:SDWebImageRefreshCached];
  131. tcell.lblJoin.value = invitation;
  132. tcell.lblReject.value = invitation;
  133. if (!tcell.lblJoin.touchHandler) {
  134. [tcell.lblJoin addTouchEventHandler:^(id label) {
  135. [self lblJoinTouched:label];
  136. }];
  137. }
  138. if (!tcell.lblReject.touchHandler) {
  139. [tcell.lblReject addTouchEventHandler:^(id label) {
  140. [self lblRejectTouched:label];
  141. }];
  142. }
  143. return tcell;
  144. }
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  146. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  147. }
  148. #pragma mark - UI Events
  149. - (void)lblJoinTouched:(id)sender {
  150. CustomLabel *lblJoin = (CustomLabel *)sender;
  151. InvitationModel *invitation = lblJoin.value;
  152. InvitationAnswerViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"InvitationAnswerViewController" storyboardName:@"Main"];
  153. vc.invitation = invitation;
  154. NSString *message1 = [NSString stringWithFormat:@"%@님의 홈에 참여할까요?\n\n", invitation.nickname];
  155. NSString *message2 = ksEmptyString;
  156. if (![JDFacade facade].loginUser.homegrpId || [[JDFacade facade].loginUser.homegrpId isEmptyString]) {//홈이 없을 경우,
  157. message2 = @"한 아이디로 한 개의 홈만 참여 가능하며,\n다른 홈엔 더이상 참여할 수 없습니다.";
  158. } else if (![JDFacade facade].loginUser.hasHomeHub) {//홈허브가 삭제된 경우,
  159. if ([JDFacade facade].loginUser.level > 10) {//마스터일 경우,
  160. message2 = @"한 아이디로 한 개의 홈만 참여 가능하며,\n현재 운영 중인 홈이 삭제됩니다.";
  161. } else {
  162. message2 = @"한 아이디로 한 개의 홈만 참여 가능하며,\n다른 홈엔 더이상 참여할 수 없습니다.";
  163. }
  164. }
  165. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"홈 초대 수락"
  166. message:[NSString stringWithFormat:@"%@%@", message1, message2]
  167. delegate:nil OKButtonTitle:@"예" cancelButtonTitle:@"아니오"];
  168. if (message2 && ![message2 isEmptyString]) {
  169. [alert.lblMessage1 setColor:kUITextColor02 text:message2];
  170. }
  171. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  172. if (buttonIndex == 0) {//OK
  173. [self presentViewController:vc animated:YES completion:nil];
  174. }
  175. }];
  176. }
  177. - (void)lblRejectTouched:(id)sender {
  178. CustomLabel *lblReject = (CustomLabel *)sender;
  179. InvitationModel *invitation = lblReject.value;
  180. [self requestRejectInvitation:invitation];
  181. }
  182. - (IBAction)btnCloseTouched:(id)sender {
  183. LoginViewController *lvc = [[JDFacade facade] viewControllerOnPresentingViewController:[JDFacade facade].currentViewController viewControllerClass:[LoginViewController class]];
  184. // (LoginViewController *)[JDFacade facade].currentViewController.presentingViewController;
  185. if ([lvc isKindOfClass:[LoginViewController class]]) {
  186. [lvc actionAfterInvitaion];
  187. }
  188. [self dismissViewControllerAnimated:YES completion:nil];
  189. }
  190. #pragma mark - MemoryWarning
  191. - (void)didReceiveMemoryWarning
  192. {
  193. [super didReceiveMemoryWarning];
  194. // Dispose of any resources that can be recreated.
  195. }
  196. @end