WhoCanSeeViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. //
  2. // WhoCanSeeViewController.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/11/7.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "WhoCanSeeViewController.h"
  9. #import "JXSelFriendVC.h"
  10. #import "JXLabelObject.h"
  11. #import "JXWhoCanSeeCell.h"
  12. #import "JXNewLabelVC.h"
  13. #import "JXSelectFriendsVC.h"
  14. #import "BMChineseSort.h"
  15. @interface WhoCanSeeViewController ()<UITableViewDelegate,UITableViewDataSource, JXWhoCanSeeCellDelegate>
  16. @property (nonatomic, strong) NSArray * titleArray;
  17. @property (nonatomic, strong) NSArray * subTitleArray;
  18. @property (nonatomic, strong) UITableView * table;
  19. @property (nonatomic, strong) UIButton * finishBtn;
  20. @property (nonatomic, assign) int checkIndex;
  21. @property (nonatomic, strong) NSMutableArray *labelsArray;
  22. @property (nonatomic, strong) NSMutableArray * selArray;
  23. @property (nonatomic, strong) JXLabelObject *editLabel;
  24. @end
  25. @implementation WhoCanSeeViewController
  26. -(void)setType:(int)type{
  27. _type = type;
  28. _checkIndex = type - 1;
  29. }
  30. -(instancetype)init{
  31. if (self = [super init]) {
  32. self.heightHeader = JX_SCREEN_TOP;
  33. self.heightFooter = 0;
  34. self.isGotoBack = YES;
  35. _titleArray = [NSArray arrayWithObjects:Localized(@"JXBlogVisibel_public"), Localized(@"JXBlogVisibel_private"), Localized(@"JXBlogVisibel_see"), Localized(@"JXBlogVisibel_nonSee"), nil];
  36. _subTitleArray = [NSArray arrayWithObjects:Localized(@"JXBlogVisibelDes_public"), Localized(@"JXBlogVisibelDes_private"), Localized(@"JXBlogVisibelDes_see"), Localized(@"JXBlogVisibelDes_nonsee"), nil];
  37. // _labelsArray = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  38. _selLabelsArray = [NSMutableArray array];
  39. _selArray = [NSMutableArray array];
  40. _mailListUserArray = [NSMutableArray array];
  41. _checkIndex = 0;
  42. }
  43. return self;
  44. }
  45. - (void)viewDidLoad {
  46. [super viewDidLoad];
  47. [self createHeadAndFoot];
  48. [self.tableHeader addSubview:self.finishBtn];
  49. [self.view addSubview:self.table];
  50. [g_notify addObserver:self selector:@selector(refreshNotif:) name:kLabelVCRefreshNotif object:nil];
  51. }
  52. - (void)dealloc {
  53. [g_notify removeObserver:self];
  54. [g_notify removeObserver:self name:kLabelVCRefreshNotif object:nil];
  55. }
  56. - (void)refreshNotif:(NSNotification *)notif {
  57. _labelsArray = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  58. for (JXLabelObject *labelObj in _labelsArray) {
  59. NSString *userIdStr = labelObj.userIdList;
  60. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  61. if (userIdStr.length <= 0) {
  62. userIds = nil;
  63. }
  64. NSMutableArray *newUserIds = [userIds mutableCopy];
  65. for (NSInteger i = 0; i < userIds.count; i ++) {
  66. NSString *userId = userIds[i];
  67. NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  68. if (!userName || userName.length <= 0) {
  69. [newUserIds removeObject:userId];
  70. }
  71. }
  72. NSString *string = [newUserIds componentsJoinedByString:@","];
  73. labelObj.userIdList = string;
  74. [labelObj update];
  75. }
  76. [_selLabelsArray removeObject:self.editLabel];
  77. for (JXLabelObject *label in _labelsArray) {
  78. if ([label.groupName isEqualToString:self.editLabel.groupName]) {
  79. [_selLabelsArray addObject:label];
  80. break;
  81. }
  82. }
  83. [self.table reloadData];
  84. }
  85. -(UIButton *)finishBtn{
  86. if (!_finishBtn) {
  87. _finishBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  88. _finishBtn.frame = CGRectMake(JX_SCREEN_WIDTH-32-15, JX_SCREEN_TOP - 30, 32, 15);
  89. [_finishBtn setTitle:Localized(@"JX_Finish") forState:UIControlStateNormal];
  90. [_finishBtn.titleLabel setFont:SYSFONT(15)];
  91. [_finishBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
  92. [_finishBtn addTarget:self action:@selector(finishBtnAction) forControlEvents:UIControlEventTouchUpInside];
  93. }
  94. return _finishBtn;
  95. }
  96. -(UITableView *)table{
  97. if (!_table) {
  98. _table = [[UITableView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT-JX_SCREEN_TOP) style:UITableViewStylePlain];
  99. _table.dataSource = self;
  100. _table.delegate = self;
  101. _table.tableFooterView = [[UIView alloc] init];
  102. [_table registerClass:[JXWhoCanSeeCell class] forCellReuseIdentifier:@"JXWhoCanSeeCell"];
  103. }
  104. return _table;
  105. }
  106. - (void)didReceiveMemoryWarning {
  107. [super didReceiveMemoryWarning];
  108. // Dispose of any resources that can be recreated.
  109. }
  110. /*
  111. #pragma mark - Navigation
  112. // In a storyboard-based application, you will often want to do a little preparation before navigation
  113. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  114. // Get the new view controller using [segue destinationViewController].
  115. // Pass the selected object to the new view controller.
  116. }
  117. */
  118. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  119. return _titleArray.count;
  120. }
  121. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  122. return 60;
  123. }
  124. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  125. UIButton *view = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, 60)];
  126. view.tag = section;
  127. [view addTarget:self action:@selector(headerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  128. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 15, 10, 10)];
  129. imageView.image = [UIImage imageNamed:@"ic_selected_done_2"];
  130. [view addSubview:imageView];
  131. imageView.hidden = YES;
  132. if (section == _checkIndex) {
  133. imageView.hidden = NO;
  134. }
  135. JXLabel* p = [[JXLabel alloc] initWithFrame:CGRectMake(30, 6, self_width-30-30, 20)];
  136. p.text = _titleArray[section];
  137. p.font = g_factory.font16;
  138. p.backgroundColor = [UIColor clearColor];
  139. p.textColor = [UIColor blackColor];
  140. [view addSubview:p];
  141. JXLabel* detail = [[JXLabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(p.frame)+6, self_width-30-30, 17)];
  142. detail.text = _subTitleArray[section];
  143. detail.font = g_factory.font15;
  144. detail.backgroundColor = [UIColor clearColor];
  145. detail.textColor = [UIColor grayColor];
  146. [view addSubview:detail];
  147. UIImageView *showImageView = [[UIImageView alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH - 40, 15, 20, 20)];
  148. showImageView.image = [UIImage imageNamed:@"ic_selected_done_2"];
  149. [view addSubview:showImageView];
  150. showImageView.hidden = YES;
  151. if (section == 2 || section == 3) {
  152. showImageView.hidden = NO;
  153. }
  154. if (_labelsArray.count > 0) {
  155. if (section == _checkIndex) {
  156. showImageView.image = [UIImage imageNamed:@"pack_up_1"];
  157. }else {
  158. showImageView.image = [UIImage imageNamed:@"room_unfold"];
  159. }
  160. }else {
  161. showImageView.image = [UIImage imageNamed:@"room_unfold"];
  162. }
  163. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 60 - LINE_WH, JX_SCREEN_WIDTH, LINE_WH)];
  164. lineView.backgroundColor = THE_LINE_COLOR;
  165. [view addSubview:lineView];
  166. return view;
  167. }
  168. -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  169. if (section == 2 || section == 3) {
  170. if (_checkIndex == section) {
  171. if (_labelsArray.count > 0) {
  172. return _labelsArray.count + 1;
  173. }else {
  174. return 0;
  175. }
  176. }
  177. }
  178. return 0;
  179. }
  180. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  181. return 60;
  182. }
  183. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  184. if (indexPath.row == _labelsArray.count) {
  185. UITableViewCell *tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"tableViewCell"];
  186. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(60, 6, JX_SCREEN_WIDTH-60-30, 20)];
  187. label.text = Localized(@"JX_FromTheAddressBookSelection");
  188. label.font = [UIFont systemFontOfSize:16.0];
  189. label.textColor = HEXCOLOR(0x576b95);
  190. [tableViewCell.contentView addSubview:label];
  191. NSMutableString *nameStr = [NSMutableString string];
  192. for (NSInteger i = 0; i < _mailListUserArray.count; i ++) {
  193. JXUserObject *user = _mailListUserArray[i];
  194. if (i == 0) {
  195. [nameStr appendString:user.userNickname];
  196. }else {
  197. [nameStr appendFormat:@",%@", user.userNickname];
  198. }
  199. }
  200. label = [[UILabel alloc] initWithFrame:CGRectMake(60, CGRectGetMaxY(label.frame)+6, JX_SCREEN_WIDTH-60-30, 17)];
  201. label.text = nameStr;
  202. label.font = [UIFont systemFontOfSize:15.0];
  203. label.textColor = HEXCOLOR(0x4FC557);
  204. [tableViewCell.contentView addSubview:label];
  205. UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 60 - LINE_WH, JX_SCREEN_WIDTH, LINE_WH)];
  206. lineView.backgroundColor = THE_LINE_COLOR;
  207. [tableViewCell.contentView addSubview:lineView];
  208. return tableViewCell;
  209. }
  210. JXWhoCanSeeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JXWhoCanSeeCell" forIndexPath:indexPath];
  211. JXLabelObject *labelObj = _labelsArray[indexPath.row];
  212. cell.title.text = labelObj.groupName;
  213. cell.index = indexPath.row;
  214. cell.delegate = self;
  215. BOOL flag = NO;
  216. for (NSInteger i = 0; i < _selLabelsArray.count; i ++) {
  217. JXLabelObject *label = _selLabelsArray[i];
  218. if ([labelObj.groupName isEqualToString:label.groupName]) {
  219. flag = YES;
  220. break;
  221. }
  222. }
  223. if (flag) {
  224. cell.contentBtn.selected = YES;
  225. cell.selImageView.image = [UIImage imageNamed:@"sel_check_wx2"];
  226. }else {
  227. cell.contentBtn.selected = NO;
  228. cell.selImageView.image = [UIImage imageNamed:@"sel_nor_wx2"];
  229. }
  230. NSString *userIdStr = labelObj.userIdList;
  231. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  232. if (userIdStr.length <= 0) {
  233. userIds = nil;
  234. }
  235. NSMutableString *userNameStr = [NSMutableString string];
  236. for (NSInteger i = 0; i < userIds.count; i ++) {
  237. NSString *userId = userIds[i];
  238. NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  239. if (i == 0) {
  240. [userNameStr appendFormat:@"%@", userName];
  241. }else {
  242. [userNameStr appendFormat:@", %@", userName];
  243. }
  244. }
  245. cell.userNames.text = userNameStr;
  246. return cell;
  247. }
  248. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  249. if (indexPath.row == _labelsArray.count) {
  250. JXSelectFriendsVC *vc = [[JXSelectFriendsVC alloc] init];
  251. vc.type = JXSelUserTypeSelFriends;
  252. vc.isShowAlert = YES;
  253. vc.alertAction = @selector(selectFriendsAlertAction:);
  254. vc.delegate = self;
  255. vc.didSelect = @selector(selectFriendsDelegate:);
  256. NSMutableSet *set = [NSMutableSet set];
  257. for (NSInteger i = 0; i < self.mailListUserArray.count; i ++) {
  258. JXUserObject *user = self.mailListUserArray[i];
  259. [set addObject:user.userId];
  260. }
  261. NSMutableArray *friends = [[JXUserObject sharedInstance] fetchAllUserFromLocal];
  262. __block NSMutableArray *letterResultArr = [NSMutableArray array];
  263. //排序 Person对象
  264. [BMChineseSort sortAndGroup:friends key:@"userNickname" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  265. if (isSuccess) {
  266. letterResultArr = unGroupArr;
  267. }
  268. }];
  269. // NSMutableArray *letterResultArr = [BMChineseSort sortObjectArray:friends Key:@"userNickname"];
  270. NSMutableSet *numSet = [NSMutableSet set];
  271. for (NSInteger i = 0; i < letterResultArr.count; i ++) {
  272. NSMutableArray *arr = letterResultArr[i];
  273. for (NSInteger j = 0; j < arr.count; j ++) {
  274. JXUserObject *user = arr[j];
  275. if ([set containsObject:user.userId]) {
  276. [numSet addObject:[NSNumber numberWithInteger:i * 1000 + j]];
  277. }
  278. }
  279. }
  280. if (numSet.count > 0) {
  281. vc.set = numSet;
  282. }
  283. vc.existSet = set;
  284. [g_navigation pushViewController:vc animated:YES];
  285. }
  286. // NSArray * cellArray = [_table visibleCells];
  287. // for (UITableViewCell * cell in cellArray) {
  288. // cell.accessoryType = UITableViewCellAccessoryNone;
  289. // }
  290. // _checkIndex = (int)indexPath.row;
  291. // UITableViewCell * cell = [_table cellForRowAtIndexPath:indexPath];
  292. // cell.accessoryType = UITableViewCellAccessoryCheckmark;
  293. // cell.selected = NO;
  294. //
  295. // if (indexPath.row == 2 || indexPath.row == 3) {
  296. // JXSelFriendVC * selVC = [[JXSelFriendVC alloc] init];
  297. // selVC.delegate = self;
  298. // selVC.didSelect = @selector(selFriendsDelegate:);
  299. //
  300. // if (indexPath.row == 2) {
  301. //
  302. // }else if (indexPath.row == 3) {
  303. //
  304. // }
  305. //// [g_window addSubview:selVC.view];
  306. // [g_navigation pushViewController:selVC animated:YES];
  307. // }
  308. }
  309. - (void)selectFriendsDelegate:(JXSelFriendVC *)vc {
  310. [_mailListUserArray removeAllObjects];
  311. for (NSInteger i = 0; i < vc.userIds.count; i ++) {
  312. JXUserObject *user = [[JXUserObject alloc] init];
  313. user.userId = vc.userIds[i];
  314. user.userNickname = vc.userNames[i];
  315. [_mailListUserArray addObject:user];
  316. }
  317. [self.table reloadData];
  318. }
  319. - (void)selectFriendsAlertAction:(JXSelFriendVC *)selFriendVC {
  320. JXLabelObject *label = [[JXLabelObject alloc] init];
  321. label.userIdList = [selFriendVC.userIds componentsJoinedByString:@","];
  322. JXNewLabelVC *vc = [[JXNewLabelVC alloc] init];
  323. vc.title = Localized(@"JX_SettingLabel");
  324. vc.labelObj = label;
  325. [g_navigation pushViewController:vc animated:YES];
  326. }
  327. - (void)whoCanSeeCell:(JXWhoCanSeeCell *)whoCanSeeCell selectAction:(NSInteger)index {
  328. JXLabelObject *labelObj = _labelsArray[index];
  329. JXLabelObject *selObj;
  330. for (JXLabelObject *obj in _selLabelsArray) {
  331. if ([labelObj.groupName isEqualToString:obj.groupName]) {
  332. selObj = obj;
  333. break;
  334. }
  335. }
  336. if (whoCanSeeCell.contentBtn.selected) {
  337. [_selLabelsArray addObject:labelObj];
  338. }else {
  339. [_selLabelsArray removeObject:selObj];
  340. }
  341. }
  342. - (void)whoCanSeeCell:(JXWhoCanSeeCell *)whoCanSeeCell editBtnAction:(NSInteger)index {
  343. JXLabelObject *label = _labelsArray[index];
  344. self.editLabel = label;
  345. JXNewLabelVC *vc = [[JXNewLabelVC alloc] init];
  346. vc.title = Localized(@"JX_SettingLabel");
  347. vc.labelObj = label;
  348. [g_navigation pushViewController:vc animated:YES];
  349. }
  350. - (void) headerBtnAction:(UIButton *)btn {
  351. btn.selected = !btn.selected;
  352. if (_labelsArray.count > 0) {
  353. _labelsArray = nil;
  354. }else {
  355. _labelsArray = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  356. }
  357. if (_checkIndex != btn.tag) {
  358. _checkIndex = (int)btn.tag;
  359. _labelsArray = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  360. [_selLabelsArray removeAllObjects];
  361. [_mailListUserArray removeAllObjects];
  362. }else {
  363. }
  364. for (JXLabelObject *labelObj in _labelsArray) {
  365. NSString *userIdStr = labelObj.userIdList;
  366. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  367. if (userIdStr.length <= 0) {
  368. userIds = nil;
  369. }
  370. NSMutableArray *newUserIds = [userIds mutableCopy];
  371. for (NSInteger i = 0; i < userIds.count; i ++) {
  372. NSString *userId = userIds[i];
  373. NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  374. if (!userName || userName.length <= 0) {
  375. [newUserIds removeObject:userId];
  376. }
  377. }
  378. NSString *string = [newUserIds componentsJoinedByString:@","];
  379. labelObj.userIdList = string;
  380. [labelObj update];
  381. }
  382. [_table reloadData];
  383. }
  384. -(void)finishBtnAction{
  385. [_selArray removeAllObjects];
  386. for (NSInteger i = 0; i < _selLabelsArray.count; i ++) {
  387. JXLabelObject *labelObj = _selLabelsArray[i];
  388. NSArray *arr = [labelObj.userIdList componentsSeparatedByString:@","];
  389. for (NSInteger j = 0; j < arr.count; j ++) {
  390. NSString *userId = arr[j];
  391. JXUserObject *user = [[JXUserObject alloc] init];
  392. user.userId = userId;
  393. BOOL flag = NO;
  394. for (NSInteger m = 0; m < _selArray.count; m ++) {
  395. JXUserObject *selUser = _selArray[m];
  396. if ([userId isEqualToString:selUser.userId]) {
  397. flag = YES;
  398. break;
  399. }
  400. }
  401. if (!flag) {
  402. [_selArray addObject:user];
  403. }
  404. }
  405. }
  406. for (NSInteger i = 0; i < self.mailListUserArray.count; i ++) {
  407. JXUserObject *user = self.mailListUserArray[i];
  408. BOOL flag = NO;
  409. for (NSInteger m = 0; m < _selArray.count; m ++) {
  410. JXUserObject *selUser = _selArray[m];
  411. if ([user.userId isEqualToString:selUser.userId]) {
  412. flag = YES;
  413. break;
  414. }
  415. }
  416. if (!flag) {
  417. [_selArray addObject:user];
  418. }
  419. }
  420. if (self.visibelDelegate && [self.visibelDelegate respondsToSelector:@selector(seeVisibel:userArray:selLabelsArray:mailListArray:)]) {
  421. [self.visibelDelegate seeVisibel:_checkIndex userArray:_selArray selLabelsArray:_selLabelsArray mailListArray:_mailListUserArray];
  422. }
  423. [self actionQuit];
  424. }
  425. -(void)selFriendsDelegate:(JXSelFriendVC*)vc{
  426. NSArray * allArr = vc.array;
  427. NSArray * indexArr = [vc.set allObjects];
  428. NSMutableArray * adduserArr = [NSMutableArray array];
  429. for (NSNumber * index in indexArr) {
  430. JXUserObject * selUser = allArr[[index intValue]];
  431. [adduserArr addObject:selUser];
  432. }
  433. _selArray = [NSMutableArray arrayWithArray:adduserArr];
  434. }
  435. @end