HomeMemberViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 "HomeMemberViewController.h"
  9. #import "CustomButton.h"
  10. #import "LoginModel.h"
  11. #import "RequestHandler.h"
  12. #import "CustomLabel.h"
  13. #import "UIButton+WebCache.h"
  14. #import "HomeMemberDetailViewController.h"
  15. #define kfMemberTableViewCellHeight 100.0f
  16. @protocol MembersHeaderTableViewCellDelegate <NSObject>
  17. @optional
  18. - (void)didMemberHeaderTableViewCellBtnHeaderTouched:(id)sender;
  19. @end
  20. @interface MemberHeaderTableViewCell () {
  21. }
  22. @property (assign, nonatomic) id <MembersHeaderTableViewCellDelegate> delegate;
  23. @end
  24. @implementation MemberHeaderTableViewCell
  25. - (IBAction)btnHeaderTouched:(id)sender {
  26. [_btnHeader faceOffImage];
  27. _group[@"unfold"] = @(![_group[@"unfold"] boolValue]);
  28. if ([self.delegate respondsToSelector:@selector(didMemberHeaderTableViewCellBtnHeaderTouched:)]) {
  29. [self.delegate didMemberHeaderTableViewCellBtnHeaderTouched:self];
  30. }
  31. }
  32. @end
  33. @implementation MemberTableViewCell
  34. - (void)awakeFromNib {
  35. self.backgroundColor = [UIColor clearColor];
  36. self.selectionStyle = UITableViewCellSelectionStyleNone;
  37. self.opaque = NO;
  38. }
  39. @end
  40. @implementation MemberAddTableViewCell
  41. - (void)awakeFromNib {
  42. self.backgroundColor = [UIColor clearColor];
  43. self.selectionStyle = UITableViewCellSelectionStyleNone;
  44. _lblAddTitle.text = NSLocalizedString(@"주변 사람들을 초대하세요!", @"주변 사람들을 초대하세요!");
  45. _lblAddDesc.text = NSLocalizedString(@"멤버가 되면 내장치를 같이 쓸 수 있어요\n편리한 자동 규칙도 함께 만들어보세요", @"멤버가 되면 내장치를 같이 쓸 수 있어요\n편리한 자동 규칙도 함께 만들어보세요");
  46. }
  47. - (void)btnAddTouched:(id)sender {
  48. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberAddViewController" storyboardName:@"HomeMember"];
  49. [[JDFacade facade].currentViewController.navigationController pushViewController:vc animated:YES];
  50. }
  51. @end
  52. @interface HomeMemberViewController () <UITableViewDataSource, UITableViewDelegate, MembersHeaderTableViewCellDelegate> {
  53. NSMutableArray *_groups;
  54. UIImage *_bgCellImage1, *_bgCellImage2;
  55. NSMutableArray *_arrayForHeader;
  56. }
  57. @end
  58. #pragma mark - Class Definition
  59. @implementation HomeMemberViewController
  60. - (void)viewDidLoad {
  61. [super viewDidLoad];
  62. [self initUI];
  63. [self prepareViewDidLoad];
  64. }
  65. - (void)viewWillAppear:(BOOL)animated {
  66. [super viewWillAppear:animated];
  67. self.title = NSLocalizedString(@"홈 멤버", @"홈 멤버");
  68. }
  69. - (void)initUI {
  70. [self generateOptionButton];
  71. //set tableview option
  72. _tableView.delegate = self;
  73. _tableView.dataSource = self;
  74. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  75. _tableView.backgroundColor = [UIColor clearColor];
  76. _tableView.tableFooterView = [[UIView alloc] init]; //this call table events;
  77. UIEdgeInsets insets = UIEdgeInsetsMake(4, 4, 4, 4);
  78. _bgCellImage1 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_02"]];
  79. _bgCellImage2 = [CommonUtil resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch img:[UIImage imageNamed:@"tp_01_img_list_bg_01"]];
  80. [self setThingsPopoverOptions];
  81. }
  82. - (void)setThingsPopoverOptions {
  83. //set Popover Contents
  84. __weak typeof(self) weakSelf = self;
  85. _popooverOptionArray = [[NSMutableArray alloc] init];
  86. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  87. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  88. @"target": weakSelf,
  89. @"selector": [NSValue valueWithPointer:@selector(refreshHomeMemberList)]}];
  90. if ([JDFacade facade].loginHomeGroup.level == 90) {
  91. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새 멤버 추가",nil),
  92. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  93. @"target": weakSelf,
  94. @"selector": [NSValue valueWithPointer:@selector(addNewMember)]}];
  95. }
  96. }
  97. - (void)prepareViewDidLoad {
  98. [self performSelector:@selector(requestHomeMemberList) withObject:nil afterDelay:0.0f];
  99. }
  100. - (void)refreshHomeMemberList {
  101. if (_groups && _groups.count) {
  102. [_groups removeAllObjects];
  103. _groups = nil;
  104. }
  105. [self requestHomeMemberList];
  106. }
  107. - (void)addNewMember {
  108. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberAddViewController" storyboardName:@"HomeMember"];
  109. [self.navigationController pushViewController:vc animated:YES];
  110. }
  111. #pragma mark - Main Logic
  112. - (void)requestHomeMemberList {
  113. NSString *path = [NSString stringWithFormat:API_GET_HOMEGROUP_MEMBERS];
  114. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[HomeMemberListModel class] completion:^(id responseObject) {
  115. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  116. return;
  117. }
  118. HomeMemberListModel *fetchedMemberList = (HomeMemberListModel *)responseObject;
  119. NSArray<HomeMemberModel> *homegrpMemberList = fetchedMemberList.homegrpMemberList;
  120. if (homegrpMemberList && homegrpMemberList.count) {
  121. _groups = [[NSMutableArray alloc] init];
  122. _arrayForHeader = [[NSMutableArray alloc] init]; //헤더 저장용으로 사용
  123. NSIndexSet *isComplete = [homegrpMemberList indexesOfObjectsPassingTest:^BOOL(HomeMemberModel *lmember, NSUInteger idx, BOOL *stop) {//초대완료
  124. return [lmember.invitation isEqualToString:@"11"];
  125. }];
  126. NSIndexSet *isProgress = [homegrpMemberList indexesOfObjectsPassingTest:^BOOL(HomeMemberModel *lmember, NSUInteger idx, BOOL *stop) {//초대 중
  127. return [lmember.invitation isEqualToString:@"01"];
  128. }];
  129. NSIndexSet *isReject = [homegrpMemberList indexesOfObjectsPassingTest:^BOOL(HomeMemberModel *lmember, NSUInteger idx, BOOL *stop) {//초대 거절
  130. return [lmember.invitation isEqualToString:@"23"];
  131. }];
  132. [_groups addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"list":[homegrpMemberList objectsAtIndexes:isComplete]}]];
  133. [_groups addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"list":[homegrpMemberList objectsAtIndexes:isProgress]}]];
  134. [_groups addObject:[NSMutableDictionary dictionaryWithDictionary:@{@"list":[homegrpMemberList objectsAtIndexes:isReject]}]];
  135. } else {//조회 내역이 없을 경우,
  136. }
  137. [_tableView reloadData];
  138. } failure:^(id errorObject) {
  139. JDErrorModel *error = (JDErrorModel *)errorObject;
  140. [[JDFacade facade] alert:error.errorMessage];
  141. }];
  142. }
  143. #pragma mark - UITableView DataSource & Delegate
  144. - (NSString *)titleForSection:(NSInteger)section {
  145. NSString *title = nil;
  146. switch (section) {
  147. case 0:
  148. title = NSLocalizedString(@"이용 중인 멤버", @"이용 중인 멤버");
  149. break;
  150. case 1:
  151. title = NSLocalizedString(@"초대 중인 멤버", @"초대 중인 멤버");
  152. break;
  153. case 2:
  154. title = NSLocalizedString(@"거절한 멤버", @"거절한 멤버");
  155. break;
  156. }
  157. return title;
  158. }
  159. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  160. return _groups.count + ([JDFacade facade].loginHomeGroup.level == 90 ? 1 : 0);
  161. }
  162. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  163. if (section == _groups.count) {
  164. return nil;
  165. }
  166. if (!_groups.count) {
  167. return nil;
  168. }
  169. NSMutableDictionary *group = _groups[section];
  170. UIView *view = _arrayForHeader.count > section ? _arrayForHeader[section] : nil;
  171. if (!view) {
  172. MemberHeaderTableViewCell *hcell = (MemberHeaderTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HeaderCellIdentifier"];
  173. view = [[UIView alloc] initWithFrame:hcell.contentView.frame];
  174. [view addSubview:hcell];
  175. // NSLog(@"hcell=%p", &hcell);
  176. hcell.delegate = self;
  177. hcell.section = section;
  178. hcell.group = group;
  179. NSString *headerTitle = [NSString stringWithFormat:@"%@", [self titleForSection:section]];
  180. [hcell.btnHeader setTitle:headerTitle forState:UIControlStateNormal];
  181. if (_arrayForHeader.count == section) {
  182. [_arrayForHeader insertObject:view atIndex:section];
  183. }
  184. } else {
  185. MemberHeaderTableViewCell *hcell = (MemberHeaderTableViewCell *)view.subviews[0];
  186. NSString *headerTitle = [NSString stringWithFormat:@"%@", [self titleForSection:section]];
  187. [hcell.btnHeader setTitle:headerTitle forState:UIControlStateNormal];
  188. }
  189. return view;
  190. }
  191. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  192. if (section == _groups.count) {
  193. return 0.01f;
  194. }
  195. return 40.0f;
  196. }
  197. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  198. return 0.01f;
  199. }
  200. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  201. if (_groups.count == section) {//추가 셀,
  202. return 1;
  203. }
  204. NSMutableDictionary *group = _groups[section];
  205. NSArray<HomeMemberModel> *memberList = group[@"list"];
  206. if ([group[@"unfold"] boolValue]) {
  207. return 0;
  208. }
  209. return memberList.count;
  210. }
  211. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  212. CGFloat height = kfMemberTableViewCellHeight;
  213. if (indexPath.section == _groups.count) {
  214. height = _groups.count ? 210.0f : IPHONE_HEIGHT - kfNavigationBarHeight;
  215. _tableView.scrollEnabled = _groups.count;
  216. }
  217. return height;
  218. }
  219. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  220. UITableViewCell *cell = nil;
  221. // NSInteger section = indexPath.section;
  222. if (indexPath.section < _groups.count) {
  223. NSMutableDictionary *group = _groups[indexPath.section];
  224. NSArray<HomeMemberModel> *memberList = group[@"list"];
  225. HomeMemberModel *member = memberList[indexPath.row];
  226. MemberTableViewCell *tcell = [tableView dequeueReusableCellWithIdentifier:@"CellIdentifier"];
  227. if (tcell == nil) {
  228. tcell = [[MemberTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CellIdentifier"];
  229. }
  230. tcell.lblTitle.text = member.nickname;
  231. tcell.lblLevel.text = [NSString stringWithFormat:@"[%@]", member.gradeCodeName];
  232. [tcell.btnMemberIcon sd_setImageWithURL:[NSURL URLWithString:member.imageFileName] forState:UIControlStateNormal];
  233. cell = tcell;
  234. //set background image
  235. if (indexPath.row % 2 == 1) {
  236. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage1];
  237. } else {
  238. cell.backgroundView = [[UIImageView alloc] initWithImage:_bgCellImage2];
  239. }
  240. } else {
  241. MemberAddTableViewCell *acell = [tableView dequeueReusableCellWithIdentifier:@"AddCellIdentifier"];
  242. if (acell == nil) {
  243. acell = [[MemberAddTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AddCellIdentifier"];
  244. }
  245. cell = acell;
  246. }
  247. return cell;
  248. }
  249. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  250. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  251. if (indexPath.section == _groups.count) {//add new
  252. [self addNewMember];
  253. return;
  254. }
  255. if ([JDFacade facade].loginHomeGroup.level == 90) {//마스터일 경우에만, 상세로 이동
  256. NSMutableDictionary *group = _groups[indexPath.section];
  257. NSArray<HomeMemberModel> *memberList = group[@"list"];
  258. HomeMemberModel *member = memberList[indexPath.row];
  259. HomeMemberDetailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeMemberDetailViewController" storyboardName:@"HomeMember"];
  260. vc.member = member;
  261. [self.navigationController pushViewController:vc animated:YES];
  262. }
  263. }
  264. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
  265. // Remove seperator inset
  266. if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
  267. [cell setSeparatorInset:UIEdgeInsetsZero];
  268. }
  269. // Prevent the cell from inheriting the Table View's margin settings
  270. if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
  271. [cell setPreservesSuperviewLayoutMargins:NO];
  272. }
  273. // Explictly set your cell's layout margins
  274. if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
  275. [cell setLayoutMargins:UIEdgeInsetsZero];
  276. }
  277. }
  278. #pragma mark - MemberHeaderTableViewCellDelegate
  279. - (void)didMemberHeaderTableViewCellBtnHeaderTouched:(id)sender {
  280. MemberHeaderTableViewCell *hcell = (MemberHeaderTableViewCell *)sender;
  281. BOOL isExpanded = ![hcell.group[@"unfold"] boolValue];
  282. [_tableView enumarateTableViewCellsSection:hcell.section UsingBlock:^(UITableViewCell *cell) {
  283. MemberHeaderTableViewCell *tmpCell = (MemberHeaderTableViewCell *)cell;
  284. tmpCell.contentView.hidden = isExpanded;
  285. }];
  286. [_tableView reloadData];
  287. }
  288. #pragma mark - UI Events
  289. #pragma mark - MemoryWarning
  290. - (void)didReceiveMemoryWarning
  291. {
  292. [super didReceiveMemoryWarning];
  293. // Dispose of any resources that can be recreated.
  294. }
  295. @end