TUIKitConfig.m 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. //
  2. // TUIKitConfig.m
  3. // TUIKit
  4. //
  5. // Created by kennethmiao on 2018/11/5.
  6. // Copyright © 2018年 kennethmiao. All rights reserved.
  7. //
  8. #import "TUIKitConfig.h"
  9. #import "THeader.h"
  10. #import "THelper.h"
  11. #import "TFaceCell.h"
  12. #import "TFaceView.h"
  13. #import "TMoreCell.h"
  14. @interface TUIKitConfig ()
  15. @property (nonatomic, strong) NSMutableDictionary *resourceCache;
  16. @property (nonatomic, strong) NSMutableDictionary *faceCache;
  17. @property (nonatomic, strong) dispatch_queue_t decodeResourceQueue;
  18. @property (nonatomic, strong) dispatch_queue_t decodeFaceQueue;
  19. @end
  20. @implementation TUIKitConfig
  21. - (id)init {
  22. self = [super init];
  23. if(self){
  24. _decodeResourceQueue = dispatch_queue_create("tuikit.decoderesourcequeue", DISPATCH_QUEUE_SERIAL);
  25. _decodeFaceQueue = dispatch_queue_create("tuikit.decodefacequeue", DISPATCH_QUEUE_SERIAL);
  26. _resourceCache = [NSMutableDictionary dictionary];
  27. _faceCache = [NSMutableDictionary dictionary];
  28. }
  29. return self;
  30. }
  31. + (id)defaultConfig {
  32. TUIKitConfig *config = [[TUIKitConfig alloc] init];
  33. if(config){
  34. config.msgCountPerRequest = 20;
  35. [config defaultResourceCache];
  36. [config defaultFace];
  37. [config defaultMore];
  38. [config groupMore];
  39. }
  40. return config;
  41. }
  42. - (void)defaultFace {
  43. NSMutableArray *faceGroups = [NSMutableArray array];
  44. //emoji group
  45. NSMutableArray *emojiFaces = [NSMutableArray array];
  46. /*
  47. NSArray *emojis = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"YBFaceEmoji.bundle/emoji/emoji" ofType:@"plist"]];
  48. for (NSDictionary *dic in emojis) {
  49. TFaceCellData *data = [[TFaceCellData alloc] init];
  50. NSString *name = [dic objectForKey:@"face_name"];
  51. NSString *path = [NSString stringWithFormat:@"emoji/%@", name];
  52. data.name = name;
  53. data.path = YBFaceEmoji(path);
  54. [self addFaceToCache:data.path];
  55. [emojiFaces addObject:data];
  56. }
  57. */
  58. NSArray *emojis = @[@"[微笑]",@"[色]",@"[发呆]",@"[抽烟]",@"[抠鼻]",@"[哭]",@"[发怒]",@"[呲牙]",@"[睡]",@"[害羞]",@"[调皮]",@"[晕]",@"[衰]",@"[闭嘴]",@"[指点]",@"[关注]",@"[搞定]",@"[胜利]",@"[无奈]",@"[打脸]",@"[大笑]",@"[哈欠]",@"[害怕]",@"[喜欢]",@"[困]",@"[疑问]",@"[伤心]",@"[鼓掌]",@"[得意]",@"[捂嘴]",@"[惊恐]",@"[思考]",@"[吐血]",@"[卖萌]",@"[嘘]",@"[生气]",@"[尴尬]",@"[笑哭]",@"[口罩]",@"[斜眼]",@"[酷]",@"[脸红]",@"[大叫]",@"[眼泪]",@"[见钱]",@"[嘟]",@"[吓]",@"[开心]",@"[想哭]",@"[郁闷]",@"[互粉]",@"[赞]",@"[拜托]",@"[唇]",@"[粉]",@"[666]",@"[玫瑰]",@"[黄瓜]",@"[啤酒]",@"[无语]",@"[纠结]",@"[吐舌]",@"[差评]",@"[飞吻]",@"[再见]",@"[拒绝]",@"[耳机]",@"[抱抱]",@"[嘴]",@"[露牙]",@"[黄狗]",@"[灰狗]",@"[蓝狗]",@"[狗]",@"[脸黑]",@"[吃瓜]",@"[绿帽]",@"[汗]",@"[摸头]",@"[阴险]",@"[擦汗]",@"[瞪眼]",@"[疼]",@"[鬼脸]",@"[拇指]",@"[亲]",@"[大吐]",@"[高兴]",@"[敲打]",@"[加油]",@"[吐]",@"[握手]",@"[18禁]",@"[菜刀]",@"[威武]",@"[给力]",@"[爱心]",@"[心碎]",@"[便便]",@"[礼物]",@"[生日]",@"[喝彩]",@"[雷]"];
  59. for (NSString *emojiName in emojis) {
  60. TFaceCellData *data = [[TFaceCellData alloc] init];
  61. NSString *name = emojiName;
  62. NSString *path = emojiName;
  63. data.name = name;
  64. data.path = path;//YBFaceEmoji(path);
  65. //[self addFaceToCache:data.path];
  66. [emojiFaces addObject:data];
  67. }
  68. if(emojiFaces.count != 0){
  69. TFaceGroup *emojiGroup = [[TFaceGroup alloc] init];
  70. emojiGroup.groupIndex = 0;
  71. emojiGroup.groupPath = YBFaceEmoji(@"emoji/");
  72. emojiGroup.faces = emojiFaces;
  73. emojiGroup.rowCount = 3;
  74. emojiGroup.itemCountPerRow = 7;
  75. emojiGroup.needBackDelete = YES;
  76. emojiGroup.menuPath = YBFaceEmoji(@"emoji/menu");
  77. //[self addFaceToCache:emojiGroup.menuPath];
  78. [faceGroups addObject:emojiGroup];
  79. //[self addFaceToCache:YBFaceEmoji(@"msg_del")];
  80. }
  81. _faceGroups = faceGroups;
  82. }
  83. - (void)defaultMore {
  84. /**
  85. * 每行4个,两行的话,这里是纵向排序
  86. *
  87. * xxxxxxxxxxxxxxxxxxx
  88. * x
  89. * x 0 2 4 6
  90. * x 1 3
  91. * x
  92. * xxxxxxxxxxxxxxxxxx
  93. */
  94. NSMutableArray *moreMenus = [NSMutableArray array];
  95. TMoreCellData *picture = [[TMoreCellData alloc] init];
  96. picture.title = YZMsg(@"图片");
  97. picture.path = @"chat_tool_album";
  98. [moreMenus addObject:picture];
  99. TMoreCellData *camera = [[TMoreCellData alloc] init];
  100. camera.title = YZMsg(@"相机-sm");
  101. camera.path = @"chat_tool_camera";
  102. [moreMenus addObject:camera];
  103. TMoreCellData *gift = [[TMoreCellData alloc] init];
  104. gift.title = YZMsg(@"语音");
  105. gift.path = @"chat_tool_v_input.png";
  106. [moreMenus addObject:gift];
  107. TMoreCellData *video = [[TMoreCellData alloc] init];
  108. video.title = YZMsg(@"位置");
  109. video.path = @"chat_tool_location.png";
  110. [moreMenus addObject:video];
  111. _moreMenus = moreMenus;
  112. }
  113. -(void)groupMore {
  114. /**
  115. * 每行4个,单行行的话,这里是横向排序
  116. *
  117. * xxxxxxxxxxxxxxxxxxx
  118. * x
  119. * x 0 1 2 3
  120. * x
  121. * xxxxxxxxxxxxxxxxxx
  122. */
  123. NSMutableArray *moreMenus = [NSMutableArray array];
  124. TMoreCellData *picture = [[TMoreCellData alloc] init];
  125. picture.title = YZMsg(@"相册");
  126. picture.path = TUIKitResource(@"more_picture");
  127. [moreMenus addObject:picture];
  128. TMoreCellData *camera = [[TMoreCellData alloc] init];
  129. camera.title = YZMsg(@"拍摄");
  130. camera.path = TUIKitResource(@"more_camera");
  131. [moreMenus addObject:camera];
  132. _groutMoreMenus = moreMenus;
  133. }
  134. #pragma mark - resource
  135. - (void)defaultResourceCache {
  136. //common
  137. [self addResourceToCache:TUIKitResource(@"more_normal")];
  138. [self addResourceToCache:TUIKitResource(@"more_pressed")];
  139. [self addResourceToCache:TUIKitResource(@"face_normal")];
  140. [self addResourceToCache:TUIKitResource(@"face_pressed")];
  141. [self addResourceToCache:TUIKitResource(@"keyboard_normal")];
  142. [self addResourceToCache:TUIKitResource(@"keyboard_pressed")];
  143. [self addResourceToCache:TUIKitResource(@"voice_normal")];
  144. [self addResourceToCache:TUIKitResource(@"voice_pressed")];
  145. //text msg
  146. [self addResourceToCache:TUIKitResource(@"sender_text_normal")];
  147. [self addResourceToCache:TUIKitResource(@"sender_text_pressed")];
  148. [self addResourceToCache:TUIKitResource(@"receiver_text_normal")];
  149. [self addResourceToCache:TUIKitResource(@"receiver_text_pressed")];
  150. //void msg
  151. [self addResourceToCache:TUIKitResource(@"sender_voice")];
  152. [self addResourceToCache:TUIKitResource(@"receiver_voice")];
  153. [self addResourceToCache:TUIKitResource(@"sender_voice_play_1")];
  154. [self addResourceToCache:TUIKitResource(@"sender_voice_play_2")];
  155. [self addResourceToCache:TUIKitResource(@"sender_voice_play_3")];
  156. [self addResourceToCache:TUIKitResource(@"receiver_voice_play_1")];
  157. [self addResourceToCache:TUIKitResource(@"receiver_voice_play_2")];
  158. [self addResourceToCache:TUIKitResource(@"receiver_voice_play_3")];
  159. //file msg
  160. [self addResourceToCache:TUIKitResource(@"msg_file")];
  161. //video msg
  162. [self addResourceToCache:TUIKitResource(@"play_normal")];
  163. }
  164. - (void)addResourceToCache:(NSString *)path {
  165. __weak typeof(self) ws = self;
  166. // dispatch_async(_decodeResourceQueue, ^{
  167. // UIImage *image = [UIImage imageNamed:path];
  168. // [ws.resourceCache setValue:image forKey:path];
  169. // });
  170. [THelper asyncDecodeImage:path queue:_decodeResourceQueue complete:^(NSString *key, UIImage *image) {
  171. [ws.resourceCache setValue:image forKey:key];
  172. }];
  173. }
  174. - (UIImage *)getResourceFromCache:(NSString *)path
  175. {
  176. if(path.length == 0){
  177. return nil;
  178. }
  179. UIImage *image = [_resourceCache objectForKey:path];
  180. if(!image){
  181. image = [UIImage imageNamed:path];
  182. }
  183. return image;
  184. }
  185. - (void)addFaceToCache:(NSString *)path
  186. {
  187. __weak typeof(self) ws = self;
  188. // dispatch_async(_decodeFaceQueue, ^{
  189. // UIImage *image = [UIImage imageNamed:path];
  190. // [ws.faceCache setValue:image forKey:path];
  191. // });
  192. [THelper asyncDecodeImage:path queue:_decodeFaceQueue complete:^(NSString *key, UIImage *image) {
  193. [ws.faceCache setValue:image forKey:key];
  194. }];
  195. }
  196. - (UIImage *)getFaceFromCache:(NSString *)path
  197. {
  198. if(path.length == 0){
  199. return nil;
  200. }
  201. UIImage *image = [_faceCache objectForKey:path];
  202. if(!image){
  203. image = [UIImage imageNamed:path];
  204. }
  205. return image;
  206. }
  207. @end