Files
yinmeng-ios/xplan-ios/Main/Room/View/QuickMessageView/XPRoomQuickMessageContainView.m
2022-10-31 17:31:59 +08:00

186 lines
7.1 KiB
Objective-C

//
// XPRoomQuickMessageContainView.m
// xplan-ios
//
// Created by GreenLand on 2022/9/28.
//
#import "XPRoomQuickMessageContainView.h"
#import "XPRoomQuidkMessageCell.h"
#import <Masonry/Masonry.h>
#import <NIMSDK/NIMSDK.h>
///Tool
#import "AccountInfoStorage.h"
#import "XPConstant.h"
#import "XCHUDTool.h"
///Model
#import "XPMessageRemoteExtModel.h"
#import "UserInfoModel.h"
#import "RoomInfoModel.h"
@interface XPRoomQuickMessageContainView ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *titleArray;
///关闭
@property (nonatomic,strong) UIButton *closeButton;
///代理
@property (nonatomic,weak) id<RoomHostDelegate> delegate;
///最后发送消息的时间
@property (nonatomic, assign) long long lastSendTime;
@end
@implementation XPRoomQuickMessageContainView
- (instancetype)initWithDelegate:(id<RoomHostDelegate>)delegate {
self = [super init];
if (self) {
self.delegate = delegate;
self.clipsToBounds = YES;
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.collectionView];
[self addSubview:self.closeButton];
}
- (void)initSubViewConstraints {
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(self).offset(15);
make.right.mas_equalTo(self.closeButton.mas_left).offset(-8);
make.top.bottom.mas_equalTo(self);
}];
[self.closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-8);
make.width.height.mas_equalTo(30);
make.centerY.mas_equalTo(self);
}];
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.titleArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
XPRoomQuidkMessageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([XPRoomQuidkMessageCell class]) forIndexPath:indexPath];
cell.title = self.titleArray[indexPath.row];
return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = self.titleArray[indexPath.row];
NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:12]};
CGSize size = [str boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, 25) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
return CGSizeMake(size.width + 10, 30);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
NSDate *datenow = [NSDate date];//现在时间
long time2 = (long)([datenow timeIntervalSince1970]*1000);
long aTime = (time2 - self.lastSendTime) / 1000;
if (aTime < 10) {
[XCHUDTool showErrorWithMessage:@"你发送的太频繁啦,请稍后再试"];
return;
}
self.lastSendTime = time2;
NSString *str = self.titleArray[indexPath.row];
[self sendText:str];
}
- (void)sendText:(NSString *)text {
if ([self.delegate getRoomInfo].isCloseScreen) {
return;
}
//移除记录关闭次数
[[NSUserDefaults standardUserDefaults] removeObjectForKey:kRoomQuickMessageCloseCount];
[[NSUserDefaults standardUserDefaults] synchronize];
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.charmUrl = userInfo.userLevelVo.charmUrl;
extModel.experLevelSeq = userInfo.userLevelVo.experLevelSeq;
extModel.experUrl = userInfo.userLevelVo.experUrl;
extModel.newUser = userInfo.newUser;
extModel.vipIcon = userInfo.userVipInfoVO.vipIcon;
extModel.androidBubbleUrl = userInfo.androidBubbleUrl;
extModel.iosBubbleUrl = userInfo.iosBubbleUrl;
extModel.fromSayHelloChannel = userInfo.fromSayHelloChannel;
NIMMessage * message = [[NIMMessage alloc] init];
message.text = text;
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) {
}];
}
- (void)onCloseButtonClick:(UIButton *)sender {
[self mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0);
}];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//记录关闭的次数
NSNumber *count = [defaults objectForKey:kRoomQuickMessageCloseCount];
[defaults setObject:@(count.intValue +1) forKey:kRoomQuickMessageCloseCount];
[defaults synchronize];
}
#pragma mark - Getters And Setters
- (UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
layout.minimumInteritemSpacing = 8;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.showsHorizontalScrollIndicator = NO;
_collectionView.backgroundColor = [UIColor clearColor];
_collectionView.dataSource = self;
_collectionView.delegate = self;
[_collectionView registerClass:[XPRoomQuidkMessageCell class] forCellWithReuseIdentifier:NSStringFromClass([XPRoomQuidkMessageCell class])];
}
return _collectionView;
}
- (NSMutableArray *)titleArray {
if (!_titleArray) {
_titleArray = [NSMutableArray arrayWithObjects:@"萌新求关注", @"ღ( ´・ᴗ・` )比心", @"贴贴", @"这个怎么玩呀?", @"我要上麦", @"么么叽!么么哒", @"老板大气", @"下次一定!", nil];
}
return _titleArray;
}
- (UIButton *)closeButton {
if (!_closeButton) {
_closeButton = [[UIButton alloc] init];
[_closeButton setImage:[UIImage imageNamed:@"room_gift_graffiti_close"] forState:UIControlStateNormal];
[_closeButton addTarget:self action:@selector(onCloseButtonClick:) forControlEvents:UIControlEventTouchUpInside];
}
return _closeButton;
}
@end