HomeMemberViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // HomeMemberViewController.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 "CustomImageView.h"
  13. #import "CustomCheckBox.h"
  14. #import "UIImageView+WebCache.h"
  15. #import "HomeMemberViewController.h"
  16. #import "CustomAlertView.h"
  17. #define kfMemberTableViewCellHeight 100.0f
  18. @interface HomeMemberCollectionCell () {
  19. }
  20. @property (weak, nonatomic) NSIndexPath *indexPath;
  21. @end
  22. @implementation HomeMemberCollectionCell
  23. - (void)awakeFromNib {
  24. _chkSelect.hidden = YES;
  25. }
  26. @end
  27. @implementation HomeMemberAddCollectionCell
  28. @end
  29. @implementation HomeMemberCollectionFooterView
  30. - (void)awakeFromNib {
  31. }
  32. @end
  33. @interface HomeMemberViewController () <UICollectionViewDelegate, UICollectionViewDataSource> {
  34. NSArray<HomeMemberModel> *_memberList;
  35. BOOL _isNotFirstLoading, _isDeleteMode;
  36. }
  37. @end
  38. #pragma mark - Class Definition
  39. @implementation HomeMemberViewController
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. [self initUI];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated {
  45. [super viewWillAppear:animated];
  46. [self prepareViewDidLoad];
  47. }
  48. - (void)initUI {
  49. //set tableview option
  50. _collectionView.delegate = self;
  51. _collectionView.dataSource = self;
  52. _collectionView.backgroundColor = kUIBgColor01;
  53. [self setThingsPopoverOptions];
  54. }
  55. - (void)setThingsPopoverOptions {
  56. //set Popover Contents
  57. __weak typeof(self) weakSelf = self;
  58. _popooverOptionArray = [[NSMutableArray alloc] init];
  59. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  60. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  61. @"target": weakSelf,
  62. @"selector": [NSValue valueWithPointer:@selector(refreshHomeMemberList)]}];
  63. if ([JDFacade facade].loginUser.level == 90) {
  64. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"초대", @"초대"),
  65. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  66. @"target": weakSelf,
  67. @"selector": [NSValue valueWithPointer:@selector(addNewMember)]}];
  68. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
  69. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  70. @"target": weakSelf,
  71. @"selector": [NSValue valueWithPointer:@selector(toggleEditMode)]}];
  72. }
  73. }
  74. - (void)prepareViewDidLoad {
  75. [self performSelector:@selector(requestHomeMemberList) withObject:nil afterDelay:0.0f];
  76. }
  77. - (void)updateHomeHubStatusToMembers {
  78. }
  79. - (void)refreshHomeMemberList {
  80. [self requestHomeMemberList];
  81. }
  82. - (IBAction)btnOptionTouched:(id)sender {
  83. [self toggleOptions:sender];
  84. }
  85. - (IBAction)btnCloseEditModeTouched:(id)sender {
  86. }
  87. - (void)addNewMember {
  88. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberAddViewController" storyboardName:@"HomeMember"];
  89. [self presentViewController:vc animated:YES completion:nil];
  90. }
  91. - (void)toggleEditMode {
  92. _isDeleteMode = !_isDeleteMode;
  93. if (!_isDeleteMode) {
  94. for (HomeMemberModel *member in _memberList) {
  95. [[JDFacade facade] setCheckBoxStatus:@(NO) object:member];
  96. }
  97. }
  98. [_collectionView reloadData];
  99. _constraintEditModeRight.constant = _isDeleteMode ? 0 : -_editModeView.width;
  100. [UIView animateWithDuration:kfAnimationDur animations:^{
  101. [self.view layoutIfNeeded];
  102. }];
  103. }
  104. #pragma mark - Main Logic
  105. - (void)requestHomeMemberList {
  106. NSString *path = [NSString stringWithFormat:API_GET_HOMEGROUP_MEMBERS];
  107. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[HomeMemberListModel class] completion:^(id responseObject) {
  108. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  109. return;
  110. }
  111. HomeMemberListModel *fetchedMemberList = (HomeMemberListModel *)responseObject;
  112. NSArray<HomeMemberModel> *homegrpMemberList = fetchedMemberList.homegrpMemberList;
  113. if (homegrpMemberList && homegrpMemberList.count) {
  114. _memberList = homegrpMemberList;
  115. } else {//조회 내역이 없을 경우,
  116. }
  117. [_collectionView reloadData];
  118. } failure:^(id errorObject) {
  119. JDErrorModel *error = (JDErrorModel *)errorObject;
  120. [[JDFacade facade] alert:error.errorMessage];
  121. }];
  122. }
  123. - (void)requestDeleteMembers:(NSString *)memberIds invitations:(NSString *)invitations {
  124. //parameters
  125. NSDictionary *parameter = @{@"member_ids" : ![memberIds isEmptyString] ? memberIds : ksEmptyString,
  126. @"invitation_hist_ids": ![invitations isEmptyString] ? invitations : ksEmptyString};
  127. NSString *path = [NSString stringWithFormat:API_DELETE_HOMEGROUP_MEMBERS];
  128. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:parameter modelClass:[JDJSONModel class] showLoadingView:YES completion:^(id responseObject) {
  129. [self requestHomeMemberList];
  130. [[JDFacade facade] toast:NSLocalizedString(@"멤버를 삭제했습니다", @"멤버를 삭제했습니다")];
  131. } failure:^(id errorObject) {
  132. JDErrorModel *error = (JDErrorModel *)errorObject;
  133. [[JDFacade facade] alert:error.errorMessage];
  134. }];
  135. }
  136. #pragma mark - UICollectionView Delegate
  137. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  138. NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
  139. NSInteger count = _memberList.count % 2 == 0 ? _memberList.count : _memberList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
  140. return count;
  141. }
  142. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  143. UICollectionViewCell *rcell = nil;
  144. if (indexPath.row < _memberList.count) {
  145. HomeMemberCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCellIdentifier" forIndexPath:indexPath];
  146. HomeMemberModel *member =_memberList[indexPath.row];
  147. cell.indexPath = indexPath;
  148. [cell.imgvMemberIcon sd_setImageWithURL:[NSURL URLWithString:member.imageFileName] placeholderImage:nil options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  149. [cell layoutIfNeeded];
  150. }];
  151. cell.lblNickname.text = member.nickname;
  152. cell.lblStatus.text = member.gradeCodeName;
  153. cell.chkSelect.hidden = !_isDeleteMode;
  154. cell.chkSelect.value = member;
  155. cell.chkSelect.checked = [cell.chkSelect getCheckStatusFromValue];
  156. rcell = cell;
  157. } else {
  158. HomeMemberAddCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
  159. if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  160. [cell.btnAdd addTarget:self action:@selector(addNewMember) forControlEvents:UIControlEventTouchUpInside];
  161. }
  162. rcell = cell;
  163. }
  164. return rcell;
  165. }
  166. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  167. viewForSupplementaryElementOfKind:(NSString *)kind
  168. atIndexPath:(NSIndexPath *)indexPath
  169. {
  170. HomeMemberCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
  171. withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
  172. if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  173. [footerView.btnAdd addTarget:self action:@selector(addNewMember) forControlEvents:UIControlEventTouchUpInside];
  174. }
  175. return footerView;
  176. }
  177. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  178. //FIXME : 권한 추가
  179. // if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  180. if (_memberList.count % 2 == 1 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  181. return CGSizeZero;
  182. }
  183. return CGSizeMake(IPHONE_WIDTH, 160.0f);
  184. }
  185. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  186. if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
  187. return CGSizeMake(212.0f, 160.0f);
  188. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
  189. return CGSizeMake(187.5, 160.0f);
  190. }
  191. return CGSizeMake(160.0f, 160.0f);
  192. }
  193. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  194. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  195. if (indexPath.row < _memberList.count) {
  196. HomeMemberCollectionCell *cell = (HomeMemberCollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCellIdentifier" forIndexPath:indexPath];
  197. HomeMemberModel *member = _memberList[indexPath.row];
  198. cell.chkSelect.value = member;
  199. [cell.chkSelect checkBoxClicked];
  200. }
  201. }
  202. #pragma mark - UI Events
  203. - (IBAction)btnDeleteOnEditModeTouched:(id)sender {
  204. if (!_isDeleteMode)
  205. return;
  206. NSMutableString *memberIds = [[NSMutableString alloc] init];
  207. NSMutableString *invitationHistIds = [[NSMutableString alloc] init];
  208. for (HomeMemberModel *member in _memberList) {
  209. if ([[[JDFacade facade] getCheckBoxStatus:member] boolValue]) {
  210. if ([member.memberTypeCode isEqualToString:KNEET_MEMBER_TYPE_HOME]) {//홈멤버일 경우,
  211. NSString *prefix = [memberIds isEmptyString] ? ksEmptyString : @",";
  212. [memberIds appendFormat:@"%@%@", prefix, member.memberId];
  213. } else if ([member.memberTypeCode isEqualToString:KNEET_MEMBER_TYPE_INV]) {//초대중일 경우,
  214. NSString *prefix = [invitationHistIds isEmptyString] ? ksEmptyString : @",";
  215. [invitationHistIds appendFormat:@"%@%@", prefix, member.invitationHistId];
  216. }
  217. }
  218. }
  219. if ([memberIds isEmptyString] && [invitationHistIds isEmptyString]) {
  220. [[JDFacade facade] alert:NSLocalizedString(@"선택된 멤버가 없습니다", @"선택된 멤버가 없습니다")];
  221. return;
  222. }
  223. CustomAlertView *alert = [[CustomAlertView alloc] initWithTitle:@"알림" message:@"멤버를 삭제하시겠습니까?\n\n삭제된 멤버는 홈의 모든 기능을\n이용할 수 없게되며,\n참여 대기중인 사람은 초대가\n취소됩니다." delegate:nil OKButtonTitle:@"멤버삭제" cancelButtonTitle:@"취소"];
  224. [alert showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  225. if (buttonIndex == 0) {//OK
  226. [self requestDeleteMembers:memberIds invitations:invitationHistIds];
  227. }
  228. }];
  229. }
  230. - (IBAction)btnCloseOnEditModeTouched:(id)sender {
  231. [self toggleEditMode];
  232. }
  233. //- (IBAction)btnCoverIconTouched:(id)sender {
  234. //
  235. // if (_selectedCoverIcon) {
  236. // [_selectedCoverIcon faceOffImage];
  237. // }
  238. //
  239. // CustomButton *btn = (CustomButton *)sender;
  240. // _selectedCoverIcon = btn;
  241. // [_selectedCoverIcon faceOffImage];
  242. //}
  243. #pragma mark - MemoryWarning
  244. - (void)didReceiveMemoryWarning
  245. {
  246. [super didReceiveMemoryWarning];
  247. // Dispose of any resources that can be recreated.
  248. }
  249. @end