chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// TLGroup+CreateAvatar.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 2017/9/19.
|
||||
// Copyright © 2017年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLGroup.h"
|
||||
|
||||
@interface TLGroup (CreateAvatar)
|
||||
|
||||
- (void)createGroupAvatarWithCompleteAction:(void (^)(NSString *groupID))completeAction;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// TLGroup+CreateAvatar.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 2017/9/19.
|
||||
// Copyright © 2017年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLGroup+CreateAvatar.h"
|
||||
#import "NSFileManager+TLChat.h"
|
||||
#import "TLMacros.h"
|
||||
|
||||
@implementation TLGroup (CreateAvatar)
|
||||
|
||||
- (void)createGroupAvatarWithCompleteAction:(void (^)(NSString *groupID))completeAction
|
||||
{
|
||||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||||
NSInteger usersCount = self.users.count > 9 ? 9 : self.users.count;
|
||||
CGFloat viewWidth = 200;
|
||||
CGFloat width = viewWidth / 3 * 0.85;
|
||||
CGFloat space3 = (viewWidth - width * 3) / 4; // 三张图时的边距(图与图之间的边距)
|
||||
CGFloat space2 = (viewWidth - width * 2 + space3) / 2; // 两张图时的边距
|
||||
CGFloat space1 = (viewWidth - width) / 2; // 一张图时的边距
|
||||
CGFloat y = ({
|
||||
int ans = space1;
|
||||
if (usersCount > 6) {
|
||||
ans = space3;
|
||||
}
|
||||
else if (usersCount >= 3) {
|
||||
ans = space2 - space3;
|
||||
}
|
||||
ans;
|
||||
});
|
||||
CGFloat x = ({
|
||||
CGFloat ans = space1;
|
||||
if (usersCount % 3 == 0) {
|
||||
ans = usersCount == 3 ? space1 :space3;
|
||||
}
|
||||
else if (usersCount % 2 == 0) {
|
||||
ans = space2 - space3;
|
||||
}
|
||||
ans;
|
||||
});
|
||||
|
||||
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewWidth, viewWidth)];
|
||||
[view setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:0.6]];
|
||||
__block NSInteger count = 0; // 下载完成图片计数器
|
||||
for (NSInteger i = usersCount - 1; i >= 0; i--) {
|
||||
TLUser *user = [self.users objectAtIndex:i];
|
||||
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, width)];
|
||||
[view addSubview:imageView];
|
||||
[imageView tt_setImageWithURL:user.avatarURL.toURL placeholderImage:[UIImage imageNamed:DEFAULT_AVATAR_PATH] completed:^(UIImage *image, NSError *error, TLImageCacheType cacheType, NSURL *imageURL) {
|
||||
count ++;
|
||||
if (count == usersCount) { // 图片全部下载完成
|
||||
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 2.0);
|
||||
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
|
||||
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
CGImageRef imageRef = image.CGImage;
|
||||
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, view.width * 2, view.height * 2));
|
||||
UIImage *ansImage = [[UIImage alloc] initWithCGImage:imageRefRect];
|
||||
NSData *imageViewData = UIImagePNGRepresentation(ansImage);
|
||||
NSString *savedImagePath = [NSFileManager pathUserAvatar:self.groupAvatarPath];
|
||||
[imageViewData writeToFile:savedImagePath atomically:YES];
|
||||
CGImageRelease(imageRefRect);
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (completeAction) {
|
||||
completeAction(self.groupID);
|
||||
}
|
||||
});
|
||||
}
|
||||
}];
|
||||
if (i % 3 == 0) { // 换行
|
||||
y += (width + space3);
|
||||
x = space3;
|
||||
}
|
||||
else if (i == 2 && usersCount == 3) { // 换行,只有三个时
|
||||
y += (width + space3);
|
||||
x = space2;
|
||||
}
|
||||
else {
|
||||
x += (width + space3);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// TLGroup.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/7.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLUser.h"
|
||||
|
||||
@interface TLGroup : NSObject
|
||||
|
||||
/**
|
||||
* 讨论组名称
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *groupName;
|
||||
|
||||
|
||||
@property (nonatomic, strong) NSString *groupAvatarPath;
|
||||
|
||||
/**
|
||||
* 讨论组ID
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *groupID;
|
||||
|
||||
/**
|
||||
* 讨论组成员
|
||||
*/
|
||||
@property (nonatomic, strong) NSMutableArray *users;
|
||||
|
||||
/**
|
||||
* 群公告
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *post;
|
||||
|
||||
/**
|
||||
* 我的群昵称
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *myNikeName;
|
||||
|
||||
@property (nonatomic, strong) NSString *pinyin;
|
||||
|
||||
@property (nonatomic, strong) NSString *pinyinInitial;
|
||||
|
||||
@property (nonatomic, assign, readonly) NSInteger count;
|
||||
|
||||
@property (nonatomic, assign) BOOL showNameInChat;
|
||||
|
||||
|
||||
- (void)addObject:(id)anObject;
|
||||
|
||||
- (id)objectAtIndex:(NSUInteger)index;
|
||||
|
||||
- (TLUser *)memberByUserID:(NSString *)uid;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// TLGroup.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/7.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLGroup.h"
|
||||
#import "NSString+PinYin.h"
|
||||
#import "TLUserHelper.h"
|
||||
|
||||
@implementation TLGroup
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
[TLGroup mj_setupObjectClassInArray:^NSDictionary *{
|
||||
return @{ @"users" : @"TLUser" };
|
||||
}];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSInteger)count
|
||||
{
|
||||
return self.users.count;
|
||||
}
|
||||
|
||||
- (void)addObject:(id)anObject
|
||||
{
|
||||
[self.users addObject:anObject];
|
||||
}
|
||||
|
||||
- (id)objectAtIndex:(NSUInteger)index
|
||||
{
|
||||
return [self.users objectAtIndex:index];
|
||||
}
|
||||
|
||||
- (TLUser *)memberByUserID:(NSString *)uid
|
||||
{
|
||||
for (TLUser *user in self.users) {
|
||||
if ([user.userID isEqualToString:uid]) {
|
||||
return user;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (NSString *)groupName
|
||||
{
|
||||
if (_groupName == nil || _groupName.length == 0) {
|
||||
for (TLUser *user in self.users) {
|
||||
if (user.showName.length > 0) {
|
||||
if (_groupName == nil || _groupName.length <= 0) {
|
||||
_groupName = user.showName;
|
||||
}
|
||||
else {
|
||||
_groupName = [NSString stringWithFormat:@"%@,%@", _groupName, user.showName];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _groupName;
|
||||
}
|
||||
|
||||
- (NSString *)myNikeName
|
||||
{
|
||||
if (_myNikeName.length == 0) {
|
||||
_myNikeName = [TLUserHelper sharedHelper].user.showName;
|
||||
}
|
||||
return _myNikeName;
|
||||
}
|
||||
|
||||
- (NSString *)pinyin
|
||||
{
|
||||
if (_pinyin == nil) {
|
||||
_pinyin = self.groupName.pinyin;
|
||||
}
|
||||
return _pinyin;
|
||||
}
|
||||
|
||||
- (NSString *)pinyinInitial
|
||||
{
|
||||
if (_pinyinInitial == nil) {
|
||||
_pinyinInitial = self.groupName.pinyinInitial;
|
||||
}
|
||||
return _pinyinInitial;
|
||||
}
|
||||
|
||||
- (NSString *)groupAvatarPath
|
||||
{
|
||||
if (_groupAvatarPath == nil) {
|
||||
_groupAvatarPath = [self.groupID stringByAppendingString:@".png"];
|
||||
}
|
||||
return _groupAvatarPath;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,34 @@
|
||||
//
|
||||
// TLDBFriendSQL.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/22.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TLDBFriendSQL_h
|
||||
#define TLDBFriendSQL_h
|
||||
|
||||
#define FRIENDS_TABLE_NAME @"friends"
|
||||
|
||||
#define SQL_CREATE_FRIENDS_TABLE @"CREATE TABLE IF NOT EXISTS %@(\
|
||||
uid TEXT,\
|
||||
fid TEXT,\
|
||||
username TEXT,\
|
||||
nikename TEXT, \
|
||||
avatar TEXT,\
|
||||
remark TEXT,\
|
||||
ext1 TEXT,\
|
||||
ext2 TEXT,\
|
||||
ext3 TEXT,\
|
||||
ext4 TEXT,\
|
||||
ext5 TEXT,\
|
||||
PRIMARY KEY(uid, fid))"
|
||||
|
||||
#define SQL_UPDATE_FRIEND @"REPLACE INTO %@ ( uid, fid, username, nikename, avatar, remark, ext1, ext2, ext3, ext4, ext5) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
|
||||
#define SQL_SELECT_FRIENDS @"SELECT * FROM %@ WHERE uid = %@"
|
||||
|
||||
#define SQL_DELETE_FRIEND @"DELETE FROM %@ WHERE uid = '%@' and fid = '%@'"
|
||||
|
||||
#endif /* TLDBFriendSQL_h */
|
||||
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// TLDBFriendStore.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/22.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBBaseStore.h"
|
||||
|
||||
@class TLUser;
|
||||
@interface TLDBFriendStore : TLDBBaseStore
|
||||
|
||||
- (BOOL)updateFriendsData:(NSArray *)friendData
|
||||
forUid:(NSString *)uid;
|
||||
|
||||
- (BOOL)addFriend:(TLUser *)user forUid:(NSString *)uid;
|
||||
|
||||
- (NSMutableArray *)friendsDataByUid:(NSString *)uid;
|
||||
|
||||
- (BOOL)deleteFriendByFid:(NSString *)fid forUid:(NSString *)uid;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,106 @@
|
||||
//
|
||||
// TLDBFriendStore.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/22.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBFriendStore.h"
|
||||
#import "TLDBFriendSQL.h"
|
||||
#import "TLDBManager.h"
|
||||
#import "TLUser.h"
|
||||
|
||||
@implementation TLDBFriendStore
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.dbQueue = [TLDBManager sharedInstance].commonQueue;
|
||||
BOOL ok = [self createTable];
|
||||
if (!ok) {
|
||||
DDLogError(@"DB: 好友表创建失败");
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)createTable
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_CREATE_FRIENDS_TABLE, FRIENDS_TABLE_NAME];
|
||||
return [self createTable:FRIENDS_TABLE_NAME withSQL:sqlString];
|
||||
}
|
||||
|
||||
- (BOOL)addFriend:(TLUser *)user forUid:(NSString *)uid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_UPDATE_FRIEND, FRIENDS_TABLE_NAME];
|
||||
NSArray *arrPara = [NSArray arrayWithObjects:
|
||||
TLNoNilString(uid),
|
||||
TLNoNilString(user.userID),
|
||||
TLNoNilString(user.username),
|
||||
TLNoNilString(user.nikeName),
|
||||
TLNoNilString(user.avatarURL),
|
||||
TLNoNilString(user.remarkName),
|
||||
@"", @"", @"", @"", @"", nil];
|
||||
BOOL ok = [self excuteSQL:sqlString withArrParameter:arrPara];
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)updateFriendsData:(NSArray *)friendData forUid:(NSString *)uid
|
||||
{
|
||||
NSArray *oldData = [self friendsDataByUid:uid];
|
||||
if (oldData.count > 0) {
|
||||
// 建立新数据的hash表,用于删除数据库中的过时数据
|
||||
NSMutableDictionary *newDataHash = [[NSMutableDictionary alloc] init];
|
||||
for (TLUser *user in friendData) {
|
||||
[newDataHash setValue:@"YES" forKey:user.userID];
|
||||
}
|
||||
for (TLUser *user in oldData) {
|
||||
if ([newDataHash objectForKey:user.userID] == nil) {
|
||||
BOOL ok = [self deleteFriendByFid:user.userID forUid:uid];
|
||||
if (!ok) {
|
||||
DDLogError(@"DBError: 删除过期好友失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (TLUser *user in friendData) {
|
||||
BOOL ok = [self addFriend:user forUid:uid];
|
||||
if (!ok) {
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSMutableArray *)friendsDataByUid:(NSString *)uid
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_FRIENDS, FRIENDS_TABLE_NAME, uid];
|
||||
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLUser *user = [[TLUser alloc] init];
|
||||
user.userID = [retSet stringForColumn:@"uid"];
|
||||
user.username = [retSet stringForColumn:@"username"];
|
||||
user.nikeName = [retSet stringForColumn:@"nikename"];
|
||||
user.avatarURL = [retSet stringForColumn:@"avatar"];
|
||||
user.remarkName = [retSet stringForColumn:@"remark"];
|
||||
[data addObject:user];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
- (BOOL)deleteFriendByFid:(NSString *)fid forUid:(NSString *)uid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_FRIEND, FRIENDS_TABLE_NAME, uid, fid];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,60 @@
|
||||
//
|
||||
// TLDBGroupSQL.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/4/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TLDBGroupSQL_h
|
||||
#define TLDBGroupSQL_h
|
||||
|
||||
#pragma mark - # GROUPS
|
||||
|
||||
#define GROUPS_TABLE_NAME @"groups"
|
||||
|
||||
#define SQL_CREATE_GROUPS_TABLE @"CREATE TABLE IF NOT EXISTS %@(\
|
||||
uid TEXT,\
|
||||
gid TEXT,\
|
||||
name TEXT,\
|
||||
ext1 TEXT,\
|
||||
ext2 TEXT,\
|
||||
ext3 TEXT,\
|
||||
ext4 TEXT,\
|
||||
ext5 TEXT,\
|
||||
PRIMARY KEY(uid, gid))"
|
||||
|
||||
#define SQL_UPDATE_GROUP @"REPLACE INTO %@ ( uid, gid, name, ext1, ext2, ext3, ext4, ext5) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? )"
|
||||
|
||||
#define SQL_SELECT_GROUPS @"SELECT * FROM %@ WHERE uid = %@"
|
||||
|
||||
#define SQL_DELETE_GROUP @"DELETE FROM %@ WHERE uid = '%@' and gid = '%@'"
|
||||
|
||||
|
||||
|
||||
#pragma mark - # GROUP MEMBERS
|
||||
#define GROUP_MEMBER_TABLE_NAMGE @"group_members"
|
||||
|
||||
#define SQL_CREATE_GROUP_MEMBERS_TABLE @"CREATE TABLE IF NOT EXISTS %@(\
|
||||
uid TEXT,\
|
||||
gid TEXT,\
|
||||
fid TEXT,\
|
||||
username TEXT,\
|
||||
nikename TEXT, \
|
||||
avatar TEXT,\
|
||||
remark TEXT,\
|
||||
ext1 TEXT,\
|
||||
ext2 TEXT,\
|
||||
ext3 TEXT,\
|
||||
ext4 TEXT,\
|
||||
ext5 TEXT,\
|
||||
PRIMARY KEY(uid, gid, fid))"
|
||||
|
||||
#define SQL_UPDATE_GROUP_MEMBER @"REPLACE INTO %@ ( uid, gid, fid, username, nikename, avatar, remark, ext1, ext2, ext3, ext4, ext5) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
|
||||
#define SQL_SELECT_GROUP_MEMBERS @"SELECT * FROM %@ WHERE uid = %@"
|
||||
|
||||
#define SQL_DELETE_GROUP_MEMBER @"DELETE FROM %@ WHERE uid = '%@' and gid = '%@' and fid = '%@'"
|
||||
|
||||
|
||||
#endif /* TLDBGroupSQL_h */
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// TLDBGroupStore.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/4/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBBaseStore.h"
|
||||
#import "TLGroup.h"
|
||||
|
||||
@interface TLDBGroupStore : TLDBBaseStore
|
||||
|
||||
- (BOOL)updateGroupsData:(NSArray *)groupData
|
||||
forUid:(NSString *)uid;
|
||||
|
||||
- (BOOL)addGroup:(TLGroup *)group forUid:(NSString *)uid;
|
||||
|
||||
|
||||
- (NSMutableArray *)groupsDataByUid:(NSString *)uid;
|
||||
|
||||
- (BOOL)deleteGroupByGid:(NSString *)gid forUid:(NSString *)uid;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,188 @@
|
||||
//
|
||||
// TLDBGroupStore.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/4/17.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBGroupStore.h"
|
||||
#import "TLDBGroupSQL.h"
|
||||
#import "TLGroup.h"
|
||||
|
||||
@implementation TLDBGroupStore
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.dbQueue = [TLDBManager sharedInstance].commonQueue;
|
||||
BOOL ok = [self createTable];
|
||||
if (!ok) {
|
||||
DDLogError(@"DB: 讨论组表创建失败");
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)createTable
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_CREATE_GROUPS_TABLE, GROUPS_TABLE_NAME];
|
||||
BOOL ok = [self createTable:GROUPS_TABLE_NAME withSQL:sqlString];
|
||||
if (ok) {
|
||||
sqlString = [NSString stringWithFormat:SQL_CREATE_GROUP_MEMBERS_TABLE, GROUP_MEMBER_TABLE_NAMGE];
|
||||
ok = [self createTable:GROUP_MEMBER_TABLE_NAMGE withSQL:sqlString];
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)addGroup:(TLGroup *)group forUid:(NSString *)uid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_UPDATE_GROUP, GROUPS_TABLE_NAME];
|
||||
NSArray *arrPara = [NSArray arrayWithObjects:
|
||||
TLNoNilString(uid),
|
||||
TLNoNilString(group.groupID),
|
||||
TLNoNilString(group.groupName),
|
||||
@"", @"", @"", @"", @"", nil];
|
||||
BOOL ok = [self excuteSQL:sqlString withArrParameter:arrPara];
|
||||
if (ok) {
|
||||
// 将通讯录成员插入数据库
|
||||
ok = [self addGroupMembers:group.users forUid:uid andGid:group.groupID];
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)updateGroupsData:(NSArray *)groupData forUid:(NSString *)uid
|
||||
{
|
||||
NSArray *oldData = [self groupsDataByUid:uid];
|
||||
if (oldData.count > 0) {
|
||||
// 建立新数据的hash表,用于删除数据库中的过时数据
|
||||
NSMutableDictionary *newDataHash = [[NSMutableDictionary alloc] init];
|
||||
for (TLGroup *group in groupData) {
|
||||
[newDataHash setValue:@"YES" forKey:group.groupID];
|
||||
}
|
||||
for (TLGroup *group in oldData) {
|
||||
if ([newDataHash objectForKey:group.groupID] == nil) {
|
||||
BOOL ok = [self deleteGroupByGid:group.groupID forUid:uid];
|
||||
if (!ok) {
|
||||
DDLogError(@"DBError: 删除过期讨论组失败!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 将数据插入数据库
|
||||
for (TLGroup *group in groupData) {
|
||||
BOOL ok = [self addGroup:group forUid:uid];
|
||||
if (!ok) {
|
||||
return ok;
|
||||
}
|
||||
}
|
||||
|
||||
return YES;
|
||||
}
|
||||
|
||||
|
||||
- (NSMutableArray *)groupsDataByUid:(NSString *)uid
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_GROUPS, GROUPS_TABLE_NAME, uid];
|
||||
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLGroup *group = [[TLGroup alloc] init];
|
||||
group.groupID = [retSet stringForColumn:@"gid"];
|
||||
group.groupName = [retSet stringForColumn:@"name"];
|
||||
[data addObject:group];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
|
||||
// 获取讨论组成员
|
||||
for (TLGroup *group in data) {
|
||||
group.users = [self groupMembersForUid:uid andGid:group.groupID];
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
- (BOOL)deleteGroupByGid:(NSString *)gid forUid:(NSString *)uid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_GROUP, GROUPS_TABLE_NAME, uid, gid];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - # Group Members
|
||||
- (BOOL)addGroupMember:(TLUser *)user forUid:(NSString *)uid andGid:(NSString *)gid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_UPDATE_GROUP_MEMBER, GROUP_MEMBER_TABLE_NAMGE];
|
||||
NSArray *arrPara = [NSArray arrayWithObjects:
|
||||
TLNoNilString(uid),
|
||||
TLNoNilString(gid),
|
||||
TLNoNilString(user.userID),
|
||||
TLNoNilString(user.username),
|
||||
TLNoNilString(user.nikeName),
|
||||
TLNoNilString(user.avatarURL),
|
||||
TLNoNilString(user.remarkName),
|
||||
@"", @"", @"", @"", @"", nil];
|
||||
BOOL ok = [self excuteSQL:sqlString withArrParameter:arrPara];
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)addGroupMembers:(NSArray *)users forUid:(NSString *)uid andGid:(NSString *)gid
|
||||
{
|
||||
NSArray *oldData = [self groupMembersForUid:uid andGid:gid];
|
||||
if (oldData.count > 0) {
|
||||
// 建立新数据的hash表,用于删除数据库中的过时数据
|
||||
NSMutableDictionary *newDataHash = [[NSMutableDictionary alloc] init];
|
||||
for (TLUser *user in users) {
|
||||
[newDataHash setValue:@"YES" forKey:user.userID];
|
||||
}
|
||||
for (TLUser *user in oldData) {
|
||||
if ([newDataHash objectForKey:user.userID] == nil) {
|
||||
BOOL ok = [self deleteGroupMemberForUid:uid gid:gid andFid:user.userID];
|
||||
if (!ok) {
|
||||
DDLogError(@"DBError: 删除过期好友失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (TLUser *user in users) {
|
||||
BOOL ok = [self addGroupMember:user forUid:uid andGid:gid];
|
||||
if (!ok) {
|
||||
return NO;
|
||||
}
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (NSMutableArray *)groupMembersForUid:(NSString *)uid andGid:(NSString *)gid
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_GROUP_MEMBERS, GROUP_MEMBER_TABLE_NAMGE, uid];
|
||||
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLUser *user = [[TLUser alloc] init];
|
||||
user.userID = [retSet stringForColumn:@"uid"];
|
||||
user.username = [retSet stringForColumn:@"username"];
|
||||
user.nikeName = [retSet stringForColumn:@"nikename"];
|
||||
user.avatarURL = [retSet stringForColumn:@"avatar"];
|
||||
user.remarkName = [retSet stringForColumn:@"remark"];
|
||||
[data addObject:user];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
- (BOOL)deleteGroupMemberForUid:(NSString *)uid gid:(NSString *)gid andFid:(NSString *)fid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_GROUP_MEMBER, GROUP_MEMBER_TABLE_NAMGE, uid, gid, fid];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// TLFriendHelper.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/1/27.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLUserGroup.h"
|
||||
#import "TLGroup.h"
|
||||
|
||||
@interface TLFriendHelper : NSObject
|
||||
|
||||
#pragma mark - # 好友
|
||||
|
||||
/// 好友数据(原始)
|
||||
@property (nonatomic, strong, readonly) NSMutableArray *friendsData;
|
||||
|
||||
/// 格式化的好友数据(二维数组,列表用)
|
||||
@property (nonatomic, strong) NSMutableArray *data;
|
||||
|
||||
/// 格式化好友数据的分组标题
|
||||
@property (nonatomic, strong) NSMutableArray *sectionHeaders;
|
||||
|
||||
/// 好友数量
|
||||
@property (nonatomic, assign, readonly) NSInteger friendCount;
|
||||
|
||||
@property (nonatomic, strong) void(^dataChangedBlock)(NSMutableArray *friends, NSMutableArray *headers, NSInteger friendCount);
|
||||
|
||||
|
||||
#pragma mark - # 群
|
||||
/// 群数据
|
||||
@property (nonatomic, strong) NSMutableArray *groupsData;
|
||||
|
||||
|
||||
#pragma mark - # 标签
|
||||
/// 标签数据
|
||||
@property (nonatomic, strong) NSMutableArray *tagsData;
|
||||
|
||||
|
||||
+ (TLFriendHelper *)sharedFriendHelper;
|
||||
|
||||
- (TLUser *)getFriendInfoByUserID:(NSString *)userID;
|
||||
|
||||
- (TLGroup *)getGroupInfoByGroupID:(NSString *)groupID;
|
||||
|
||||
- (BOOL)deleteFriendByUserId:(NSString *)userID;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,243 @@
|
||||
//
|
||||
// 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
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// TLUserHelper.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/6.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLUser.h"
|
||||
|
||||
@interface TLUserHelper : NSObject
|
||||
|
||||
@property (nonatomic, strong) TLUser *user;
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *userID;
|
||||
|
||||
@property (nonatomic, assign, readonly) BOOL isLogin;
|
||||
|
||||
+ (TLUserHelper *)sharedHelper;
|
||||
|
||||
- (void)loginTestAccount;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// TLUserHelper.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/6.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLUserHelper.h"
|
||||
#import "TLDBUserStore.h"
|
||||
|
||||
@implementation TLUserHelper
|
||||
@synthesize user = _user;
|
||||
|
||||
+ (TLUserHelper *)sharedHelper
|
||||
{
|
||||
static TLUserHelper *helper;
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{
|
||||
helper = [[TLUserHelper alloc] init];
|
||||
});
|
||||
return helper;
|
||||
}
|
||||
|
||||
- (void)loginTestAccount
|
||||
{
|
||||
TLUser *user = [[TLUser alloc] init];
|
||||
user.userID = @"1000";
|
||||
user.avatarURL = @"http://p1.qq181.com/cms/120506/2012050623111097826.jpg";
|
||||
user.nikeName = @"李伯坤";
|
||||
user.username = @"li-bokun";
|
||||
user.detailInfo.qqNumber = @"1159197873";
|
||||
user.detailInfo.email = @"libokun@126.com";
|
||||
user.detailInfo.location = @"山东 滨州";
|
||||
user.detailInfo.sex = @"男";
|
||||
user.detailInfo.motto = @"Hello world!";
|
||||
user.detailInfo.momentsWallURL = @"http://pic1.win4000.com/wallpaper/c/5791e49b37a5c.jpg";
|
||||
|
||||
[self setUser:user];
|
||||
}
|
||||
|
||||
- (void)setUser:(TLUser *)user
|
||||
{
|
||||
_user = user;
|
||||
TLDBUserStore *userStore = [[TLDBUserStore alloc] init];
|
||||
if (![userStore updateUser:user]) {
|
||||
DDLogError(@"登录数据存库失败");
|
||||
}
|
||||
|
||||
[[NSUserDefaults standardUserDefaults] setObject:user.userID forKey:@"loginUid"];
|
||||
}
|
||||
- (TLUser *)user
|
||||
{
|
||||
if (!_user) {
|
||||
if (self.userID.length > 0) {
|
||||
TLDBUserStore *userStore = [[TLDBUserStore alloc] init];
|
||||
_user = [userStore userByID:self.userID];
|
||||
_user.detailInfo.momentsWallURL = @"http://pic1.win4000.com/wallpaper/c/5791e49b37a5c.jpg";
|
||||
if (!_user) {
|
||||
[[NSUserDefaults standardUserDefaults] setObject:@"" forKey:@"loginUid"];
|
||||
}
|
||||
}
|
||||
}
|
||||
return _user;
|
||||
}
|
||||
|
||||
- (NSString *)userID
|
||||
{
|
||||
NSString *uid = [[NSUserDefaults standardUserDefaults] objectForKey:@"loginUid"];
|
||||
return uid;
|
||||
}
|
||||
|
||||
- (BOOL)isLogin
|
||||
{
|
||||
return self.user.userID.length > 0;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user