-
Notifications
You must be signed in to change notification settings - Fork 429
/
ChatModel.m
135 lines (111 loc) · 5.14 KB
/
ChatModel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// ChatModel.m
// UUChatTableView
//
// Created by shake on 15/1/6.
// Copyright (c) 2015年 uyiuyao. All rights reserved.
//
#import "ChatModel.h"
#import "UUMessage.h"
#import "UUMessageFrame.h"
@implementation ChatModel
- (void)populateRandomDataSource {
self.dataSource = [NSMutableArray array];
[self.dataSource addObjectsFromArray:[self additems:5]];
}
- (void)addRandomItemsToDataSource:(NSInteger)number{
for (int i=0; i<number; i++) {
[self.dataSource insertObject:[[self additems:1] firstObject] atIndex:0];
}
}
- (void)recountFrame
{
[self.dataSource enumerateObjectsUsingBlock:^(UUMessageFrame * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.message = obj.message;
}];
}
// 添加自己的item
- (void)addSpecifiedItem:(NSDictionary *)dic
{
UUMessageFrame *messageFrame = [[UUMessageFrame alloc]init];
UUMessage *message = [[UUMessage alloc] init];
NSMutableDictionary *dataDic = [NSMutableDictionary dictionaryWithDictionary:dic];
NSString *URLStr = @"http://img0.bdstatic.com/img/image/shouye/xinshouye/mingxing16.jpg";
[dataDic setObject:@(UUMessageFromMe) forKey:@"from"];
[dataDic setObject:[[NSDate date] description] forKey:@"strTime"];
[dataDic setObject:@"Hi:sister" forKey:@"strName"];
[dataDic setObject:URLStr forKey:@"strIcon"];
[message setWithDict:dataDic];
[message minuteOffSetStart:previousTime end:dataDic[@"strTime"]];
messageFrame.showTime = message.showDateLabel;
[messageFrame setMessage:message];
if (message.showDateLabel) {
previousTime = dataDic[@"strTime"];
}
[self.dataSource addObject:messageFrame];
}
// 添加聊天item(一个cell内容)
static NSString *previousTime = nil;
- (NSArray<UUMessageFrame *> *)additems:(NSInteger)number
{
NSMutableArray<UUMessageFrame *> *result = [NSMutableArray array];
for (int i=0; i<number; i++) {
NSDictionary *dataDic = [self getDic];
UUMessageFrame *messageFrame = [[UUMessageFrame alloc]init];
UUMessage *message = [[UUMessage alloc] init];
[message setWithDict:dataDic];
[message minuteOffSetStart:previousTime end:dataDic[@"strTime"]];
messageFrame.showTime = message.showDateLabel;
[messageFrame setMessage:message];
if (message.showDateLabel) {
previousTime = dataDic[@"strTime"];
}
[result addObject:messageFrame];
}
return result;
}
// 如下:群聊(groupChat)
static int dateNum = 10;
- (NSDictionary *)getDic
{
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
int randomNum = arc4random()%5;
if (randomNum == UUMessageTypePicture) {
[dictionary setObject:[UIImage imageNamed:[NSString stringWithFormat:@"%zd.jpeg",arc4random()%2]] forKey:@"picture"];
} else {
// 文字出现概率4倍于图片(暂不出现Voice类型)
randomNum = UUMessageTypeText;
[dictionary setObject:[self getRandomString] forKey:@"strContent"];
}
NSDate *date = [[NSDate date]dateByAddingTimeInterval:arc4random()%1000*(dateNum++) ];
[dictionary setObject:@(UUMessageFromOther) forKey:@"from"];
[dictionary setObject:@(randomNum) forKey:@"type"];
[dictionary setObject:[date description] forKey:@"strTime"];
// 这里判断是否是私人会话、群会话
int index = _isGroupChat ? arc4random()%6 : 0;
[dictionary setObject:[self getName:index] forKey:@"strName"];
[dictionary setObject:[self getImageStr:index] forKey:@"strIcon"];
return dictionary;
}
- (NSString *)getRandomString {
NSString *lorumIpsum = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent non quam ac massa viverra semper. Maecenas mattis justo ac augue volutpat congue. Maecenas laoreet, nulla eu faucibus gravida, felis orci dictum risus, sed sodales sem eros eget risus. Morbi imperdiet sed diam et sodales.";
NSArray *lorumIpsumArray = [lorumIpsum componentsSeparatedByString:@" "];
int r = arc4random() % [lorumIpsumArray count];
r = MAX(6, r); // no less than 6 words
NSArray *lorumIpsumRandom = [lorumIpsumArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, r)]];
return [NSString stringWithFormat:@"%@!!", [lorumIpsumRandom componentsJoinedByString:@" "]];
}
- (NSString *)getImageStr:(NSInteger)index{
NSArray *array = @[@"http://www.120ask.com/static/upload/clinic/article/org/201311/201311061651418413.jpg",
@"http://p1.qqyou.com/touxiang/uploadpic/2011-3/20113212244659712.jpg",
@"http://www.qqzhi.com/uploadpic/2014-09-14/004638238.jpg",
@"http://e.hiphotos.baidu.com/image/pic/item/5ab5c9ea15ce36d3b104443639f33a87e950b1b0.jpg",
@"http://ts1.mm.bing.net/th?&id=JN.C21iqVw9uSuD2ZyxElpacA&w=300&h=300&c=0&pid=1.9&rs=0&p=0",
@"http://ts1.mm.bing.net/th?&id=JN.7g7SEYKd2MTNono6zVirpA&w=300&h=300&c=0&pid=1.9&rs=0&p=0"];
return array[index];
}
- (NSString *)getName:(NSInteger)index{
NSArray *array = @[@"Hi,Daniel",@"Hi,Juey",@"Hey,Jobs",@"Hey,Bob",@"Hah,Dane",@"Wow,Boss"];
return array[index];
}
@end