462 lines
19 KiB
Objective-C
462 lines
19 KiB
Objective-C
//
|
|
// XPRoomSendTextView.m
|
|
// xplan-ios
|
|
//
|
|
// Created by 冯硕 on 2021/10/29.
|
|
//
|
|
|
|
#import "XPRoomSendTextView.h"
|
|
///Third
|
|
#import <Masonry/Masonry.h>
|
|
#import <NIMSDK/NIMSDK.h>
|
|
#import "NSObject+MJExtension.h"
|
|
#import <IQKeyboardManager/IQKeyboardManager.h>
|
|
///Tool
|
|
#import "ThemeColor+Room.h"
|
|
#import "UIImage+Utils.h"
|
|
#import "AccountInfoStorage.h"
|
|
///Model
|
|
#import "XPMessageRemoteExtModel.h"
|
|
#import "UserInfoModel.h"
|
|
#import "RoomInfoModel.h"
|
|
#import "ClientConfig.h"
|
|
|
|
#import "Api+Message.h"
|
|
#import "UploadFile.h"
|
|
#import "TZImagePickerController.h"
|
|
//公屏限制最大字数
|
|
#define MAX_STARWORDS_LENGTH 300
|
|
|
|
@interface XPRoomSendTextView () <NIMChatManagerDelegate, TZImagePickerControllerDelegate>
|
|
|
|
///输入框
|
|
@property (nonatomic, strong) MSBaseTextField *editTextFiled;
|
|
@property (nonatomic, strong) UIView *bgEditTextFiled;
|
|
///发送按钮
|
|
@property (nonatomic, strong) UIButton *sendButton;
|
|
///发图片
|
|
@property(nonatomic, strong) UIButton *imageButton;
|
|
///文本输入的内容
|
|
@property (nonatomic,copy) NSString *inputMessage;
|
|
///代理
|
|
@property (nonatomic,weak) id<RoomHostDelegate> delegate;
|
|
|
|
///被@的人的数组 such as:@["'@小明','"@小红']
|
|
@property (nonatomic, strong) NSMutableArray *atNames;
|
|
///被@的人的uid 需要跟上面的名字对应位置
|
|
@property (nonatomic, strong) NSMutableArray *atUids;
|
|
@end
|
|
|
|
@implementation XPRoomSendTextView
|
|
|
|
- (void)dealloc {
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
}
|
|
|
|
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate {
|
|
self = [super initWithFrame:CGRectMake(0, KScreenHeight - 60, KScreenWidth, 60)];
|
|
if (self) {
|
|
self.delegate = delegate;
|
|
[[NIMSDK sharedSDK].chatManager addDelegate:self];
|
|
[self addNotification];
|
|
[self initSubViews];
|
|
[self initSubViewConstraints];
|
|
[IQKeyboardManager sharedManager].enable = NO;
|
|
}
|
|
return self;
|
|
}
|
|
|
|
- (void)sendMessage:(NIMMessage *)message didCompleteWithError:(NSError *)error {
|
|
|
|
}
|
|
|
|
- (void)didTapImageButton {
|
|
UserVipInfoVo *vipVO = [[self.delegate getUserInfo] userVipInfoVO];
|
|
if (!vipVO || vipVO.roomPicScreen == NO) {
|
|
[XNDJTDDLoadingTool showErrorWithMessage:YMLocalizedString(@"1.0.37_text_33")];
|
|
return;
|
|
}
|
|
|
|
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:self];
|
|
imagePickerVc.modalPresentationStyle = UIModalPresentationOverFullScreen;
|
|
imagePickerVc.allowPickingVideo = NO;
|
|
imagePickerVc.allowTakeVideo = NO;
|
|
imagePickerVc.maxImagesCount = 1;
|
|
imagePickerVc.naviBgColor = [DJDKMIMOMColor appCellBackgroundColor];
|
|
imagePickerVc.naviTitleColor = [DJDKMIMOMColor mainTextColor];
|
|
imagePickerVc.barItemTextColor = [DJDKMIMOMColor mainTextColor];
|
|
[self.delegate.getCurrentNav presentViewController:imagePickerVc animated:YES completion:nil];
|
|
}
|
|
|
|
#pragma mark - TZImagePickerControllerDelegate
|
|
- (void)imagePickerController:(TZImagePickerController *)picker
|
|
didFinishPickingPhotos:(NSArray<UIImage *> *)photos
|
|
sourceAssets:(NSArray *)assets
|
|
isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto
|
|
infos:(NSArray<NSDictionary *> *)infos {
|
|
|
|
[self.editTextFiled resignFirstResponder];
|
|
|
|
[XNDJTDDLoadingTool showOnlyView:kWindow enabled:YES];
|
|
|
|
[self uploadAlbumPicList:photos finish:^(NSArray *list) {
|
|
[XNDJTDDLoadingTool hideHUD];
|
|
NSDictionary *dic = [list xpSafeObjectAtIndex:0];
|
|
NSString *picUrl = [dic objectForKey:@"resUrl"];
|
|
if ([NSString isEmpty:picUrl]) {
|
|
return;
|
|
}
|
|
[Api sendRoomImageMessage:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) {
|
|
if (code == 200) {
|
|
|
|
} else {
|
|
[XNDJTDDLoadingTool showErrorWithMessage:msg];
|
|
}
|
|
} picUrl:picUrl roomUid:[self.delegate.getRoomInfo uid]];
|
|
}];
|
|
}
|
|
|
|
///上传图片
|
|
- (void)uploadAlbumPicList:(NSArray <UIImage *>*)array
|
|
finish:(void(^)(NSArray *list))finish {
|
|
dispatch_semaphore_t semaphore = dispatch_semaphore_create(1);
|
|
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
|
|
NSMutableArray * dataArray = [NSMutableArray array];
|
|
__block NSInteger imageCount = 0;
|
|
dispatch_async(queue, ^{
|
|
for (UIImage * image in array) {
|
|
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
|
|
NSData *data = UIImageJPEGRepresentation(image, 0.5);
|
|
NSString *format = [UIImage getImageTypeWithImageData:data];
|
|
NSString *name = [NSString stringWithFormat:@"image/%@.%@",[NSString createUUID],format];
|
|
[[UploadFile share]QCloudUploadImage:data named:name success:^(NSString * _Nonnull key, NSDictionary * _Nonnull resp) {
|
|
dispatch_semaphore_signal(semaphore);
|
|
imageCount ++;
|
|
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
|
|
if (image.size.width > 0){
|
|
[dict safeSetObject:@(image.size.width) forKey:@"width"];
|
|
}
|
|
if (image.size.height){
|
|
[dict safeSetObject:@(image.size.height) forKey:@"height"];
|
|
}
|
|
if (key.length > 0) {
|
|
[dict safeSetObject:key forKey:@"resUrl"];
|
|
}
|
|
[dict safeSetObject:format forKey:@"format"];
|
|
[dataArray addObject:dict];
|
|
if (imageCount == array.count) {
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
finish(dataArray);
|
|
});
|
|
}
|
|
} failure:^(NSNumber * _Nonnull resCode, NSString * _Nonnull message) {
|
|
imageCount ++;
|
|
dispatch_semaphore_signal(semaphore);
|
|
[XNDJTDDLoadingTool showErrorWithMessage:message];
|
|
}];
|
|
}
|
|
});
|
|
}
|
|
|
|
#pragma mark - Response
|
|
|
|
- (void)sendButtonDidClick:(UIButton *)sender {
|
|
UserInfoModel * userInfo = [self.delegate getUserInfo];
|
|
XPMessageRemoteExtModel *extModel = [[XPMessageRemoteExtModel alloc] init];
|
|
extModel.defUser = userInfo.defUser;
|
|
extModel.erbanNo = userInfo.erbanNo;
|
|
extModel.carName = userInfo.carName;
|
|
extModel.inRoomNameplatePic = userInfo.nameplatePic;
|
|
extModel.inRoomNameplateWord = userInfo.nameplateWord;
|
|
extModel.isCustomWord = userInfo.isCustomWord;
|
|
extModel.charmUrl = userInfo.userLevelVo.charmUrl;
|
|
extModel.experLevelSeq = userInfo.userLevelVo.experLevelSeq;
|
|
extModel.experUrl = userInfo.userLevelVo.experUrl;
|
|
extModel.newUser = userInfo.newUser;
|
|
extModel.vipIcon = userInfo.userVipInfoVO.nameplateUrl;
|
|
extModel.androidBubbleUrl = userInfo.androidBubbleUrl;
|
|
extModel.iosBubbleUrl = userInfo.iosBubbleUrl;
|
|
extModel.fromSayHelloChannel = userInfo.fromSayHelloChannel;
|
|
extModel.platformRole = userInfo.platformRole;
|
|
extModel.nick = userInfo.nick;
|
|
|
|
if([self.delegate getPublicScreenType]==1){
|
|
extModel.avatar = userInfo.avatar;
|
|
extModel.nick = userInfo.nick;
|
|
NSString * headwearUrl= userInfo.headwearEffect.length > 0 ? userInfo.headwearEffect : userInfo.headwearPic;
|
|
extModel.headWearUrl = headwearUrl;
|
|
}
|
|
|
|
NIMMessage * message = [[NIMMessage alloc] init];
|
|
message.text = self.inputMessage;
|
|
NSMutableDictionary *remoteExt = [NSMutableDictionary dictionaryWithObject:extModel.model2dictionary forKey:[AccountInfoStorage instance].getUid];
|
|
if([self.delegate getPublicScreenType] == 0){
|
|
//查找消息中是否有@人
|
|
NSMutableArray *nickArray = [NSMutableArray array];
|
|
NSMutableArray *uidArray = [NSMutableArray array];
|
|
for (int i = 0; i<self.atNames.count; i++) {
|
|
NSString *nick = self.atNames[i];
|
|
if ([self.editTextFiled.text containsString:nick]) {
|
|
if ([nickArray containsObject:nick]) {
|
|
continue;
|
|
} else {
|
|
[nickArray addObject:nick];
|
|
[uidArray addObject:self.atUids[i]];
|
|
}
|
|
}
|
|
}
|
|
id uidStr = [uidArray toJSONObject];
|
|
id nickStr = [nickArray toJSONObject];
|
|
[remoteExt setObject:uidStr forKey:@"atUids"];
|
|
[remoteExt setObject:nickStr forKey:@"atNames"];
|
|
}
|
|
message.remoteExt = remoteExt;
|
|
NSString *sessionId = [NSString stringWithFormat:@"%ld", [self.delegate getRoomInfo].roomId];
|
|
//构造会话
|
|
self.sendButton.enabled = NO;
|
|
NIMSession *session = [NIMSession session:sessionId type:NIMSessionTypeChatroom];
|
|
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session completion:^(NSError * _Nullable error) {
|
|
self.editTextFiled.text = @"";
|
|
[self.editTextFiled resignFirstResponder];
|
|
[self.atUids removeAllObjects];
|
|
[self.atNames removeAllObjects];
|
|
self.inputMessage = nil;
|
|
}];
|
|
}
|
|
|
|
- (void)keyboardWillShow:(NSNotification *)notification {
|
|
[self.superview bringSubviewToFront:self];
|
|
NSDictionary *info = [notification userInfo];
|
|
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
|
|
CGFloat keyBoardEndY = value.CGRectValue.origin.y;
|
|
NSNumber *curve = [info objectForKey:UIKeyboardAnimationCurveUserInfoKey];
|
|
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
|
|
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
|
|
CGFloat h = endKeyboardRect.size.height;
|
|
[UIView animateWithDuration:duration animations:^{
|
|
if(self.superview){
|
|
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.equalTo(self.superview);
|
|
make.height.equalTo(@60);
|
|
make.bottom.equalTo(self.superview).offset(-h);
|
|
}];
|
|
}
|
|
[UIView setAnimationBeginsFromCurrentState:YES];
|
|
[UIView setAnimationCurve:[curve intValue]];
|
|
self.center = CGPointMake(self.center.x, keyBoardEndY - statusbarHeight - self.bounds.size.height/2.0);
|
|
}];
|
|
}
|
|
|
|
//键盘隐藏
|
|
- (void)keyboardWillHidden:(NSNotification *)notification {
|
|
|
|
NSDictionary *info = [notification userInfo];
|
|
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
|
|
[UIView animateWithDuration:duration animations:^{
|
|
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.bottom.leading.trailing.equalTo(self.superview);
|
|
make.height.equalTo(@60);
|
|
}];
|
|
self.hidden = YES;
|
|
}];
|
|
}
|
|
|
|
-(void)textFieldEditChanged:(NSNotification *)notification{
|
|
|
|
UITextField *textField = (UITextField *)notification.object;
|
|
NSString *toBeString = textField.text;
|
|
NSString *lang = [textField.textInputMode primaryLanguage];
|
|
if ([lang isEqualToString:@"zh-Hans"]){// 简体中文输入
|
|
//获取高亮部分
|
|
UITextRange *selectedRange = [textField markedTextRange];
|
|
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
|
|
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
|
|
if (!position){
|
|
if (toBeString.length > MAX_STARWORDS_LENGTH){
|
|
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
|
|
if (rangeIndex.length == 1){
|
|
|
|
textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
|
|
}else{
|
|
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
|
|
textField.text = [toBeString substringWithRange:rangeRange];
|
|
}
|
|
}
|
|
}
|
|
}else{ // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
|
|
if (toBeString.length > MAX_STARWORDS_LENGTH){
|
|
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:MAX_STARWORDS_LENGTH];
|
|
if (rangeIndex.length == 1){
|
|
|
|
textField.text = [toBeString substringToIndex:MAX_STARWORDS_LENGTH];
|
|
}else{
|
|
|
|
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, MAX_STARWORDS_LENGTH)];
|
|
textField.text = [toBeString substringWithRange:rangeRange];
|
|
}
|
|
}
|
|
}
|
|
|
|
self.inputMessage = textField.text;
|
|
|
|
if (self.inputMessage.length > 0) {
|
|
self.sendButton.enabled = YES;
|
|
} else {
|
|
self.sendButton.enabled = NO;
|
|
}
|
|
}
|
|
|
|
#pragma mark - Public Method
|
|
+ (instancetype)showTextView:(UIView *)view delegate:(id<RoomHostDelegate>)delegate atUid:(NSString *)uid atNick:(NSString *)nick {
|
|
__block XPRoomSendTextView * textView;
|
|
[view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
if ([obj isKindOfClass:[XPRoomSendTextView class]]) {
|
|
textView = obj;
|
|
*stop = YES;
|
|
}
|
|
}];
|
|
if (textView == nil) {
|
|
textView = [[XPRoomSendTextView alloc] initWithDelegate:delegate];
|
|
[view addSubview:textView];
|
|
}else{
|
|
UIButton *sendButton = [textView viewWithTag:101];
|
|
[sendButton mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.mas_equalTo(58);
|
|
make.height.mas_equalTo(29);
|
|
make.trailing.mas_equalTo(-15);
|
|
make.centerY.equalTo(textView);
|
|
}];
|
|
}
|
|
textView.hidden = NO;
|
|
|
|
[textView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.trailing.bottom.mas_equalTo(view);
|
|
make.height.mas_equalTo(60);
|
|
}];
|
|
[textView.editTextFiled becomeFirstResponder];
|
|
if (nick) {
|
|
[textView.atUids addObject:uid];
|
|
[textView.atNames addObject:[NSString stringWithFormat:@"@%@", nick]];
|
|
textView.editTextFiled.text = [NSString stringWithFormat:@"%@@%@\u2004", textView.editTextFiled.text, nick];
|
|
textView.inputMessage = textView.editTextFiled.text;
|
|
textView.sendButton.enabled = YES;
|
|
}
|
|
return textView;
|
|
}
|
|
|
|
#pragma mark - Private Method
|
|
- (void)initSubViews {
|
|
self.backgroundColor = UIColorFromRGB(0x260159);
|
|
[self addSubview:self.bgEditTextFiled];
|
|
[self.bgEditTextFiled addSubview:self.editTextFiled];
|
|
[self addSubview:self.sendButton];
|
|
[self addSubview:self.imageButton];
|
|
}
|
|
|
|
- (void)initSubViewConstraints {
|
|
|
|
[self.bgEditTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.mas_equalTo(6);
|
|
make.height.mas_equalTo(36);
|
|
make.top.mas_equalTo(12);
|
|
make.trailing.equalTo(self.sendButton.mas_leading).mas_offset(-8);
|
|
|
|
}];
|
|
|
|
[self.imageButton mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.centerY.mas_equalTo(self.bgEditTextFiled);
|
|
make.size.mas_equalTo(CGSizeMake(26, 26));
|
|
make.trailing.mas_equalTo(self.bgEditTextFiled).offset(-10);
|
|
}];
|
|
|
|
[self.editTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
make.leading.mas_equalTo(20);
|
|
make.trailing.mas_equalTo(self.imageButton.mas_leading).offset(-8);
|
|
make.top.bottom.mas_equalTo(0);
|
|
}];
|
|
|
|
[self.sendButton mas_remakeConstraints:^(MASConstraintMaker *make) {
|
|
make.width.mas_equalTo(58);
|
|
make.height.mas_equalTo(29);
|
|
make.trailing.mas_equalTo(-15);
|
|
make.centerY.equalTo(self);
|
|
}];
|
|
}
|
|
|
|
- (void)addNotification {
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
|
|
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditChanged:) name:UITextFieldTextDidChangeNotification object:self.editTextFiled];
|
|
}
|
|
|
|
-(void)showInputView:(NSString *)text{
|
|
self.editTextFiled.text = text;
|
|
self.inputMessage = text;
|
|
if(text.length > 0){
|
|
self.sendButton.enabled = YES;
|
|
}
|
|
}
|
|
|
|
#pragma mark - Getters And Setters
|
|
- (MSBaseTextField *)editTextFiled{
|
|
if (!_editTextFiled) {
|
|
_editTextFiled = [[MSBaseTextField alloc] init];
|
|
NSAttributedString * attribute = [[NSAttributedString alloc] initWithString:YMLocalizedString(@"XPRoomSendTextView0") attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : [UIColor colorWithWhite:1 alpha:0.5]}];
|
|
_editTextFiled.attributedPlaceholder = attribute;
|
|
_editTextFiled.borderStyle = UITextBorderStyleNone;
|
|
_editTextFiled.textColor = [UIColor whiteColor];
|
|
_editTextFiled.font = [UIFont systemFontOfSize:13 weight:UIFontWeightMedium];
|
|
[_editTextFiled setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
|
|
|
|
}
|
|
return _editTextFiled;
|
|
}
|
|
- (UIButton *)sendButton{
|
|
if (!_sendButton) {
|
|
_sendButton = [[UIButton alloc] init];
|
|
[_sendButton setTitle:YMLocalizedString(@"XPRoomSendTextView1") forState:UIControlStateNormal];
|
|
_sendButton.titleLabel.textColor = [UIColor whiteColor];
|
|
_sendButton.titleLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightMedium];
|
|
[_sendButton setBackgroundImage:[UIImage imageWithColor:[DJDKMIMOMColor disableButtonColor] ]forState:UIControlStateDisabled];
|
|
[_sendButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[UIColorFromRGB(0x7E5AFF), UIColorFromRGB(0x52CAD3)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
|
|
_sendButton.enabled = NO;
|
|
_sendButton.layer.cornerRadius = 7.5;
|
|
_sendButton.layer.masksToBounds = YES;
|
|
[_sendButton addTarget:self action:@selector(sendButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
|
|
_sendButton.tag = 101;
|
|
}
|
|
return _sendButton;
|
|
}
|
|
|
|
- (NSMutableArray *)atNames {
|
|
if (!_atNames) {
|
|
_atNames = [NSMutableArray array];
|
|
}
|
|
return _atNames;
|
|
}
|
|
|
|
- (NSMutableArray *)atUids {
|
|
if (!_atUids) {
|
|
_atUids = [NSMutableArray array];
|
|
}
|
|
return _atUids;
|
|
}
|
|
- (UIButton *)imageButton{
|
|
if(!_imageButton){
|
|
_imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
|
|
[_imageButton setBackgroundImage:kImage(@"send_image_icon") forState:UIControlStateNormal];
|
|
[_imageButton addTarget:self action:@selector(didTapImageButton) forControlEvents:UIControlEventTouchUpInside];
|
|
}
|
|
return _imageButton;
|
|
}
|
|
- (UIView *)bgEditTextFiled{
|
|
if(!_bgEditTextFiled){
|
|
_bgEditTextFiled = [UIView new];
|
|
_bgEditTextFiled.backgroundColor = UIColorFromRGB(0x361661);
|
|
_bgEditTextFiled.layer.cornerRadius = 18;
|
|
_bgEditTextFiled.layer.masksToBounds = YES;
|
|
}
|
|
return _bgEditTextFiled;
|
|
}
|
|
@end
|