// // XHMessage.m // MessageDisplayExample // // Created by HUAJIE-1 on 14-4-24. // Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved. // #import "XHMessage.h" #import "User.h" @implementation XHMessage @synthesize timestamp = _timestamp; @synthesize avator = _avator; - (void)setType:(NSNumber *)type{ _type = type; if ([type isEqualToNumber:@(10005)]) { self.messageMediaType = XHBubbleMessageMediaTypeText; }else{ self.messageMediaType = [type integerValue]; } } - (void)setMessageMediaType:(XHBubbleMessageMediaType)messageMediaType{ _messageMediaType = messageMediaType; switch (messageMediaType) { case XHBubbleMessageMediaTypeText: break; case XHBubbleMessageMediaTypePhoto: break; case XHBubbleMessageMediaTypeVoice: break; case XHBubbleMessageMediaTypeVideo: break; case XHBubbleMessageMediaTypeEmotion: break; case XHBubbleMessageMediaTypeLocalPosition: break; default: break; } } - (void)setSenderID:(NSString *)senderID{ _senderID = senderID; if ([senderID isEqualToString:[User currentUser].openid]) { self.bubbleMessageType = XHBubbleMessageTypeSending; }else{ self.bubbleMessageType = XHBubbleMessageTypeReceiving; } } - (instancetype)initWithText:(NSString *)text sender:(NSString *)sender timestamp:(NSDate *)timestamp { self = [super init]; if (self) { self.content = text; self.senderID = sender; self.timestamp = @([timestamp timeIntervalSince1970]); self.messageMediaType = XHBubbleMessageMediaTypeText; } return self; } /** * 初始化图片类型的消息 * * @param photo 目标图片 * @param thumbnailUrl 目标图片在服务器的缩略图地址 * @param originPhotoUrl 目标图片在服务器的原图地址 * @param sender 发送者 * @param timestamp 发送时间 * * @return 返回Message model 对象 */ - (instancetype)initWithPhoto:(UIImage *)photo thumbnailUrl:(NSString *)thumbnailUrl originPhotoUrl:(NSString *)originPhotoUrl sender:(NSString *)sender timestamp:(NSDate *)timestamp { self = [super init]; if (self) { self.photo = photo; // self.thumbnailUrl = thumbnailUrl; // self.originPhotoUrl = originPhotoUrl; self.senderID = sender; self.timestamp = @([timestamp timeIntervalSince1970]); self.messageMediaType = XHBubbleMessageMediaTypePhoto; } return self; } /** * 初始化视频类型的消息 * * @param videoConverPhoto 目标视频的封面图 * @param videoPath 目标视频的本地路径,如果是下载过,或者是从本地发送的时候,会存在 * @param videoUrl 目标视频在服务器上的地址 * @param sender 发送者 * @param timestamp 发送时间 * * @return 返回Message model 对象 */ - (instancetype)initWithVideoConverPhoto:(UIImage *)videoConverPhoto videoPath:(NSString *)videoPath videoUrl:(NSString *)videoUrl sender:(NSString *)sender timestamp:(NSDate *)timestamp { self = [super init]; if (self) { self.videoConverPhoto = videoConverPhoto; self.videoPath = videoPath; // self.videoUrl = videoUrl; self.senderID = sender; self.timestamp = @([timestamp timeIntervalSince1970]); self.messageMediaType = XHBubbleMessageMediaTypeVideo; } return self; } /** * 初始化语音类型的消息 * * @param voicePath 目标语音的本地路径 * @param voiceUrl 目标语音在服务器的地址 * @param voiceDuration 目标语音的时长 * @param sender 发送者 * @param timestamp 发送时间 * * @return 返回Message model 对象 */ - (instancetype)initWithVoicePath:(NSString *)voicePath voiceUrl:(NSString *)voiceUrl voiceDuration:(NSNumber *)voiceDuration sender:(NSString *)sender timestamp:(NSDate *)timestamp { return [self initWithVoicePath:voicePath voiceUrl:voiceUrl voiceDuration:voiceDuration sender:sender timestamp:timestamp isRead:YES]; } /** * 初始化语音类型的消息。增加已读未读标记 * * @param voicePath 目标语音的本地路径 * @param voiceUrl 目标语音在服务器的地址 * @param voiceDuration 目标语音的时长 * @param sender 发送者 * @param timestamp 发送时间 * @param isRead 已读未读标记 * * @return 返回Message model 对象 */ - (instancetype)initWithVoicePath:(NSString *)voicePath voiceUrl:(NSString *)voiceUrl voiceDuration:(NSNumber *)voiceDuration sender:(NSString *)sender timestamp:(NSDate *)timestamp isRead:(BOOL)isRead { self = [super init]; if (self) { self.voicePath = voicePath; // self.voiceUrl = voiceUrl; self.duration = voiceDuration; self.senderID = sender; self.timestamp = @([timestamp timeIntervalSince1970]); self.isRead = isRead; self.messageMediaType = XHBubbleMessageMediaTypeVoice; } return self; } - (instancetype)initWithEmotionPath:(NSString *)emotionPath sender:(NSString *)sender timestamp:(NSDate *)timestamp { self = [super init]; if (self) { self.emotionPath = emotionPath; self.senderID = sender; self.timestamp = @([timestamp timeIntervalSince1970]); self.messageMediaType = XHBubbleMessageMediaTypeEmotion; } return self; } - (instancetype)initWithLocalPositionPhoto:(UIImage *)localPositionPhoto geolocations:(NSString *)geolocations location:(CLLocation *)location sender:(NSString *)sender timestamp:(NSDate *)timestamp { self = [super init]; if (self) { self.localPositionPhoto = localPositionPhoto; self.geolocations = geolocations; self.location = location; self.senderID = sender; self.timestamp = @([timestamp timeIntervalSince1970]); self.messageMediaType = XHBubbleMessageMediaTypeLocalPosition; } return self; } #pragma mark - NSCoding - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { _content = [aDecoder decodeObjectForKey:@"content"]; _photo = [aDecoder decodeObjectForKey:@"photo"]; // _thumbnailUrl = [aDecoder decodeObjectForKey:@"thumbnailUrl"]; // _originPhotoUrl = [aDecoder decodeObjectForKey:@"originPhotoUrl"]; _videoConverPhoto = [aDecoder decodeObjectForKey:@"videoConverPhoto"]; _videoPath = [aDecoder decodeObjectForKey:@"videoPath"]; // _videoUrl = [aDecoder decodeObjectForKey:@"videoUrl"]; _voicePath = [aDecoder decodeObjectForKey:@"voicePath"]; // _voiceUrl = [aDecoder decodeObjectForKey:@"voiceUrl"]; _duration = [aDecoder decodeObjectForKey:@"duration"]; _emotionPath = [aDecoder decodeObjectForKey:@"emotionPath"]; _localPositionPhoto = [aDecoder decodeObjectForKey:@"localPositionPhoto"]; _geolocations = [aDecoder decodeObjectForKey:@"geolocations"]; _location = [aDecoder decodeObjectForKey:@"location"]; _avatar = [aDecoder decodeObjectForKey:@"avatar"]; _avator = [aDecoder decodeObjectForKey:@"avator"]; _senderID = [aDecoder decodeObjectForKey:@"senderID"]; _timestamp = [aDecoder decodeObjectForKey:@"timestamp"]; _messageMediaType = [[aDecoder decodeObjectForKey:@"messageMediaType"] integerValue]; _bubbleMessageType = [[aDecoder decodeObjectForKey:@"bubbleMessageType"] integerValue]; _isRead = [[aDecoder decodeObjectForKey:@"isRead"] boolValue]; } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:self.content forKey:@"content"]; [aCoder encodeObject:self.photo forKey:@"photo"]; // [aCoder encodeObject:self.thumbnailUrl forKey:@"thumbnailUrl"]; // [aCoder encodeObject:self.originPhotoUrl forKey:@"originPhotoUrl"]; [aCoder encodeObject:self.videoConverPhoto forKey:@"videoConverPhoto"]; [aCoder encodeObject:self.videoPath forKey:@"videoPath"]; // [aCoder encodeObject:self.videoUrl forKey:@"videoUrl"]; [aCoder encodeObject:self.voicePath forKey:@"voicePath"]; // [aCoder encodeObject:self.voiceUrl forKey:@"voiceUrl"]; [aCoder encodeObject:self.duration forKey:@"duration"]; [aCoder encodeObject:self.emotionPath forKey:@"emotionPath"]; [aCoder encodeObject:self.localPositionPhoto forKey:@"localPositionPhoto"]; [aCoder encodeObject:self.geolocations forKey:@"geolocations"]; [aCoder encodeObject:self.location forKey:@"location"]; [aCoder encodeObject:self.avatar forKey:@"avatar"]; [aCoder encodeObject:self.avator forKey:@"avator"]; [aCoder encodeObject:self.senderID forKey:@"senderID"]; [aCoder encodeObject:self.timestamp forKey:@"timestamp"]; [aCoder encodeObject:[NSNumber numberWithInteger:self.messageMediaType] forKey:@"messageMediaType"]; [aCoder encodeObject:[NSNumber numberWithInteger:self.bubbleMessageType] forKey:@"bubbleMessageType"]; [aCoder encodeObject:[NSNumber numberWithBool:self.isRead] forKey:@"isRead"]; } #pragma mark - NSCopying - (id)copyWithZone:(NSZone *)zone { switch (self.messageMediaType) { case XHBubbleMessageMediaTypeText: return [[[self class] allocWithZone:zone] initWithText:[self.content copy] sender:[self.senderID copy] timestamp:[self.timestamp copy]]; case XHBubbleMessageMediaTypePhoto: return [[[self class] allocWithZone:zone] initWithPhoto:[self.photo copy] thumbnailUrl:[self.content copy] originPhotoUrl:nil sender:[self.senderID copy] timestamp:[self.timestamp copy]]; case XHBubbleMessageMediaTypeVideo: return [[[self class] allocWithZone:zone] initWithVideoConverPhoto:[self.videoConverPhoto copy] videoPath:[self.videoPath copy] videoUrl:[self.content copy] sender:[self.senderID copy] timestamp:[self.timestamp copy]]; case XHBubbleMessageMediaTypeVoice: return [[[self class] allocWithZone:zone] initWithVoicePath:[self.voicePath copy] voiceUrl:[self.content copy] voiceDuration:[self.duration copy] sender:[self.senderID copy] timestamp:[self.timestamp copy]]; case XHBubbleMessageMediaTypeEmotion: return [[[self class] allocWithZone:zone] initWithEmotionPath:[self.emotionPath copy] sender:[self.senderID copy] timestamp:[self.timestamp copy]]; case XHBubbleMessageMediaTypeLocalPosition: return [[[self class] allocWithZone:zone] initWithLocalPositionPhoto:[self.localPositionPhoto copy] geolocations:self.geolocations location:[self.location copy] sender:[self.senderID copy] timestamp:[self.timestamp copy]]; default: return nil; } } @end