WFCUUserSectionKeySupport.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // WFCUUserSectionKeySupport.m
  3. // WFChatUIKit
  4. //
  5. // Created by Zack Zhang on 2020/4/4.
  6. // Copyright © 2020 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUUserSectionKeySupport.h"
  9. #import "pinyin.h"
  10. #import "WFCUSelectedUserInfo.h"
  11. static NSMutableDictionary *hanziStringDictory = nil;
  12. @implementation WFCUUserSectionKeySupport
  13. + (NSMutableDictionary *)userSectionKeys:(NSArray *)userList {
  14. if (!userList)
  15. return nil;
  16. NSArray *_keys = @[
  17. @"A",
  18. @"B",
  19. @"C",
  20. @"D",
  21. @"E",
  22. @"F",
  23. @"G",
  24. @"H",
  25. @"I",
  26. @"J",
  27. @"K",
  28. @"L",
  29. @"M",
  30. @"N",
  31. @"O",
  32. @"P",
  33. @"Q",
  34. @"R",
  35. @"S",
  36. @"T",
  37. @"U",
  38. @"V",
  39. @"W",
  40. @"X",
  41. @"Y",
  42. @"Z",
  43. @"#"
  44. ];
  45. NSMutableDictionary *infoDic = [NSMutableDictionary new];
  46. NSMutableArray *_tempOtherArr = [NSMutableArray new];
  47. BOOL isReturn = NO;
  48. NSMutableDictionary *firstLetterDict = [[NSMutableDictionary alloc] init];
  49. for (NSString *key in _keys) {
  50. if ([_tempOtherArr count]) {
  51. isReturn = YES;
  52. }
  53. NSMutableArray *tempArr = [NSMutableArray new];
  54. for (id user in userList) {
  55. NSString *firstLetter;
  56. WFCCUserInfo *userInfo = (WFCCUserInfo*)user;
  57. NSString *userName = userInfo.displayName;
  58. if (userInfo.friendAlias.length) {
  59. userName = userInfo.friendAlias;
  60. }
  61. if (userName.length == 0) {
  62. userInfo.displayName = [NSString stringWithFormat:@"<%@>", userInfo.userId];
  63. userName = userInfo.displayName;
  64. }
  65. firstLetter = [firstLetterDict objectForKey:userName];
  66. if (!firstLetter) {
  67. firstLetter = [self getFirstUpperLetter:userName];
  68. [firstLetterDict setObject:firstLetter forKey:userName];
  69. }
  70. if ([firstLetter isEqualToString:key]) {
  71. [tempArr addObject:user];
  72. }
  73. if (isReturn)
  74. continue;
  75. char c = [firstLetter characterAtIndex:0];
  76. if (isalpha(c) == 0) {
  77. [_tempOtherArr addObject:user];
  78. }
  79. }
  80. if (![tempArr count])
  81. continue;
  82. [infoDic setObject:tempArr forKey:key];
  83. }
  84. if ([_tempOtherArr count])
  85. [infoDic setObject:_tempOtherArr forKey:@"#"];
  86. NSArray *keys = [[infoDic allKeys]
  87. sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  88. return [obj1 compare:obj2 options:NSNumericSearch];
  89. }];
  90. NSMutableArray *allKeys = [[NSMutableArray alloc] initWithArray:keys];
  91. if ([allKeys containsObject:@"#"]) {
  92. [allKeys removeObject:@"#"];
  93. [allKeys insertObject:@"#" atIndex:allKeys.count];
  94. }
  95. NSMutableDictionary *resultDic = [NSMutableDictionary new];
  96. [resultDic setObject:infoDic forKey:@"infoDic"];
  97. [resultDic setObject:allKeys forKey:@"allKeys"];
  98. [infoDic enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  99. NSMutableArray *_tempOtherArr = (NSMutableArray *)obj;
  100. [_tempOtherArr sortUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  101. WFCUSelectedUserInfo *user1 = (WFCUSelectedUserInfo *)obj1;
  102. WFCUSelectedUserInfo *user2 = (WFCUSelectedUserInfo *)obj2;
  103. NSString *user1Pinyin = [[self class] hanZiToPinYinWithString:user1.displayName];
  104. NSString *user2Pinyin = [[self class] hanZiToPinYinWithString:user2.displayName];
  105. return [user1Pinyin compare:user2Pinyin];
  106. }];
  107. }];
  108. return resultDic;
  109. }
  110. + (NSString *)getFirstUpperLetter:(NSString *)hanzi {
  111. NSString *pinyin = [self hanZiToPinYinWithString:hanzi];
  112. NSString *firstUpperLetter = [[pinyin substringToIndex:1] uppercaseString];
  113. if ([firstUpperLetter compare:@"A"] != NSOrderedAscending &&
  114. [firstUpperLetter compare:@"Z"] != NSOrderedDescending) {
  115. return firstUpperLetter;
  116. } else {
  117. return @"#";
  118. }
  119. }
  120. + (NSString *)hanZiToPinYinWithString:(NSString *)hanZi {
  121. if (!hanZi) {
  122. return nil;
  123. }
  124. if (!hanziStringDictory) {
  125. hanziStringDictory = [[NSMutableDictionary alloc] init];
  126. }
  127. NSString *pinYinResult = [hanziStringDictory objectForKey:hanZi];
  128. if (pinYinResult) {
  129. return pinYinResult;
  130. }
  131. pinYinResult = [NSString string];
  132. for (int j = 0; j < hanZi.length; j++) {
  133. NSString *singlePinyinLetter = nil;
  134. if ([self isChinese:[hanZi substringWithRange:NSMakeRange(j, 1)]]) {
  135. singlePinyinLetter = [[NSString
  136. stringWithFormat:@"%c", pinyinFirstLetter([hanZi characterAtIndex:j])]
  137. uppercaseString];
  138. }else{
  139. singlePinyinLetter = [hanZi substringWithRange:NSMakeRange(j, 1)];
  140. }
  141. pinYinResult = [pinYinResult stringByAppendingString:singlePinyinLetter];
  142. }
  143. [hanziStringDictory setObject:pinYinResult forKey:hanZi];
  144. return pinYinResult;
  145. }
  146. + (BOOL)isChinese:(NSString *)text
  147. {
  148. NSString *match = @"(^[\u4e00-\u9fa5]+$)";
  149. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF matches %@", match];
  150. return [predicate evaluateWithObject:text];
  151. }
  152. @end