From 7b02207537d35bfa1714bf8beafc921f717d100a Mon Sep 17 00:00:00 2001
From: 单军华
Date: Wed, 11 Jul 2018 10:47:42 +0800
Subject: [PATCH] 首次上传

---
 screendisplay/Pods/HMEmoticon/表情键盘/Emoticon/HMEmoticonManager.m |  235 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 235 insertions(+), 0 deletions(-)

diff --git "a/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonManager.m" "b/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonManager.m"
new file mode 100755
index 0000000..28ebc62
--- /dev/null
+++ "b/screendisplay/Pods/HMEmoticon/\350\241\250\346\203\205\351\224\256\347\233\230/Emoticon/HMEmoticonManager.m"
@@ -0,0 +1,235 @@
+//
+//  HMEmoticonManager.m
+//  ������������
+//
+//  Created by ������ on 16/3/3.
+//  Copyright �� 2016��� itcast. All rights reserved.
+//
+
+#import "HMEmoticonManager.h"
+#import "NSBundle+HMEmoticon.h"
+#import "HMEmoticonAttachment.h"
+
+/// ���������������������������
+static NSInteger kEmoticonsCountOfPage = 20;
+/// ������������������
+NSString *const HMEmoticonNamespace = @"cn.itcast";
+/// ���������������������
+NSString *const HMEmoticonDefaultUserIdentifier = @"DefaultUser";
+/// ���������������
+NSString *const HMEmoticonFileName = @".emoticons.json";
+
+@implementation HMEmoticonManager {
+    NSMutableArray <HMEmoticon *> *_recentEmoticonList;
+}
+
+#pragma mark - ������ & ������������
++ (instancetype)sharedManager {
+    static id instance;
+    
+    static dispatch_once_t onceToken;
+    dispatch_once(&onceToken, ^{
+        instance = [[self alloc] init];
+    });
+    
+    return instance;
+}
+
+- (instancetype)init {
+    self = [super init];
+    if (self) {
+        _packages = [NSMutableArray array];
+        
+        [self loadPackages];
+    }
+    return self;
+}
+
+#pragma mark - ���������������
+- (NSAttributedString *)emoticonStringWithString:(NSString *)string font:(UIFont *)font textColor:(UIColor *)textColor {
+    
+    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]
+                                                   initWithString:string
+                                                   attributes:@{NSFontAttributeName: font,
+                                                                NSForegroundColorAttributeName: textColor}];
+    
+    NSString *pattern = @"\\[.*?\\]";
+    NSRegularExpression *regx = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:NULL];
+    
+    NSArray *matches = [regx matchesInString:string options:0 range:NSMakeRange(0, string.length)];
+    for (NSTextCheckingResult *result in matches.reverseObjectEnumerator) {
+        
+        NSRange range = [result rangeAtIndex:0];
+        NSString *str = [string substringWithRange:range];
+        
+        HMEmoticon *emoticon = [self emoticonWithString:str];
+        if (emoticon != nil) {
+            NSAttributedString *emoticonString = [HMEmoticonAttachment emoticonStringWithEmoticon:emoticon font:font textColor:textColor];
+            
+            [attributedString replaceCharactersInRange:range withAttributedString:emoticonString];
+        }
+    }
+    
+    return attributedString.copy;
+}
+
+- (HMEmoticon *)emoticonWithString:(NSString *)string {
+    
+    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"chs == %@", string];
+    HMEmoticon *emoticon = nil;
+    
+    for (NSInteger i = 1; i < _packages.count; i++) {
+        
+        HMEmoticonPackage *package = _packages[i];
+        
+        NSArray *filter = [package.emoticonsList filteredArrayUsingPredicate:predicate];
+        
+        if (filter.count == 1) {
+            emoticon = filter[0];
+            
+            break;
+        }
+    }
+    
+    return emoticon;
+}
+
+#pragma mark - ���������������
+- (NSInteger)numberOfPagesInSection:(NSInteger)section {
+    HMEmoticonPackage *package = _packages[section];
+    
+    return ((NSInteger)package.emoticonsList.count - 1) / kEmoticonsCountOfPage + 1;
+}
+
+- (NSArray *)emoticonsWithIndexPath:(NSIndexPath *)indexPath {
+    HMEmoticonPackage *package = self.packages[indexPath.section];
+    
+    NSInteger location = indexPath.item * kEmoticonsCountOfPage;
+    NSInteger length = kEmoticonsCountOfPage;
+    
+    // ������������������
+    if ((location + length) > package.emoticonsList.count) {
+        length = package.emoticonsList.count - location;
+    }
+    
+    NSRange range = NSMakeRange(location, length);
+    
+    return [package.emoticonsList subarrayWithRange:range];
+}
+
+#pragma mark - ������������������
+- (void)addRecentEmoticon:(HMEmoticon *)emoticon {
+    // 0. ������������ ++
+    emoticon.times++;
+    
+    // 1. ������������������������������
+    if (![_recentEmoticonList containsObject:emoticon]) {
+        [_recentEmoticonList addObject:emoticon];
+    }
+    
+    // 2. ������
+    [_recentEmoticonList sortUsingComparator:^NSComparisonResult(HMEmoticon *obj1, HMEmoticon *obj2) {
+        return obj1.times < obj2.times;
+    }];
+    
+    // 3. ���������������������
+    [self updateRecentPackage];
+    
+    // 4. ���������������
+    [self saveRecentEmoticonList];
+}
+
+/// ���������������������������
+- (void)updateRecentPackage {
+    NSInteger length = _recentEmoticonList.count < kEmoticonsCountOfPage ? _recentEmoticonList.count : kEmoticonsCountOfPage;
+    
+    _packages[0].emoticonsList = [_recentEmoticonList subarrayWithRange:NSMakeRange(0, length)].mutableCopy;
+}
+
+/// ������������������������
+- (void)saveRecentEmoticonList {
+    
+    NSMutableArray *jsonDict = [NSMutableArray array];
+    for (HMEmoticon *emoticon in _recentEmoticonList) {
+        [jsonDict addObject:[emoticon dictionary]];
+    }
+    
+    NSData *data = [NSJSONSerialization dataWithJSONObject:jsonDict options:NSJSONWritingPrettyPrinted error:NULL];
+    [data writeToFile:[self filePathForRecentEmoticon] atomically:YES];
+}
+
+/// ������������������������
+///
+/// @return ������������������
+- (NSMutableArray <HMEmoticon *>*)loadRecentEmoticonList {
+    
+    NSMutableArray *arrayM = [NSMutableArray array];
+    NSData *data = [NSData dataWithContentsOfFile:[self filePathForRecentEmoticon]];
+    
+    if (data == nil) {
+        return arrayM;
+    }
+    
+    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
+    
+    for (NSDictionary *dict in array) {
+        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"chs CONTAINS %@ || code CONTAINS %@", dict[@"chs"], dict[@"code"]];
+        
+        for (NSInteger i = 1; i < _packages.count; i++) {
+            HMEmoticonPackage *package = _packages[i];
+            
+            NSArray *filter = [package.emoticonsList filteredArrayUsingPredicate:predicate];
+            
+            if (filter.count == 1) {
+                [arrayM addObject:filter[0]];
+                break;
+            }
+        }
+    }
+    
+    return arrayM;
+}
+
+/// ������������������������
+- (NSString *)filePathForRecentEmoticon {
+    NSString *dir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).lastObject;
+    
+    return [[dir stringByAppendingPathComponent:self.userIdentifier] stringByAppendingString:HMEmoticonFileName];
+}
+
+#pragma mark - ���������������������
+- (void)loadPackages {
+    
+    // 1. ������ emoticons.plist
+    NSString *path = [[NSBundle hm_emoticonBundle] pathForResource:@"emoticons.plist" ofType:nil];
+    NSArray *array = [NSArray arrayWithContentsOfFile:path];
+    
+    // 2. ��������������������� packages ������
+    for (NSDictionary *dict in array) {
+        [_packages addObject:[HMEmoticonPackage packageWithDict:dict]];
+    }
+    
+    // 3. ������������������������������
+    _recentEmoticonList = [self loadRecentEmoticonList];
+    
+    // 4. ���������������������
+    [self updateRecentPackage];
+}
+
+#pragma mark - ���������������
+@synthesize userIdentifier = _userIdentifier;
+- (void)setUserIdentifier:(NSString *)userIdentifier {
+    _userIdentifier = userIdentifier.copy;
+    
+    _recentEmoticonList = [self loadRecentEmoticonList];
+    [self updateRecentPackage];
+}
+
+- (NSString *)userIdentifier {
+    if (_userIdentifier == nil) {
+        _userIdentifier = HMEmoticonDefaultUserIdentifier;
+    }
+    return [NSString stringWithFormat:@"%@.%@", HMEmoticonNamespace, _userIdentifier];;
+}
+
+@end

--
Gitblit v1.8.0