Files
yinmeng-ios-store/yinmeng-ios/DingDangApp/CodeClass/Room/View/Micro/RoomUserMicroView.m
2024-01-05 17:46:44 +08:00

207 lines
7.9 KiB
Objective-C

//
// RoomUserMicroView.m
// DingDangApp
//
// Created by apple on 2023/5/27.
//
#import "RoomUserMicroView.h"
#import "RoomUserMicroCell.h"
#import "RoomMicroModel.h"
#import "DDRoomRequest.h"
#import <NIMSDK/NIMSDK.h>
#import "UserInfoModel.h"
@interface RoomUserMicroView ()<UICollectionViewDelegate,UICollectionViewDataSource,TRTCCloudDelegate>
@property (nonatomic,strong) NSMutableArray *dataSource;
@end
@implementation RoomUserMicroView
-(instancetype)init{
self = [super init];
if(self){
_engine = [TRTCCloud sharedInstance];
[_engine enableAudioVolumeEvaluation:900];
[TRTCCloud setConsoleEnabled:NO];
_engine.delegate = self;
}
return self;
}
- (void)initView{
[self collectionView];
}
-(void)setInfoModel:(RoomInfoModel *)infoModel{
_infoModel = infoModel;
[[NIMSDK sharedSDK].chatroomManager fetchChatroomQueue:[NSString stringWithFormat:@"%ld", (long)self.infoModel.roomId] completion:^(NSError * _Nullable error, NSArray<NSDictionary<NSString *,NSString *> *> * _Nullable info) {
if (error) return;
for (int i = 0 ; i < 8; i++) {
if(i < info.count){
NSDictionary *item = info[i];
UserInfoModel *userInfo = [UserInfoModel DD_ModelWithDict:item.allValues.firstObject];
RoomMicroModel *room = [RoomMicroModel new];
room.user_id = @(userInfo.uid).stringValue;
room.nickname = userInfo.nick;
room.avatar = userInfo.avatar;
room.open_mic = @(YES).stringValue;
room.mic_seat_valueString = @(arc4random() % 10000).stringValue;
[self.dataSource addObject:room];
}else{
RoomMicroModel *room = [RoomMicroModel new];
[self.dataSource addObject:room];
}
}
[self.collectionView reloadData];
}];
NSInteger roomId = infoModel.roomId > INT_MAX ? infoModel.uid : infoModel.roomId;
[self dd_setTRTC:[NSString stringWithFormat:@"%ld",roomId] sign:infoModel.trtcSig];
}
- (BOOL)dd_setTRTC:(NSString *)Id sign:(nonnull NSString *)sign {
[self.engine enableAudioVolumeEvaluation:900];
TRTCParams *params = [[TRTCParams alloc] init];
UInt32 appId;
NSString *curTtcKey = [[NSUserDefaults standardUserDefaults]valueForKey:@"kTrtcAppId"];
if(curTtcKey != nil && curTtcKey.length > 0){
sscanf([curTtcKey UTF8String], "%u", &appId);
}else{
sscanf([@"1400798783" UTF8String], "%u", &appId);
}
params.sdkAppId = appId;
UInt32 roomId;
sscanf([Id UTF8String], "%u", &roomId);
params.roomId = roomId;
params.userId = [[AccountInfoStorage instance] getUid];
params.userSig = sign;
params.role = TRTCRoleAudience;
[self.engine enterRoom:params appScene:TRTCAppSceneLIVE];
[self.engine muteAllRemoteAudio:NO];
[self.engine muteLocalAudio:NO];
return YES;
}
- (void)showMicRoEmojiMessageToUser:(NSString *)userID url:(NSString *)url{
for (RoomMicroModel *obj in self.dataSource) {
if ([obj.user_id isEqualToString:userID]) {
NSInteger index = [self.dataSource indexOfObject:obj];
NSLog(@"第 %li个 显示表情",(long)index);
RoomUserMicroCell * cell = (RoomUserMicroCell*)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
[cell.microView starAnimateEmojiImage:url];
break;
}
}
}
/**刷新麦////息*/
-(void)reloadMicroData:(NSMutableArray *)dataArray{
self.dataSource = [NSMutableArray arrayWithArray:dataArray];
[self.collectionView reloadData];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 8;
// return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
RoomUserMicroCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"RoomUserMicroCell" forIndexPath:indexPath];
if(indexPath.row < self.dataSource.count){
RoomMicroModel *infoModel = self.dataSource[indexPath.row];
cell.microView.microModel = infoModel;
}
// RoomMicroModel *model = self.dataSource[indexPath.row];
WeakSelf(ws);
cell.backHeaderTapBlock = ^{
if(cell.microView.microModel.user_id == nil){
RoomMicroModel *r;
int j = 0;
for (int i = 0; i < self.dataSource.count; i++) {
RoomMicroModel *obj = self.dataSource[i];
if([obj.user_id isEqualToString:@(self.userInfo.uid).stringValue] && indexPath.row != i){
r = obj;
j = i;
break;
}
}
if(r == nil){
UserInfoModel *userInfo = ws.userInfo;
RoomMicroModel *room = [RoomMicroModel new];
room.user_id = @(userInfo.uid).stringValue;
room.nickname = userInfo.nick;
room.avatar = userInfo.avatar;
room.open_mic = @(YES).stringValue;
room.mic_seat_valueString = @(arc4random() % 10000).stringValue;
[ws.dataSource replaceObjectAtIndex:indexPath.row withObject:room];
}else{
RoomMicroModel *rooms = [RoomMicroModel new];
[ws.dataSource replaceObjectAtIndex:j withObject:rooms];
UserInfoModel *userInfo = ws.userInfo;
RoomMicroModel *room = [RoomMicroModel new];
room.user_id = @(userInfo.uid).stringValue;
room.nickname = userInfo.nick;
room.avatar = userInfo.avatar;
room.open_mic = @(YES).stringValue;
room.mic_seat_valueString = @(arc4random() % 10000).stringValue;
[ws.dataSource replaceObjectAtIndex:indexPath.row withObject:room];
}
[ws.collectionView reloadData];
}
};
// cell.backIncomeTapBlock = ^{
// NSLog(@"11");
// };
return cell;
}
- (void)starAnimationToIndex:(NSInteger)index{
RoomUserMicroCell * cell = (RoomUserMicroCell*)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index inSection:0]];
[cell.microView.headeImageView starMicroAnimation];
}
- (WLBaseCollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(kWidth/4, 107);
layout.sectionInset = UIEdgeInsetsMake(0, 0,0, 0);
layout.minimumLineSpacing = 5;
layout.minimumInteritemSpacing = 0;
layout.headerReferenceSize = CGSizeZero;
_collectionView = [[WLBaseCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
[_collectionView registerClass:[RoomUserMicroCell class] forCellWithReuseIdentifier:@"RoomUserMicroCell"];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = Kclear_color;
if ([_collectionView respondsToSelector:@selector(setLayoutMargins:)]){
[_collectionView setLayoutMargins:UIEdgeInsetsZero];
}
_collectionView.translatesAutoresizingMaskIntoConstraints = NO;
if (@available(iOS 11.0, *)) {
[_collectionView adjustedContentInsetDidChange];
_collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
}
[self addSubview:_collectionView];
[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self).with.insets(UIEdgeInsetsMake(0, 0, 0, 0));
}];
}
return _collectionView;
}
- (NSMutableArray*)dataSource{
if (!_dataSource) {
_dataSource = [[NSMutableArray alloc] init];
}
return _dataSource;
}
@end