244 lines
7.5 KiB
Objective-C
244 lines
7.5 KiB
Objective-C
//
|
|
// TLFriendHelper.m
|
|
// TLChat
|
|
//
|
|
// Created by 李伯坤 on 16/1/27.
|
|
// Copyright © 2016年 李伯坤. All rights reserved.
|
|
//
|
|
|
|
#import "TLFriendHelper.h"
|
|
#import "TLDBFriendStore.h"
|
|
#import "TLDBGroupStore.h"
|
|
#import "TLGroup+CreateAvatar.h"
|
|
#import "TLUserHelper.h"
|
|
#import "TLMessageManager+MessageRecord.h"
|
|
#import "TLChatNotificationKey.h"
|
|
|
|
static TLFriendHelper *friendHelper = nil;
|
|
|
|
@interface TLFriendHelper ()
|
|
|
|
@property (nonatomic, strong) TLDBFriendStore *friendStore;
|
|
|
|
@property (nonatomic, strong) TLDBGroupStore *groupStore;
|
|
|
|
@end
|
|
|
|
@implementation TLFriendHelper
|
|
|
|
+ (TLFriendHelper *)sharedFriendHelper
|
|
{
|
|
static dispatch_once_t once;
|
|
dispatch_once(&once, ^{
|
|
friendHelper = [[TLFriendHelper alloc] init];
|
|
});
|
|
return friendHelper;
|
|
}
|
|
|
|
- (id)init
|
|
{
|
|
if (self = [super init]) {
|
|
// 初始化好友数据
|
|
_friendsData = [self.friendStore friendsDataByUid:[TLUserHelper sharedHelper].userID];
|
|
self.data = [[NSMutableArray alloc] init];
|
|
self.sectionHeaders = [[NSMutableArray alloc] initWithObjects:UITableViewIndexSearch, nil];
|
|
// 初始化群数据
|
|
self.groupsData = [self.groupStore groupsDataByUid:[TLUserHelper sharedHelper].userID];
|
|
// 初始化标签数据
|
|
self.tagsData = [[NSMutableArray alloc] init];
|
|
[self p_initTestData];
|
|
}
|
|
return self;
|
|
}
|
|
|
|
#pragma mark - Public Methods -
|
|
- (TLUser *)getFriendInfoByUserID:(NSString *)userID
|
|
{
|
|
if (userID == nil) {
|
|
return nil;
|
|
}
|
|
for (TLUser *user in self.friendsData) {
|
|
if ([user.userID isEqualToString:userID]) {
|
|
return user;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
- (TLGroup *)getGroupInfoByGroupID:(NSString *)groupID
|
|
{
|
|
if (groupID == nil) {
|
|
return nil;
|
|
}
|
|
for (TLGroup *group in self.groupsData) {
|
|
if ([group.groupID isEqualToString:groupID]) {
|
|
return group;
|
|
}
|
|
}
|
|
return nil;
|
|
}
|
|
|
|
- (BOOL)deleteFriendByUserId:(NSString *)userID
|
|
{
|
|
BOOL ok = [[TLMessageManager sharedInstance] deleteMessagesByPartnerID:userID];
|
|
if (ok) {
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:NOTI_CHAT_VIEW_RESET object:nil];
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
#pragma mark - Private Methods -
|
|
- (void)p_resetFriendData
|
|
{
|
|
// 1、排序
|
|
NSArray *serializeArray = [self.friendsData sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
|
|
int i;
|
|
NSString *strA = ((TLUser *)obj1).pinyin;
|
|
NSString *strB = ((TLUser *)obj2).pinyin;
|
|
for (i = 0; i < strA.length && i < strB.length; i ++) {
|
|
char a = toupper([strA characterAtIndex:i]);
|
|
char b = toupper([strB characterAtIndex:i]);
|
|
if (a > b) {
|
|
return (NSComparisonResult)NSOrderedDescending;
|
|
}
|
|
else if (a < b) {
|
|
return (NSComparisonResult)NSOrderedAscending;
|
|
}
|
|
}
|
|
if (strA.length > strB.length) {
|
|
return (NSComparisonResult)NSOrderedDescending;
|
|
}
|
|
else if (strA.length < strB.length){
|
|
return (NSComparisonResult)NSOrderedAscending;
|
|
}
|
|
return (NSComparisonResult)NSOrderedSame;
|
|
}];
|
|
|
|
// 2、分组
|
|
NSMutableArray *ansData = [[NSMutableArray alloc] init];
|
|
NSMutableArray *ansSectionHeaders = [[NSMutableArray alloc] initWithObjects:UITableViewIndexSearch, nil];
|
|
NSMutableDictionary *tagsDic = [[NSMutableDictionary alloc] init];
|
|
char lastC = '1';
|
|
TLUserGroup *curGroup;
|
|
TLUserGroup *othGroup = [[TLUserGroup alloc] init];
|
|
[othGroup setGroupName:@"#"];
|
|
[othGroup setTag:27];
|
|
for (TLUser *user in serializeArray) {
|
|
// 获取拼音失败
|
|
if (user.pinyin == nil || user.pinyin.length == 0) {
|
|
[othGroup addObject:user];
|
|
continue;
|
|
}
|
|
|
|
char c = toupper([user.pinyin characterAtIndex:0]);
|
|
if (!isalpha(c)) { // #组
|
|
[othGroup addObject:user];
|
|
}
|
|
else if (c != lastC){
|
|
if (curGroup && curGroup.count > 0) {
|
|
[ansData addObject:curGroup];
|
|
[ansSectionHeaders addObject:curGroup.groupName];
|
|
}
|
|
lastC = c;
|
|
curGroup = [[TLUserGroup alloc] init];
|
|
[curGroup setGroupName:[NSString stringWithFormat:@"%c", c]];
|
|
[curGroup addObject:user];
|
|
[curGroup setTag:(NSInteger)c];
|
|
}
|
|
else {
|
|
[curGroup addObject:user];
|
|
}
|
|
|
|
// TAGs
|
|
if (user.detailInfo.tags.count > 0) {
|
|
for (NSString *tag in user.detailInfo.tags) {
|
|
TLUserGroup *group = [tagsDic objectForKey:tag];
|
|
if (group == nil) {
|
|
group = [[TLUserGroup alloc] init];
|
|
group.groupName = tag;
|
|
[tagsDic setObject:group forKey:tag];
|
|
[self.tagsData addObject:group];
|
|
}
|
|
[group.users addObject:user];
|
|
}
|
|
}
|
|
}
|
|
if (curGroup && curGroup.count > 0) {
|
|
[ansData addObject:curGroup];
|
|
[ansSectionHeaders addObject:curGroup.groupName];
|
|
}
|
|
if (othGroup.count > 0) {
|
|
[ansData addObject:othGroup];
|
|
[ansSectionHeaders addObject:othGroup.groupName];
|
|
}
|
|
|
|
[self.data removeAllObjects];
|
|
[self.data addObjectsFromArray:ansData];
|
|
[self.sectionHeaders removeAllObjects];
|
|
[self.sectionHeaders addObjectsFromArray:ansSectionHeaders];
|
|
if (self.dataChangedBlock) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
self.dataChangedBlock(self.data, self.sectionHeaders, self.friendCount);
|
|
});
|
|
}
|
|
}
|
|
|
|
- (void)p_initTestData
|
|
{
|
|
// 好友数据
|
|
NSString *path = [[NSBundle mainBundle] pathForResource:@"FriendList" ofType:@"json"];
|
|
NSData *jsonData = [NSData dataWithContentsOfFile:path];
|
|
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
|
|
NSArray *arr = [TLUser mj_objectArrayWithKeyValuesArray:jsonArray];
|
|
[self.friendsData removeAllObjects];
|
|
[self.friendsData addObjectsFromArray:arr];
|
|
// 更新好友数据到数据库
|
|
BOOL ok = [self.friendStore updateFriendsData:self.friendsData forUid:[TLUserHelper sharedHelper].userID];
|
|
if (!ok) {
|
|
DDLogError(@"保存好友数据到数据库失败!");
|
|
}
|
|
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
|
[self p_resetFriendData];
|
|
});
|
|
|
|
// 群数据
|
|
path = [[NSBundle mainBundle] pathForResource:@"FriendGroupList" ofType:@"json"];
|
|
jsonData = [NSData dataWithContentsOfFile:path];
|
|
jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:nil];
|
|
arr = [TLGroup mj_objectArrayWithKeyValuesArray:jsonArray];
|
|
[self.groupsData removeAllObjects];
|
|
[self.groupsData addObjectsFromArray:arr];
|
|
ok = [self.groupStore updateGroupsData:self.groupsData forUid:[TLUserHelper sharedHelper].userID];
|
|
if (!ok) {
|
|
DDLogError(@"保存群数据到数据库失败!");
|
|
}
|
|
// 生成Group Icon
|
|
for (TLGroup *group in self.groupsData) {
|
|
[group createGroupAvatarWithCompleteAction:nil];
|
|
}
|
|
}
|
|
|
|
#pragma mark - Getter
|
|
- (NSInteger)friendCount
|
|
{
|
|
return self.friendsData.count;
|
|
}
|
|
|
|
- (TLDBFriendStore *)friendStore
|
|
{
|
|
if (_friendStore == nil) {
|
|
_friendStore = [[TLDBFriendStore alloc] init];
|
|
}
|
|
return _friendStore;
|
|
}
|
|
|
|
- (TLDBGroupStore *)groupStore
|
|
{
|
|
if (_groupStore == nil) {
|
|
_groupStore = [[TLDBGroupStore alloc] init];
|
|
}
|
|
return _groupStore;
|
|
}
|
|
|
|
@end
|