JXNewLabelVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. //
  2. // JXNewLabelVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2018/6/21.
  6. // Copyright © 2018年 Reese. All rights reserved.
  7. //
  8. #import "JXNewLabelVC.h"
  9. #import "JXSelFriendVC.h"
  10. #import "JXSelectFriendsVC.h"
  11. #import "JXCell.h"
  12. #import "BMChineseSort.h"
  13. #import "JXUserInfoVC.h"
  14. #import "JXChatViewController.h"
  15. #define HEIGHT 54
  16. @interface JXNewLabelVC ()
  17. @property (nonatomic, strong) NSMutableArray *array;
  18. @property (nonatomic, strong) UITextField *labelName;
  19. @property (nonatomic, strong) UILabel *labelUserNum;
  20. @end
  21. @implementation JXNewLabelVC
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. self.heightHeader = JX_SCREEN_TOP;
  26. self.heightFooter = 0;
  27. self.isShowFooterPull = NO;
  28. self.isGotoBack = YES;
  29. [self createHeadAndFoot];
  30. // self.tableView.backgroundColor = HEXCOLOR(0xf0eff4);
  31. _array = [NSMutableArray array];
  32. UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeSystem];
  33. doneBtn.layer.masksToBounds = YES;
  34. doneBtn.layer.cornerRadius = 3.f;
  35. [doneBtn setBackgroundColor:THEMECOLOR];
  36. [doneBtn.titleLabel setFont:SYSFONT(15)];
  37. doneBtn.frame = CGRectMake(JX_SCREEN_WIDTH - 51 - 15, JX_SCREEN_TOP - 8 - 29, 51, 29);
  38. [doneBtn setTitle:Localized(@"JX_Confirm") forState:UIControlStateNormal];
  39. [doneBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  40. [doneBtn addTarget:self action:@selector(doneBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  41. [self.tableHeader addSubview:doneBtn];
  42. [self createTableHeaderView];
  43. }
  44. - (void)createTableHeaderView {
  45. UIView *view = [[UIView alloc] init];
  46. UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, JX_SCREEN_WIDTH, 15)];
  47. label.text = Localized(@"JX_LabelName");
  48. label.textColor = HEXCOLOR(0x333333);
  49. label.font = SYSFONT(15);
  50. [view addSubview:label];
  51. UIView *fieldView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(label.frame)+9, JX_SCREEN_WIDTH-30, 40)];
  52. fieldView.backgroundColor = HEXCOLOR(0xF5F5F5);
  53. fieldView.layer.masksToBounds = YES;
  54. fieldView.layer.cornerRadius = 7.f;
  55. [view addSubview:fieldView];
  56. self.labelName = [[UITextField alloc] initWithFrame:CGRectMake(12, 0, fieldView.frame.size.width - 24, fieldView.frame.size.height)];
  57. self.labelName.backgroundColor = [UIColor clearColor];
  58. self.labelName.font = [UIFont systemFontOfSize:16.0];
  59. self.labelName.placeholder = Localized(@"JX_LabelForExample");
  60. if (self.labelObj.groupName.length > 0) {
  61. self.labelName.text = self.labelObj.groupName;
  62. }
  63. [fieldView addSubview:self.labelName];
  64. self.labelUserNum = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(fieldView.frame) + 20, JX_SCREEN_WIDTH-30, 15)];
  65. self.labelUserNum.text = [NSString stringWithFormat:@"%@(0)",Localized(@"JX_LabelMembers")];
  66. self.labelUserNum.textColor = HEXCOLOR(0x333333);
  67. self.labelUserNum.font = SYSFONT(15);
  68. [view addSubview:self.labelUserNum];
  69. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-101-15, CGRectGetMaxY(fieldView.frame) + 20, 101, 15)];
  70. [btn setBackgroundColor:[UIColor whiteColor]];
  71. [btn addTarget:self action:@selector(addFriendAction) forControlEvents:UIControlEventTouchUpInside];
  72. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 0, 15, 15)];
  73. imageView.center = CGPointMake(imageView.center.x, btn.frame.size.height / 2);
  74. imageView.image = [[UIImage imageNamed:@"person_add_green"] imageWithTintColor:THEMECOLOR];
  75. [btn addSubview:imageView];
  76. label = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(imageView.frame) + 10, 0, btn.frame.size.width, btn.frame.size.height)];
  77. label.textColor = THEMECOLOR;
  78. label.text = Localized(@"JX_AddMembers");
  79. label.font = SYSFONT(15);
  80. [btn addSubview:label];
  81. [view addSubview:btn];
  82. view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, CGRectGetMaxY(btn.frame) + 10);
  83. self.tableView.tableHeaderView = view;
  84. NSString *userIdStr = self.labelObj.userIdList;
  85. NSArray *userIds = [userIdStr componentsSeparatedByString:@","];
  86. if (userIdStr.length <= 0) {
  87. userIds = nil;
  88. }
  89. [_array removeAllObjects];
  90. for (NSInteger i = 0; i < userIds.count; i ++) {
  91. JXUserObject *user = [[JXUserObject alloc] init];
  92. user.userId = userIds[i];
  93. // NSString *userName = [JXUserObject getUserNameWithUserId:userIds[i]];
  94. // user.userNickname = userName;
  95. [_array addObject:user];
  96. }
  97. self.labelUserNum.text = [NSString stringWithFormat:@"%@(%ld)",Localized(@"JX_LabelMembers"),_array.count];
  98. [self.tableView reloadData];
  99. }
  100. - (void)addFriendAction {
  101. JXSelectFriendsVC *vc = [[JXSelectFriendsVC alloc] init];
  102. vc.type = JXSelectFriendTypeSelFriends;
  103. vc.delegate = self;
  104. vc.didSelect = @selector(selectFriendsDelegate:);
  105. NSMutableSet *set = [NSMutableSet set];
  106. for (NSInteger i = 0; i < self.array.count; i ++) {
  107. JXUserObject *user = self.array[i];
  108. [set addObject:user.userId];
  109. }
  110. NSMutableArray *friends = [[JXUserObject sharedInstance] fetchAllUserFromLocal];
  111. __block NSMutableArray *letterResultArr = [NSMutableArray array];
  112. //排序 Person对象
  113. [BMChineseSort sortAndGroup:friends key:@"userNickname" finish:^(bool isSuccess, NSMutableArray *unGroupArr, NSMutableArray *sectionTitleArr, NSMutableArray<NSMutableArray *> *sortedObjArr) {
  114. if (isSuccess) {
  115. letterResultArr = unGroupArr;
  116. }
  117. }];
  118. // NSMutableArray *letterResultArr = [BMChineseSort sortObjectArray:friends Key:@"userNickname"];
  119. NSMutableSet *numSet = [NSMutableSet set];
  120. for (NSInteger i = 0; i < letterResultArr.count; i ++) {
  121. NSMutableArray *arr = letterResultArr[i];
  122. for (NSInteger j = 0; j < arr.count; j ++) {
  123. JXUserObject *user = arr[j];
  124. if ([set containsObject:user.userId]) {
  125. [numSet addObject:[NSNumber numberWithInteger:(i + 1) * 100000 + j + 1]];
  126. }
  127. }
  128. }
  129. if (numSet.count > 0) {
  130. vc.set = numSet;
  131. }
  132. vc.existSet = set;
  133. [g_navigation pushViewController:vc animated:YES];
  134. }
  135. - (void)selectFriendsDelegate:(JXSelectFriendsVC *)vc {
  136. [_array removeAllObjects];
  137. for (NSInteger i = 0; i < vc.userIds.count; i ++) {
  138. JXUserObject *user = [[JXUserObject alloc] init];
  139. user.userId = vc.userIds[i];
  140. user.userNickname = vc.userNames[i];
  141. [_array addObject:user];
  142. }
  143. self.labelUserNum.text = [NSString stringWithFormat:@"%@(%ld)",Localized(@"JX_LabelMembers"),_array.count];
  144. [self.tableView reloadData];
  145. }
  146. - (void)doneBtnAction:(UIButton *)btn {
  147. if (self.labelName.text.length <= 0) {
  148. [g_App showAlert:Localized(@"JX_EnterLabelName")];
  149. return;
  150. }
  151. self.labelName.text = [self.labelName.text stringByReplacingOccurrencesOfString:@" " withString:@""];
  152. if (self.labelName.text.length <= 0) {
  153. [JXMyTools showTipView:@"请输入有效的标签名"];
  154. return;
  155. }
  156. // if (self.array.count <= 0) {
  157. // [g_App showAlert:Localized(@"JX_AddMember")];
  158. // return;
  159. // }
  160. if (self.labelObj.groupId.length > 0) {
  161. if (![self.labelName.text isEqualToString:self.labelObj.groupName]) {
  162. [g_server friendGroupUpdate:self.labelObj.groupId groupName:self.labelName.text toView:self];
  163. }
  164. NSMutableString *userIdListStr = [NSMutableString string];
  165. for (NSInteger i = 0; i < self.array.count; i ++) {
  166. JXUserObject *user = self.array[i];
  167. if (i == 0) {
  168. [userIdListStr appendFormat:@"%@", user.userId];
  169. }else {
  170. [userIdListStr appendFormat:@",%@", user.userId];
  171. }
  172. }
  173. JXLabelObject *label = [[JXLabelObject alloc] init];
  174. label.userId = self.labelObj.userId;
  175. label.groupId = self.labelObj.groupId;
  176. label.groupName = self.labelName.text;
  177. label.userIdList = userIdListStr;
  178. [label insert];
  179. [g_server friendGroupUpdateGroupUserList:label.groupId userIdListStr:userIdListStr toView:self];
  180. }else {
  181. [g_server friendGroupAdd:self.labelName.text toView:self];
  182. }
  183. }
  184. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  185. return _array.count;
  186. }
  187. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  188. JXUserObject *user = _array[indexPath.row];
  189. JXCell *cell=nil;
  190. NSString* cellName = @"JXCell";
  191. cell = [tableView dequeueReusableCellWithIdentifier:cellName];
  192. if(cell==nil){
  193. cell = [[JXCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
  194. [_table addToPool:cell];
  195. }
  196. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  197. cell.title = [JXUserObject getUserNameWithUserId:user.userId];
  198. cell.index = (int)indexPath.row;
  199. cell.tag = indexPath.row;
  200. cell.delegate = self;
  201. cell.didTouch = @selector(onHeadImage:);
  202. cell.timeLabel.hidden = YES;
  203. cell.userId = user.userId;
  204. [cell.lbTitle setText:cell.title];
  205. cell.headImageView.tag = indexPath.row;
  206. cell.headImageView.delegate = cell.delegate;
  207. cell.headImageView.didTouch = cell.didTouch;
  208. cell.dataObj = user;
  209. cell.isSmall = YES;
  210. [cell headImageViewImageWithUserId:nil roomId:nil];
  211. return cell;
  212. }
  213. -(void)onHeadImage:(UIView*)sender{
  214. NSMutableArray *array;
  215. array = _array;
  216. JXUserObject *user = [array objectAtIndex:sender.tag];
  217. if([user.userId isEqualToString:FRIEND_CENTER_USERID] || [user.userId isEqualToString:CALL_CENTER_USERID])
  218. return;
  219. JXUserInfoVC* vc = [JXUserInfoVC alloc];
  220. vc.userId = user.userId;
  221. vc.fromAddType = 6;
  222. vc = [vc init];
  223. [g_navigation pushViewController:vc animated:YES];
  224. }
  225. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  226. {
  227. return 59;
  228. }
  229. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  230. JXUserObject *userObj = [_array objectAtIndex:indexPath.row];
  231. userObj = [[JXUserObject sharedInstance] getUserById:userObj.userId];
  232. if ([userObj.status intValue] == -1) {
  233. [JXMyTools showTipView:@"此用户已被拉入黑名单"];
  234. return;
  235. }
  236. JXChatViewController *sendView=[JXChatViewController alloc];
  237. sendView.scrollLine = 0;
  238. sendView.title = userObj.userNickname;
  239. sendView.chatPerson = userObj;
  240. sendView = [sendView init];
  241. // [g_App.window addSubview:sendView.view];
  242. [g_navigation pushViewController:sendView animated:YES];
  243. sendView.view.hidden = NO;
  244. }
  245. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
  246. return YES;
  247. }
  248. - (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath {
  249. UITableViewRowAction *deleteBtn = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:Localized(@"JX_Delete") handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
  250. JXUserObject *user = _array[indexPath.row];
  251. [_array removeObject:user];
  252. [_table reloadData];
  253. }];
  254. return @[deleteBtn];
  255. }
  256. //服务器返回数据
  257. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  258. [_wait stop];
  259. if ([aDownload.action isEqualToString:act_FriendGroupAdd] || [aDownload.action isEqualToString:act_FriendGroupUpdate]) {
  260. NSMutableString *userIdListStr = [NSMutableString string];
  261. for (NSInteger i = 0; i < self.array.count; i ++) {
  262. JXUserObject *user = self.array[i];
  263. if (i == 0) {
  264. [userIdListStr appendFormat:@"%@", user.userId];
  265. }else {
  266. [userIdListStr appendFormat:@",%@", user.userId];
  267. }
  268. }
  269. JXLabelObject *label = [[JXLabelObject alloc] init];
  270. if (dict) {
  271. label.userId = dict[@"userId"];
  272. label.groupId = dict[@"groupId"];
  273. label.groupName = dict[@"groupName"];
  274. }else {
  275. label.userId = self.labelObj.userId;
  276. label.groupId = self.labelObj.groupId;
  277. label.groupName = self.labelName.text;
  278. }
  279. label.userIdList = userIdListStr;
  280. [label insert];
  281. [g_server friendGroupUpdateGroupUserList:label.groupId userIdListStr:userIdListStr toView:self];
  282. }
  283. if ([aDownload.action isEqualToString:act_FriendGroupUpdateGroupUserList]) {
  284. [g_notify postNotificationName:kLabelVCRefreshNotif object:nil];
  285. [self actionQuit];
  286. }
  287. }
  288. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  289. [_wait hide];
  290. return show_error;
  291. }
  292. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  293. [_wait hide];
  294. return show_error;
  295. }
  296. -(void) didServerConnectStart:(JXConnection*)aDownload{
  297. [_wait start];
  298. }
  299. - (void)didReceiveMemoryWarning {
  300. [super didReceiveMemoryWarning];
  301. // Dispose of any resources that can be recreated.
  302. }
  303. /*
  304. #pragma mark - Navigation
  305. // In a storyboard-based application, you will often want to do a little preparation before navigation
  306. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  307. // Get the new view controller using [segue destinationViewController].
  308. // Pass the selected object to the new view controller.
  309. }
  310. */
  311. @end