Files
yinmeng-ios/xplan-ios/Main/ModuleKit/SendGiftView/View/XPSendGiftView.m

357 lines
13 KiB
Objective-C

//
// XPSendGiftView.m
// xplan-ios
//
// Created by 冯硕 on 2021/11/9.
//
#import "XPSendGiftView.h"
///Third
#import <Masonry/Masonry.h>
#import <NIMSDK/NIMSDK.h>
///Tool
#import "XPMacro.h"
#import "XPGiftPresenter.h"
#import "TTPopup.h"
#import "ThemeColor+SendGift.h"
#import "XCHUDTool.h"
#import "IMMessageHelper.h"
///Model
#import "GiftInfoModel.h"
#import "XPGiftCountModel.h"
#import "GiftReceiveInfoModel.h"
#import "AttachmentModel.h"
#import "RoomInfoModel.h"
///View
#import "XPGiftUsersView.h"
#import "XPGiftInfoView.h"
#import "XPGiftBarView.h"
///P
#import "XPGiftProtocol.h"
///VC
#import "XPMineRechargeViewController.h"
#import "XPWebViewController.h"
@interface XPSendGiftView ()< XPGiftBarViewDelegate, XPGiftProtocol, XPGiftInfoViewDelegate>
///
@property (nonatomic,strong) UIStackView *stackView;
///送礼物的人
@property (nonatomic,strong) XPGiftUsersView *userView;
///送的什么礼物
@property (nonatomic,strong) XPGiftInfoView *giftInfoView;
///送多少礼物
@property (nonatomic,strong) XPGiftBarView *giftBarView;
///
@property (nonatomic,strong) XPGiftPresenter *giftPresenter;
///房主的uid
@property (nonatomic,copy) NSString *roomUid;
///使用的地方
@property (nonatomic,assign) SendGiftType usingplaceType;
@end
@implementation XPSendGiftView
- (void)dealloc {
NSLog(@"六面板销毁了");
}
- (instancetype)initWithType:(SendGiftType)type uid:(NSString *)uid{
if (self = [super init]) {
self.roomUid = uid;
self.usingplaceType = type;
[self initSubViews];
[self initSubViewConstraints];
[self initHttpRequest];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
self.backgroundColor = [ThemeColor giftBackGroundColor];
[self addSubview:self.stackView];
[self.stackView addArrangedSubview:self.userView];
[self.stackView addArrangedSubview:self.giftInfoView];
[self.stackView addArrangedSubview:self.giftBarView];
}
- (void)initSubViewConstraints {
[self mas_makeConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(KScreenWidth);
make.bottom.mas_equalTo(self.stackView.mas_bottom);
}];
[self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.mas_equalTo(self);
}];
}
- (void)initHttpRequest {
[self.giftPresenter getUserWallInfo];
[self.giftPresenter getNormalGiftList:self.roomUid];
[self.giftPresenter getPackGiftList];
}
#pragma mark - Send Custom Message
- (void)sendCustomMessage:(GiftReceiveInfoModel *)receiveModel oringinDic:(NSDictionary *)originDic {
AttachmentModel *attachement = [[AttachmentModel alloc] init];
NSDictionary * dict = originDic;
NSString *sessionID = [NSString stringWithFormat:@"%ld", [self.delegate getRoomInfo].roomId];
UserInfoModel * userInfo = [self.delegate getUserInfo];
if (receiveModel.roomSendGiftType == RoomSendGiftType_AllMic) { // 全麦
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data addEntriesFromDictionary:dict];
if (receiveModel.gift.giftType == GiftType_Lucky) { // 如果是福袋 需要分开发送消息
attachement.first = CustomMessageType_ALLMicroSend;
attachement.second = Custom_Message_Sub_AllMicroLuckySend;
NSArray *luckyBagGifts = (NSArray *)[data objectForKey:@"luckyBagGifts"];
if (luckyBagGifts.count >0) {
[luckyBagGifts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[data setObject:idx == 0 ? @(YES):@(NO) forKey:@"isShowAnimation"];
[data setObject:obj forKey:@"luckyGiftList"];
[data setObject:receiveModel.gift.giftName forKey:@"giftName"];
[data setObject:[obj valueForKeyPath:@"user.uid"] forKey:@"targetUid"];
[data setObject:[dict valueForKeyPath:@"targetUsers.uid"] forKey:@"targetUids"];
[data removeObjectForKey:@"luckyBagGifts"];
[data removeObjectForKey:@"targetUsers"];
attachement.data = data;
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
}];
}
}else { // 全麦赠送普通礼物 包含背包礼物
attachement.first = CustomMessageType_ALLMicroSend;
attachement.second = Custom_Message_Sub_AllMicroSend;
[data setObject:[dict valueForKeyPath:@"targetUsers.uid"] forKey:@"targetUids"];
attachement.data = data;
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
}
} else if (receiveModel.roomSendGiftType == RoomSendGiftType_MutableOnMic) { // 多人非全麦
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data addEntriesFromDictionary:dict];
if (receiveModel.gift.giftType == GiftType_Lucky) { // 多人非全麦 福袋礼物
attachement.first = CustomMessageType_ALLMicroSend;
attachement.second = Custom_Message_Sub_AllBatchMicroLuckySend;
NSArray *luckyBagGifts = (NSArray *)[data objectForKey:@"luckyBagGifts"];
if (luckyBagGifts.count >0) {
// 产品想一个消息 多次发送 只能遍历插入消息体
[luckyBagGifts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[data setObject:idx == 0 ? @(YES):@(NO) forKey:@"isShowAnimation"];
[data setObject:obj forKey:@"luckyGiftList"];
[data setObject:receiveModel.gift.giftName forKey:@"giftName"];
[data setObject:[obj valueForKeyPath:@"user.uid"] forKey:@"targetUid"];
[data setObject:[dict valueForKeyPath:@"targetUsers.uid"] forKey:@"targetUids"];
[data removeObjectForKey:@"luckyBagGifts"];
[data removeObjectForKey:@"targetUsers"];
attachement.data = data;
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
}];
}
}else { // 多人非全麦 普通礼物
attachement.first = CustomMessageType_ALLMicroSend;
attachement.second = Custom_Message_Sub_AllBatchSend;
attachement.data = data;
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
}
} else if (receiveModel.roomSendGiftType == RoomSendGiftType_ToOne) { // 单人
NSMutableDictionary *data = [NSMutableDictionary dictionary];
[data addEntriesFromDictionary:dict];
if (receiveModel.gift.giftType == GiftType_Lucky) { // 一对一 福袋礼物
attachement.first = CustomMessageType_Gift;
attachement.second = Custom_Message_Sub_Gift_LuckySend;
NSArray *luckyBagGifts = (NSArray *)[data objectForKey:@"luckyBagGifts"];
[luckyBagGifts enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[data setObject:idx == 0 ? @(YES):@(NO) forKey:@"isShowAnimation"];
[data setObject:obj forKey:@"luckyGiftList"];
[data setObject:receiveModel.gift.giftName forKey:@"giftName"];
[data setObject:[obj valueForKeyPath:@"user.uid"] forKey:@"targetUid"];
[data setObject:[obj valueForKeyPath:@"user.nick"] forKey:@"targetNick"];
[data setObject:[dict valueForKeyPath:@"targetUsers.uid"] forKey:@"targetUids"];
[data removeObjectForKey:@"luckyBagGifts"];
[data removeObjectForKey:@"targetUsers"];
attachement.data = data;
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
}];
}else { // 一对一 普通礼物
attachement.first = CustomMessageType_Gift;
attachement.second = Custom_Message_Sub_Gift_Send;
NSDictionary *targetUsers = ((NSArray *)[data objectForKey:@"targetUsers"]).firstObject;
[data setObject:[targetUsers valueForKeyPath:@"uid"] forKey:@"targetUid"];
[data setObject:[targetUsers valueForKeyPath:@"nick"] forKey:@"targetNick"];
[data setObject:[targetUsers valueForKeyPath:@"avatar"] forKey:@"targetAvatar"];
attachement.data = data;
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
}
}
}
#pragma mark - Deal Send Gift Data
- (NSString *)dealSendGiftCount:(XPGiftCountModel *)model gift:(GiftInfoModel *)gift {
NSString * count = @"1";
if (model.isTotal) {
count = [NSString stringWithFormat:@"%ld", gift.count];
} else {
count = model.giftNumber;
}
return count;
}
- (NSString *)dealSendGiftUids:(NSArray<NSString *> *)uids {
NSString *uidString = [[NSString alloc] init];
for (NSString *item in uids) {
if (uidString.length > 0) {
uidString = [uidString stringByAppendingString:@","];
}
uidString = [uidString stringByAppendingString:item];
}
return uidString;
}
- (RoomSendGiftType)dealRoomSendGiftType:(GiftInfoModel *)giftInfo giftCount:(XPGiftCountModel *)giftCount {
NSArray * uids = self.userView.selectUserArray;
RoomSendGiftType roomSendGiftType;
if (self.userView.isSelectAll) {
if (giftInfo.sourceType == GiftSourceType_Pack && giftCount.isTotal && giftInfo && uids.count > 1) {
[XCHUDTool showErrorWithMessage:@"只能全部赠送给一人"];
return -1;
}
roomSendGiftType = RoomSendGiftType_AllMic;
} else if(self.userView.selectUserArray.count > 1) {
if (giftInfo.sourceType == GiftSourceType_Pack && giftCount.isTotal) {
[XCHUDTool showErrorWithMessage:@"只能全部赠送给一人"];
return -1;
}
roomSendGiftType = RoomSendGiftType_MutableOnMic;
} else if(self.userView.selectUserArray.count == 1) {
roomSendGiftType = RoomSendGiftType_ToOne;
} else {
roomSendGiftType = -1;
}
return roomSendGiftType;
}
#pragma mark - Public Method
- (void)configGiftUsers:(NSArray<XPGiftUserInfoModel *> *)users {
[self.userView configGiftUsers:users];
}
#pragma mark - XPGiftBarViewDelegate
- (void)xPGiftBarView:(XPGiftBarView *)view didClickSendGift:(XPGiftCountModel *)giftCount {
///总礼物 三要素 送礼物的人 送的什么礼物 送多少个礼物
NSArray * uids = self.userView.selectUserArray;
GiftInfoModel * giftInfo = self.giftInfoView.lastSelectGift;
if (self.usingplaceType == SendGiftType_Room) {
if (uids.count > 0) {
///送礼物的人
NSString * uidString = [self dealSendGiftUids:uids];
///送礼物的个数
NSString * giftNumber = [self dealSendGiftCount:giftCount gift:giftInfo];
///送的什么礼物
NSString * giftId = [NSString stringWithFormat:@"%ld", giftInfo.giftId];
///给麦上的人送礼物
GiftSendType sendType = GiftSendType_OnMic;
///礼物来源
GiftSourceType sourceType = giftInfo.sourceType;
///送一个人 还是全麦 还是多人非全麦
RoomSendGiftType roomSendType = [self dealRoomSendGiftType:giftInfo giftCount:giftCount];
if (roomSendType == -1) {
return;
}
///发送消息
[self.giftPresenter sendGift:uidString giftNum:giftNumber sendType:sendType giftId:giftId giftSource:sourceType giftType:giftInfo.giftType roomSendType:roomSendType roomUid:self.roomUid msg:@""];
} else {
[XCHUDTool showErrorWithMessage:@"请选择至少一个人"];
return;
}
}
}
///充值
- (void)xPGiftBarViewDidClickRecharge:(XPGiftBarView *)view {
[TTPopup dismiss];
XPMineRechargeViewController * rechargeVC = [[XPMineRechargeViewController alloc] init];
[self.delegate.getCurrentNav pushViewController:rechargeVC animated:YES];
}
#pragma mark - XPGiftInfoViewDelegate
- (void)xPGiftInfoView:(XPGiftInfoView *)view didClickPlayRule:(NSString *)ruleUrl {
[TTPopup dismiss];
XPWebViewController * webVC = [[XPWebViewController alloc] init];
webVC.url = ruleUrl;
[self.delegate.getCurrentNav pushViewController:webVC animated:YES];
}
- (void)xPGiftInfoView:(XPGiftInfoView *)view didClickSegment:(GiftSegmentType)type {
self.giftBarView.type = type;
}
#pragma mark - XPGiftProtocol
- (void)getUserWalletInfo:(WalletInfoModel *)balanceInfo {
self.giftBarView.walletInfoModel = balanceInfo;
}
///背包礼物
- (void)getPacketGiftListSuccess:(NSArray<GiftInfoModel *> *)giftList {
self.giftInfoView.packOriginArray = giftList;
}
///普通礼物 /幸运礼物
- (void)getNormalGiftListSuccess:(NSArray<GiftInfoModel *> *)giftList {
self.giftInfoView.normalOriginArray = giftList;
}
- (void)sendGiftSuccess:(GiftReceiveInfoModel *)receiveInfo originDic:(NSDictionary *)originDic {
if (receiveInfo.sourceType == GiftSourceType_Pack) {
///更新个数
[self.giftInfoView updatePackSource:receiveInfo];
} else {
[self.giftPresenter getUserWallInfo];
}
[self sendCustomMessage:receiveInfo oringinDic:originDic];
}
#pragma mark - Getters And Setters
- (XPGiftUsersView *)userView {
if (!_userView) {
_userView = [[XPGiftUsersView alloc] init];
}
return _userView;
}
- (XPGiftInfoView *)giftInfoView {
if (!_giftInfoView) {
_giftInfoView = [[XPGiftInfoView alloc] init];
_giftInfoView.delegate = self;
}
return _giftInfoView;
}
- (XPGiftBarView *)giftBarView {
if (!_giftBarView) {
_giftBarView = [[XPGiftBarView alloc] init];
_giftBarView.delegate = self;
}
return _giftBarView;
}
- (UIStackView *)stackView {
if (!_stackView) {
_stackView = [[UIStackView alloc] init];
_stackView.axis = UILayoutConstraintAxisVertical;
_stackView.distribution = UIStackViewDistributionFill;
_stackView.alignment = UIStackViewAlignmentFill;
_stackView.spacing = 0;
_stackView.backgroundColor = [UIColor clearColor];
}
return _stackView;
}
- (XPGiftPresenter *)giftPresenter {
if (!_giftPresenter) {
_giftPresenter = [[XPGiftPresenter alloc] init];
[_giftPresenter attatchView:self];
}
return _giftPresenter;
}
@end