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

357 lines
13 KiB
Mathematica
Raw Normal View History

2021-11-10 18:42:27 +08:00
//
// XPSendGiftView.m
// xplan-ios
//
// Created by on 2021/11/9.
//
#import "XPSendGiftView.h"
///Third
#import <Masonry/Masonry.h>
2021-11-16 19:56:37 +08:00
#import <NIMSDK/NIMSDK.h>
2021-11-10 18:42:27 +08:00
///Tool
#import "XPMacro.h"
#import "XPGiftPresenter.h"
2021-11-15 18:59:44 +08:00
#import "TTPopup.h"
2021-11-10 18:42:27 +08:00
#import "ThemeColor+SendGift.h"
2021-11-16 16:31:24 +08:00
#import "XCHUDTool.h"
2021-11-16 19:56:37 +08:00
#import "IMMessageHelper.h"
2021-11-16 16:31:24 +08:00
///Model
#import "GiftInfoModel.h"
#import "XPGiftCountModel.h"
#import "GiftReceiveInfoModel.h"
2021-11-16 19:56:37 +08:00
#import "AttachmentModel.h"
#import "RoomInfoModel.h"
2021-11-10 18:42:27 +08:00
///View
#import "XPGiftUsersView.h"
#import "XPGiftInfoView.h"
#import "XPGiftBarView.h"
///P
#import "XPGiftProtocol.h"
2021-11-15 18:59:44 +08:00
///VC
#import "XPMineRechargeViewController.h"
#import "XPWebViewController.h"
2021-11-10 18:42:27 +08:00
2021-11-16 16:31:24 +08:00
@interface XPSendGiftView ()< XPGiftBarViewDelegate, XPGiftProtocol, XPGiftInfoViewDelegate>
2021-11-10 18:42:27 +08:00
///
@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;
2021-11-16 16:31:24 +08:00
///使
@property (nonatomic,assign) SendGiftType usingplaceType;
2021-11-10 18:42:27 +08:00
@end
@implementation XPSendGiftView
2021-11-15 18:59:44 +08:00
- (void)dealloc {
NSLog(@"六面板销毁了");
}
2021-11-10 18:42:27 +08:00
2021-11-17 19:29:14 +08:00
- (instancetype)initWithType:(SendGiftType)type uid:(NSString *)uid{
2021-11-10 18:42:27 +08:00
if (self = [super init]) {
self.roomUid = uid;
2021-11-16 16:31:24 +08:00
self.usingplaceType = type;
2021-11-10 18:42:27 +08:00
[self initSubViews];
[self initSubViewConstraints];
[self initHttpRequest];
2021-11-10 18:42:27 +08:00
}
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];
}
2021-11-16 19:56:37 +08:00
#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];
2021-11-17 19:29:14 +08:00
UserInfoModel * userInfo = [self.delegate getUserInfo];
2021-11-16 19:56:37 +08:00
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;
2021-11-17 19:29:14 +08:00
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
2021-11-16 19:56:37 +08:00
}];
}
}else { //
attachement.first = CustomMessageType_ALLMicroSend;
attachement.second = Custom_Message_Sub_AllMicroSend;
[data setObject:[dict valueForKeyPath:@"targetUsers.uid"] forKey:@"targetUids"];
attachement.data = data;
2021-11-17 19:29:14 +08:00
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
2021-11-16 19:56:37 +08:00
}
} 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;
2021-11-17 19:29:14 +08:00
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
2021-11-16 19:56:37 +08:00
}];
}
}else { //
attachement.first = CustomMessageType_ALLMicroSend;
attachement.second = Custom_Message_Sub_AllBatchSend;
attachement.data = data;
2021-11-17 19:29:14 +08:00
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
2021-11-16 19:56:37 +08:00
}
} 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;
2021-11-17 19:29:14 +08:00
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
2021-11-16 19:56:37 +08:00
}];
}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;
2021-11-17 19:29:14 +08:00
[IMMessageHelper sendCustomMessage:attachement sessionId:sessionID type:NIMSessionTypeChatroom userInfo:userInfo];
2021-11-16 19:56:37 +08:00
}
}
}
2021-11-16 16:31:24 +08:00
#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;
}
2021-11-15 18:59:44 +08:00
#pragma mark - Public Method
2021-11-17 19:29:14 +08:00
- (void)configGiftUsers:(NSArray<XPGiftUserInfoModel *> *)users {
[self.userView configGiftUsers:users];
}
#pragma mark - XPGiftBarViewDelegate
- (void)xPGiftBarView:(XPGiftBarView *)view didClickSendGift:(XPGiftCountModel *)giftCount {
2021-11-16 16:31:24 +08:00
///
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;
}
}
}
///
2021-11-15 18:59:44 +08:00
- (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];
}
2021-11-16 16:31:24 +08:00
- (void)xPGiftInfoView:(XPGiftInfoView *)view didClickSegment:(GiftSegmentType)type {
self.giftBarView.type = type;
}
#pragma mark - XPGiftProtocol
- (void)getUserWalletInfo:(WalletInfoModel *)balanceInfo {
self.giftBarView.walletInfoModel = balanceInfo;
}
2021-11-15 18:59:44 +08:00
///
- (void)getPacketGiftListSuccess:(NSArray<GiftInfoModel *> *)giftList {
2021-11-12 19:30:53 +08:00
self.giftInfoView.packOriginArray = giftList;
}
2021-11-15 18:59:44 +08:00
/// /
- (void)getNormalGiftListSuccess:(NSArray<GiftInfoModel *> *)giftList {
2021-11-12 19:30:53 +08:00
self.giftInfoView.normalOriginArray = giftList;
2021-11-10 18:42:27 +08:00
}
2021-11-16 16:31:24 +08:00
2021-11-16 19:56:37 +08:00
- (void)sendGiftSuccess:(GiftReceiveInfoModel *)receiveInfo originDic:(NSDictionary *)originDic {
2021-11-16 16:31:24 +08:00
if (receiveInfo.sourceType == GiftSourceType_Pack) {
///
[self.giftInfoView updatePackSource:receiveInfo];
} else {
[self.giftPresenter getUserWallInfo];
}
2021-11-16 19:56:37 +08:00
[self sendCustomMessage:receiveInfo oringinDic:originDic];
2021-11-16 16:31:24 +08:00
}
2021-11-10 18:42:27 +08:00
#pragma mark - Getters And Setters
- (XPGiftUsersView *)userView {
if (!_userView) {
_userView = [[XPGiftUsersView alloc] initWithType:SendGiftType_Room];
}
return _userView;
}
- (XPGiftInfoView *)giftInfoView {
if (!_giftInfoView) {
_giftInfoView = [[XPGiftInfoView alloc] init];
2021-11-15 18:59:44 +08:00
_giftInfoView.delegate = self;
2021-11-10 18:42:27 +08:00
}
return _giftInfoView;
}
- (XPGiftBarView *)giftBarView {
if (!_giftBarView) {
_giftBarView = [[XPGiftBarView alloc] init];
_giftBarView.delegate = self;
2021-11-10 18:42:27 +08:00
}
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;
}
2021-11-10 18:42:27 +08:00
@end