JXSetNoteAndLabelVC.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. //
  2. // JXSetNoteAndLabelVC.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 2019/5/7.
  6. // Copyright © 2019年 Reese. All rights reserved.
  7. //
  8. #import "JXSetNoteAndLabelVC.h"
  9. #import "JXLabelObject.h"
  10. #import "JXSetLabelVC.h"
  11. #import "UIImage+Color.h"
  12. #define HEIGHT 54
  13. @interface JXSetNoteAndLabelVC () <UIScrollViewDelegate,UITextViewDelegate,UITextFieldDelegate>
  14. @property (nonatomic, strong) UITextField *name; //备注
  15. @property (nonatomic, strong) UITextField *textField; //标签
  16. @property (nonatomic, strong) UITextView *detail; //描述
  17. @property (nonatomic, strong) UILabel *labT;
  18. @property (nonatomic, strong) UILabel *labContLab;
  19. @property (nonatomic, strong) UIView *baseView;
  20. @property (nonatomic, strong) UIColor *textVColor;
  21. @property (nonatomic, strong) NSMutableArray *array; // 已选择标签
  22. @property (nonatomic, strong) NSMutableArray *allArray; // 所有标签
  23. @property (nonatomic, strong) UILabel *watermarkLab;// 水印
  24. @end
  25. @implementation JXSetNoteAndLabelVC
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.heightHeader = JX_SCREEN_TOP;
  29. self.heightFooter = 0;
  30. self.isGotoBack = YES;
  31. [self createHeadAndFoot];
  32. self.tableBody.delegate = self;
  33. self.textVColor = HEXCOLOR(0xC4C4CA);
  34. // self.tableBody.backgroundColor = HEXCOLOR(0xf0eff4);
  35. // _array = [NSMutableArray array];
  36. // _allArray = [NSMutableArray array];
  37. [self customView];
  38. }
  39. - (void)customView {
  40. JXLabel *p = [self createLabel:self.tableHeader default:Localized(@"JX_Confirm") selector:@selector(onSave)];
  41. p.textColor = [UIColor whiteColor];
  42. p.layer.masksToBounds = YES;
  43. p.layer.cornerRadius = 3.f;
  44. [p setBackgroundColor:THEMECOLOR];
  45. [p setFont:SYSFONT(15)];
  46. p.textAlignment = NSTextAlignmentCenter;
  47. p.frame = CGRectMake(JX_SCREEN_WIDTH - 51 - 15, JX_SCREEN_TOP - 8 - 29, 51, 29);
  48. // 备注
  49. UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(15, INSETS, 60, 18)];
  50. nameLab.text = Localized(@"JX_MemoName");
  51. nameLab.textColor = HEXCOLOR(0x333333);
  52. nameLab.font = SYSFONT(15);
  53. [self.tableBody addSubview:nameLab];
  54. _name = [[UITextField alloc] initWithFrame:CGRectMake(15,CGRectGetMaxY(nameLab.frame)+INSETS,JX_SCREEN_WIDTH-30,40)];
  55. _name.placeholder = Localized(@"JX_AddRemarkName");
  56. _name.font = [UIFont systemFontOfSize:15.0];
  57. _name.delegate = self;
  58. _name.returnKeyType = UIReturnKeyDone;
  59. _name.backgroundColor = HEXCOLOR(0xF5F5F5);
  60. _name.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 0)];
  61. _name.leftViewMode = UITextFieldViewModeAlways;
  62. _name.layer.masksToBounds = YES;
  63. _name.layer.cornerRadius = 7.f;
  64. _name.text = _user.remarkName;
  65. [self.tableBody addSubview:_name];
  66. // 标签
  67. _labT = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_name.frame)+20, 40, 18)];
  68. _labT.text = Localized(@"JX_Label");
  69. _labT.textColor = HEXCOLOR(0x333333);
  70. _labT.font = SYSFONT(15);
  71. [self.tableBody addSubview:_labT];
  72. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(_labT.frame)+INSETS, JX_SCREEN_WIDTH-30, 40)];
  73. btn.backgroundColor = THESIMPLESTYLE ? [UIColor blackColor] : [UIColor whiteColor];
  74. btn.layer.masksToBounds = YES;
  75. btn.layer.cornerRadius = 7.f;
  76. [btn setBackgroundImage:[UIImage createImageWithColor:HEXCOLOR(0xF5F5F5)] forState:UIControlStateNormal];
  77. [btn setBackgroundImage:[UIImage createImageWithColor:self.textVColor] forState:UIControlStateHighlighted];
  78. [btn addTarget:self action:@selector(onLabel) forControlEvents:UIControlEventTouchUpInside];
  79. [self.tableBody addSubview:btn];
  80. _labContLab = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, JX_SCREEN_WIDTH-INSETS-41, 40)];
  81. _labContLab.backgroundColor = [UIColor clearColor];
  82. _labContLab.textColor = HEXCOLOR(0x333333);
  83. _labContLab.font = SYSFONT(15);
  84. // _labContLab.userInteractionEnabled = YES;
  85. [btn addSubview:_labContLab];
  86. [self getLab];
  87. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onLabel)];
  88. // [_labContLab addGestureRecognizer:tap];
  89. // UIImageView *imgV =[[UIImageView alloc] initWithFrame:CGRectMake(btn.frame.size.width-15-7, 13.5, 7, 13)];
  90. // imgV.image = [UIImage imageNamed:@"new_icon_>"];
  91. // [btn addSubview:imgV];
  92. // // 描述
  93. // UILabel *deatilLab = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(btn.frame)+20, 40, 18)];
  94. // deatilLab.text = Localized(@"JX_UserInfoDescribe");
  95. // deatilLab.textColor = HEXCOLOR(0x333333);
  96. // deatilLab.font = SYSFONT(15);
  97. // [self.tableBody addSubview:deatilLab];
  98. //
  99. // _baseView = [[UIView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(deatilLab.frame)+INSETS, JX_SCREEN_WIDTH-30, 40)];
  100. // _baseView.backgroundColor = HEXCOLOR(0xF5F5F5);
  101. // _baseView.layer.masksToBounds = YES;
  102. // _baseView.layer.cornerRadius = 7.f;
  103. // [self.tableBody addSubview:_baseView];
  104. //
  105. // _detail = [self createTextField:_baseView default:_user.remarkName];
  106. // CGSize sizeD = [_detail sizeThatFits:CGSizeMake(_detail.frame.size.width, MAXFLOAT)];
  107. // _detail.frame = CGRectMake(15,3,_baseView.frame.size.width-30,sizeD.height);
  108. // _detail.text = _user.describe.length > 0 ? _user.describe : @"";
  109. // _detail.returnKeyType = UIReturnKeyDefault;
  110. //
  111. //水印
  112. // _watermarkLab = [[UILabel alloc] initWithFrame:CGRectMake(5, (_detail.frame.size.height-20)/2, _detail.frame.size.width-10, 20)];
  113. // _watermarkLab.text = Localized(@"JX_AddMoreComments");
  114. // _watermarkLab.textColor = self.textVColor;
  115. // _watermarkLab.font = SYSFONT(15);
  116. // [_detail addSubview:_watermarkLab];
  117. //
  118. // if (_user.describe.length > 0) {
  119. // [self textViewDidChange:_detail];
  120. // }
  121. //
  122. // if ([self validateCellPhoneNumber:[self getNumber:_detail.text]]) {
  123. // NSMutableAttributedString *atbs =[[NSMutableAttributedString alloc] initWithAttributedString: _detail.attributedText];
  124. // NSRange range = [[atbs string] rangeOfString:[self getNumber:_detail.text]];
  125. // [atbs addAttributes:@{NSLinkAttributeName:[self getNumber:_detail.text],NSForegroundColorAttributeName:[UIColor redColor]} range:range];
  126. // _detail.attributedText= atbs;
  127. // _detail.selectable=YES;
  128. // }
  129. }
  130. - (void)getLab {
  131. NSMutableArray *array = [[JXLabelObject sharedInstance] fetchLabelsWithUserId:self.user.userId];
  132. NSMutableString *labelsName = [NSMutableString string];
  133. for (NSInteger i = 0; i < array.count; i ++) {
  134. JXLabelObject *labelObj = array[i];
  135. if (i == 0) {
  136. [labelsName appendString:labelObj.groupName];
  137. }else {
  138. [labelsName appendFormat:@",%@",labelObj.groupName];
  139. }
  140. }
  141. if (labelsName.length > 0) {
  142. _labContLab.text = labelsName;
  143. _labContLab.textColor = HEXCOLOR(0x333333);
  144. }else {
  145. _labContLab.text = Localized(@"JX_ClassifyContactsByLabel");
  146. _labContLab.textColor = self.textVColor;
  147. }
  148. }
  149. - (void)onLabel {
  150. JXSetLabelVC *vc = [[JXSetLabelVC alloc] init];
  151. vc.title = Localized(@"JX_SettingLabel");
  152. vc.delegate = self;
  153. vc.didSelect = @selector(refreshLabel:);
  154. vc.array = _array;
  155. vc.allArray = _allArray;
  156. vc.user = self.user;
  157. [g_navigation pushViewController:vc animated:YES];
  158. }
  159. - (void)refreshLabel:(JXSetLabelVC *)vc {
  160. [self getLab];
  161. _array = [vc.array mutableCopy];
  162. _allArray = [vc.allArray mutableCopy];
  163. NSMutableString *labelsName = [NSMutableString string];
  164. for (NSInteger i = 0; i < _array.count; i ++) {
  165. JXLabelObject *labelObj = _array[i];
  166. if (i == 0) {
  167. [labelsName appendString:labelObj.groupName];
  168. }else {
  169. [labelsName appendFormat:@",%@",labelObj.groupName];
  170. }
  171. }
  172. if (labelsName.length > 0) {
  173. _labContLab.text = labelsName;
  174. _labContLab.textColor = HEXCOLOR(0x333333);
  175. }else {
  176. _labContLab.text = Localized(@"JX_ClassifyContactsByLabel");
  177. _labContLab.textColor = self.textVColor;
  178. }
  179. }
  180. // 确定按钮
  181. - (void)onSave {
  182. BOOL flag = NO;
  183. for (NSInteger i = 0; i < _array.count; i ++) {
  184. JXLabelObject *labelObj = _array[i];
  185. // 添加输入框输入的新创建的标签
  186. if (!labelObj.groupId) {
  187. flag = YES;
  188. [g_server friendGroupAdd:labelObj.groupName toView:self];
  189. }
  190. }
  191. // 没有新创建的标签,直接更新已存在标签
  192. if (!flag) {
  193. NSMutableString *userIdListStr = [NSMutableString string];
  194. for (NSInteger i = 0; i < _array.count; i ++) {
  195. JXLabelObject *obj = _array[i];
  196. if (i == 0) {
  197. [userIdListStr appendFormat:@"%@", obj.groupId];
  198. }else {
  199. [userIdListStr appendFormat:@",%@", obj.groupId];
  200. }
  201. }
  202. [g_server friendGroupUpdateFriendToUserId:self.user.userId groupIdStr:userIdListStr toView:self];
  203. }
  204. }
  205. -(BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
  206. if ([self validateCellPhoneNumber:textView.text]) {
  207. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",textView.text]]];
  208. }
  209. return YES;
  210. }
  211. - (void)textViewDidChange:(UITextView *)textView {
  212. _watermarkLab.hidden = textView.text.length > 0;
  213. static CGFloat maxHeight = 70.0f;
  214. //防止输入时在中文后输入英文过长直接中文和英文换行
  215. CGRect frame = textView.frame;
  216. CGSize constraintSize = CGSizeMake(JX_SCREEN_WIDTH-14, MAXFLOAT);
  217. CGSize size = [textView sizeThatFits:constraintSize];
  218. if (size.height >= maxHeight)
  219. {
  220. size.height = maxHeight;
  221. textView.scrollEnabled = YES; // 允许滚动
  222. }
  223. else
  224. {
  225. textView.scrollEnabled = NO; // 不允许滚动
  226. }
  227. textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
  228. _baseView.frame = CGRectMake(_baseView.frame.origin.x, _baseView.frame.origin.y, _baseView.frame.size.width, size.height+6);
  229. }
  230. //服务器返回数据
  231. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  232. [_wait stop];
  233. if ([aDownload.action isEqualToString:act_FriendGroupAdd]) {
  234. NSMutableString *userIdListStr = [NSMutableString stringWithFormat:@"%@", self.user.userId];
  235. // 添加新标签后更新标签的用户列表
  236. [g_server friendGroupUpdateGroupUserList:dict[@"groupId"] userIdListStr:userIdListStr toView:self];
  237. JXLabelObject *label = [[JXLabelObject alloc] init];
  238. if (dict) {
  239. label.userId = dict[@"userId"];
  240. label.groupId = dict[@"groupId"];
  241. label.groupName = dict[@"groupName"];
  242. }
  243. label.userIdList = userIdListStr;
  244. // 插入新创建的标签
  245. [label insert];
  246. JXLabelObject *lastObj;
  247. // 查找到新创建的标签的最后一个
  248. for (NSInteger i = _array.count - 1; i >= 0; i --) {
  249. JXLabelObject *obj = _array[i];
  250. if (!obj.groupId) {
  251. lastObj = obj;
  252. break;
  253. }
  254. }
  255. // 更新新创建的标签的其他字段
  256. for (JXLabelObject *labelObj in _array) {
  257. if ([label.groupName isEqualToString:labelObj.groupName]) {
  258. labelObj.groupId = label.groupId;
  259. labelObj.userId = label.userId;
  260. labelObj.userIdList = label.userIdList;
  261. break;
  262. }
  263. }
  264. // 如果接口已成功添加完最后一条标签后,再更新用户的标签列表
  265. if ([label.groupName isEqualToString:lastObj.groupName]) {
  266. NSMutableString *userIdListStr = [NSMutableString string];
  267. for (NSInteger i = 0; i < _array.count; i ++) {
  268. JXLabelObject *obj = _array[i];
  269. if (i == 0) {
  270. [userIdListStr appendFormat:@"[%@", obj.groupId];
  271. }else if (i == self.array.count - 1) {
  272. [userIdListStr appendFormat:@",%@]", obj.groupId];
  273. }else {
  274. [userIdListStr appendFormat:@",%@", obj.groupId];
  275. }
  276. }
  277. [g_server friendGroupUpdateFriendToUserId:self.user.userId groupIdStr:userIdListStr toView:self];
  278. }
  279. }
  280. if ([aDownload.action isEqualToString:act_FriendGroupUpdateFriend]) {
  281. // 更新数据库
  282. for (JXLabelObject *labelObj in _allArray) {
  283. [labelObj update];
  284. }
  285. self.user.remarkName = _name.text;
  286. if (_detail.textColor != self.textVColor) {
  287. self.user.describe = _detail.text;
  288. }else {
  289. self.user.describe = nil;
  290. }
  291. if ([self.delegate respondsToSelector:self.didSelect]) {
  292. [self.delegate performSelectorOnMainThread:self.didSelect withObject:self.user waitUntilDone:NO];
  293. }
  294. [self actionQuit];
  295. }
  296. }
  297. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  298. [_wait hide];
  299. return show_error;
  300. }
  301. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  302. [_wait hide];
  303. return show_error;
  304. }
  305. -(void) didServerConnectStart:(JXConnection*)aDownload{
  306. [_wait start];
  307. }
  308. -(UITextView*)createTextField:(UIView*)parent default:(NSString*)s {
  309. UITextView* p = [[UITextView alloc] init];
  310. p.delegate = self;
  311. p.autocorrectionType = UITextAutocorrectionTypeNo;
  312. p.autocapitalizationType = UITextAutocapitalizationTypeNone;
  313. p.enablesReturnKeyAutomatically = YES;
  314. p.scrollEnabled = NO;
  315. p.returnKeyType = UIReturnKeyDone;
  316. p.showsVerticalScrollIndicator = NO;
  317. p.showsHorizontalScrollIndicator = NO;
  318. p.textAlignment = NSTextAlignmentLeft;
  319. p.userInteractionEnabled = YES;
  320. p.backgroundColor = HEXCOLOR(0xF5F5F5);
  321. // p.layer.masksToBounds = YES;
  322. // p.layer.cornerRadius = 3.f;
  323. p.text = s;
  324. p.font = g_factory.font15;
  325. [parent addSubview:p];
  326. return p;
  327. }
  328. - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
  329. if (scrollView == self.tableBody) {
  330. [self.view endEditing:YES];
  331. }
  332. }
  333. - (BOOL)validateCellPhoneNumber:(NSString *)cellNum{
  334. /**
  335. * 手机号码
  336. * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
  337. * 联通:130,131,132,152,155,156,185,186
  338. * 电信:133,1349,153,180,189
  339. */
  340. NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
  341. /**
  342. 10 * 中国移动:China Mobile
  343. 11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,184,187,188
  344. 12 */
  345. NSString * CM = @"^1(34[0-8]|(3[5-9]|5[017-9]|8[2478])\\d)\\d{7}$";
  346. /**
  347. 15 * 中国联通:China Unicom
  348. 16 * 130,131,132,152,155,156,175,176,185,186
  349. 17 */
  350. NSString * CU = @"^1(3[0-2]|5[256]|7[56]|8[56])\\d{8}$";
  351. /**
  352. 20 * 中国电信:China Telecom
  353. 21 * 133,1349,153,177,180,189
  354. 22 */
  355. NSString * CT = @"^1((33|53|77|8[09])[0-9]|349)\\d{7}$";
  356. /**
  357. 25 * 大陆地区固话及小灵通
  358. 26 * 区号:010,020,021,022,023,024,025,027,028,029
  359. 27 * 号码:七位或八位
  360. 28 */
  361. // NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$";
  362. NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
  363. NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
  364. NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
  365. NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
  366. // NSPredicate *regextestPHS = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
  367. if(([regextestmobile evaluateWithObject:cellNum] == YES)
  368. || ([regextestcm evaluateWithObject:cellNum] == YES)
  369. || ([regextestct evaluateWithObject:cellNum] == YES)
  370. || ([regextestcu evaluateWithObject:cellNum] == YES)){
  371. return YES;
  372. }else{
  373. return NO;
  374. }
  375. }
  376. - (NSString *)getNumber:(NSString *)string {
  377. NSString *pattern = @"\\d*";
  378. NSError *error;
  379. NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
  380. NSLog(@"%@",error);
  381. __block NSString *number = [NSString string];
  382. [regex enumerateMatchesInString:string options:NSMatchingReportProgress range:NSMakeRange(0, string.length) usingBlock:^(NSTextCheckingResult * _Nullable result, NSMatchingFlags flags, BOOL * _Nonnull stop) {
  383. if (NSMatchingReportProgress==flags) {
  384. }else{
  385. /**
  386. * 系统内置方法
  387. */
  388. if (NSTextCheckingTypePhoneNumber==result.resultType) {
  389. number = [string substringWithRange:result.range];
  390. }
  391. /**
  392. * 长度为11位的数字串
  393. */
  394. if (result.range.length==11) {
  395. number = [string substringWithRange:result.range];
  396. }
  397. }
  398. }];
  399. return number;
  400. }
  401. -(JXLabel*)createLabel:(UIView*)parent default:(NSString*)s selector:(SEL)selector{
  402. JXLabel* p = [[JXLabel alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH/2,INSETS,JX_SCREEN_WIDTH/2 -20,44-INSETS*2)];
  403. p.userInteractionEnabled = NO;
  404. p.text = s;
  405. p.font = g_factory.font14;
  406. p.textAlignment = NSTextAlignmentLeft;
  407. p.didTouch = selector;
  408. p.delegate = self;
  409. [parent addSubview:p];
  410. return p;
  411. }
  412. @end