YOUPAINIMInputAtCache.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // YOUPAINIMInputAtCache.m
  3. // NIMKit
  4. //
  5. // Created by chris on 2016/12/8.
  6. // Copyright © 2016年 NetEase. All rights reserved.
  7. //
  8. #import "YOUPAINIMInputAtCache.h"
  9. @interface YOUPAINIMInputAtCache()
  10. @property (nonatomic,strong) NSMutableArray *items;
  11. @end
  12. @implementation YOUPAINIMInputAtCache
  13. - (instancetype)init
  14. {
  15. self = [super init];
  16. if (self) {
  17. _items = [[NSMutableArray alloc] init];
  18. }
  19. return self;
  20. }
  21. - (NSArray *)youpaifallAtUid:(NSString *)sendText;
  22. {
  23. NSArray *names = [self matchString:sendText];
  24. NSMutableArray *uids = [[NSMutableArray alloc] init];
  25. for (NSString *name in names) {
  26. NIMInputAtItem *item = [self item:name];
  27. if (item)
  28. {
  29. [uids addObject:item.uid];
  30. }
  31. }
  32. return [NSArray arrayWithArray:uids];
  33. }
  34. - (void)clean
  35. {
  36. [self.items removeAllObjects];
  37. }
  38. - (void)addAtItem:(NIMInputAtItem *)item
  39. {
  40. [_items addObject:item];
  41. }
  42. - (NIMInputAtItem *)item:(NSString *)name
  43. {
  44. __block NIMInputAtItem *item;
  45. [_items enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  46. NIMInputAtItem *object = obj;
  47. if ([object.name isEqualToString:name])
  48. {
  49. item = object;
  50. *stop = YES;
  51. }
  52. }];
  53. return item;
  54. }
  55. - (NIMInputAtItem *)removeName:(NSString *)name
  56. {
  57. __block NIMInputAtItem *item;
  58. [_items enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  59. NIMInputAtItem *object = obj;
  60. if ([object.name isEqualToString:name]) {
  61. item = object;
  62. *stop = YES;
  63. }
  64. }];
  65. if (item) {
  66. [_items removeObject:item];
  67. }
  68. return item;
  69. }
  70. - (NSArray *)matchString:(NSString *)sendText
  71. {
  72. NSString *pattern = [NSString stringWithFormat:@"%@([^%@]+)%@",NIMInputAtStartChar,NIMInputAtEndChar,NIMInputAtEndChar];
  73. NSError *error = nil;
  74. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
  75. NSArray *results = [regex matchesInString:sendText options:0 range:NSMakeRange(0, sendText.length)];
  76. NSMutableArray *matchs = [[NSMutableArray alloc] init];
  77. for (NSTextCheckingResult *result in results) {
  78. NSString *name = [sendText substringWithRange:result.range];
  79. name = [name substringFromIndex:1];
  80. name = [name substringToIndex:name.length -1];
  81. [matchs addObject:name];
  82. }
  83. return matchs;
  84. }
  85. @end
  86. @implementation NIMInputAtItem
  87. @end