Files
yinmeng-ios/xplan-ios/Main/Room/View/MenuContainerView/XPRoomSendTextView.m
2021-10-29 15:53:44 +08:00

252 lines
8.8 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"
///Tool
#import "ThemeColor.h"
#import "XPMacro.h"
#import "UIImage+Utils.h"
#import "XPConstant.h"
#import "AccountInfoStorage.h"
///Model
#import "XPMessageRemoteExtModel.h"
#import "UserInfoModel.h"
#import "RoomInfoModel.h"
//公屏限制最大字数
#define MAX_STARWORDS_LENGTH 300
@interface XPRoomSendTextView ()
///
@property (nonatomic,strong) UIStackView *stackView;
///输入框
@property (nonatomic, strong) UITextField *editTextFiled;
///发送按钮
@property (nonatomic, strong) UIButton *sendButton;
///文本输入的内容
@property (nonatomic,copy) NSString *inputMessage;
///代理
@property (nonatomic,weak) id<RoomHostDelegate> delegate;
@end
@implementation XPRoomSendTextView
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate {
self = [super init];
if (self) {
self.delegate = delegate;
[self addNotification];
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Response
- (void)sendButtonDidClick:(UIButton *)sender {
UserInfoModel * userInfo = [self.delegate getUserInfo];
XPMessageRemoteExtModel *extModel = [[XPMessageRemoteExtModel alloc] init];
if (userInfo.userLevelVo.charmUrl) {
extModel.charmUrl = userInfo.userLevelVo.charmUrl;
}
if (userInfo.userLevelVo.experUrl) {
extModel.experUrl = userInfo.userLevelVo.experUrl;
}
extModel.defUser = userInfo.userLevelVo.defUser;
if (userInfo.erbanNo) {
extModel.erbanNo = [NSString stringWithFormat:@"%ld", userInfo.erbanNo];
}
extModel.newUser = userInfo.newUser;
NIMMessage * message = [[NIMMessage alloc] init];
message.text = self.inputMessage;
NSMutableDictionary *remoteExt = [NSMutableDictionary dictionaryWithObject:extModel.model2dictionary forKey:[AccountInfoStorage instance].getUid];
message.remoteExt = remoteExt;
///网易易盾 拦截高风险
NIMAntiSpamOption *option = [[NIMAntiSpamOption alloc]init];
option.yidunEnabled = YES;
option.businessId = KeyWithType(keyType_YiDunBussinessId);
message.antiSpamOption = option;
NSString * sessionId = [NSString stringWithFormat:@"%ld", [self.delegate getRoomInfo].roomId];
//构造会话
NIMSession *session = [NIMSession session:sessionId type:NIMSessionTypeChatroom];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session completion:^(NSError * _Nullable error) {
self.editTextFiled.text = @"";
[self.editTextFiled resignFirstResponder];
}];
}
- (void)keyboardWillShow:(NSNotification *)notification {
[self.superview bringSubviewToFront:self];
NSDictionary *info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat h = endKeyboardRect.size.height;
[UIView animateWithDuration:duration animations:^{
[self mas_remakeConstraints:^(MASConstraintMaker *make) {
make.left.right.equalTo(self.superview);
make.height.equalTo(@40);
make.bottom.equalTo(self.superview).offset(-h);
}];
}];
}
//键盘隐藏
- (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.left.right.equalTo(self.superview);
make.height.equalTo(@40);
}];
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
+ (void)showTextView:(UIView *)view delegate:(id<RoomHostDelegate>)delegate {
__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];
}
textView.hidden = NO;
[view addSubview:textView];
[textView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.mas_equalTo(view);
make.height.mas_equalTo(40);
}];
[textView.editTextFiled becomeFirstResponder];
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [ThemeColor mainTextColor];
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.editTextFiled];
[self.stackView addArrangedSubview:self.sendButton];
}
- (void)initSubViewConstraints {
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self).offset(8);
make.right.mas_equalTo(self).offset(-5);
make.top.bottom.mas_equalTo(self);
}];
[self.sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(50);
make.top.bottom.mas_equalTo(self).inset(5);
}];
}
- (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];
}
#pragma mark - Getters And Setters
- (UITextField *)editTextFiled{
if (!_editTextFiled) {
_editTextFiled = [[UITextField alloc] init];
_editTextFiled.placeholder = @"请输入消息...";
_editTextFiled.borderStyle = UITextBorderStyleNone;
[_editTextFiled setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
}
return _editTextFiled;
}
- (UIButton *)sendButton{
if (!_sendButton) {
_sendButton = [[UIButton alloc] init];
[_sendButton setTitle:@"发送" forState:UIControlStateNormal];
_sendButton.titleLabel.textColor = [ThemeColor mainTextColor];
_sendButton.titleLabel.font = [UIFont systemFontOfSize:15];
[_sendButton setBackgroundImage:[UIImage imageWithColor:[ThemeColor disableButtonColor] ]forState:UIControlStateDisabled];
[_sendButton setBackgroundImage:[UIImage gradientColorImageFromColors:@[[ThemeColor confirmButtonGradientStartColor], [ThemeColor confirmButtonGradientEndColor]] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(10, 10)] forState:UIControlStateNormal];
_sendButton.enabled = NO;
_sendButton.layer.cornerRadius = 5.0;
_sendButton.layer.masksToBounds = YES;
[_sendButton addTarget:self action:@selector(sendButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _sendButton;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisHorizontal;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentCenter;
_stackView.spacing = 10;
}
return _stackView;
}
@end