JXLabelVC.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. //
  2. // JXLabelVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2018/6/21.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXLabelVC.h"
  9. #import "JXLabelObject.h"
  10. #import "JXNewLabelVC.h"
  11. #import "QBPopupMenu.h"
  12. #define HEIGHT 60
  13. @interface JXLabelVC ()
  14. @property (nonatomic, strong) NSMutableArray *array;
  15. @property (nonatomic, strong) UIView *emptyView;
  16. @property (nonatomic, strong) JXLabelObject *currentLabelObj;
  17. @property (nonatomic, strong) QBPopupMenu *popupMenu;
  18. @end
  19. @implementation JXLabelVC
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view.
  23. self.title = Localized(@"JX_Label");
  24. self.heightHeader = JX_SCREEN_TOP;
  25. self.heightFooter = 0;
  26. self.isGotoBack = YES;
  27. // self.isShowFooterPull = NO;
  28. [self createHeadAndFoot];
  29. _array = [NSMutableArray array];
  30. _array = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  31. if (_array <= 0) {
  32. // [self scrollToPageUp];
  33. }
  34. // for (JXLabelObject *labelObj in _array) {
  35. // NSString *userIdStr = labelObj.userIdList;
  36. // NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  37. // if (userIdStr.length <= 0) {
  38. // userIds = nil;
  39. // }
  40. //
  41. // NSMutableArray *newUserIds = [userIds mutableCopy];
  42. // for (NSInteger i = 0; i < userIds.count; i ++) {
  43. // NSString *userId = userIds[i];
  44. // NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  45. //
  46. // if (!userName || userName.length <= 0) {
  47. // [newUserIds removeObject:userId];
  48. // }
  49. //
  50. // }
  51. // NSString *string = [newUserIds componentsJoinedByString:@","];
  52. //
  53. // labelObj.userIdList = string;
  54. // [labelObj update];
  55. // }
  56. // [self.tableView reloadData];
  57. // if (!_array || _array.count <= 0) {
  58. // [self.view insertSubview:self.emptyView aboveSubview:self.tableView];
  59. // self.emptyView.hidden = NO;
  60. // }else {
  61. // self.emptyView.hidden = YES;
  62. // }
  63. NSMutableArray *items = [NSMutableArray array];
  64. QBPopupMenuItem *item1 = [QBPopupMenuItem itemWithTitle:[NSString stringWithFormat:@" %@ ", Localized(@"JX_Delete")] target:self action:@selector(deleteAction)];
  65. [items addObject:item1];
  66. QBPopupMenu *popupMenu = [[QBPopupMenu alloc] initWithItems:items];
  67. popupMenu.highlightedColor = [UIColor lightGrayColor];
  68. popupMenu.height = 35;
  69. self.popupMenu = popupMenu;
  70. [self customView];
  71. [g_notify addObserver:self selector:@selector(refreshNotif:) name:kLabelVCRefreshNotif object:nil];
  72. // [g_notify addObserver:self selector:@selector(updateLabels:) name:kXMPPMessageUpadtePasswordNotification object:nil];
  73. [g_notify addObserver:self selector:@selector(refreshNotif:) name:kOfflineOperationUpdateLabelList object:nil];
  74. }
  75. //- (void)updateLabels:(NSNotification *)noti {
  76. // JXMessageObject *msg = noti.object;
  77. // if ([msg.objectId isEqualToString:SYNC_LABEL]) {
  78. // // 同步标签
  79. // [g_server friendGroupListToView:self];
  80. // }
  81. //}
  82. //- (void)scrollToPageUp {
  83. // [self stopLoading];
  84. // [self refreshNotif:nil];
  85. //}
  86. #pragma mark langPress 长按手势事件
  87. -(void)longPress:(UILongPressGestureRecognizer *)sender{
  88. if (sender.state == UIGestureRecognizerStateBegan) {
  89. UIView *view = sender.view;
  90. _currentLabelObj = _array[view.tag];
  91. [self.popupMenu showInView:self.tableBody targetRect:CGRectMake(view.frame.origin.x, view.frame.origin.y, 60, 35) animated:YES];
  92. }
  93. }
  94. - (void)deleteAction {
  95. [g_server friendGroupDelete:_currentLabelObj.groupId toView:self];
  96. }
  97. - (void)customView {
  98. for (UIView *subV in self.tableBody.subviews) {
  99. [subV removeFromSuperview];
  100. }
  101. UIButton *btn;
  102. CGFloat w = 108;
  103. CGFloat h = 40;
  104. int inset = (JX_SCREEN_WIDTH-w*3)/4;
  105. for (int i = 0; i < _array.count+1; i++) {
  106. CGFloat x = (w+inset)*(i % 3)+inset;
  107. int m = i / 3;
  108. NSString *title;
  109. if (i < _array.count) {
  110. JXLabelObject *label = _array[i];
  111. NSString *userIdStr = label.userIdList;
  112. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  113. if (userIdStr.length <= 0) {
  114. userIds = nil;
  115. }
  116. title = [NSString stringWithFormat:@"%@ (%ld)",label.groupName, userIds.count];
  117. }
  118. btn = [self createButtonWihtFrame:CGRectMake(x, m*41+(15 * (m +1)), w, h) title:title index:i];
  119. }
  120. self.tableBody.contentSize = CGSizeMake(0, CGRectGetMaxY(btn.frame)+10);
  121. // UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, HEIGHT)];
  122. // [btn setBackgroundColor:[UIColor whiteColor]];
  123. // [btn addTarget:self action:@selector(createLabelAction) forControlEvents:UIControlEventTouchUpInside];
  124. // UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 15, 15)];
  125. // imageView.center = CGPointMake(imageView.center.x, HEIGHT / 2);
  126. // imageView.image = [[UIImage imageNamed:@"person_add_green"] imageWithTintColor:THEMECOLOR];
  127. // [btn addSubview:imageView];
  128. // UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 0, btn.frame.size.width, btn.frame.size.height)];
  129. // label.textColor = THEMECOLOR;
  130. // label.text = Localized(@"JX_NewLabel");
  131. // label.font = [UIFont systemFontOfSize:16.0];
  132. // [btn addSubview:label];
  133. //
  134. // UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - .5, JX_SCREEN_WIDTH, .5)];
  135. // line.backgroundColor = HEXCOLOR(0xdcdcdc);
  136. // [btn addSubview:line];
  137. // self.tableView.tableHeaderView = btn;
  138. }
  139. - (void)refreshNotif:(NSNotification *)notif {
  140. // 同步标签
  141. [g_server friendGroupListToView:self];
  142. }
  143. - (UIView *)emptyView {
  144. if (!_emptyView) {
  145. _emptyView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  146. _emptyView.backgroundColor = [UIColor whiteColor];
  147. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, JX_SCREEN_WIDTH, 20)];
  148. label.font = [UIFont systemFontOfSize:16.0];
  149. label.textColor = [UIColor grayColor];
  150. label.text = Localized(@"JX_NoLabel");
  151. label.textAlignment = NSTextAlignmentCenter;
  152. [_emptyView addSubview:label];
  153. label = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(label.frame) + 10, JX_SCREEN_WIDTH, 20)];
  154. label.font = [UIFont systemFontOfSize:15.0];
  155. label.textColor = [UIColor grayColor];
  156. label.text = Localized(@"JX_LabelFindContacts");
  157. label.textAlignment = NSTextAlignmentCenter;
  158. [_emptyView addSubview:label];
  159. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(label.frame) + 150, JX_SCREEN_WIDTH - 40, 50)];
  160. [btn setTitle:Localized(@"JX_NewLabel") forState:UIControlStateNormal];
  161. [btn setBackgroundColor:THEMECOLOR];
  162. [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  163. btn.layer.cornerRadius = 3.0;
  164. btn.layer.masksToBounds = 0;
  165. [btn addTarget:self action:@selector(createLabelAction) forControlEvents:UIControlEventTouchUpInside];
  166. [_emptyView addSubview:btn];
  167. }
  168. return _emptyView;
  169. }
  170. - (void)createLabelAction {
  171. JXNewLabelVC *vc = [[JXNewLabelVC alloc] init];
  172. vc.title = Localized(@"JX_NewLabel");
  173. [g_navigation pushViewController:vc animated:YES];
  174. }
  175. //- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  176. // return _array.count;
  177. //}
  178. //
  179. //- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  180. //
  181. // JXLabelObject *label = _array[indexPath.row];
  182. // NSString *userIdStr = label.userIdList;
  183. // NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  184. // if (userIdStr.length <= 0) {
  185. // userIds = nil;
  186. // }
  187. //
  188. // NSMutableString *userNameStr = [NSMutableString string];
  189. // for (NSInteger i = 0; i < userIds.count; i ++) {
  190. // NSString *userId = userIds[i];
  191. // NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  192. // if (i == 0) {
  193. // [userNameStr appendFormat:@"%@", userName];
  194. // }else {
  195. // [userNameStr appendFormat:@", %@", userName];
  196. // }
  197. //
  198. // }
  199. //
  200. // UITableViewCell *cell=nil;
  201. // NSString* cellName = @"labelCell";
  202. // cell = [tableView dequeueReusableCellWithIdentifier:cellName];
  203. //
  204. // if(cell==nil){
  205. //
  206. // cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellName];
  207. // [_table addToPool:cell];
  208. //
  209. // }
  210. //
  211. // cell.selectionStyle = UITableViewCellSelectionStyleNone;
  212. // cell.textLabel.text = [NSString stringWithFormat:@"%@ (%ld)",label.groupName, userIds.count];
  213. // cell.textLabel.textColor = [UIColor blackColor];
  214. // cell.textLabel.font = [UIFont systemFontOfSize:16.0];
  215. //
  216. // cell.detailTextLabel.text = userNameStr;
  217. // cell.detailTextLabel.textColor = [UIColor grayColor];
  218. // cell.detailTextLabel.font= [UIFont systemFontOfSize:14.0];
  219. //
  220. // UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - .5, JX_SCREEN_WIDTH, .5)];
  221. // view.backgroundColor = HEXCOLOR(0xdcdcdc);
  222. // [cell addSubview:view];
  223. //
  224. // return cell;
  225. //}
  226. //
  227. //-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  228. //{
  229. // return HEIGHT;
  230. //}
  231. //
  232. //- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  233. // JXLabelObject *label = _array[indexPath.row];
  234. // JXNewLabelVC *vc = [[JXNewLabelVC alloc] init];
  235. // vc.title = Localized(@"JX_SettingLabel");
  236. // vc.labelObj = label;
  237. // [g_navigation pushViewController:vc animated:YES];
  238. //}
  239. //
  240. //
  241. //-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  242. //
  243. // return YES;
  244. //}
  245. //
  246. //- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  247. //
  248. // UITableViewRowAction *deleteBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:Localized(@"JX_Delete") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  249. // JXLabelObject *labelObj = _array[indexPath.row];
  250. // _currentLabelObj = labelObj;
  251. // [g_server friendGroupDelete:labelObj.groupId toView:self];
  252. // }];
  253. //
  254. // return @[deleteBtn];
  255. //
  256. //}
  257. - (void)onDidLabel:(UIButton *)button {
  258. if (button.tag == _array.count) {
  259. JXNewLabelVC *vc = [[JXNewLabelVC alloc] init];
  260. vc.title = Localized(@"JX_NewLabel");
  261. [g_navigation pushViewController:vc animated:YES];
  262. }else {
  263. JXLabelObject *label = _array[button.tag];
  264. JXNewLabelVC *vc = [[JXNewLabelVC alloc] init];
  265. vc.title = Localized(@"JX_SettingLabel");
  266. vc.labelObj = label;
  267. [g_navigation pushViewController:vc animated:YES];
  268. }
  269. }
  270. //服务器返回数据
  271. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  272. [_wait stop];
  273. if ([aDownload.action isEqualToString:act_FriendGroupDelete]) {
  274. [_currentLabelObj delete];
  275. [_array removeObject:_currentLabelObj];
  276. [self customView];
  277. // [self.tableView reloadData];
  278. }
  279. // 同步标签
  280. if ([aDownload.action isEqualToString:act_FriendGroupList]) {
  281. for (NSInteger i = 0; i < array1.count; i ++) {
  282. NSDictionary *dict = array1[i];
  283. JXLabelObject *labelObj = [[JXLabelObject alloc] init];
  284. labelObj.groupId = dict[@"groupId"];
  285. labelObj.groupName = dict[@"groupName"];
  286. labelObj.userId = dict[@"userId"];
  287. NSArray *userIdList = dict[@"userIdList"];
  288. NSString *userIdListStr = [userIdList componentsJoinedByString:@","];
  289. if (userIdListStr.length > 0) {
  290. labelObj.userIdList = [NSString stringWithFormat:@"%@", userIdListStr];
  291. }
  292. [labelObj insert];
  293. }
  294. // 删除服务器上已经删除的
  295. NSArray *arr = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  296. for (NSInteger i = 0; i < arr.count; i ++) {
  297. JXLabelObject *locLabel = arr[i];
  298. BOOL flag = NO;
  299. for (NSInteger j = 0; j < array1.count; j ++) {
  300. NSDictionary * dict = array1[j];
  301. if ([locLabel.groupId isEqualToString:dict[@"groupId"]]) {
  302. flag = YES;
  303. break;
  304. }
  305. }
  306. if (!flag) {
  307. [locLabel delete];
  308. }
  309. }
  310. _array = [[JXLabelObject sharedInstance] fetchAllLabelsFromLocal];
  311. // for (JXLabelObject *labelObj in _array) {
  312. // NSString *userIdStr = labelObj.userIdList;
  313. // NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  314. // if (userIdStr.length <= 0) {
  315. // userIds = nil;
  316. // }
  317. //
  318. // NSMutableArray *newUserIds = [userIds mutableCopy];
  319. // for (NSInteger i = 0; i < userIds.count; i ++) {
  320. // NSString *userId = userIds[i];
  321. // NSString *userName = [JXUserObject getUserNameWithUserId:userId];
  322. //
  323. // if (!userName || userName.length <= 0) {
  324. // [newUserIds removeObject:userId];
  325. // }
  326. //
  327. // }
  328. //
  329. // NSString *string = [newUserIds componentsJoinedByString:@","];
  330. //
  331. // labelObj.userIdList = string;
  332. //
  333. // [labelObj update];
  334. // }
  335. [self customView];
  336. // if (!_array || _array.count <= 0) {
  337. // self.emptyView.hidden = NO;
  338. // }else {
  339. // self.emptyView.hidden = YES;
  340. // }
  341. // [self.tableView reloadData];
  342. }
  343. }
  344. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  345. [_wait hide];
  346. return show_error;
  347. }
  348. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  349. [_wait hide];
  350. return show_error;
  351. }
  352. -(void) didServerConnectStart:(JXConnection*)aDownload{
  353. [_wait start];
  354. }
  355. - (void)didReceiveMemoryWarning {
  356. [super didReceiveMemoryWarning];
  357. // Dispose of any resources that can be recreated.
  358. }
  359. - (UIButton *)createButtonWihtFrame:(CGRect)frame title:(NSString *)title index:(NSInteger)index {
  360. UIButton *btn = [[UIButton alloc] initWithFrame:frame];
  361. [btn setTag:index];
  362. btn.layer.cornerRadius = 7.f;
  363. btn.layer.masksToBounds = YES;
  364. btn.layer.borderColor = THEMECOLOR.CGColor;
  365. btn.layer.borderWidth = 1.f;
  366. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, btn.frame.size.width-20, btn.frame.size.height)];
  367. label.text = title;
  368. label.backgroundColor = [UIColor clearColor];
  369. label.textColor = HEXCOLOR(0x333333);
  370. label.textAlignment = NSTextAlignmentCenter;
  371. label.lineBreakMode = NSLineBreakByTruncatingMiddle;
  372. label.font = SYSFONT(15);
  373. [btn addSubview:label];
  374. if (index == _array.count) {
  375. UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width/2-17/2, frame.size.height/2-17/2, 17, 17)];
  376. imgV.image = [[UIImage imageNamed:@"groupHelper_add"] imageWithTintColor:HEXCOLOR(0x333333)];
  377. [btn addSubview:imgV];
  378. }else {
  379. UILongPressGestureRecognizer *press = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
  380. press.minimumPressDuration = 1;
  381. [btn addGestureRecognizer:press];
  382. }
  383. [btn addTarget:self action:@selector(onDidLabel:) forControlEvents:UIControlEventTouchUpInside];
  384. [self.tableBody addSubview:btn];
  385. return btn;
  386. }
  387. @end