HomeMemberViewController.m 11 KB

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