Files
keyboard/keyBoard/Class/Me/VC/KBConsumptionRecordVC.m
2025-12-22 20:53:24 +08:00

338 lines
12 KiB
Objective-C

//
// KBConsumptionRecordVC.m
// keyBoard
//
#import "KBConsumptionRecordVC.h"
#import "KBConsumptionRecord.h"
#import "KBConsumptionRecordCell.h"
#import "KBMyVM.h"
#import "KBShopVM.h"
#import "KBJfPay.h"
#import <Masonry/Masonry.h>
#import "UIColor+Extension.h"
#import "KBHUD.h"
#import <MJRefresh/MJRefresh.h>
@interface KBConsumptionRecordVC () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIView *cardView;
@property (nonatomic, strong) UILabel *pointsTitleLabel;
@property (nonatomic, strong) UILabel *pointsLabel;
@property (nonatomic, strong) UIImageView *pointsIconView;
@property (nonatomic, strong) UIButton *rechargeButton;
@property (nonatomic, strong) UIImageView *sectionIconView;
@property (nonatomic, strong) UILabel *sectionTitleLabel;
@property (nonatomic, strong) NSMutableArray<KBConsumptionRecord *> *records;
@property (nonatomic, strong) KBMyVM *viewModel;
@property (nonatomic, strong) KBShopVM *shopVM;
@property (nonatomic, strong) UIImageView *bgImageView; // 全屏背景图
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger pageSize;
@property (nonatomic, assign) BOOL isLoading;
@end
@implementation KBConsumptionRecordVC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.kb_titleLabel.text = KBLocalized(@"Consumption Record");
self.kb_navView.backgroundColor = [UIColor clearColor];
self.bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"my_bg_icon"]];
self.bgImageView.contentMode = UIViewContentModeScaleAspectFill;
[self.view insertSubview:self.bgImageView belowSubview:self.kb_navView];
[self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.view);
}];
self.records = [NSMutableArray array];
[self.view addSubview:self.tableView];
[self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.bottom.equalTo(self.view);
make.top.equalTo(self.view).offset(KB_NAV_TOTAL_HEIGHT);
}];
self.tableView.tableHeaderView = self.headerView;
self.pageNumber = 1;
self.pageSize = 10;
[self setupRefresh];
[self fetchWalletBalance];
[self.tableView.mj_header beginRefreshing];
}
#pragma mark - Data
- (void)fetchWalletBalance {
__weak typeof(self) weakSelf = self;
[self.shopVM fetchWalletBalanceWithCompletion:^(NSString * _Nullable balance, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
if (!error && balance.length > 0) {
weakSelf.pointsLabel.text = balance;
}
});
}];
}
- (void)setupRefresh {
__weak typeof(self) weakSelf = self;
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf refreshRecords];
}];
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
[weakSelf loadMoreRecords];
}];
self.tableView.mj_footer.hidden = YES;
}
- (void)refreshRecords {
if (self.isLoading) {
[self.tableView.mj_header endRefreshing];
return;
}
self.pageNumber = 1;
[self.tableView.mj_footer resetNoMoreData];
[self fetchPurchaseRecordsIsRefresh:YES];
}
- (void)loadMoreRecords {
if (self.isLoading || self.tableView.mj_footer.state == MJRefreshStateNoMoreData) {
[self.tableView.mj_footer endRefreshing];
return;
}
self.pageNumber += 1;
[self fetchPurchaseRecordsIsRefresh:NO];
}
- (void)fetchPurchaseRecordsIsRefresh:(BOOL)isRefresh {
self.isLoading = YES;
BOOL showHUD = !self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing;
if (showHUD) {
[KBHUD show];
}
__weak typeof(self) weakSelf = self;
[self.viewModel fetchWalletTransactionsWithPage:self.pageNumber
pageSize:self.pageSize
completion:^(NSArray<KBConsumptionRecord *> * _Nullable records, NSError * _Nullable error) {
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.isLoading = NO;
if (showHUD) {
[KBHUD dismiss];
}
if (isRefresh) {
[weakSelf.tableView.mj_header endRefreshing];
} else {
[weakSelf.tableView.mj_footer endRefreshing];
}
if (error) {
if (!isRefresh) {
weakSelf.pageNumber = MAX(1, weakSelf.pageNumber - 1);
}
NSString *msg = error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
return;
}
if (isRefresh) {
[weakSelf.records removeAllObjects];
}
if (records.count > 0) {
[weakSelf.records addObjectsFromArray:records];
}
[weakSelf.tableView reloadData];
weakSelf.tableView.mj_footer.hidden = (weakSelf.records.count == 0);
if (records.count < weakSelf.pageSize) {
[weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
}
});
}];
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.records.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return KBFit(78);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
KBConsumptionRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:[KBConsumptionRecordCell reuseId]
forIndexPath:indexPath];
if (indexPath.row < self.records.count) {
[cell configWithRecord:self.records[indexPath.row]];
}
return cell;
}
#pragma mark - Actions
- (void)onRecharge {
KBJfPay *vc = [[KBJfPay alloc] init];
[self.navigationController pushViewController:vc animated:YES];
}
#pragma mark - Lazy
- (BaseTableView *)tableView {
if (!_tableView) {
_tableView = [[BaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_tableView.backgroundColor = [UIColor clearColor];
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.dataSource = self;
_tableView.delegate = self;
[_tableView registerClass:KBConsumptionRecordCell.class forCellReuseIdentifier:[KBConsumptionRecordCell reuseId]];
}
return _tableView;
}
- (UIView *)headerView {
if (!_headerView) {
CGFloat headerHeight = KBFit(210);
_headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KB_SCREEN_WIDTH, headerHeight)];
_headerView.backgroundColor = [UIColor clearColor];
[_headerView addSubview:self.cardView];
[_headerView addSubview:self.sectionIconView];
[_headerView addSubview:self.sectionTitleLabel];
[self.cardView addSubview:self.pointsTitleLabel];
[self.cardView addSubview:self.pointsIconView];
[self.cardView addSubview:self.pointsLabel];
[self.cardView addSubview:self.rechargeButton];
[self.cardView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_headerView).offset(16);
make.right.equalTo(_headerView).offset(-16);
make.top.equalTo(_headerView).offset(24);
make.height.mas_equalTo(KBFit(126));
}];
[self.pointsTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.cardView).offset(16);
make.top.equalTo(self.cardView).offset(18);
}];
[self.pointsIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.cardView).offset(16);
make.centerY.equalTo(self.cardView);
make.width.height.mas_equalTo(38);
}];
[self.pointsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.pointsIconView.mas_right).offset(8);
make.centerY.equalTo(self.pointsIconView).offset(0);
}];
[self.rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.equalTo(self.cardView).offset(-10);
make.centerY.equalTo(self.pointsIconView);
make.width.mas_equalTo(114);
make.height.mas_equalTo(42);
}];
[self.sectionIconView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(_headerView).offset(16);
make.top.equalTo(self.cardView.mas_bottom).offset(18);
make.width.height.mas_equalTo(24);
}];
[self.sectionTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.sectionIconView);
make.left.equalTo(self.sectionIconView.mas_right).offset(8);
}];
}
return _headerView;
}
- (UIView *)cardView {
if (!_cardView) {
_cardView = [UIView new];
_cardView.backgroundColor = [UIColor colorWithHex:0xC5FFF6];
_cardView.layer.cornerRadius = 20;
_cardView.layer.masksToBounds = YES;
}
return _cardView;
}
- (UILabel *)pointsTitleLabel {
if (!_pointsTitleLabel) {
_pointsTitleLabel = [UILabel new];
_pointsTitleLabel.text = KBLocalized(@"My Points");
_pointsTitleLabel.font = [KBFont medium:14];
_pointsTitleLabel.textColor = [UIColor colorWithHex:KBBlackValue];
}
return _pointsTitleLabel;
}
- (UILabel *)pointsLabel {
if (!_pointsLabel) {
_pointsLabel = [UILabel new];
_pointsLabel.text = @"0";
_pointsLabel.font = [KBFont bold:40];
_pointsLabel.textColor = [UIColor colorWithHex:0x02BEAC];
}
return _pointsLabel;
}
- (UIImageView *)pointsIconView {
if (!_pointsIconView) {
_pointsIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shop_jb_icon"]];
_pointsIconView.contentMode = UIViewContentModeScaleAspectFit;
}
return _pointsIconView;
}
- (UIButton *)rechargeButton {
if (!_rechargeButton) {
_rechargeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_rechargeButton setTitle:KBLocalized(@"Recharge") forState:UIControlStateNormal];
_rechargeButton.titleLabel.font = [KBFont medium:13];
[_rechargeButton setTitleColor:[UIColor colorWithHex:0x1B1F1A] forState:UIControlStateNormal];
// _rechargeButton.backgroundColor = [UIColor colorWithHex:0xCFF7EA];
[_rechargeButton setBackgroundImage:[UIImage imageNamed:@"my_chongzhi_bg"] forState:UIControlStateNormal];
_rechargeButton.layer.cornerRadius = 21;
_rechargeButton.layer.masksToBounds = YES;
[_rechargeButton addTarget:self action:@selector(onRecharge) forControlEvents:UIControlEventTouchUpInside];
}
return _rechargeButton;
}
- (UIImageView *)sectionIconView {
if (!_sectionIconView) {
_sectionIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"shop_jb_icon"]];
_sectionIconView.contentMode = UIViewContentModeScaleAspectFit;
}
return _sectionIconView;
}
- (UILabel *)sectionTitleLabel {
if (!_sectionTitleLabel) {
_sectionTitleLabel = [UILabel new];
_sectionTitleLabel.text = KBLocalized(@"Consumption Details");
_sectionTitleLabel.font = [KBFont medium:14];
_sectionTitleLabel.textColor = [UIColor colorWithHex:KBBlackValue];
}
return _sectionTitleLabel;
}
- (KBMyVM *)viewModel {
if (!_viewModel) {
_viewModel = [[KBMyVM alloc] init];
}
return _viewModel;
}
- (KBShopVM *)shopVM {
if (!_shopVM) {
_shopVM = [[KBShopVM alloc] init];
}
return _shopVM;
}
@end