Initial commit

This commit is contained in:
ifish
2017-08-15 16:59:01 +08:00
commit bdc83e07ef
5766 changed files with 423223 additions and 0 deletions
@@ -0,0 +1,20 @@
//
// NTESChartletAttachment.h
// NIM
//
// Created by chris on 15/7/10.
// Copyright © 2015年 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NTESCustomAttachmentDefines.h"
@interface NTESChartletAttachment : NSObject<NIMCustomAttachment,NTESCustomAttachmentInfo>
@property (nonatomic,copy) NSString *chartletId;
@property (nonatomic,copy) NSString *chartletCatalog;
@property (nonatomic,strong) UIImage *showCoverImage;
@end
@@ -0,0 +1,57 @@
//
// NTESChartletAttachment.h
// NIM
//
// Created by chris on 15/7/10.
// Copyright © 2015年 Netease. All rights reserved.
//
#import "NTESChartletAttachment.h"
#import "UIImage+NTES.h"
#import "NTESSessionUtil.h"
@implementation NTESChartletAttachment
- (NSString *)encodeAttachment
{
NSDictionary *dict = @{
CMType : @( CustomMessageTypeChartlet),
CMData : @{ CMCatalog : self.chartletCatalog? self.chartletCatalog : @"",
CMChartlet : self.chartletId?self.chartletId : @""
}
};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:nil];
NSString *content = nil;
if (data) {
content = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
}
return content;
}
- (NSString *)cellContent:(NIMMessage *)message{
return @"NTESSessionChartletContentView";
}
- (CGSize)contentSize:(NIMMessageModel *)model cellWidth:(CGFloat)width{
if (!self.showCoverImage) {
UIImage *image = [UIImage fetchChartlet:self.chartletId chartletId:self.chartletCatalog];
self.showCoverImage = image;
}
return self.showCoverImage.size;
}
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
CGFloat bubblePaddingForImage = 3.f;
CGFloat bubbleArrowWidthForImage = 5.f;
if (message.isOutgoingMsg) {
return UIEdgeInsetsMake(bubblePaddingForImage,bubblePaddingForImage,bubblePaddingForImage,bubblePaddingForImage + bubbleArrowWidthForImage);
}else{
return UIEdgeInsetsMake(bubblePaddingForImage,bubblePaddingForImage + bubbleArrowWidthForImage, bubblePaddingForImage,bubblePaddingForImage);
}
}
@end
@@ -0,0 +1,14 @@
//
// NTESCustomAttachmentDecoder.h
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <NIMSDK/NIMSDK.h>
@interface NTESCustomAttachmentDecoder : NSObject<NIMCustomAttachmentCoding>
@end
@@ -0,0 +1,106 @@
//
// NTESCustomAttachmentDecoder.m
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import "NTESCustomAttachmentDecoder.h"
#import "NTESCustomAttachmentDefines.h"
#import "NTESJanKenPonAttachment.h"
#import "NTESSnapchatAttachment.h"
#import "NTESChartletAttachment.h"
#import "NTESWhiteboardAttachment.h"
#import "NSDictionary+NTESJson.h"
#import "NTESSessionUtil.h"
#import "IfishAttachment.h"
@implementation NTESCustomAttachmentDecoder
- (id<NIMCustomAttachment>)decodeAttachment:(NSString *)content
{
id<NIMCustomAttachment> attachment = nil;
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
if (data) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data
options:0
error:nil];
if ([dict isKindOfClass:[NSDictionary class]])
{
NSInteger type = [dict jsonInteger:CMType];
NSDictionary *data = [dict jsonDict:CMData];
switch (type) {
case CustomMessageTypeJanKenPon:
{
attachment = [[NTESJanKenPonAttachment alloc] init];
((NTESJanKenPonAttachment *)attachment).value = [data jsonInteger:CMValue];
}
break;
case CustomMessageTypeSnapchat:
{
attachment = [[NTESSnapchatAttachment alloc] init];
((NTESSnapchatAttachment *)attachment).md5 = [data jsonString:CMMD5];
((NTESSnapchatAttachment *)attachment).url = [data jsonString:CMURL];
((NTESSnapchatAttachment *)attachment).isFired = [data jsonBool:CMFIRE];
}
break;
case CustomMessageTypeChartlet:
{
attachment = [[NTESChartletAttachment alloc] init];
((NTESChartletAttachment *)attachment).chartletCatalog = [data jsonString:CMCatalog];
((NTESChartletAttachment *)attachment).chartletId = [data jsonString:CMChartlet];
}
break;
case CustomMessageTypeWhiteboard:
{
attachment = [[NTESWhiteboardAttachment alloc] init];
((NTESWhiteboardAttachment *)attachment).flag = [data jsonInteger:CMFlag];
}
break;
case CustomMessageTypeIfishPushMassage:
{
//爱鱼奇自定义推送消息
attachment = [[IfishAttachment alloc] init];
((IfishAttachment *)attachment).pushlink=[data jsonString:CMIfishPushlink];
((IfishAttachment *)attachment).timestamp=[data jsonString:CMIfishTimestamp];
((IfishAttachment *)attachment).pushType=[data jsonString:CMIfishPushType];
((IfishAttachment *)attachment).pushId=[data jsonString:CMIfishPushId];
}
break;
default:
break;
}
attachment = [self checkAttachment:attachment] ? attachment : nil;
}
}
return attachment;
}
- (BOOL)checkAttachment:(id<NIMCustomAttachment>)attachment{
BOOL check = NO;
if ([attachment isKindOfClass:[NTESJanKenPonAttachment class]]) {
NSInteger value = [((NTESJanKenPonAttachment *)attachment) value];
check = (value>=CustomJanKenPonValueKen && value<=CustomJanKenPonValuePon) ? YES : NO;
}
else if ([attachment isKindOfClass:[NTESSnapchatAttachment class]]) {
check = YES;
}
else if ([attachment isKindOfClass:[NTESChartletAttachment class]]) {
NSString *chartletCatalog = ((NTESChartletAttachment *)attachment).chartletCatalog;
NSString *chartletId =((NTESChartletAttachment *)attachment).chartletId;
check = chartletCatalog.length&&chartletId.length ? YES : NO;
}
else if ([attachment isKindOfClass:[NTESWhiteboardAttachment class]]) {
NSInteger flag = [((NTESWhiteboardAttachment *)attachment) flag];
check = ((flag >= CustomWhiteboardFlagInvite) && (flag <= CustomWhiteboardFlagClose)) ? YES : NO;
}
return check;
}
@end
@@ -0,0 +1,57 @@
//
// NTESCustomAttachmentDefines.h
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#ifndef NIM_NTESCustomAttachmentTypes_h
#define NIM_NTESCustomAttachmentTypes_h
typedef NS_ENUM(NSInteger,NTESCustomMessageType){
CustomMessageTypeJanKenPon = 1, //剪子石头布
CustomMessageTypeSnapchat = 2, //阅后即焚
CustomMessageTypeChartlet = 3, //贴图表情
CustomMessageTypeWhiteboard = 4, //白板会话
CustomMessageTypeIfishPushMassage = 5,//爱鱼奇官方帐号推送消息文本
};
#define CMType @"type"
#define CMData @"data"
#define CMValue @"value"
#define CMFlag @"flag"
#define CMURL @"url"
#define CMMD5 @"md5"
#define CMFIRE @"fired" //阅后即焚消息是否被焚毁
#define CMCatalog @"catalog" //贴图类别
#define CMChartlet @"chartlet" //贴图表情ID
#define CMIfishPushlink @"pushlink"
#define CMIfishTimestamp @"timestamp"
#define CMIfishPushType @"pushType"
#define CMIfishPushId @"pushId"
#endif
@protocol NTESCustomAttachmentInfo <NSObject>
@optional
- (NSString *)cellContent:(NIMMessage *)message;
- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width;
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message;
- (NSString *)formatedMessage;
- (UIImage *)showCoverImage;
- (void)setShowCoverImage:(UIImage *)image;
@end
@@ -0,0 +1,24 @@
//
// NTESJanKenPonAttachment.h
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NTESCustomAttachmentDefines.h"
typedef NS_ENUM(NSInteger, CustomJanKenPonValue) {
CustomJanKenPonValueKen = 1,//石头
CustomJanKenPonValueJan = 2,//剪子
CustomJanKenPonValuePon = 3,//布
};
@interface NTESJanKenPonAttachment : NSObject<NIMCustomAttachment,NTESCustomAttachmentInfo>
@property (nonatomic,assign) CustomJanKenPonValue value;
@property (nonatomic,strong) UIImage *showCoverImage;
@end
@@ -0,0 +1,83 @@
//
// NTESJanKenPonAttachment.m
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import "NTESJanKenPonAttachment.h"
#import "NTESSessionUtil.h"
@implementation NTESJanKenPonAttachment
- (NSString *)encodeAttachment
{
NSDictionary *dict = @{CMType : @(CustomMessageTypeJanKenPon),
CMData : @{CMValue:@(self.value)}};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:nil];
NSString *content = nil;
if (data) {
content = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
}
return content;
}
- (NSString *)cellContent:(NIMMessage *)message{
return @"NTESSessionJankenponContentView";
}
- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width{
return self.showCoverImage.size;
}
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
if (message.session.sessionType == NIMSessionTypeChatroom)
{
CGFloat bubbleMarginTopForImage = 15.f;
CGFloat bubbleMarginLeftForImage = 12.f;
return UIEdgeInsetsMake(bubbleMarginTopForImage,bubbleMarginLeftForImage,0,0);
}
else
{
CGFloat bubbleMarginForImage = 3.f;
CGFloat bubbleArrowWidthForImage = 5.f;
if (message.isOutgoingMsg) {
return UIEdgeInsetsMake(bubbleMarginForImage,bubbleMarginForImage,bubbleMarginForImage,bubbleMarginForImage + bubbleArrowWidthForImage);
}else{
return UIEdgeInsetsMake(bubbleMarginForImage,bubbleMarginForImage + bubbleArrowWidthForImage, bubbleMarginForImage,bubbleMarginForImage);
}
}
}
- (UIImage *)showCoverImage
{
if (_showCoverImage == nil)
{
UIImage *image;
switch (self.value) {
case CustomJanKenPonValueJan:
image = [UIImage imageNamed:@"custom_msg_jan"];
break;
case CustomJanKenPonValueKen:
image = [UIImage imageNamed:@"custom_msg_ken"];
break;
case CustomJanKenPonValuePon:
image = [UIImage imageNamed:@"custom_msg_pon"];
break;
default:
break;
}
_showCoverImage = image;
}
return _showCoverImage;
}
@end
@@ -0,0 +1,23 @@
//
// NTESLocationPoint.h
// NIM
//
// Created by chris on 15/2/28.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@class NIMLocationObject;
@interface NTESLocationPoint : NSObject<MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, readonly, copy) NSString *title;
- (instancetype)initWithLocationObject:(NIMLocationObject *)locationObject;
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate andTitle:(NSString*)title;
@end
@@ -0,0 +1,37 @@
//
// NTESLocationPoint.m
// NIM
//
// Created by chris on 15/2/28.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import "NTESLocationPoint.h"
@implementation NTESLocationPoint
- (instancetype)initWithLocationObject:(NIMLocationObject *)locationObject{
self = [super init];
if (self) {
CLLocationCoordinate2D coordinate;
coordinate.longitude = locationObject.longitude;
coordinate.latitude = locationObject.latitude;
_coordinate = coordinate;
_title = locationObject.title;
}
return self;
}
- (instancetype)initWithCoordinate:(CLLocationCoordinate2D)coordinate andTitle:(NSString *)title{
self = [super init];
if (self) {
_coordinate = coordinate;
_title = title;
}
return self;
}
@end
@@ -0,0 +1,35 @@
//
// NTESSearchLocalHistoryObject.h
// NIM
//
// Created by chris on 15/7/8.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef NS_ENUM(NSInteger, NTESSearchLocalHistoryType){
SearchLocalHistoryTypeEntrance,
SearchLocalHistoryTypeContent,
};
@class NTESSearchLocalHistoryObject;
@protocol NTESSearchObjectRefresh <NSObject>
- (void)refresh:(NTESSearchLocalHistoryObject *)object;
@end
@interface NTESSearchLocalHistoryObject : NSObject
@property (nonatomic,copy) NSString *content;
@property (nonatomic,assign) CGFloat uiHeight;
@property (nonatomic,assign) NTESSearchLocalHistoryType type;
@property (nonatomic,readonly) NIMMessage *message;
- (instancetype)initWithMessage:(NIMMessage *)message;
@end
@@ -0,0 +1,36 @@
//
// NTESSearchLocalHistoryObject.m
// NIM
//
// Created by chris on 15/7/8.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import "NTESSearchLocalHistoryObject.h"
#import "NTESSearchCellLayoutConstant.h"
@implementation NTESSearchLocalHistoryObject
- (instancetype)initWithMessage:(NIMMessage *)message{
self = [super init];
if (self) {
_message = message;
[self calculateHistoryItemHeight];
}
return self;
}
- (void)calculateHistoryItemHeight{
NSString *content = _message.text;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
label.numberOfLines = 0;
label.font = [UIFont systemFontOfSize:SearchCellContentFontSize];
label.text = content;
CGSize labelSize = [label sizeThatFits:CGSizeMake(SearchCellContentMaxWidth * UISreenWidthScale, CGFLOAT_MAX)];
CGFloat labelHeight = MAX(SearchCellContentMinHeight, labelSize.height);
CGFloat height = labelHeight + SearchCellContentTop + SearchCellContentBottom;
_uiHeight = height;
}
@end
@@ -0,0 +1,22 @@
//
// NTESSessionFileTransDelegate.h
// NIMDemo
//
// Created by ght on 15-1-29.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
/**
* 图片 视频等上传进度 和 语音播放等子状态回调
*/
@protocol NTESSessionFileStatusProtocol <NSObject>
@optional
- (void)updateMsgSubStatusByAudio:(NSInteger)status;
- (void)updateProgress:(float)progress;
@end
@@ -0,0 +1,45 @@
//
// NTESSessionMsgHelper.h
// NIMDemo
//
// Created by ght on 15-1-28.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <NIMSDK/NIMSDK.h>
@class NTESLocationPoint;
@class NTESJanKenPonAttachment;
@class NTESSnapchatAttachment;
@class NTESChartletAttachment;
@class NTESWhiteboardAttachment;
@interface NTESSessionMsgConverter : NSObject
+ (NIMMessage *)msgWithText:(NSString*)text;
+ (NIMMessage *)msgWithImage:(UIImage*)image;
+ (NIMMessage *)msgWithAudio:(NSString*)filePath;
+ (NIMMessage *)msgWithVideo:(NSString*)filePath;
+ (NIMMessage *)msgWithLocation:(NTESLocationPoint*)locationPoint;
+ (NIMMessage *)msgWithJenKenPon:(NTESJanKenPonAttachment *)attachment;
+ (NIMMessage *)msgWithSnapchatAttachment:(NTESSnapchatAttachment *)attachment;
+ (NIMMessage *)msgWithChartletAttachment:(NTESChartletAttachment *)attachment;
+ (NIMMessage *)msgWithWhiteboardAttachment:(NTESWhiteboardAttachment *)attachment;
+ (NIMMessage *)msgWithFilePath:(NSString*)path;
+ (NIMMessage *)msgWithFileData:(NSData*)data extension:(NSString*)extension;
+ (NIMMessage *)msgWithTip:(NSString *)tip;
@end
@@ -0,0 +1,168 @@
//
// NTESSessionMsgHelper.m
// NIMDemo
//
// Created by ght on 15-1-28.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import "NTESSessionMsgConverter.h"
#import "NTESLocationPoint.h"
#import "NSString+NTES.h"
#import "NTESJanKenPonAttachment.h"
#import "NTESSnapchatAttachment.h"
#import "NTESChartletAttachment.h"
#import "NTESWhiteboardAttachment.h"
@implementation NTESSessionMsgConverter
+ (NIMMessage*)msgWithText:(NSString*)text
{
NIMMessage *textMessage = [[NIMMessage alloc] init];
textMessage.text = text;
return textMessage;
}
+ (NIMMessage*)msgWithImage:(UIImage*)image
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NIMImageObject * imageObject = [[NIMImageObject alloc] initWithImage:image];
imageObject.displayName = [NSString stringWithFormat:@"图片发送于%@",dateString];
NIMImageOption *option = [[NIMImageOption alloc] init];
option.compressQuality = 0.8;
imageObject.option = option;
NIMMessage *message = [[NIMMessage alloc] init];
message.messageObject = imageObject;
message.apnsContent = @"发来了一张图片";
return message;
}
+ (NIMMessage*)msgWithAudio:(NSString*)filePath
{
NIMAudioObject *audioObject = [[NIMAudioObject alloc] initWithSourcePath:filePath];
NIMMessage *message = [[NIMMessage alloc] init];
message.messageObject = audioObject;
message.apnsContent = @"发来了一段语音";
return message;
}
+ (NIMMessage*)msgWithVideo:(NSString*)filePath
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NIMVideoObject *videoObject = [[NIMVideoObject alloc] initWithSourcePath:filePath];
videoObject.displayName = [NSString stringWithFormat:@"视频发送于%@",dateString];
NIMMessage *message = [[NIMMessage alloc] init];
message.messageObject = videoObject;
message.apnsContent = @"发来了一段视频";
return message;
}
+ (NIMMessage*)msgWithLocation:(NTESLocationPoint*)locationPoint{
NIMLocationObject *locationObject = [[NIMLocationObject alloc] initWithLatitude:locationPoint.coordinate.latitude
longitude:locationPoint.coordinate.longitude
title:locationPoint.title];
NIMMessage *message = [[NIMMessage alloc] init];
message.messageObject = locationObject;
message.apnsContent = @"发来了一条位置信息";
return message;
}
+ (NIMMessage*)msgWithJenKenPon:(NTESJanKenPonAttachment *)attachment
{
NIMMessage *message = [[NIMMessage alloc] init];
NIMCustomObject *customObject = [[NIMCustomObject alloc] init];
customObject.attachment = attachment;
message.messageObject = customObject;
message.apnsContent = @"发来了猜拳信息";
return message;
}
+ (NIMMessage*)msgWithSnapchatAttachment:(NTESSnapchatAttachment *)attachment
{
NIMMessage *message = [[NIMMessage alloc] init];
NIMCustomObject *customObject = [[NIMCustomObject alloc] init];
customObject.attachment = attachment;
message.messageObject = customObject;
message.apnsContent = @"发来了阅后即焚";
NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
setting.historyEnabled = NO;
setting.roamingEnabled = NO;
setting.syncEnabled = NO;
message.setting = setting;
return message;
}
+ (NIMMessage*)msgWithFilePath:(NSString*)path{
NIMFileObject *fileObject = [[NIMFileObject alloc] initWithSourcePath:path];
NSString *displayName = path.lastPathComponent;
fileObject.displayName = displayName;
NIMMessage *message = [[NIMMessage alloc] init];
message.messageObject = fileObject;
message.apnsContent = @"发来了一个文件";
return message;
}
+ (NIMMessage*)msgWithFileData:(NSData*)data extension:(NSString*)extension{
NIMFileObject *fileObject = [[NIMFileObject alloc] initWithData:data extension:extension];
NSString *displayName;
if (extension.length) {
displayName = [NSString stringWithFormat:@"%@.%@",[NSUUID UUID].UUIDString.MD5String,extension];
}else{
displayName = [NSString stringWithFormat:@"%@",[NSUUID UUID].UUIDString.MD5String];
}
fileObject.displayName = displayName;
NIMMessage *message = [[NIMMessage alloc] init];
message.messageObject = fileObject;
message.apnsContent = @"发来了一个文件";
return message;
}
+ (NIMMessage*)msgWithChartletAttachment:(NTESChartletAttachment *)attachment{
NIMMessage *message = [[NIMMessage alloc] init];
NIMCustomObject *customObject = [[NIMCustomObject alloc] init];
customObject.attachment = attachment;
message.messageObject = customObject;
message.apnsContent = @"[贴图]";
return message;
}
+ (NIMMessage*)msgWithWhiteboardAttachment:(NTESWhiteboardAttachment *)attachment
{
NIMMessage *message = [[NIMMessage alloc] init];
NIMCustomObject *customObject = [[NIMCustomObject alloc] init];
customObject.attachment = attachment;
message.messageObject = customObject;
NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
setting.apnsEnabled = NO;
message.setting = setting;
return message;
}
+ (NIMMessage *)msgWithTip:(NSString *)tip
{
NIMMessage *message = [[NIMMessage alloc] init];
NIMTipObject *tipObject = [[NIMTipObject alloc] init];
message.messageObject = tipObject;
message.text = tip;
NIMMessageSetting *setting = [[NIMMessageSetting alloc] init];
setting.apnsEnabled = NO;
setting.shouldBeCounted = NO;
message.setting = setting;
return message;
}
@end
@@ -0,0 +1,29 @@
//
// SnapchatAttachment.h
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <NIMSDK/NIMSDK.h>
#import "NTESCustomAttachmentDefines.h"
@interface NTESSnapchatAttachment : NSObject<NIMCustomAttachment,NTESCustomAttachmentInfo>
@property (nonatomic,copy) NSString *md5;
@property (nonatomic,copy) NSString *url;
@property (nonatomic,assign) BOOL isFired; //是否焚毁
@property (nonatomic,strong) UIImage *showCoverImage;
- (void)setImage:(UIImage *)image;
- (void)setImageFilePath:(NSString *)path;
- (NSString *)filepath;
@end
@@ -0,0 +1,155 @@
//
// SnapchatAttachment.m
// NIM
//
// Created by amao on 7/2/15.
// Copyright (c) 2015 Netease. All rights reserved.
//
#import "NTESSnapchatAttachment.h"
#import "NTESFileLocationHelper.h"
#import "NSData+NTES.h"
#import "NTESSessionUtil.h"
@interface NTESSnapchatAttachment()
@property (nonatomic,assign) BOOL isFromMe;
@end
@implementation NTESSnapchatAttachment
- (void)setImage:(UIImage *)image
{
NSData *data = UIImageJPEGRepresentation(image, 0.75);
NSString *md5= [data MD5String];
self.md5 = md5;
[data writeToFile:[self filepath]
atomically:YES];
}
- (void)setImageFilePath:(NSString *)path
{
if ([[NSFileManager defaultManager] fileExistsAtPath:path]){
NSData *data = [NSData dataWithContentsOfFile:path];
self.md5 = [data MD5String];
[data writeToFile:[self filepath]
atomically:YES];
}
}
- (void)setIsFired:(BOOL)isFired{
if (_isFired != isFired) {
_isFired = isFired;
[self updateCover];
}
}
- (NSString *)filepath
{
NSString *filename = [_md5 stringByAppendingFormat:@".%@",ImageExt];
return [NTESFileLocationHelper filepathForImage:filename];
}
- (NSString *)cellContent:(NIMMessage *)message{
return @"NTESSessionSnapchatContentView";
}
- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width{
self.isFromMe = message.isOutgoingMsg;
CGSize size = self.showCoverImage.size;
CGFloat customSnapMessageImageRightToText = 5;
return CGSizeMake(size.width + customSnapMessageImageRightToText, size.height);
}
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
CGFloat bubblePaddingForImage = 3.f;
CGFloat bubbleArrowWidthForImage = 5.f;
if (message.isOutgoingMsg) {
return UIEdgeInsetsMake(bubblePaddingForImage,bubblePaddingForImage,bubblePaddingForImage,bubblePaddingForImage + bubbleArrowWidthForImage);
}else{
return UIEdgeInsetsMake(bubblePaddingForImage,bubblePaddingForImage + bubbleArrowWidthForImage, bubblePaddingForImage,bubblePaddingForImage);
}
}
- (void)setIsFromMe:(BOOL)isFromMe{
if (_isFromMe != isFromMe) {
_isFromMe = isFromMe;
[self updateCover];
}
}
#pragma NIMCustomAttachment
- (NSString *)encodeAttachment
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[dict setObject:@(CustomMessageTypeSnapchat) forKey:CMType];
[data setObject:_md5?_md5:@"" forKey:CMMD5];
[data setObject:@(_isFired) forKey:CMFIRE];
if ([_url length])
{
[data setObject:_url forKey:CMURL];
}
[dict setObject:data forKey:CMData];
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:nil];
return [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
}
#pragma mark - 实现文件上传需要接口
- (BOOL)attachmentNeedsUpload
{
return [_url length] == 0;
}
- (NSString *)attachmentPathForUploading
{
return [self filepath];
}
- (void)updateAttachmentURL:(NSString *)urlString
{
self.url = urlString;
}
#pragma mark - Private
- (void)updateCover{
UIImage *image;
if (!self.isFromMe) {
if (self.isFired) {
image = [UIImage imageNamed:@"session_snapchat_other_readed"];
}else{
image = [UIImage imageNamed:@"session_snapchat_other_unread"];
}
}else{
if (self.isFired) {
image = [UIImage imageNamed:@"session_snapchat_self_readed"];
}else{
image = [UIImage imageNamed:@"session_snapchat_self_unread"];
}
}
self.showCoverImage = image;
}
- (UIImage *)showCoverImage
{
if (_showCoverImage == nil)
{
[self updateCover];
}
return _showCoverImage;
}
@end
@@ -0,0 +1,21 @@
//
// NTESWhiteboardAttachment.h
// NIM
//
// Created by 高峰 on 15/7/28.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "NTESCustomAttachmentDefines.h"
typedef NS_ENUM(NSInteger, CustomWhiteboardFlag) {
CustomWhiteboardFlagInvite = 0,//邀请
CustomWhiteboardFlagClose = 1,//关闭
};
@interface NTESWhiteboardAttachment : NSObject<NIMCustomAttachment,NTESCustomAttachmentInfo>
@property (nonatomic,assign) CustomWhiteboardFlag flag;
@end
@@ -0,0 +1,118 @@
//
// NTESWhiteboardAttachment.m
// NIM
//
// Created by 高峰 on 15/7/28.
// Copyright (c) 2015年 Netease. All rights reserved.
//
#import "NTESWhiteboardAttachment.h"
#import "M80AttributedLabel.h"
#import "NTESSessionUtil.h"
@implementation NTESWhiteboardAttachment
- (NSString *)encodeAttachment
{
NSDictionary *dict = @{CMType : @(CustomMessageTypeWhiteboard),
CMData : @{CMFlag:@(self.flag)}};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:nil];
NSString *content = nil;
if (data) {
content = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
}
return content;
}
- (NSString *)formatedMessage{
NSString *msg = @"";
switch (self.flag) {
case CustomWhiteboardFlagInvite:
msg = @"我发起了白板演示";
break;
case CustomWhiteboardFlagClose:
msg = @"白板演示已结束";
default:
break;
}
return msg;
}
- (NSString *)cellContent:(NIMMessage *)message{
NSString *content;
switch (self.flag) {
case CustomWhiteboardFlagInvite:
content = @"NTESSessionWhiteBoardContentView";
break;
case CustomWhiteboardFlagClose:
content = @"NIMSessionNotificationContentView";
default:
break;
}
return content;
}
- (CGSize)contentSize:(NIMMessage *)message cellWidth:(CGFloat)width{
CGSize contentSize;
switch (self.flag) {
case CustomWhiteboardFlagInvite:{
M80AttributedLabel *label = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
label.font = [UIFont systemFontOfSize:Message_Font_Size];
[label setText:self.formatedMessage];
UIImage *image = [UIImage imageNamed:@"icon_whiteboard_session_msg"];
CGFloat msgBubbleMaxWidth = (UIScreenWidth - 130);
CGFloat bubbleLeftToContent = 14;
CGFloat contentRightToBubble = 14;
CGFloat msgContentMaxWidth = (msgBubbleMaxWidth - contentRightToBubble - bubbleLeftToContent);
CGFloat customWhiteBorardMessageImageRightToText = 10.f;
CGSize labelSize = [label sizeThatFits:CGSizeMake(msgContentMaxWidth, CGFLOAT_MAX)];
contentSize = CGSizeMake(labelSize.width + image.size.width + customWhiteBorardMessageImageRightToText, labelSize.height);
break;
}
case CustomWhiteboardFlagClose:{
CGFloat messageWidth = width;
CGFloat messageHeight = 40;
contentSize = CGSizeMake(messageWidth, messageHeight);
break;
}
default:
break;
}
return contentSize;
}
- (UIEdgeInsets)contentViewInsets:(NIMMessage *)message
{
if (self.flag == CustomWhiteboardFlagClose) {
return UIEdgeInsetsZero;
}else{
CGFloat selfBubbleTopToContentForBoard = 11.f;
CGFloat selfBubbleLeftToContentForBoard = 11.f;
CGFloat selfContentButtomToBubbleForBoard = 9.f;
CGFloat selfBubbleRightToContentForBoard = 15.f;
CGFloat otherBubbleTopToContentForBoard = 11.f;
CGFloat otherBubbleLeftToContentForBoard = 15.f;
CGFloat otherContentButtomToBubbleForBoard = 9.f;
CGFloat otherContentRightToBubbleForBoard = 9.f;
return message.isOutgoingMsg ? UIEdgeInsetsMake(selfBubbleTopToContentForBoard,
selfBubbleLeftToContentForBoard,
selfContentButtomToBubbleForBoard,
selfBubbleRightToContentForBoard):
UIEdgeInsetsMake(otherBubbleTopToContentForBoard,
otherBubbleLeftToContentForBoard,
otherContentButtomToBubbleForBoard,
otherContentRightToBubbleForBoard);
}
}
@end