332 lines
12 KiB
Objective-C
332 lines
12 KiB
Objective-C
//
|
||
// LuckyPackageLogicManager.m
|
||
// YuMi
|
||
//
|
||
// Created by P on 2025/2/7.
|
||
//
|
||
|
||
#import "LuckyPackageLogicManager.h"
|
||
|
||
#import "GiftInfoModel.h"
|
||
#import "AttachmentModel.h"
|
||
#import "WalletInfoModel.h"
|
||
#import "LuckyPackagePresenter.h"
|
||
|
||
@interface LuckyPackageLogicManager()
|
||
|
||
@property(nonatomic, strong) WalletInfoModel *walletInfo;
|
||
@property(nonatomic, strong) RoomLuckyPackageInfoModel *infoModel;
|
||
|
||
// 礼物红包
|
||
@property(nonatomic, strong) NSMutableArray <GiftInfoModel *>*selectedGiftModels;
|
||
@property(nonatomic, strong) NSMutableArray *selectedGiftCounts;
|
||
@property(nonatomic, assign) NSInteger selectedWaitingTime_gift;
|
||
@property(nonatomic, strong) NSMutableAttributedString *totalCostString_Gift;
|
||
//@property(nonatomic, copy) void(^giftTotalCostBlock)(NSString *content);
|
||
|
||
@property(nonatomic, strong) NSMutableArray *giftTotalCostBlockArray;
|
||
|
||
// 金币红包
|
||
@property(nonatomic, assign) NSInteger selectedWaitingTime_coin;
|
||
@property(nonatomic, assign) NSInteger selectedBagMoney;
|
||
@property(nonatomic, assign) NSInteger selectedBagCount;
|
||
@property(nonatomic, strong) NSMutableAttributedString *totalCostString_Coin;
|
||
|
||
@property(nonatomic, assign) NSInteger packageNum;
|
||
|
||
@property(nonatomic, copy) void(^coinTotalCostBlock)(NSAttributedString *content);
|
||
@property(nonatomic, copy) void(^luckyPackageUpdate)(RoomLuckyPackageInfoModel *model);
|
||
@end
|
||
|
||
@implementation LuckyPackageLogicManager
|
||
|
||
+ (instancetype)sharedInstance {
|
||
static LuckyPackageLogicManager *instance = nil;
|
||
static dispatch_once_t onceToken;
|
||
dispatch_once(&onceToken, ^{
|
||
instance = [[self alloc] init];
|
||
instance.selectedGiftCounts = @[].mutableCopy;
|
||
instance.selectedGiftModels = @[].mutableCopy;
|
||
instance.giftTotalCostBlockArray = @[].mutableCopy;
|
||
});
|
||
|
||
return instance;
|
||
}
|
||
|
||
- (void)requestRoomLuckyPackageAPI:(NSInteger)roomUid success:(void(^)(RoomLuckyPackageInfoModel *model))success{
|
||
[self reset];
|
||
LuckyPackagePresenter *presenter = [[LuckyPackagePresenter alloc] init];
|
||
@kWeakify(self);
|
||
[presenter loadRoomLuckyPackageInfo:roomUid success:^(RoomLuckyPackageInfoModel * _Nonnull model) {
|
||
@kStrongify(self);
|
||
NSArray *temp = [model.redEnvelopeListVoList sortedArrayUsingComparator:^NSComparisonResult(RedEnvelopeListVo * _Nonnull obj1, RedEnvelopeListVo * _Nonnull obj2) {
|
||
return [@(obj1.beginTime) compare:@(obj2.beginTime)];
|
||
}];
|
||
model.redEnvelopeListVoList = temp;
|
||
self.packageNum = model.redEnvelopeListVoList.count;
|
||
self.infoModel = model;
|
||
self.selectedWaitingTime_coin = [[model.redEnvelopeV2Config.timeItems xpSafeObjectAtIndex:0] integerValue];
|
||
self.selectedWaitingTime_gift = [[model.redEnvelopeV2Config.timeItems xpSafeObjectAtIndex:0] integerValue];
|
||
[self updateSelectedBag:[[model.redEnvelopeV2Config.goldItems xpSafeObjectAtIndex:0] integerValue]];
|
||
[self updateSelectedBagNum:[[model.redEnvelopeV2Config.numItems xpSafeObjectAtIndex:0] integerValue]];
|
||
if (success) {
|
||
success(model);
|
||
}
|
||
}];
|
||
}
|
||
|
||
- (void)receiveNewPostLuckyPackage:(AttachmentModel *)obj {
|
||
NSDictionary *data = obj.data;
|
||
if (data) {
|
||
RedEnvelopeListVo *vo = [RedEnvelopeListVo modelWithJSON:data];
|
||
if (vo.roomUid != self.roomUid.integerValue) {
|
||
return;
|
||
}
|
||
|
||
vo.avatar = [data objectForKey:@"sendUserAvatar"];
|
||
vo.nick = [data objectForKey:@"sendUserNick"];
|
||
NSMutableArray *temp = [self.infoModel.redEnvelopeListVoList mutableCopy];
|
||
[temp addObject:vo];
|
||
self.infoModel.redEnvelopeListVoList = [temp sortedArrayUsingComparator:^NSComparisonResult(RedEnvelopeListVo * _Nonnull obj1, RedEnvelopeListVo * _Nonnull obj2) {
|
||
return [@(obj1.beginTime) compare:@(obj2.beginTime)];
|
||
}];
|
||
self.packageNum = self.infoModel.redEnvelopeListVoList.count;
|
||
if (_luckyPackageUpdate) {
|
||
self.luckyPackageUpdate(self.infoModel);
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)removeReceivedLuckyPackage {
|
||
NSMutableArray *array = [self.infoModel.redEnvelopeListVoList mutableCopy];
|
||
if (array.count>0) {
|
||
[array xpSafeRemoveObjectAtIndex:0];
|
||
}
|
||
self.infoModel.redEnvelopeListVoList = array.copy;
|
||
self.packageNum = self.infoModel.redEnvelopeListVoList.count;
|
||
if (_luckyPackageUpdate) {
|
||
self.luckyPackageUpdate(self.infoModel);
|
||
}
|
||
}
|
||
|
||
- (void)registerLuckyPackageUpdate:(void(^)(RoomLuckyPackageInfoModel *model))block {
|
||
_luckyPackageUpdate = block;
|
||
}
|
||
|
||
- (void)requestOpenLockyPackageAPI:(NSInteger)packageID success:(void(^)(OpenRedEnvelopeVo *model))success failure:(void(^)(NSError *error))failure {
|
||
LuckyPackagePresenter *presenter = [[LuckyPackagePresenter alloc] init];
|
||
[presenter open:packageID success:success failure:failure];
|
||
}
|
||
|
||
- (void)requestLuckyPackageReceiversAPI:(NSInteger)packageID success:(void(^)(NSArray <RedEnvelopeReceiveVo *>*array))success failure:(void(^)(NSError *error))failure {
|
||
LuckyPackagePresenter *presenter = [[LuckyPackagePresenter alloc] init];
|
||
[presenter loadReceiversOfLuckyPackage:packageID success:success failure:failure];
|
||
}
|
||
|
||
- (void)saveWalletInfo:(WalletInfoModel *)info {
|
||
self.walletInfo = info;
|
||
}
|
||
|
||
- (WalletInfoModel *)loadSavedWalletInfo {
|
||
return self.walletInfo;
|
||
}
|
||
|
||
- (void)reset {
|
||
LuckyPackagePresenter *presenter = [[LuckyPackagePresenter alloc] init];
|
||
[presenter resetGiftsCount:[self roomUid]];
|
||
[self.selectedGiftCounts removeAllObjects];
|
||
[self.selectedGiftModels removeAllObjects];
|
||
[self.giftTotalCostBlockArray removeAllObjects];
|
||
self.selectedWaitingTime_gift = [[self.infoModel.redEnvelopeV2Config.timeItems xpSafeObjectAtIndex:0] integerValue];
|
||
[self updateGiftTotalCostString];
|
||
|
||
self.selectedWaitingTime_coin = [[self.infoModel.redEnvelopeV2Config.timeItems xpSafeObjectAtIndex:0] integerValue];
|
||
[self updateSelectedBag:[[self.infoModel.redEnvelopeV2Config.goldItems xpSafeObjectAtIndex:0] integerValue]];
|
||
[self updateSelectedBagNum:[[self.infoModel.redEnvelopeV2Config.numItems xpSafeObjectAtIndex:0] integerValue]];
|
||
}
|
||
|
||
#pragma mark - 礼物红包
|
||
|
||
|
||
#pragma mark - 金币红包
|
||
|
||
- (void)updateSelectedGift:(GiftInfoModel *)gift
|
||
count:(NSInteger)count {
|
||
NSUInteger index = [self.selectedGiftModels indexOfObjectPassingTest:^BOOL(GiftInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||
return [obj isEqual:gift];
|
||
}];
|
||
|
||
if (count == 0 && index != NSNotFound) {
|
||
[self.selectedGiftModels xpSafeRemoveObjectAtIndex:index];
|
||
[self.selectedGiftCounts xpSafeRemoveObjectAtIndex:index];
|
||
} else {
|
||
if (index != NSNotFound) {
|
||
[self.selectedGiftCounts replaceObjectAtIndex:index withObject:@(count)];
|
||
} else {
|
||
[self.selectedGiftModels addObject:gift];
|
||
[self.selectedGiftCounts addObject:@(count)];
|
||
}
|
||
}
|
||
|
||
[self updateGiftTotalCostString];
|
||
}
|
||
|
||
- (void)updateSelectedGiftWaitingTime:(NSInteger)time {
|
||
self.selectedWaitingTime_gift = time;
|
||
[self updateGiftTotalCostString];
|
||
}
|
||
|
||
- (void)registerGiftTotalCostString:(void(^)(NSAttributedString *content))block key:(nonnull NSString *)key{
|
||
// 检查是否已经存在该 key
|
||
for (NSDictionary *blockDict in self.giftTotalCostBlockArray) {
|
||
if (blockDict.allKeys.firstObject && [blockDict.allKeys.firstObject isEqualToString:key]) {
|
||
// 如果 key 已经存在,则不添加,直接返回
|
||
return;
|
||
}
|
||
}
|
||
|
||
[self.giftTotalCostBlockArray addObject:@{key: block}];
|
||
}
|
||
|
||
- (void)removeGiftTotalCostRegister:(NSString *)key {
|
||
// 查找并移除对应 key 的 block
|
||
for (NSDictionary *blockDict in self.giftTotalCostBlockArray) {
|
||
if (blockDict.allKeys.firstObject && [blockDict.allKeys.firstObject isEqualToString:key]) {
|
||
[self.giftTotalCostBlockArray removeObject:blockDict];
|
||
break; // 找到并移除后退出循环
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)registerCoinTotalCostString:(void(^)(NSAttributedString *content))block {
|
||
_coinTotalCostBlock = block;
|
||
}
|
||
|
||
- (void)updateGiftTotalCostString {
|
||
NSNumber *count = [self.selectedGiftCounts valueForKeyPath:@"@sum.self"];
|
||
__block double price = 0;
|
||
[self.selectedGiftModels enumerateObjectsUsingBlock:^(GiftInfoModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||
NSNumber *giftCount = [self.selectedGiftCounts xpSafeObjectAtIndex:idx];
|
||
double giftPrice = [obj goldPrice];
|
||
price += giftCount.integerValue * giftPrice;
|
||
}];
|
||
|
||
NSString *string = [NSString stringWithFormat:YMLocalizedString(@"1.0.37_text_11"), count, @(price)];
|
||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string
|
||
attributes:@{
|
||
NSFontAttributeName : kFontRegular(12),
|
||
NSForegroundColorAttributeName : UIColorFromRGB(0xffea5c)
|
||
}];
|
||
NSRange countRange = [string rangeOfString:count.stringValue];
|
||
NSRange priceRange = [string rangeOfString:@(price).stringValue options:NSBackwardsSearch]; // 从后向前查找 price,避免匹配到 count
|
||
|
||
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:countRange];
|
||
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:priceRange];
|
||
|
||
self.totalCostString_Gift = attributedString;
|
||
for (NSDictionary *dic in self.giftTotalCostBlockArray) {
|
||
void(^block)(NSAttributedString *) = dic.allValues.firstObject;
|
||
// 执行 block,并传入所需的参数
|
||
if (block) {
|
||
block(self.totalCostString_Gift);
|
||
}
|
||
}
|
||
}
|
||
|
||
- (void)updateSelectedBag:(NSInteger)money {
|
||
self.selectedBagMoney = money;
|
||
[self updateCoinTotalCostString];
|
||
}
|
||
|
||
- (void)updateSelectedBagNum:(NSInteger)num {
|
||
self.selectedBagCount = num;
|
||
[self updateCoinTotalCostString];
|
||
}
|
||
|
||
- (void)updateSelectedBagWaitingTime:(NSInteger)time {
|
||
self.selectedWaitingTime_coin = time;
|
||
}
|
||
|
||
- (void)updateCoinTotalCostString {
|
||
NSInteger totalCoast = self.selectedBagMoney;
|
||
NSString *string = [NSString stringWithFormat:YMLocalizedString(@"1.0.37_text_9"), @(totalCoast)];
|
||
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string
|
||
attributes:@{
|
||
NSFontAttributeName : kFontRegular(12),
|
||
NSForegroundColorAttributeName : UIColorFromRGB(0xffea5c)
|
||
}];
|
||
NSRange countRange = [string rangeOfString:@(totalCoast).stringValue];
|
||
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:countRange];
|
||
self.totalCostString_Coin = attributedString;
|
||
if (_coinTotalCostBlock) {
|
||
self.coinTotalCostBlock(self.totalCostString_Coin.copy);
|
||
}
|
||
}
|
||
|
||
- (NSAttributedString *)loadGiftTotalCostString {
|
||
return self.totalCostString_Gift.copy;
|
||
}
|
||
|
||
- (NSAttributedString *)loadCoinTotalCostString {
|
||
return self.totalCostString_Coin.copy;
|
||
}
|
||
|
||
- (NSInteger)loadGiftWaitingTime {
|
||
return self.selectedWaitingTime_gift;
|
||
}
|
||
|
||
- (NSInteger)loadCoinWaitingTime {
|
||
return self.selectedWaitingTime_coin;
|
||
}
|
||
|
||
- (NSInteger)loadSelectedBagMoney {
|
||
return self.selectedBagMoney;
|
||
}
|
||
|
||
- (NSInteger)loadSelectedBagNum {
|
||
return self.selectedBagCount;
|
||
}
|
||
|
||
- (NSArray <GiftInfoModel *> *)loadAllSelectedGifts {
|
||
return self.selectedGiftModels;
|
||
}
|
||
|
||
- (NSInteger)numberOfGifts {
|
||
return self.selectedGiftModels.count;
|
||
}
|
||
|
||
- (GiftInfoModel *)giftForIndex:(NSInteger)index {
|
||
return [self.selectedGiftModels xpSafeObjectAtIndex:index];
|
||
}
|
||
|
||
- (NSInteger)countOfGiftForIndex:(NSInteger)index {
|
||
return [[self.selectedGiftCounts xpSafeObjectAtIndex:index] integerValue];
|
||
}
|
||
|
||
- (NSArray <NSNumber *> *)loadConfigWaitingTimes {
|
||
return self.infoModel.redEnvelopeV2Config.timeItems;
|
||
}
|
||
|
||
- (NSArray <NSNumber *> *)loadConfigWaitingTimesMins {
|
||
NSMutableArray *arr = @[].mutableCopy;
|
||
for (NSNumber *time in self.infoModel.redEnvelopeV2Config.timeItems) {
|
||
[arr addObject:@(time.integerValue/60)];
|
||
}
|
||
return arr.copy;
|
||
}
|
||
|
||
- (NSArray <NSNumber *> *)loadConfigLuckyBagNumbers {
|
||
return self.infoModel.redEnvelopeV2Config.numItems;
|
||
}
|
||
|
||
- (NSArray <NSNumber *> *)loadConfigLuckyBagMoneys {
|
||
return self.infoModel.redEnvelopeV2Config.goldItems;
|
||
}
|
||
|
||
- (NSInteger)packageBadgeNum {
|
||
return self.packageNum;
|
||
}
|
||
|
||
|
||
@end
|