InvitationListViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. @implementation InvitationTableViewCell
  22. - (void)awakeFromNib {
  23. self.selectionStyle = UITableViewCellSelectionStyleNone;
  24. self.backgroundColor = [UIColor clearColor];
  25. }
  26. - (void)didMoveToSuperview {
  27. _lblJoin.text = NSLocalizedString(@"참여", @"참여");
  28. _lblReject.text = NSLocalizedString(@"거절", @"거절");
  29. }
  30. @end
  31. @interface InvitationListViewController () <UITableViewDataSource, UITableViewDelegate> {
  32. NSMutableArray<InvitationModel> *_invitationList;
  33. }
  34. @end
  35. #pragma mark - Class Definition
  36. @implementation InvitationListViewController
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. // Do any additional setup after loading the view.
  40. [self initUI];
  41. [self prepareViewDidLoad];
  42. }
  43. - (void)initUI {
  44. [self initTableViewAsDefaultStyle:_tableView];
  45. }
  46. - (void)prepareViewDidLoad {
  47. // [[JDFacade facade] updateLoginInfo:^{
  48. // _invitationList = [JDFacade facade].loginUser.invitationList;
  49. //
  50. // dispatch_async(dispatch_get_main_queue(), ^{
  51. // [_tableView reloadData];
  52. // });
  53. // }];
  54. [self requestInvitationList];
  55. }
  56. #pragma mark - Main Logic
  57. - (void)requestInvitationList {
  58. //parameters
  59. NSString *path = [NSString stringWithFormat:API_GET_HOMEGROUP_MEMBER_INVITATIONS];
  60. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[InvitationListModel class] completion:^(id responseObject) {
  61. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  62. return;
  63. }
  64. InvitationListModel *fetchedInvitationList = (InvitationListModel *) responseObject;
  65. if (fetchedInvitationList && fetchedInvitationList.list) {//API 성공 ,
  66. _invitationList = fetchedInvitationList.list;
  67. }
  68. [_tableView reloadData];
  69. if (_invitationList && !_invitationList.count) {
  70. [self btnCloseTouched:nil];
  71. }
  72. } failure:^(id errorObject) {
  73. JDErrorModel *error = (JDErrorModel *)errorObject;
  74. [[JDFacade facade] alert:error.errorMessage];
  75. }];
  76. }
  77. - (void)requestRejectInvitation:(InvitationModel *)invitation {
  78. //parameters
  79. NSDictionary *parameter = @{@"homegrp_id": invitation.homegrpId,
  80. @"status_code": ksNO};
  81. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_MEMBER_INVITATIONS];
  82. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  83. [[JDFacade facade] alert:NSLocalizedString(@"아쉽지만 다음에\n다시 초대할께요", @"아쉽지만 다음에\n다시 초대할께요") completionHander:^{
  84. [_invitationList removeObject:invitation];
  85. if (_invitationList.count > 0) {//초대건수가 있으면, 다시 로드함.
  86. [_tableView reloadData];
  87. } else {//다음 플로우 진행
  88. [[JDFacade facade] alertTitle:@"홈 초대 거절" message:@"참여한 홈이 없습니다.\n홈허브에 연결해서 직접 홈을 만들어 보세요" completionHander:^{
  89. [self btnCloseTouched:nil];
  90. }];
  91. }
  92. }];
  93. } failure:^(id errorObject) {
  94. JDErrorModel *error = (JDErrorModel *)errorObject;
  95. [[JDFacade facade] alert:error.errorMessage];
  96. }];
  97. }
  98. #pragma mark - UITableView DataSource & Delegate
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  100. return 1;
  101. }
  102. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  103. NSInteger count = _invitationList.count;
  104. return count;
  105. }
  106. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  107. CGFloat height = 0;
  108. CGFloat width = IPHONE_WIDTH - 80 - 20;
  109. CustomLabel *lblTemp = [[CustomLabel alloc] initWithFrame:CGRectMake(0, 0, width, 17)];
  110. lblTemp.font = [UIFont systemFontOfSize:kUIFontSize01];
  111. lblTemp.numberOfLines = 0;
  112. InvitationModel *invitation = _invitationList[indexPath.row];
  113. NSString *fromNickname = invitation.nickname;
  114. CGFloat adjustHeight = [CommonUtil getSizeFromString:fromNickname font:lblTemp.font width:width].height;
  115. adjustHeight = adjustHeight < 20 ? 0 : adjustHeight - 20;
  116. height = 95 + adjustHeight;
  117. return height;
  118. }
  119. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  120. InvitationModel *invitation = _invitationList[indexPath.row];
  121. InvitationTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"InvitationCellIdentifier"];
  122. tcell.lblFromNickname.text = invitation.nickname;
  123. //TODO : crop or set circle background
  124. [tcell.imgvProfile sd_setImageWithURL:[NSURL URLWithString:invitation.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached];
  125. tcell.lblJoin.value = invitation;
  126. tcell.lblReject.value = invitation;
  127. if (!tcell.lblJoin.touchHandler) {
  128. [tcell.lblJoin addTouchEventHandler:^(id label) {
  129. [self lblJoinTouched:label];
  130. }];
  131. }
  132. if (!tcell.lblReject.touchHandler) {
  133. [tcell.lblReject addTouchEventHandler:^(id label) {
  134. [self lblRejectTouched:label];
  135. }];
  136. }
  137. return tcell;
  138. }
  139. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  140. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  141. }
  142. #pragma mark - UI Events
  143. - (void)lblJoinTouched:(id)sender {
  144. CustomLabel *lblJoin = (CustomLabel *)sender;
  145. InvitationModel *invitation = lblJoin.value;
  146. InvitationAnswerViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"InvitationAnswerViewController" storyboardName:@"Main"];
  147. vc.invitation = invitation;
  148. if (_invitationList.count > 1) {
  149. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"홈 초대 수락"
  150. message:@"둘리님의 홈에 참여할까요?\n\n한 아이디로는 하나의 홈에만\n참여할 수 있기 때문에\n다른 홈에서 받은 초대는\n자동으로 거절합니다."
  151. delegate:nil OKButtonTitle:@"확인" cancelButtonTitle:@"취소"];
  152. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  153. if (buttonIndex == 0) {//OK
  154. [self presentViewController:vc animated:YES completion:nil];
  155. }
  156. }];
  157. } else {
  158. [self presentViewController:vc animated:YES completion:nil];
  159. }
  160. }
  161. - (void)lblRejectTouched:(id)sender {
  162. CustomLabel *lblReject = (CustomLabel *)sender;
  163. InvitationModel *invitation = lblReject.value;
  164. [self requestRejectInvitation:invitation];
  165. }
  166. - (IBAction)btnCloseTouched:(id)sender {
  167. [self dismissViewControllerAnimated:YES completion:^{
  168. if ([[JDFacade facade].currentViewController isKindOfClass:[LoginViewController class]]) {
  169. LoginViewController *lvc = (LoginViewController *)[JDFacade facade].currentViewController;
  170. [lvc actionAfterInvitaion];
  171. }
  172. }];
  173. }
  174. #pragma mark - MemoryWarning
  175. - (void)didReceiveMemoryWarning
  176. {
  177. [super didReceiveMemoryWarning];
  178. // Dispose of any resources that can be recreated.
  179. }
  180. @end