chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// TLEmojiKBHelper.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLExpressionGroupModel.h"
|
||||
|
||||
@interface TLEmojiKBHelper : NSObject
|
||||
|
||||
+ (TLEmojiKBHelper *)sharedKBHelper;
|
||||
|
||||
- (void)emojiGroupDataByUserID:(NSString *)userID complete:(void (^)(NSMutableArray *))complete;
|
||||
|
||||
- (void)updateEmojiGroupData;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// TLEmojiKBHelper.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLEmojiKBHelper.h"
|
||||
#import "TLExpressionHelper.h"
|
||||
#import "TLExpressionGroupModel.h"
|
||||
|
||||
static TLEmojiKBHelper *helper;
|
||||
|
||||
@interface TLEmojiKBHelper ()
|
||||
|
||||
@property (nonatomic, strong) NSString *userID;
|
||||
|
||||
@property (nonatomic, strong) TLExpressionGroupModel *systemEditGroup;
|
||||
|
||||
@property (nonatomic, strong) void (^complete)(NSMutableArray *);
|
||||
|
||||
@end
|
||||
|
||||
@implementation TLEmojiKBHelper
|
||||
|
||||
+ (TLEmojiKBHelper *)sharedKBHelper
|
||||
{
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{
|
||||
helper = [[TLEmojiKBHelper alloc] init];
|
||||
});
|
||||
return helper;
|
||||
}
|
||||
|
||||
- (void)updateEmojiGroupData
|
||||
{
|
||||
if (self.userID && self.complete) {
|
||||
[self emojiGroupDataByUserID:self.userID complete:self.complete];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)emojiGroupDataByUserID:(NSString *)userID complete:(void (^)(NSMutableArray *))complete
|
||||
{
|
||||
self.userID = userID;
|
||||
self.complete = complete;
|
||||
dispatch_async(dispatch_get_global_queue(0, 0), ^{
|
||||
NSMutableArray *emojiGroupData = [[NSMutableArray alloc] init];
|
||||
|
||||
// 默认表情包
|
||||
[emojiGroupData addObject:[TLExpressionHelper sharedHelper].defaultFaceGroup];
|
||||
[emojiGroupData addObject:[TLExpressionHelper sharedHelper].defaultSystemEmojiGroup];
|
||||
|
||||
// 用户收藏的表情包
|
||||
TLExpressionGroupModel *preferEmojiGroup = [TLExpressionHelper sharedHelper].userPreferEmojiGroup;
|
||||
if (preferEmojiGroup && preferEmojiGroup.count > 0) {
|
||||
[emojiGroupData addObject:preferEmojiGroup];
|
||||
}
|
||||
|
||||
// 用户的表情包
|
||||
NSArray *userGroups = [TLExpressionHelper sharedHelper].userEmojiGroups;
|
||||
if (userGroups && userGroups.count > 0) {
|
||||
[emojiGroupData addObjectsFromArray:userGroups];
|
||||
}
|
||||
|
||||
// 系统设置
|
||||
[emojiGroupData addObject:self.systemEditGroup];
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
complete(emojiGroupData);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - # Getter
|
||||
- (TLExpressionGroupModel *)systemEditGroup
|
||||
{
|
||||
if (_systemEditGroup == nil) {
|
||||
_systemEditGroup = [[TLExpressionGroupModel alloc] init];
|
||||
_systemEditGroup.type = TLEmojiTypeOther;
|
||||
_systemEditGroup.iconPath = @"emojiKB_settingBtn";
|
||||
}
|
||||
return _systemEditGroup;
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// TLMoreKBHelper.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/18.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface TLMoreKBHelper : NSObject
|
||||
|
||||
@property (nonatomic, strong) NSMutableArray *chatMoreKeyboardData;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// TLMoreKBHelper.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/18.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMoreKBHelper.h"
|
||||
#import "TLMoreKeyboardItem.h"
|
||||
|
||||
@implementation TLMoreKBHelper
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.chatMoreKeyboardData = [[NSMutableArray alloc] init];
|
||||
[self p_initTestData];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)p_initTestData
|
||||
{
|
||||
TLMoreKeyboardItem *imageItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeImage
|
||||
title:@"照片"
|
||||
imagePath:@"moreKB_image"];
|
||||
TLMoreKeyboardItem *cameraItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeCamera
|
||||
title:@"拍摄"
|
||||
imagePath:@"moreKB_video"];
|
||||
TLMoreKeyboardItem *videoItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeVideo
|
||||
title:@"小视频"
|
||||
imagePath:@"moreKB_sight"];
|
||||
TLMoreKeyboardItem *videoCallItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeVideoCall
|
||||
title:@"视频聊天"
|
||||
imagePath:@"moreKB_video_call"];
|
||||
TLMoreKeyboardItem *walletItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeWallet
|
||||
title:@"红包"
|
||||
imagePath:@"moreKB_wallet"];
|
||||
TLMoreKeyboardItem *transferItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeTransfer
|
||||
title:@"转账"
|
||||
imagePath:@"moreKB_pay"];
|
||||
TLMoreKeyboardItem *positionItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypePosition
|
||||
title:@"位置"
|
||||
imagePath:@"moreKB_location"];
|
||||
TLMoreKeyboardItem *favoriteItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeFavorite
|
||||
title:@"收藏"
|
||||
imagePath:@"moreKB_favorite"];
|
||||
TLMoreKeyboardItem *businessCardItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeBusinessCard
|
||||
title:@"个人名片"
|
||||
imagePath:@"moreKB_friendcard" ];
|
||||
TLMoreKeyboardItem *voiceItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeVoice
|
||||
title:@"语音输入"
|
||||
imagePath:@"moreKB_voice"];
|
||||
TLMoreKeyboardItem *cardsItem = [TLMoreKeyboardItem createByType:TLMoreKeyboardItemTypeCards
|
||||
title:@"卡券"
|
||||
imagePath:@"moreKB_wallet"];
|
||||
[self.chatMoreKeyboardData addObjectsFromArray:@[imageItem, cameraItem, videoItem, videoCallItem, walletItem, transferItem, positionItem, favoriteItem, businessCardItem, voiceItem, cardsItem]];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,40 @@
|
||||
|
||||
//
|
||||
// TLDBConversationSQL.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TLDBConversationSQL_h
|
||||
#define TLDBConversationSQL_h
|
||||
|
||||
#define CONV_TABLE_NAME @"conversation"
|
||||
|
||||
|
||||
#define SQL_CREATE_CONV_TABLE @"CREATE TABLE IF NOT EXISTS %@(\
|
||||
uid TEXT,\
|
||||
fid TEXT,\
|
||||
conv_type INTEGER DEFAULT (0), \
|
||||
date TEXT,\
|
||||
unread_count INTEGER DEFAULT (0),\
|
||||
ext1 TEXT,\
|
||||
ext2 TEXT,\
|
||||
ext3 TEXT,\
|
||||
ext4 TEXT,\
|
||||
ext5 TEXT,\
|
||||
PRIMARY KEY(uid, fid))"
|
||||
|
||||
|
||||
#define SQL_ADD_CONV @"REPLACE INTO %@ ( uid, fid, conv_type, date, unread_count, ext1, ext2, ext3, ext4, ext5) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
|
||||
|
||||
#define SQL_SELECT_CONVS @"SELECT * FROM %@ WHERE uid = %@ ORDER BY date DESC"
|
||||
#define SQL_SELECT_CONV_UNREAD @"SELECT unread_count FROM %@ WHERE uid = '%@' and fid = '%@'"
|
||||
|
||||
|
||||
#define SQL_DELETE_CONV @"DELETE FROM %@ WHERE uid = '%@' and fid = '%@'"
|
||||
#define SQL_DELETE_ALL_CONVS @"DELETE FROM %@ WHERE uid = '%@'"
|
||||
|
||||
#endif /* TLDBConversationSQL_h */
|
||||
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// TLDBConversationStore.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBBaseStore.h"
|
||||
|
||||
@interface TLDBConversationStore : TLDBBaseStore
|
||||
|
||||
/**
|
||||
* 新的会话(未读)
|
||||
*/
|
||||
- (BOOL)addConversationByUid:(NSString *)uid fid:(NSString *)fid type:(NSInteger)type date:(NSDate *)date;
|
||||
|
||||
/**
|
||||
* 更新会话状态(已读)
|
||||
*/
|
||||
- (void)updateConversationByUid:(NSString *)uid fid:(NSString *)fid;
|
||||
|
||||
/**
|
||||
* 查询所有会话
|
||||
*/
|
||||
- (NSArray *)conversationsByUid:(NSString *)uid;
|
||||
|
||||
/**
|
||||
* 未读消息数
|
||||
*/
|
||||
- (NSInteger)unreadMessageByUid:(NSString *)uid fid:(NSString *)fid;
|
||||
|
||||
/**
|
||||
* 删除单条会话
|
||||
*/
|
||||
- (BOOL)deleteConversationByUid:(NSString *)uid fid:(NSString *)fid;
|
||||
|
||||
/**
|
||||
* 删除用户的所有会话
|
||||
*/
|
||||
- (BOOL)deleteConversationsByUid:(NSString *)uid;
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,140 @@
|
||||
//
|
||||
// TLDBConversationStore.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBConversationStore.h"
|
||||
#import "TLDBMessageStore.h"
|
||||
#import "TLDBConversationSQL.h"
|
||||
#import "TLDBManager.h"
|
||||
#import "TLConversation.h"
|
||||
#import "TLMacros.h"
|
||||
|
||||
@interface TLDBConversationStore ()
|
||||
|
||||
@property (nonatomic, strong) TLDBMessageStore *messageStore;
|
||||
|
||||
@end
|
||||
|
||||
@implementation TLDBConversationStore
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.dbQueue = [TLDBManager sharedInstance].messageQueue;
|
||||
BOOL ok = [self createTable];
|
||||
if (!ok) {
|
||||
DDLogError(@"DB: 聊天记录表创建失败");
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)createTable
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_CREATE_CONV_TABLE, CONV_TABLE_NAME];
|
||||
return [self createTable:CONV_TABLE_NAME withSQL:sqlString];
|
||||
}
|
||||
|
||||
- (BOOL)addConversationByUid:(NSString *)uid fid:(NSString *)fid type:(NSInteger)type date:(NSDate *)date;
|
||||
{
|
||||
NSInteger unreadCount = [self unreadMessageByUid:uid fid:fid] + 1;
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_ADD_CONV, CONV_TABLE_NAME];
|
||||
NSArray *arrPara = [NSArray arrayWithObjects:
|
||||
uid,
|
||||
fid,
|
||||
[NSNumber numberWithInteger:type],
|
||||
TLTimeStamp(date),
|
||||
[NSNumber numberWithInteger:unreadCount],
|
||||
@"", @"", @"", @"", @"", nil];
|
||||
BOOL ok = [self excuteSQL:sqlString withArrParameter:arrPara];
|
||||
return ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新会话状态(已读)
|
||||
*/
|
||||
- (void)updateConversationByUid:(NSString *)uid fid:(NSString *)fid
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有会话
|
||||
*/
|
||||
- (NSArray *)conversationsByUid:(NSString *)uid
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat: SQL_SELECT_CONVS, CONV_TABLE_NAME, uid];
|
||||
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLConversation *conversation = [[TLConversation alloc] init];
|
||||
conversation.partnerID = [retSet stringForColumn:@"fid"];
|
||||
conversation.convType = [retSet intForColumn:@"conv_type"];
|
||||
NSString *dateString = [retSet stringForColumn:@"date"];
|
||||
conversation.date = [NSDate dateWithTimeIntervalSince1970:dateString.doubleValue];
|
||||
conversation.unreadCount = @([retSet intForColumn:@"unread_count"]).stringValue;
|
||||
[data addObject:conversation];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
|
||||
// 获取conv对应的msg
|
||||
for (TLConversation *conversation in data) {
|
||||
TLMessage * message = [self.messageStore lastMessageByUserID:uid partnerID:conversation.partnerID];
|
||||
if (message) {
|
||||
conversation.content = [message conversationContent];
|
||||
conversation.date = message.date;
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
- (NSInteger)unreadMessageByUid:(NSString *)uid fid:(NSString *)fid
|
||||
{
|
||||
__block NSInteger unreadCount = 0;
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_CONV_UNREAD, CONV_TABLE_NAME, uid, fid];
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
if ([retSet next]) {
|
||||
unreadCount = [retSet intForColumn:@"unread_count"];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
return unreadCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除单条会话
|
||||
*/
|
||||
- (BOOL)deleteConversationByUid:(NSString *)uid fid:(NSString *)fid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_CONV, CONV_TABLE_NAME, uid, fid];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户的所有会话
|
||||
*/
|
||||
- (BOOL)deleteConversationsByUid:(NSString *)uid
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_ALL_CONVS, CONV_TABLE_NAME, uid];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
#pragma mark - Getter -
|
||||
- (TLDBMessageStore *)messageStore
|
||||
{
|
||||
if (_messageStore == nil) {
|
||||
_messageStore = [[TLDBMessageStore alloc] init];
|
||||
}
|
||||
return _messageStore;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// TLDBMessageStore.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/13.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBBaseStore.h"
|
||||
#import "TLMessage.h"
|
||||
|
||||
@interface TLDBMessageStore : TLDBBaseStore
|
||||
|
||||
#pragma mark - 添加
|
||||
/**
|
||||
* 添加消息记录
|
||||
*/
|
||||
- (BOOL)addMessage:(TLMessage *)message;
|
||||
|
||||
#pragma mark - 查询
|
||||
/**
|
||||
* 获取与某个好友的聊天记录
|
||||
*/
|
||||
- (void)messagesByUserID:(NSString *)userID
|
||||
partnerID:(NSString *)partnerID
|
||||
fromDate:(NSDate *)date
|
||||
count:(NSUInteger)count
|
||||
complete:(void (^)(NSArray *data, BOOL hasMore))complete;
|
||||
|
||||
/**
|
||||
* 获取与某个好友/讨论组的聊天文件
|
||||
*/
|
||||
- (NSArray *)chatFilesByUserID:(NSString *)userID partnerID:(NSString *)partnerID;
|
||||
|
||||
/**
|
||||
* 获取与某个好友/讨论组的聊天图片及视频
|
||||
*/
|
||||
- (NSArray *)chatImagesAndVideosByUserID:(NSString *)userID partnerID:(NSString *)partnerID;
|
||||
|
||||
/**
|
||||
* 最后一条聊天记录(消息页用)
|
||||
*/
|
||||
- (TLMessage *)lastMessageByUserID:(NSString *)userID partnerID:(NSString *)partnerID;
|
||||
|
||||
#pragma mark - 删除
|
||||
/**
|
||||
* 删除单条消息
|
||||
*/
|
||||
- (BOOL)deleteMessageByMessageID:(NSString *)messageID;
|
||||
|
||||
/**
|
||||
* 删除与某个好友/讨论组的所有聊天记录
|
||||
*/
|
||||
- (BOOL)deleteMessagesByUserID:(NSString *)userID partnerID:(NSString *)partnerID;
|
||||
|
||||
/**
|
||||
* 删除用户的所有聊天记录
|
||||
*/
|
||||
- (BOOL)deleteMessagesByUserID:(NSString *)userID;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,198 @@
|
||||
//
|
||||
// TLDBMessageStore.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/13.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLDBMessageStore.h"
|
||||
#import "TLDBMessageStoreSQL.h"
|
||||
#import "TLMacros.h"
|
||||
|
||||
@implementation TLDBMessageStore
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.dbQueue = [TLDBManager sharedInstance].messageQueue;
|
||||
BOOL ok = [self createTable];
|
||||
if (!ok) {
|
||||
DDLogError(@"DB: 聊天记录表创建失败");
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)createTable
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_CREATE_MESSAGE_TABLE, MESSAGE_TABLE_NAME];
|
||||
return [self createTable:MESSAGE_TABLE_NAME withSQL:sqlString];
|
||||
}
|
||||
|
||||
- (BOOL)addMessage:(TLMessage *)message
|
||||
{
|
||||
if (message == nil || message.messageID == nil || message.userID == nil || (message.friendID == nil && message.groupID == nil)) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
NSString *fid = @"";
|
||||
NSString *subfid;
|
||||
if (message.partnerType == TLPartnerTypeUser) {
|
||||
fid = message.friendID;
|
||||
}
|
||||
else {
|
||||
fid = message.groupID;
|
||||
subfid = message.friendID;
|
||||
}
|
||||
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_ADD_MESSAGE, MESSAGE_TABLE_NAME];
|
||||
NSArray *arrPara = [NSArray arrayWithObjects:
|
||||
message.messageID,
|
||||
message.userID,
|
||||
fid,
|
||||
TLNoNilString(subfid),
|
||||
TLTimeStamp(message.date),
|
||||
[NSNumber numberWithInteger:message.partnerType],
|
||||
[NSNumber numberWithInteger:message.ownerTyper],
|
||||
[NSNumber numberWithInteger:message.messageType],
|
||||
[message.content mj_JSONString],
|
||||
[NSNumber numberWithInteger:message.sendState],
|
||||
[NSNumber numberWithInteger:message.readState],
|
||||
@"", @"", @"", @"", @"", nil];
|
||||
BOOL ok = [self excuteSQL:sqlString withArrParameter:arrPara];
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (void)messagesByUserID:(NSString *)userID partnerID:(NSString *)partnerID fromDate:(NSDate *)date count:(NSUInteger)count complete:(void (^)(NSArray *, BOOL))complete
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat:
|
||||
SQL_SELECT_MESSAGES_PAGE,
|
||||
MESSAGE_TABLE_NAME,
|
||||
userID,
|
||||
partnerID,
|
||||
[NSString stringWithFormat:@"%lf", date.timeIntervalSince1970],
|
||||
(long)(count + 1)];
|
||||
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLMessage *message = [self p_createDBMessageByFMResultSet:retSet];
|
||||
[data insertObject:message atIndex:0];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
|
||||
BOOL hasMore = NO;
|
||||
if (data.count == count + 1) {
|
||||
hasMore = YES;
|
||||
[data removeObjectAtIndex:0];
|
||||
}
|
||||
complete(data, hasMore);
|
||||
}
|
||||
|
||||
- (NSArray *)chatFilesByUserID:(NSString *)userID partnerID:(NSString *)partnerID
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_CHAT_FILES, MESSAGE_TABLE_NAME, userID, partnerID];
|
||||
|
||||
__block NSDate *lastDate = [NSDate date];
|
||||
__block NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLMessage * message = [self p_createDBMessageByFMResultSet:retSet];
|
||||
if (([message.date isThisWeek] && [lastDate isThisWeek]) || (![message.date isThisWeek] && [lastDate isSameMonthAsDate:message.date])) {
|
||||
[array addObject:message];
|
||||
}
|
||||
else {
|
||||
lastDate = message.date;
|
||||
if (array.count > 0) {
|
||||
[data addObject:array];
|
||||
}
|
||||
array = [[NSMutableArray alloc] initWithObjects:message, nil];
|
||||
}
|
||||
}
|
||||
if (array.count > 0) {
|
||||
[data addObject:array];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
return data;
|
||||
}
|
||||
|
||||
- (NSArray *)chatImagesAndVideosByUserID:(NSString *)userID partnerID:(NSString *)partnerID
|
||||
{
|
||||
__block NSMutableArray *data = [[NSMutableArray alloc] init];
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_CHAT_MEDIA, MESSAGE_TABLE_NAME, userID, partnerID];
|
||||
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
TLMessage *message = [self p_createDBMessageByFMResultSet:retSet];
|
||||
[data addObject:message];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
return data;
|
||||
}
|
||||
|
||||
- (TLMessage *)lastMessageByUserID:(NSString *)userID partnerID:(NSString *)partnerID
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_SELECT_LAST_MESSAGE, MESSAGE_TABLE_NAME, MESSAGE_TABLE_NAME, userID, partnerID];
|
||||
__block TLMessage * message;
|
||||
[self excuteQuerySQL:sqlString resultBlock:^(FMResultSet *retSet) {
|
||||
while ([retSet next]) {
|
||||
message = [self p_createDBMessageByFMResultSet:retSet];
|
||||
}
|
||||
[retSet close];
|
||||
}];
|
||||
return message;
|
||||
}
|
||||
|
||||
- (BOOL)deleteMessageByMessageID:(NSString *)messageID
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_MESSAGE, MESSAGE_TABLE_NAME, messageID];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)deleteMessagesByUserID:(NSString *)userID partnerID:(NSString *)partnerID;
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_FRIEND_MESSAGES, MESSAGE_TABLE_NAME, userID, partnerID];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)deleteMessagesByUserID:(NSString *)userID
|
||||
{
|
||||
NSString *sqlString = [NSString stringWithFormat:SQL_DELETE_USER_MESSAGES, MESSAGE_TABLE_NAME, userID];
|
||||
BOOL ok = [self excuteSQL:sqlString, nil];
|
||||
return ok;
|
||||
}
|
||||
|
||||
#pragma mark - Private Methods -
|
||||
- (TLMessage *)p_createDBMessageByFMResultSet:(FMResultSet *)retSet
|
||||
{
|
||||
TLMessageType type = [retSet intForColumn:@"msg_type"];
|
||||
TLMessage * message = [TLMessage createMessageByType:type];
|
||||
message.messageID = [retSet stringForColumn:@"msgid"];
|
||||
message.userID = [retSet stringForColumn:@"uid"];
|
||||
message.partnerType = [retSet intForColumn:@"partner_type"];
|
||||
if (message.partnerType == TLPartnerTypeGroup) {
|
||||
message.groupID = [retSet stringForColumn:@"fid"];
|
||||
message.friendID = [retSet stringForColumn:@"subfid"];
|
||||
}
|
||||
else {
|
||||
message.friendID = [retSet stringForColumn:@"fid"];
|
||||
message.groupID = [retSet stringForColumn:@"subfid"];
|
||||
}
|
||||
NSString *dateString = [retSet stringForColumn:@"date"];
|
||||
message.date = [NSDate dateWithTimeIntervalSince1970:dateString.doubleValue];
|
||||
message.ownerTyper = [retSet intForColumn:@"own_type"];
|
||||
NSString *content = [retSet stringForColumn:@"content"];
|
||||
message.content = [[NSMutableDictionary alloc] initWithDictionary:[content mj_JSONObject]];
|
||||
message.sendState = [retSet intForColumn:@"send_status"];
|
||||
message.readState = [retSet intForColumn:@"received_status"];
|
||||
return message;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,49 @@
|
||||
//
|
||||
// TLDBMessageStoreSQL.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/13.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TLDBMessageStoreSQL_h
|
||||
#define TLDBMessageStoreSQL_h
|
||||
|
||||
#define MESSAGE_TABLE_NAME @"message"
|
||||
|
||||
|
||||
#define SQL_CREATE_MESSAGE_TABLE @"CREATE TABLE IF NOT EXISTS %@(\
|
||||
msgid TEXT,\
|
||||
uid TEXT,\
|
||||
fid TEXT,\
|
||||
subfid TEXT,\
|
||||
date TEXT,\
|
||||
partner_type INTEGER DEFAULT (0),\
|
||||
own_type INTEGER DEFAULT (0),\
|
||||
msg_type INTEGER DEFAULT (0),\
|
||||
content TEXT,\
|
||||
send_status INTEGER DEFAULT (0),\
|
||||
received_status BOOLEAN DEFAULT (0),\
|
||||
ext1 TEXT,\
|
||||
ext2 TEXT,\
|
||||
ext3 TEXT,\
|
||||
ext4 TEXT,\
|
||||
ext5 TEXT,\
|
||||
PRIMARY KEY(uid, msgid, fid, subfid))"
|
||||
|
||||
|
||||
#define SQL_ADD_MESSAGE @"REPLACE INTO %@ ( msgid, uid, fid, subfid, date, partner_type, own_type, msg_type, content, send_status, received_status, ext1, ext2, ext3, ext4, ext5) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||
|
||||
|
||||
#define SQL_SELECT_MESSAGES_PAGE @"SELECT * FROM %@ WHERE uid = '%@' and fid = '%@' and date < '%@' order by date desc LIMIT '%ld'"
|
||||
#define SQL_SELECT_CHAT_FILES @"SELECT * FROM %@ WHERE uid = '%@' and fid = '%@' and msg_type = '2'"
|
||||
#define SQL_SELECT_CHAT_MEDIA @"SELECT * FROM %@ WHERE uid = '%@' and fid = '%@' and msg_type = '2'"
|
||||
#define SQL_SELECT_LAST_MESSAGE @"SELECT * FROM %@ WHERE date = ( SELECT MAX(date) FROM %@ WHERE uid = '%@' and fid = '%@' )"
|
||||
|
||||
|
||||
#define SQL_DELETE_MESSAGE @"DELETE FROM %@ WHERE msgid = '%@'"
|
||||
#define SQL_DELETE_FRIEND_MESSAGES @"DELETE FROM %@ WHERE uid = '%@' and fid = '%@'"
|
||||
#define SQL_DELETE_USER_MESSAGES @"DELETE FROM %@ WHERE uid = '%@'"
|
||||
|
||||
|
||||
#endif /* TLDBMessageStoreSQL_h */
|
||||
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TLConversation+TLUser.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/21.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLConversation.h"
|
||||
#import "TLUser+ChatModel.h"
|
||||
#import "TLGroup+ChatModel.h"
|
||||
|
||||
@interface TLConversation (TLUser)
|
||||
|
||||
- (void)updateUserInfo:(TLUser *)user;
|
||||
|
||||
- (void)updateGroupInfo:(TLGroup *)group;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,26 @@
|
||||
//
|
||||
// TLConversation+TLUser.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/21.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLConversation+TLUser.h"
|
||||
|
||||
@implementation TLConversation (TLUser)
|
||||
|
||||
- (void)updateUserInfo:(TLUser *)user
|
||||
{
|
||||
self.partnerName = user.showName;
|
||||
self.avatarPath = user.avatarPath;
|
||||
self.avatarURL = user.avatarURL;
|
||||
}
|
||||
|
||||
- (void)updateGroupInfo:(TLGroup *)group
|
||||
{
|
||||
self.partnerName = group.groupName;
|
||||
self.avatarPath = group.groupAvatarPath;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,78 @@
|
||||
//
|
||||
// TLConversation.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/1/23.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
/**
|
||||
* 会话类型
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, TLConversationType) {
|
||||
TLConversationTypePersonal, // 个人
|
||||
TLConversationTypeGroup, // 群聊
|
||||
TLConversationTypePublic, // 公众号
|
||||
TLConversationTypeServerGroup, // 服务组(订阅号、企业号)
|
||||
};
|
||||
|
||||
typedef NS_ENUM(NSInteger, TLMessageRemindType) {
|
||||
TLMessageRemindTypeNormal, // 正常接受
|
||||
TLMessageRemindTypeClosed, // 不提示
|
||||
TLMessageRemindTypeNotLook, // 不看
|
||||
TLMessageRemindTypeUnlike, // 不喜欢
|
||||
};
|
||||
|
||||
@interface TLConversation : NSObject
|
||||
|
||||
|
||||
/**
|
||||
* 会话类型(个人,讨论组,企业号)
|
||||
*/
|
||||
@property (nonatomic, assign) TLConversationType convType;
|
||||
|
||||
/**
|
||||
* 消息提醒类型
|
||||
*/
|
||||
@property (nonatomic, assign) TLMessageRemindType remindType;
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *partnerID;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *partnerName;
|
||||
|
||||
/**
|
||||
* 头像地址(网络)
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *avatarURL;
|
||||
|
||||
/**
|
||||
* 头像地址(本地)
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *avatarPath;
|
||||
|
||||
/**
|
||||
* 时间
|
||||
*/
|
||||
@property (nonatomic, strong) NSDate *date;
|
||||
|
||||
/**
|
||||
* 消息展示内容
|
||||
*/
|
||||
@property (nonatomic, strong) NSString *content;
|
||||
|
||||
/**
|
||||
* 未读数量
|
||||
*/
|
||||
@property (nonatomic, assign) NSInteger unreadCount;
|
||||
@property (nonatomic, strong, readonly) NSString *badgeValue;
|
||||
@property (nonatomic, assign, readonly) BOOL isRead;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,31 @@
|
||||
//
|
||||
// TLConversation.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/1/23.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLConversation.h"
|
||||
|
||||
@implementation TLConversation
|
||||
|
||||
- (BOOL)isRead
|
||||
{
|
||||
return self.unreadCount <= 0;
|
||||
}
|
||||
|
||||
- (NSString *)badgeValue
|
||||
{
|
||||
if (self.isRead) {
|
||||
return nil;
|
||||
}
|
||||
if (self.convType == TLConversationTypePersonal || self.convType == TLConversationTypeGroup) {
|
||||
return self.unreadCount <= 99 ? @(self.unreadCount).stringValue : @"99+";
|
||||
}
|
||||
else {
|
||||
return @"";
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// TLExpressionMessage.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessage.h"
|
||||
#import "TLExpressionModel.h"
|
||||
|
||||
@interface TLExpressionMessage : TLMessage
|
||||
|
||||
@property (nonatomic, strong) TLExpressionModel *emoji;
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *path;
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *url;
|
||||
|
||||
@property (nonatomic, assign, readonly) CGSize emojiSize;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// TLExpressionMessage.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLExpressionMessage.h"
|
||||
|
||||
@implementation TLExpressionMessage
|
||||
@synthesize emoji = _emoji;
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
[self setMessageType:TLMessageTypeExpression];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)setEmoji:(TLExpressionModel *)emoji
|
||||
{
|
||||
_emoji = emoji;
|
||||
[self.content setObject:emoji.gid forKey:@"groupID"];
|
||||
[self.content setObject:emoji.eId forKey:@"emojiID"];
|
||||
CGSize imageSize = [UIImage imageNamed:self.path].size;
|
||||
[self.content setObject:[NSNumber numberWithDouble:imageSize.width] forKey:@"w"];
|
||||
[self.content setObject:[NSNumber numberWithDouble:imageSize.height] forKey:@"h"];
|
||||
}
|
||||
- (TLExpressionModel *)emoji
|
||||
{
|
||||
if (_emoji == nil) {
|
||||
_emoji = [[TLExpressionModel alloc] init];
|
||||
_emoji.gid = self.content[@"groupID"];
|
||||
_emoji.eId = self.content[@"emojiID"];
|
||||
}
|
||||
return _emoji;
|
||||
}
|
||||
|
||||
- (NSString *)path
|
||||
{
|
||||
return self.emoji.path;
|
||||
}
|
||||
|
||||
- (NSString *)url
|
||||
{
|
||||
return [TLExpressionModel expressionDownloadURLWithEid:self.emoji.eId];
|
||||
}
|
||||
|
||||
- (CGSize)emojiSize
|
||||
{
|
||||
CGFloat width = [self.content[@"w"] doubleValue];
|
||||
CGFloat height = [self.content[@"h"] doubleValue];
|
||||
return CGSizeMake(width, height);
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
- (TLMessageFrame *)messageFrame
|
||||
{
|
||||
if (kMessageFrame == nil) {
|
||||
kMessageFrame = [[TLMessageFrame alloc] init];
|
||||
kMessageFrame.height = 20 + (self.showTime ? 30 : 0) + (self.showName ? 15 : 0);
|
||||
|
||||
kMessageFrame.height += 5;
|
||||
|
||||
CGSize emojiSize = self.emojiSize;
|
||||
if (CGSizeEqualToSize(emojiSize, CGSizeZero)) {
|
||||
kMessageFrame.contentSize = CGSizeMake(80, 80);
|
||||
}
|
||||
else if (emojiSize.width > emojiSize.height) {
|
||||
CGFloat height = MAX_MESSAGE_EXPRESSION_WIDTH * emojiSize.height / emojiSize.width;
|
||||
height = height < MIN_MESSAGE_EXPRESSION_WIDTH ? MIN_MESSAGE_EXPRESSION_WIDTH : height;
|
||||
kMessageFrame.contentSize = CGSizeMake(MAX_MESSAGE_EXPRESSION_WIDTH, height);
|
||||
}
|
||||
else {
|
||||
CGFloat width = MAX_MESSAGE_EXPRESSION_WIDTH * emojiSize.width / emojiSize.height;
|
||||
width = width < MIN_MESSAGE_EXPRESSION_WIDTH ? MIN_MESSAGE_EXPRESSION_WIDTH : width;
|
||||
kMessageFrame.contentSize = CGSizeMake(width, MAX_MESSAGE_EXPRESSION_WIDTH);
|
||||
}
|
||||
|
||||
kMessageFrame.height += kMessageFrame.contentSize.height;
|
||||
}
|
||||
return kMessageFrame;
|
||||
}
|
||||
|
||||
- (NSString *)conversationContent
|
||||
{
|
||||
return @"[表情]";
|
||||
}
|
||||
|
||||
- (NSString *)messageCopy
|
||||
{
|
||||
return [self.content mj_JSONString];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// TLImageMessage.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessage.h"
|
||||
|
||||
@interface TLImageMessage : TLMessage
|
||||
|
||||
@property (nonatomic, strong) NSString *imagePath; // 本地图片Path
|
||||
@property (nonatomic, strong) NSString *imageURL; // 网络图片URL
|
||||
@property (nonatomic, assign) CGSize imageSize;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,99 @@
|
||||
//
|
||||
// TLImageMessage.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLImageMessage.h"
|
||||
|
||||
@implementation TLImageMessage
|
||||
@synthesize imagePath = _imagePath;
|
||||
@synthesize imageURL = _imageURL;
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
[self setMessageType:TLMessageTypeImage];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
- (NSString *)imagePath
|
||||
{
|
||||
if (_imagePath == nil) {
|
||||
_imagePath = [self.content objectForKey:@"path"];
|
||||
}
|
||||
return _imagePath;
|
||||
}
|
||||
- (void)setImagePath:(NSString *)imagePath
|
||||
{
|
||||
_imagePath = imagePath;
|
||||
[self.content setObject:imagePath forKey:@"path"];
|
||||
}
|
||||
|
||||
- (NSString *)imageURL
|
||||
{
|
||||
if (_imageURL == nil) {
|
||||
_imageURL = [self.content objectForKey:@"url"];
|
||||
}
|
||||
return _imageURL;
|
||||
}
|
||||
- (void)setImageURL:(NSString *)imageURL
|
||||
{
|
||||
_imageURL = imageURL;
|
||||
[self.content setObject:imageURL forKey:@"url"];
|
||||
}
|
||||
|
||||
- (CGSize)imageSize
|
||||
{
|
||||
CGFloat width = [[self.content objectForKey:@"w"] doubleValue];
|
||||
CGFloat height = [[self.content objectForKey:@"h"] doubleValue];
|
||||
return CGSizeMake(width, height);
|
||||
}
|
||||
- (void)setImageSize:(CGSize)imageSize
|
||||
{
|
||||
[self.content setObject:[NSNumber numberWithDouble:imageSize.width] forKey:@"w"];
|
||||
[self.content setObject:[NSNumber numberWithDouble:imageSize.height] forKey:@"h"];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
- (TLMessageFrame *)messageFrame
|
||||
{
|
||||
if (kMessageFrame == nil) {
|
||||
kMessageFrame = [[TLMessageFrame alloc] init];
|
||||
kMessageFrame.height = 20 + (self.showTime ? 30 : 0) + (self.showName ? 15 : 0);
|
||||
|
||||
CGSize imageSize = self.imageSize;
|
||||
if (CGSizeEqualToSize(imageSize, CGSizeZero)) {
|
||||
kMessageFrame.contentSize = CGSizeMake(100, 100);
|
||||
}
|
||||
else if (imageSize.width > imageSize.height) {
|
||||
CGFloat height = MAX_MESSAGE_IMAGE_WIDTH * imageSize.height / imageSize.width;
|
||||
height = height < MIN_MESSAGE_IMAGE_WIDTH ? MIN_MESSAGE_IMAGE_WIDTH : height;
|
||||
kMessageFrame.contentSize = CGSizeMake(MAX_MESSAGE_IMAGE_WIDTH, height);
|
||||
}
|
||||
else {
|
||||
CGFloat width = MAX_MESSAGE_IMAGE_WIDTH * imageSize.width / imageSize.height;
|
||||
width = width < MIN_MESSAGE_IMAGE_WIDTH ? MIN_MESSAGE_IMAGE_WIDTH : width;
|
||||
kMessageFrame.contentSize = CGSizeMake(width, MAX_MESSAGE_IMAGE_WIDTH);
|
||||
}
|
||||
|
||||
kMessageFrame.height += kMessageFrame.contentSize.height;
|
||||
}
|
||||
return kMessageFrame;
|
||||
}
|
||||
|
||||
- (NSString *)conversationContent
|
||||
{
|
||||
return @"[图片]";
|
||||
}
|
||||
|
||||
- (NSString *)messageCopy
|
||||
{
|
||||
return [self.content mj_JSONString];
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,80 @@
|
||||
//
|
||||
// TLMessage.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/15.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLChatUserProtocol.h"
|
||||
#import "TLMessageProtocol.h"
|
||||
#import <MapKit/MapKit.h>
|
||||
#import "TLMessageConstants.h"
|
||||
|
||||
/**
|
||||
* 消息所有者类型
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, TLPartnerType){
|
||||
TLPartnerTypeUser, // 用户
|
||||
TLPartnerTypeGroup, // 群聊
|
||||
};
|
||||
|
||||
/**
|
||||
* 消息拥有者
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, TLMessageOwnerType){
|
||||
TLMessageOwnerTypeUnknown, // 未知的消息拥有者
|
||||
TLMessageOwnerTypeSystem, // 系统消息
|
||||
TLMessageOwnerTypeSelf, // 自己发送的消息
|
||||
TLMessageOwnerTypeFriend, // 接收到的他人消息
|
||||
};
|
||||
|
||||
/**
|
||||
* 消息发送状态
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, TLMessageSendState){
|
||||
TLMessageSendSuccess, // 消息发送成功
|
||||
TLMessageSendFail, // 消息发送失败
|
||||
};
|
||||
|
||||
/**
|
||||
* 消息读取状态
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, TLMessageReadState) {
|
||||
TLMessageUnRead, // 消息未读
|
||||
TLMessageReaded, // 消息已读
|
||||
};
|
||||
|
||||
@interface TLMessage : NSObject <TLMessageProtocol>
|
||||
{
|
||||
TLMessageFrame *kMessageFrame;
|
||||
}
|
||||
|
||||
@property (nonatomic, strong) NSString *messageID; // 消息ID
|
||||
@property (nonatomic, strong) NSString *userID; // 发送者ID
|
||||
@property (nonatomic, strong) NSString *friendID; // 接收者ID
|
||||
@property (nonatomic, strong) NSString *groupID; // 讨论组ID(无则为nil)
|
||||
|
||||
@property (nonatomic, strong) NSDate *date; // 发送时间
|
||||
|
||||
@property (nonatomic, strong) id<TLChatUserProtocol> fromUser; // 发送者
|
||||
|
||||
@property (nonatomic, assign) BOOL showTime;
|
||||
@property (nonatomic, assign) BOOL showName;
|
||||
|
||||
@property (nonatomic, assign) TLPartnerType partnerType; // 对方类型
|
||||
@property (nonatomic, assign) TLMessageType messageType; // 消息类型
|
||||
@property (nonatomic, assign) TLMessageOwnerType ownerTyper; // 发送者类型
|
||||
@property (nonatomic, assign) TLMessageReadState readState; // 读取状态
|
||||
@property (nonatomic, assign) TLMessageSendState sendState; // 发送状态
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary *content;
|
||||
|
||||
@property (nonatomic, strong, readonly) TLMessageFrame *messageFrame; // 消息frame
|
||||
|
||||
+ (TLMessage *)createMessageByType:(TLMessageType)type;
|
||||
|
||||
- (void)resetMessageFrame;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,69 @@
|
||||
//
|
||||
// TLMessage.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/2/15.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessage.h"
|
||||
|
||||
@implementation TLMessage
|
||||
|
||||
+ (TLMessage *)createMessageByType:(TLMessageType)type
|
||||
{
|
||||
NSString *className;
|
||||
if (type == TLMessageTypeText) {
|
||||
className = @"TLTextMessage";
|
||||
}
|
||||
else if (type == TLMessageTypeImage) {
|
||||
className = @"TLImageMessage";
|
||||
}
|
||||
else if (type == TLMessageTypeExpression) {
|
||||
className = @"TLExpressionMessage";
|
||||
}
|
||||
else if (type == TLMessageTypeVoice) {
|
||||
className = @"TLVoiceMessage";
|
||||
}
|
||||
if (className) {
|
||||
return [[NSClassFromString(className) alloc] init];
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
self.messageID = [NSString stringWithFormat:@"%lld", (long long)([[NSDate date] timeIntervalSince1970] * 10000)];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)resetMessageFrame
|
||||
{
|
||||
kMessageFrame = nil;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - # Protocol
|
||||
- (NSString *)conversationContent
|
||||
{
|
||||
return @"子类未定义";
|
||||
}
|
||||
|
||||
- (NSString *)messageCopy
|
||||
{
|
||||
return @"子类未定义";
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - # Getter
|
||||
- (NSMutableDictionary *)content
|
||||
{
|
||||
if (_content == nil) {
|
||||
_content = [[NSMutableDictionary alloc] init];
|
||||
}
|
||||
return _content;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// TLMessageConstants.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 2017/9/20.
|
||||
// Copyright © 2017年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef TLMessageConstants_h
|
||||
#define TLMessageConstants_h
|
||||
|
||||
/**
|
||||
* 消息类型
|
||||
*/
|
||||
typedef NS_ENUM(NSInteger, TLMessageType) {
|
||||
TLMessageTypeUnknown,
|
||||
TLMessageTypeText, // 文字
|
||||
TLMessageTypeImage, // 图片
|
||||
TLMessageTypeExpression, // 表情
|
||||
TLMessageTypeVoice, // 语音
|
||||
TLMessageTypeVideo, // 视频
|
||||
TLMessageTypeURL, // 链接
|
||||
TLMessageTypePosition, // 位置
|
||||
TLMessageTypeBusinessCard, // 名片
|
||||
TLMessageTypeSystem, // 系统
|
||||
TLMessageTypeOther,
|
||||
};
|
||||
|
||||
|
||||
#define MAX_MESSAGE_WIDTH SCREEN_WIDTH * 0.58
|
||||
#define MAX_MESSAGE_IMAGE_WIDTH SCREEN_WIDTH * 0.45
|
||||
#define MIN_MESSAGE_IMAGE_WIDTH SCREEN_WIDTH * 0.25
|
||||
#define MAX_MESSAGE_EXPRESSION_WIDTH SCREEN_WIDTH * 0.35
|
||||
#define MIN_MESSAGE_EXPRESSION_WIDTH SCREEN_WIDTH * 0.2
|
||||
|
||||
#endif /* TLMessageConstants_h */
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// TLMessageFrame.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/14.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface TLMessageFrame : NSObject
|
||||
|
||||
@property (nonatomic, assign) CGFloat height;
|
||||
|
||||
@property (nonatomic, assign) CGSize contentSize;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// TLMessageFrame.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/14.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessageFrame.h"
|
||||
|
||||
@implementation TLMessageFrame
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,18 @@
|
||||
//
|
||||
// TLMessageProtocol.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLMessageFrame.h"
|
||||
|
||||
@protocol TLMessageProtocol <NSObject>
|
||||
|
||||
- (NSString *)messageCopy;
|
||||
|
||||
- (NSString *)conversationContent;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// TLTextMessage.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessage.h"
|
||||
|
||||
@interface TLTextMessage : TLMessage
|
||||
|
||||
@property (nonatomic, strong) NSString *text; // 文字信息
|
||||
|
||||
@property (nonatomic, strong) NSAttributedString *attrText; // 格式化的文字信息(仅展示用)
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,73 @@
|
||||
//
|
||||
// TLTextMessage.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by libokun on 16/3/28.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLTextMessage.h"
|
||||
#import "NSString+Message.h"
|
||||
|
||||
static UILabel *textLabel = nil;
|
||||
|
||||
@implementation TLTextMessage
|
||||
@synthesize text = _text;
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
[self setMessageType:TLMessageTypeText];
|
||||
if (textLabel == nil) {
|
||||
textLabel = [[UILabel alloc] init];
|
||||
[textLabel setFont:[UIFont fontTextMessageText]];
|
||||
[textLabel setNumberOfLines:0];
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)text
|
||||
{
|
||||
if (_text == nil) {
|
||||
_text = [self.content objectForKey:@"text"];
|
||||
}
|
||||
return _text;
|
||||
}
|
||||
- (void)setText:(NSString *)text
|
||||
{
|
||||
_text = text;
|
||||
[self.content setObject:text forKey:@"text"];
|
||||
}
|
||||
|
||||
- (NSAttributedString *)attrText
|
||||
{
|
||||
if (_attrText == nil) {
|
||||
_attrText = [self.text toMessageString];
|
||||
}
|
||||
return _attrText;
|
||||
}
|
||||
|
||||
- (TLMessageFrame *)messageFrame
|
||||
{
|
||||
if (kMessageFrame == nil) {
|
||||
kMessageFrame = [[TLMessageFrame alloc] init];
|
||||
kMessageFrame.height = 20 + (self.showTime ? 30 : 0) + (self.showName ? 15 : 0) + 20;
|
||||
[textLabel setAttributedText:self.attrText];
|
||||
kMessageFrame.contentSize = [textLabel sizeThatFits:CGSizeMake(MAX_MESSAGE_WIDTH, MAXFLOAT)];
|
||||
kMessageFrame.height += kMessageFrame.contentSize.height;
|
||||
}
|
||||
return kMessageFrame;
|
||||
}
|
||||
|
||||
- (NSString *)conversationContent
|
||||
{
|
||||
return self.text;
|
||||
}
|
||||
|
||||
- (NSString *)messageCopy
|
||||
{
|
||||
return self.text;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// TLVideoMessage.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/10/1.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessage.h"
|
||||
|
||||
@interface TLVideoMessage : TLMessage
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *videoPath;
|
||||
@property (nonatomic, strong) NSString *videoURL;
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *imagePath; // 本地图片Path
|
||||
@property (nonatomic, strong) NSString *imageURL; // 网络图片URL
|
||||
@property (nonatomic, assign) CGSize imageSize;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,13 @@
|
||||
//
|
||||
// TLVideoMessage.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/10/1.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLVideoMessage.h"
|
||||
|
||||
@implementation TLVideoMessage
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,27 @@
|
||||
//
|
||||
// TLVoiceMessage.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/7/11.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessage.h"
|
||||
|
||||
typedef NS_ENUM(NSInteger, TLVoiceMessageStatus) {
|
||||
TLVoiceMessageStatusNormal,
|
||||
TLVoiceMessageStatusRecording,
|
||||
TLVoiceMessageStatusPlaying,
|
||||
};
|
||||
|
||||
@interface TLVoiceMessage : TLMessage
|
||||
|
||||
@property (nonatomic, strong) NSString *recFileName;
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *path;
|
||||
@property (nonatomic, strong) NSString *url;
|
||||
@property (nonatomic, assign) CGFloat time;
|
||||
|
||||
@property (nonatomic, assign) TLVoiceMessageStatus msgStatus;
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,93 @@
|
||||
//
|
||||
// TLVoiceMessage.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/7/11.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLVoiceMessage.h"
|
||||
#import "NSFileManager+TLChat.h"
|
||||
|
||||
@implementation TLVoiceMessage
|
||||
@synthesize recFileName = _recFileName;
|
||||
@synthesize path = _path;
|
||||
@synthesize url = _url;
|
||||
@synthesize time = _time;
|
||||
|
||||
- (id)init
|
||||
{
|
||||
if (self = [super init]) {
|
||||
[self setMessageType:TLMessageTypeVoice];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (NSString *)recFileName
|
||||
{
|
||||
if (_recFileName == nil) {
|
||||
_recFileName = [self.content objectForKey:@"path"];
|
||||
}
|
||||
return _recFileName;
|
||||
}
|
||||
- (void)setRecFileName:(NSString *)recFileName
|
||||
{
|
||||
_recFileName = recFileName;
|
||||
[self.content setObject:recFileName forKey:@"path"];
|
||||
}
|
||||
|
||||
- (NSString *)path
|
||||
{
|
||||
if (_path == nil) {
|
||||
_path = [NSFileManager pathUserChatVoice:self.recFileName];;
|
||||
}
|
||||
return _path;
|
||||
}
|
||||
|
||||
- (NSString *)url
|
||||
{
|
||||
if (_url == nil) {
|
||||
_url = [self.content objectForKey:@"url"];
|
||||
}
|
||||
return _url;
|
||||
}
|
||||
- (void)setUrl:(NSString *)url
|
||||
{
|
||||
_url = url;
|
||||
[self.content setObject:url forKey:@"url"];
|
||||
}
|
||||
|
||||
- (CGFloat)time
|
||||
{
|
||||
return [[self.content objectForKey:@"time"] doubleValue];
|
||||
}
|
||||
- (void)setTime:(CGFloat)time
|
||||
{
|
||||
[self.content setObject:[NSNumber numberWithDouble:time] forKey:@"time"];
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
- (TLMessageFrame *)messageFrame
|
||||
{
|
||||
if (kMessageFrame == nil) {
|
||||
kMessageFrame = [[TLMessageFrame alloc] init];
|
||||
CGFloat width = 60 + (self.time > 20 ? 1.0 : self.time / 20.0) * (MAX_MESSAGE_WIDTH - 60);
|
||||
CGFloat height = 54;
|
||||
kMessageFrame.contentSize = CGSizeMake(width, height);
|
||||
kMessageFrame.height = kMessageFrame.contentSize.height + (self.showTime ? 30 : 0) + (self.showName ? 15 : 0) + 3;
|
||||
}
|
||||
return kMessageFrame;
|
||||
}
|
||||
|
||||
- (NSString *)conversationContent
|
||||
{
|
||||
return @"[语音消息]";
|
||||
}
|
||||
|
||||
- (NSString *)messageCopy
|
||||
{
|
||||
return [self.content mj_JSONString];
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
//
|
||||
// TLMessageManager+ConversationRecord.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessageManager.h"
|
||||
|
||||
@interface TLMessageManager (ConversationRecord)
|
||||
|
||||
- (BOOL)addConversationByMessage:(TLMessage *)message;
|
||||
|
||||
- (void)conversationRecord:(void (^)(NSArray *))complete;
|
||||
|
||||
- (BOOL)deleteConversationByPartnerID:(NSString *)partnerID;
|
||||
|
||||
@end
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
//
|
||||
// TLMessageManager+ConversationRecord.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessageManager+ConversationRecord.h"
|
||||
#import "TLMessageManager+MessageRecord.h"
|
||||
|
||||
@implementation TLMessageManager (ConversationRecord)
|
||||
|
||||
- (BOOL)addConversationByMessage:(TLMessage *)message
|
||||
{
|
||||
NSString *partnerID = message.friendID;
|
||||
NSInteger type = 0;
|
||||
if (message.partnerType == TLPartnerTypeGroup) {
|
||||
partnerID = message.groupID;
|
||||
type = 1;
|
||||
}
|
||||
BOOL ok = [self.conversationStore addConversationByUid:message.userID fid:partnerID type:type date:message.date];
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (void)conversationRecord:(void (^)(NSArray *))complete
|
||||
{
|
||||
NSArray *data = [self.conversationStore conversationsByUid:self.userID];
|
||||
complete(data);
|
||||
}
|
||||
|
||||
- (BOOL)deleteConversationByPartnerID:(NSString *)partnerID
|
||||
{
|
||||
BOOL ok = [self deleteMessagesByPartnerID:partnerID];
|
||||
if (ok) {
|
||||
ok = [self.conversationStore deleteConversationByUid:self.userID fid:partnerID];
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@end
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// TLMessageManager+MessageRecord.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessageManager.h"
|
||||
|
||||
@interface TLMessageManager (MessageRecord)
|
||||
|
||||
#pragma mark - 查询
|
||||
/**
|
||||
* 查询聊天记录
|
||||
*/
|
||||
- (void)messageRecordForPartner:(NSString *)partnerID
|
||||
fromDate:(NSDate *)date
|
||||
count:(NSUInteger)count
|
||||
complete:(void (^)(NSArray *, BOOL))complete;
|
||||
|
||||
/**
|
||||
* 查询聊天文件
|
||||
*/
|
||||
- (void)chatFilesForPartnerID:(NSString *)partnerID
|
||||
completed:(void (^)(NSArray *))completed;
|
||||
|
||||
|
||||
/**
|
||||
* 查询聊天图片
|
||||
*/
|
||||
- (void)chatImagesAndVideosForPartnerID:(NSString *)partnerID
|
||||
completed:(void (^)(NSArray *))completed;
|
||||
|
||||
#pragma mark - 删除
|
||||
/**
|
||||
* 删除单条聊天记录
|
||||
*/
|
||||
- (BOOL)deleteMessageByMsgID:(NSString *)msgID;
|
||||
|
||||
/**
|
||||
* 删除与某好友的聊天记录
|
||||
*/
|
||||
- (BOOL)deleteMessagesByPartnerID:(NSString *)partnerID;
|
||||
|
||||
/**
|
||||
* 删除所有聊天记录
|
||||
*/
|
||||
- (BOOL)deleteAllMessages;
|
||||
|
||||
@end
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
//
|
||||
// TLMessageManager+MessageRecord.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/20.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessageManager+MessageRecord.h"
|
||||
#import "TLChatViewController.h"
|
||||
|
||||
#import "TLChatNotificationKey.h"
|
||||
|
||||
@implementation TLMessageManager (MessageRecord)
|
||||
|
||||
- (void)messageRecordForPartner:(NSString *)partnerID
|
||||
fromDate:(NSDate *)date
|
||||
count:(NSUInteger)count
|
||||
complete:(void (^)(NSArray *, BOOL))complete
|
||||
{
|
||||
[self.messageStore messagesByUserID:self.userID partnerID:partnerID fromDate:date count:count complete:^(NSArray *data, BOOL hasMore) {
|
||||
complete(data, hasMore);
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)chatFilesForPartnerID:(NSString *)partnerID
|
||||
completed:(void (^)(NSArray *))completed
|
||||
{
|
||||
NSArray *data = [self.messageStore chatFilesByUserID:self.userID partnerID:partnerID];
|
||||
completed(data);
|
||||
}
|
||||
|
||||
- (void)chatImagesAndVideosForPartnerID:(NSString *)partnerID
|
||||
completed:(void (^)(NSArray *))completed
|
||||
|
||||
{
|
||||
NSArray *data = [self.messageStore chatImagesAndVideosByUserID:self.userID partnerID:partnerID];
|
||||
completed(data);
|
||||
}
|
||||
|
||||
- (BOOL)deleteMessageByMsgID:(NSString *)msgID
|
||||
{
|
||||
return [self.messageStore deleteMessageByMessageID:msgID];
|
||||
}
|
||||
|
||||
- (BOOL)deleteMessagesByPartnerID:(NSString *)partnerID
|
||||
{
|
||||
BOOL ok = [self.messageStore deleteMessagesByUserID:self.userID partnerID:partnerID];
|
||||
if (ok) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:NOTI_CHAT_VIEW_RESET object:nil];
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
- (BOOL)deleteAllMessages
|
||||
{
|
||||
BOOL ok = [self.messageStore deleteMessagesByUserID:self.userID];
|
||||
if (ok) {
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:NOTI_CHAT_VIEW_RESET object:nil];
|
||||
ok = [self.conversationStore deleteConversationsByUid:self.userID];
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// TLMessageManager.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/13.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "TLDBMessageStore.h"
|
||||
#import "TLDBConversationStore.h"
|
||||
#import "TLMessage.h"
|
||||
|
||||
#import "TLMessageManagerChatVCDelegate.h"
|
||||
#import "TLMessageManagerConvVCDelegate.h"
|
||||
|
||||
@interface TLMessageManager : NSObject
|
||||
|
||||
@property (nonatomic, assign) id<TLMessageManagerChatVCDelegate>messageDelegate;
|
||||
@property (nonatomic, assign) id<TLMessageManagerConvVCDelegate>conversationDelegate;
|
||||
|
||||
@property (nonatomic, strong, readonly) NSString *userID;
|
||||
|
||||
@property (nonatomic, strong) TLDBMessageStore *messageStore;
|
||||
|
||||
@property (nonatomic, strong) TLDBConversationStore *conversationStore;
|
||||
|
||||
+ (TLMessageManager *)sharedInstance;
|
||||
|
||||
#pragma mark - 发送
|
||||
- (void)sendMessage:(TLMessage *)message
|
||||
progress:(void (^)(TLMessage *, CGFloat))progress
|
||||
success:(void (^)(TLMessage *))success
|
||||
failure:(void (^)(TLMessage *))failure;
|
||||
|
||||
|
||||
@end
|
||||
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// TLMessageManager.m
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/13.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import "TLMessageManager.h"
|
||||
#import "TLMessageManager+ConversationRecord.h"
|
||||
#import "TLUserHelper.h"
|
||||
#import "TLNetwork.h"
|
||||
|
||||
#import "TLFriendHelper.h"
|
||||
#import "TLTextMessage.h"
|
||||
#import "TLImageMessage.h"
|
||||
|
||||
static TLMessageManager *messageManager;
|
||||
|
||||
@implementation TLMessageManager
|
||||
|
||||
+ (TLMessageManager *)sharedInstance
|
||||
{
|
||||
static dispatch_once_t once;
|
||||
dispatch_once(&once, ^{
|
||||
messageManager = [[TLMessageManager alloc] init];
|
||||
});
|
||||
return messageManager;
|
||||
}
|
||||
|
||||
- (void)sendMessage:(TLMessage *)message
|
||||
progress:(void (^)(TLMessage *, CGFloat))progress
|
||||
success:(void (^)(TLMessage *))success
|
||||
failure:(void (^)(TLMessage *))failure
|
||||
{
|
||||
// 图灵机器人
|
||||
[self p_sendMessageToReboot:message];
|
||||
|
||||
BOOL ok = [self.messageStore addMessage:message];
|
||||
if (!ok) {
|
||||
DDLogError(@"存储Message到DB失败");
|
||||
}
|
||||
else { // 存储到conversation
|
||||
ok = [self addConversationByMessage:message];
|
||||
if (!ok) {
|
||||
DDLogError(@"存储Conversation到DB失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - # Private
|
||||
- (void)p_sendMessageToReboot:(TLMessage *)message
|
||||
{
|
||||
if (message.messageType == TLMessageTypeText) {
|
||||
// 聊天的用户
|
||||
TLUser *user;
|
||||
if (message.partnerType == TLPartnerTypeGroup) {
|
||||
TLGroup *group = [[TLFriendHelper sharedFriendHelper] getGroupInfoByGroupID:message.groupID];
|
||||
NSInteger index = arc4random() % group.count;
|
||||
user = group.users[index];
|
||||
}
|
||||
else {
|
||||
user = [[TLFriendHelper sharedFriendHelper] getFriendInfoByUserID:message.friendID];
|
||||
}
|
||||
|
||||
NSString *text = [message.content objectForKey:@"text"];
|
||||
NSString *apiKey = ({
|
||||
NSString *key;
|
||||
if ([user.userID isEqualToString:@"1001"]) { // 曾小贤
|
||||
key = @"00916307c7b24533a23d6115224540f3";
|
||||
}
|
||||
else if ([user.userID isEqualToString:@"1002"]) { // 陈美嘉
|
||||
key = @"5f5f8d7d613f4d81a6ff354cb428ccbc";
|
||||
}
|
||||
else {
|
||||
key = @"44eb0b4ab0a640f192bd469551a7c03e";
|
||||
}
|
||||
key;
|
||||
});
|
||||
NSDictionary *json = @{@"reqType" : @"0",
|
||||
@"userInfo" : @{
|
||||
@"apiKey" : apiKey,
|
||||
@"userId" : @"100454",
|
||||
},
|
||||
@"perception" : @{
|
||||
@"inputText" : @{
|
||||
@"text" : text,
|
||||
}
|
||||
},
|
||||
};
|
||||
NSString *url = @"http://openapi.tuling123.com/openapi/api/v2";
|
||||
TLBaseRequest *request = [TLBaseRequest requestWithMethod:TLRequestMethodPOST url:url parameters:json];
|
||||
[request startRequestWithSuccessAction:^(TLResponse *response) {
|
||||
NSDictionary *json = response.responseObject;
|
||||
NSArray *results = [json objectForKey:@"results"];
|
||||
for (NSDictionary *item in results) {
|
||||
NSDictionary *values = [item objectForKey:@"values"];
|
||||
if (values[@"text"]) {
|
||||
NSString *text = values[@"text"];
|
||||
TLTextMessage *textMessage = [[TLTextMessage alloc] init];
|
||||
textMessage.partnerType = message.partnerType;
|
||||
textMessage.text = text;
|
||||
textMessage.ownerTyper = TLMessageOwnerTypeFriend;
|
||||
textMessage.userID = message.userID;
|
||||
textMessage.date = [NSDate date];
|
||||
textMessage.friendID = user.userID;
|
||||
textMessage.fromUser = (id <TLChatUserProtocol>)user;
|
||||
textMessage.groupID = message.groupID;
|
||||
[self.messageStore addMessage:textMessage];
|
||||
if (self.messageDelegate && [self.messageDelegate respondsToSelector:@selector(didReceivedMessage:)]) {
|
||||
[self.messageDelegate didReceivedMessage:textMessage];
|
||||
}
|
||||
}
|
||||
else if (values[@"image"]) {
|
||||
NSString *imageURL = values[@"image"];
|
||||
TLImageMessage *imageMessage = [[TLImageMessage alloc] init];
|
||||
imageMessage.partnerType = message.partnerType;
|
||||
imageMessage.imageURL = imageURL;
|
||||
imageMessage.ownerTyper = TLMessageOwnerTypeFriend;
|
||||
imageMessage.userID = message.userID;
|
||||
imageMessage.friendID = user.userID;
|
||||
imageMessage.date = [NSDate date];
|
||||
imageMessage.fromUser = (id <TLChatUserProtocol>)user;
|
||||
imageMessage.imageSize = CGSizeMake(120, 120);
|
||||
imageMessage.groupID = message.groupID;
|
||||
[self.messageStore addMessage:imageMessage];
|
||||
if (self.messageDelegate && [self.messageDelegate respondsToSelector:@selector(didReceivedMessage:)]) {
|
||||
[self.messageDelegate didReceivedMessage:imageMessage];
|
||||
}
|
||||
}
|
||||
}
|
||||
} failureAction:nil];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - # Getters
|
||||
- (TLDBMessageStore *)messageStore
|
||||
{
|
||||
if (_messageStore == nil) {
|
||||
_messageStore = [[TLDBMessageStore alloc] init];
|
||||
}
|
||||
return _messageStore;
|
||||
}
|
||||
|
||||
- (TLDBConversationStore *)conversationStore
|
||||
{
|
||||
if (_conversationStore == nil) {
|
||||
_conversationStore = [[TLDBConversationStore alloc] init];
|
||||
}
|
||||
return _conversationStore;
|
||||
}
|
||||
|
||||
- (NSString *)userID
|
||||
{
|
||||
return [TLUserHelper sharedHelper].userID;
|
||||
}
|
||||
|
||||
@end
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// TLMessageManagerChatVCDelegate.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/21.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@protocol TLMessageManagerChatVCDelegate <NSObject>
|
||||
|
||||
- (void)didReceivedMessage:(TLMessage *)message;
|
||||
|
||||
@end
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// TLMessageManagerConvVCDelegate.h
|
||||
// TLChat
|
||||
//
|
||||
// Created by 李伯坤 on 16/3/21.
|
||||
// Copyright © 2016年 李伯坤. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@protocol TLMessageManagerConvVCDelegate <NSObject>
|
||||
|
||||
- (void)updateConversationData;
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user