1
This commit is contained in:
@@ -6,6 +6,9 @@
|
||||
#import "KBJfPayCell.h"
|
||||
#import "FGIAPProductsFilter.h"
|
||||
#import "FGIAPManager.h"
|
||||
#import "PayVM.h"
|
||||
#import "KBPayProductModel.h"
|
||||
#import "KBBizCode.h"
|
||||
static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
|
||||
@interface KBJfPay () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
|
||||
@@ -32,10 +35,11 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
@property (nonatomic, strong) UIButton *agreementButton;
|
||||
|
||||
// 数据
|
||||
@property (nonatomic, strong) NSArray<NSDictionary *> *data; // 简单演示数据:@{coins, price}
|
||||
@property (nonatomic, strong) NSArray<KBPayProductModel *> *data; // In-App 商品展示数据
|
||||
@property (nonatomic, assign) NSInteger selectedIndex; // 当前选中项
|
||||
|
||||
@property (nonatomic, strong) FGIAPProductsFilter *filter;
|
||||
@property (nonatomic, strong) PayVM *payVM;
|
||||
|
||||
@end
|
||||
|
||||
@@ -44,6 +48,9 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
self.filter = [[FGIAPProductsFilter alloc] init];
|
||||
self.payVM = [PayVM new];
|
||||
self.data = @[];
|
||||
self.selectedIndex = NSNotFound;
|
||||
self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my_bg_icon"]];
|
||||
self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
|
||||
self.kb_navView.backgroundColor = [UIColor clearColor];
|
||||
@@ -53,17 +60,6 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
}];
|
||||
self.kb_titleLabel.text = KBLocalized(@"Points Recharge");
|
||||
|
||||
// 默认数据(演示)
|
||||
self.data = @[
|
||||
@{ @"coins": @690, @"price": @"$6.90",@"product_id" : @"100_coin" },
|
||||
@{ @"coins": @1280, @"price": @"$12.90" ,@"product_id" : @"vip_a_week" },
|
||||
@{ @"coins": @3290, @"price": @"$32.90" ,@"product_id" : @"100_coin" },
|
||||
@{ @"coins": @4990, @"price": @"$49.90" ,@"product_id" : @"100_coin" },
|
||||
@{ @"coins": @9990, @"price": @"$99.90" ,@"product_id" : @"100_coin" },
|
||||
@{ @"coins": @19990,@"price": @"$199.90" ,@"product_id" : @"100_coin" },
|
||||
];
|
||||
self.selectedIndex = 0;
|
||||
|
||||
// 视图组装
|
||||
[self.view addSubview:self.myPointsTitleLabel];
|
||||
[self.view addSubview:self.pointsLabel];
|
||||
@@ -139,34 +135,19 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
// 刷新
|
||||
[self.collectionView reloadData];
|
||||
|
||||
// 确保首次进入就出现选中态外边框与阴影
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
NSIndexPath *ip = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];
|
||||
// 让系统层面也处于选中态,便于 setSelected 同步 UI
|
||||
if (ip) {
|
||||
[self.collectionView selectItemAtIndexPath:ip animated:NO scrollPosition:UICollectionViewScrollPositionNone];
|
||||
}
|
||||
KBJfPayCell *cell = (KBJfPayCell *)[self.collectionView cellForItemAtIndexPath:ip];
|
||||
if (cell) { [cell applySelected:YES animated:NO]; }
|
||||
});
|
||||
[self fetchInAppProductList];
|
||||
}
|
||||
|
||||
- (void)viewDidAppear:(BOOL)animated {
|
||||
[super viewDidAppear:animated];
|
||||
// 再兜底一次(某些布局时机下,首屏 reload 后 cell 还未可见)
|
||||
NSIndexPath *ip = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];
|
||||
if (ip) {
|
||||
[self.collectionView selectItemAtIndexPath:ip animated:NO scrollPosition:UICollectionViewScrollPositionNone];
|
||||
}
|
||||
KBJfPayCell *cell = (KBJfPayCell *)[self.collectionView cellForItemAtIndexPath:ip];
|
||||
if (cell) { [cell applySelected:YES animated:NO]; }
|
||||
[self selectItemAtCurrentIndexAnimated:NO];
|
||||
}
|
||||
|
||||
#pragma mark - UICollectionView Delegate (ensure first show)
|
||||
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
if (![cell isKindOfClass:KBJfPayCell.class]) { return; }
|
||||
KBJfPayCell *c = (KBJfPayCell *)cell;
|
||||
BOOL sel = (indexPath.item == self.selectedIndex);
|
||||
BOOL sel = (self.selectedIndex != NSNotFound && indexPath.item == self.selectedIndex);
|
||||
if (sel) {
|
||||
[collectionView selectItemAtIndexPath:indexPath animated:NO scrollPosition:UICollectionViewScrollPositionNone];
|
||||
}
|
||||
@@ -194,9 +175,9 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
|
||||
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
||||
KBJfPayCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kKBJfPayCellId forIndexPath:indexPath];
|
||||
NSDictionary *item = self.data[indexPath.item];
|
||||
NSString *coins = [NSString stringWithFormat:@"%@", item[@"coins"]];
|
||||
NSString *price = item[@"price"]; // 形如 "$6.90"
|
||||
KBPayProductModel *model = self.data[indexPath.item];
|
||||
NSString *coins = [model coinsDisplayText];
|
||||
NSString *price = [model priceDisplayText];
|
||||
[cell configCoins:coins price:price];
|
||||
[cell applySelected:(indexPath.item == self.selectedIndex) animated:NO];
|
||||
return cell;
|
||||
@@ -235,30 +216,89 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
|
||||
return 30;
|
||||
}
|
||||
|
||||
#pragma mark - Data
|
||||
- (void)fetchInAppProductList {
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[self.payVM fetchInAppProductsNeedShow:YES completion:^(NSInteger sta, NSString * _Nullable msg, NSArray<KBPayProductModel *> * _Nullable products) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
__strong typeof(weakSelf) self = weakSelf;
|
||||
if (!self) { return; }
|
||||
if (sta != KBBizCodeSuccess || ![products isKindOfClass:NSArray.class]) {
|
||||
self.data = @[];
|
||||
self.selectedIndex = NSNotFound;
|
||||
[self.collectionView reloadData];
|
||||
NSString *tip = msg.length ? msg : KBLocalized(@"Failed to load products");
|
||||
[KBHUD showInfo:tip];
|
||||
return;
|
||||
}
|
||||
self.data = products ?: @[];
|
||||
self.selectedIndex = self.data.count > 0 ? 0 : NSNotFound;
|
||||
[self.collectionView reloadData];
|
||||
[self selectItemAtCurrentIndexAnimated:NO];
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
- (KBPayProductModel *)currentSelectedProductItem {
|
||||
if (self.selectedIndex == NSNotFound) { return nil; }
|
||||
if (self.selectedIndex < 0 || self.selectedIndex >= self.data.count) { return nil; }
|
||||
id item = self.data[self.selectedIndex];
|
||||
if (![item isKindOfClass:KBPayProductModel.class]) { return nil; }
|
||||
return item;
|
||||
}
|
||||
|
||||
- (void)selectItemAtCurrentIndexAnimated:(BOOL)animated {
|
||||
if (self.selectedIndex == NSNotFound) { return; }
|
||||
if (self.selectedIndex < 0 || self.selectedIndex >= self.data.count) { return; }
|
||||
NSIndexPath *ip = [NSIndexPath indexPathForItem:self.selectedIndex inSection:0];
|
||||
if (!ip) { return; }
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
if (self.selectedIndex == NSNotFound) { return; }
|
||||
if ([self.collectionView numberOfItemsInSection:0] <= ip.item) { return; }
|
||||
[self.collectionView selectItemAtIndexPath:ip animated:animated scrollPosition:UICollectionViewScrollPositionNone];
|
||||
KBJfPayCell *cell = (KBJfPayCell *)[self.collectionView cellForItemAtIndexPath:ip];
|
||||
if ([cell isKindOfClass:KBJfPayCell.class]) {
|
||||
[cell applySelected:YES animated:animated];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
#pragma mark - Actions
|
||||
- (void)onTapPayButton {
|
||||
// 这里只做 UI,实际支付逻辑由调用方接入
|
||||
// if (self.selectedIndex >= 0 && self.selectedIndex < self.data.count) {
|
||||
// NSDictionary *item = self.data[self.selectedIndex];
|
||||
// NSString *msg = [NSString stringWithFormat:@"购买:%@ Coins %@", item[@"coins"], item[@"price"]];
|
||||
// [KBHUD showInfo:msg];
|
||||
// }
|
||||
NSString *productId = @"com.loveKey.nyx.1day";
|
||||
/// 2.获取商品信息
|
||||
KBPayProductModel *selectedItem = [self currentSelectedProductItem];
|
||||
if (!selectedItem) {
|
||||
[KBHUD showInfo:KBLocalized(@"Please select a product")];
|
||||
return;
|
||||
}
|
||||
NSString *productId = selectedItem.productId;
|
||||
if (productId.length == 0) {
|
||||
[KBHUD showInfo:KBLocalized(@"Product unavailable")];
|
||||
return;
|
||||
}
|
||||
[KBHUD show];
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[self.filter requestProductsWith:[NSSet setWithObject:productId] completion:^(NSArray<SKProduct *> * _Nonnull products) {
|
||||
// NSLog(@"=====");
|
||||
// if (products.count > 0) {
|
||||
// SKProduct *pro = productsp[0];
|
||||
// }
|
||||
// [[FGIAPManager shared].iap buyProduct:product onCompletion:^(NSString * _Nonnull message, FGIAPManagerPurchaseRusult result) { }];
|
||||
|
||||
/// 3.支付购买
|
||||
SKProduct *product = products.firstObject;
|
||||
[[FGIAPManager shared].iap buyProduct:products.firstObject onCompletion:^(NSString * _Nonnull message, FGIAPManagerPurchaseRusult result) {
|
||||
// [self.view makeToast:message];
|
||||
[KBHUD dismiss];
|
||||
}];
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
__strong typeof(weakSelf) self = weakSelf;
|
||||
if (!self) { return; }
|
||||
SKProduct *match = nil;
|
||||
for (SKProduct *product in products) {
|
||||
if ([product.productIdentifier isEqualToString:productId]) {
|
||||
match = product;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!match) {
|
||||
[KBHUD dismiss];
|
||||
[KBHUD showInfo:KBLocalized(@"Unable to load product information")];
|
||||
return;
|
||||
}
|
||||
[[FGIAPManager shared].iap buyProduct:match onCompletion:^(NSString * _Nonnull message, FGIAPManagerPurchaseRusult result) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[KBHUD dismiss];
|
||||
});
|
||||
}];
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user