Files
mier_ios/SweetParty/主类/音悦新增/炼仙传说/WLStakeRecordAlert.m
2025-08-11 10:43:19 +08:00

325 lines
9.7 KiB
Objective-C

//
// WLStakeRecordAlert.m
// romantic
//
// Created by Xmac on 2024/4/22.
// Copyright © 2024 romantic. All rights reserved.
//
#import "WLStakeRecordAlert.h"
#import "WLStakeRecordCell.h"
#import "WLStakeRecordModel.h"
@interface WLStakeRecordAlert ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource>
@property (nonatomic, strong) UIView *mainView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *models; //礼物数组
//关于网络请求
@property (nonatomic, assign) BOOL isLoadFirst;
@property (nonatomic, assign) BOOL isOnRequesting, isNoMoreData, isNetError;
@property (nonatomic, strong) XiamuFooter *footerView;
@property (nonatomic, strong) XiamuTipView *tipsView;
@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, assign) CGFloat mainW;
@property (nonatomic, assign) CGFloat mainH;
@property (nonatomic, assign) CGFloat contentX;
@property (nonatomic, assign) CGFloat contentTop;
@property (nonatomic, assign) CGFloat contentBottom;
@property (nonatomic, copy) NSString *bgImgName;
@end
@implementation WLStakeRecordAlert
#pragma mark - set
- (NSMutableArray *)models {
if (!_models) {
_models = NSMutableArray.array;
}
return _models;
}
- (XiamuFooter *)footerView {
if (!_footerView) {
_footerView = [[XiamuFooter alloc] initWithFrame:CGRectMake(0, 0, _collectionView.width, 40)];
}
return _footerView;
}
#pragma mark -
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:UIScreen.mainScreen.bounds]) {
self.backgroundColor = [kBlackColor colorWithAlphaComponent:0.3];
[ControlCreator createButton:self rect:self.bounds text:@"" font:nil color:nil backguoundColor:UIColor.clearColor imageName:nil target:self action:@selector(backAction:)];
}
return self;
}
- (void)initMainView {
if (_mainView) {
return;
}
UIColor *color;
color = HEXCOLOR(0xFFFFFF);
_mainView = [[UIView alloc] initWithFrame:CGRectMake((APPW-_mainW)/2, APPH-_mainH, _mainW, _mainH)];
// _mainView = [[UIView alloc] initWithFrame:CGRectMake((APPW-_mainW)/2, (APPH-_mainH)/2, _mainW, _mainH)];
_mainView.backgroundColor = UIColor.clearColor;
[self addSubview:_mainView];
[ControlCreator createImageView:_mainView rect:_mainView.bounds imageName:self.bgImgName backguoundColor:nil];
// UIImageView *closeImgV = [ControlCreator createImageView:nil rect:CGRectMake((APPW-30)/2, _mainView.bottom+15, 30, 30) imageName:@"bj_box_close" backguoundColor:nil];
// [self addSubview:closeImgV];
[ControlCreator createButton:_mainView rect:CGRectMake(_mainW-80, 0, 80, 40) text:nil font:nil color:nil backguoundColor:nil imageName:nil target:self action:@selector(backAction:)];
}
- (void)initCollecionView {
if (_collectionView) {
return;
}
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake((_mainView.width-_contentX*2), 52);
layout.minimumInteritemSpacing = 0;
layout.minimumLineSpacing = 5;
layout.sectionInset = UIEdgeInsetsMake(0, 14, 20, 14);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, _contentTop, _mainView.width, _mainView.height-_contentTop-_contentBottom) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = UIColor.clearColor;
_collectionView.showsVerticalScrollIndicator = NO;
NSString *cellId = NSStringFromClass(WLStakeRecordCell.class);
[_collectionView registerNib:[UINib nibWithNibName:cellId bundle:nil] forCellWithReuseIdentifier:cellId];
[_collectionView registerClass:UICollectionReusableView.class forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
[_mainView addSubview:_collectionView];
}
- (void)initMJrefresh {
__weak typeof(self) weakSelf = self;
MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
[weakSelf loadFirst];
}];
_collectionView.mj_header = header;
}
- (void)initTipView {
if (_tipsView) {
return;
}
_tipsView = [[XiamuTipView alloc]initWithFrame:CGRectMake(0, 0, _collectionView.width, 250)];
[self.collectionView addSubview:_tipsView];
__weak typeof(self) weakSelf = self;
_tipsView.block = ^{
if (weakSelf.isNetError) {
[weakSelf loadFirst];
}
};
}
#pragma mark -
- (void)showOnView:(UIView *)parent {
CGFloat x = 0;
CGFloat w = ScreenWidth - x*2;
CGFloat h = w/375*580;
self.mainW = w;
self.mainH = h;
self.contentX = 30;
self.contentTop = 115;
self.contentBottom = 50;
self.bgImgName = @"stake_bg_record";
[self initMainView];
[self initCollecionView];
[self initMJrefresh];
// [self initTipView];
[parent addSubview:self];
[parent addSubview:self];
[_collectionView.mj_header beginRefreshing];
}
#pragma mark - action
- (IBAction)backAction:(id)sender {
[self removeFromSuperview];
}
#pragma mark - data
- (void)setIsOnRequesting:(BOOL)isOnRequesting {
_isOnRequesting = isOnRequesting;
if (isOnRequesting) {
_isNetError = NO;
[self.footerView stateLoading];
return;
}
if (_isNoMoreData) {
[self.footerView stateNoMore];
if (self.models.count==0) {
[self.tipsView stateNoResult];
[self.footerView stateHide];
}
}
if (_isNetError) {
[self.footerView stateNetError];
if (_isLoadFirst) {
[self.tipsView stateNoNet];
[self.footerView stateHide];
[self.models removeAllObjects];
[_collectionView reloadData];
}
}
}
- (void)loadFirst {
if (_isOnRequesting) {
return;
}
_pageIndex = 1;
_isLoadFirst = YES;
self.isNoMoreData = NO;
[self loadListData];
}
- (void)loadMore {
if (_isOnRequesting) {
return;
}
if (_isNoMoreData) {
return;
}
_isLoadFirst = NO;
[self loadListData];
}
- (void)loadListData {
[self.tipsView stateHide];
self.isNetError = NO;
__weak typeof(self) weakSelf = self;
self.isOnRequesting = YES;
NSDictionary *dict = @{@"page":@(self.pageIndex).stringValue,
@"page_limit":@"30",
};
NSMutableDictionary *paras = [NSMutableDictionary dictionary];
[paras addEntriesFromDictionary:dict];
NSString *urlStr = @"/api/monster/get_monster_log";
// [SVProgressHUD showWithStatus:nil];
[RCMicHTTP postWithURLString:urlStr parameters:paras response:^(RCMicHTTPResult *result) {
[SVProgressHUD dismiss];
if (result.success) {
if (result.errorCode == 200 && [result.content isKindOfClass:NSArray.class]) {
[self checkRequestResult:result.content];
}else {
[SVProgressHUD showInfoWithStatus:result.message];
self.isNetError = YES;
}
}else {
[SVProgressHUD showInfoWithStatus:@"网络错误"];
self.isNetError = YES;
}
weakSelf.isOnRequesting = NO;
[weakSelf.collectionView.mj_header endRefreshing];
}];
}
- (void)checkRequestResult:(NSArray *)listArr {
if ([listArr isKindOfClass:NSArray.class]) {
if (_isLoadFirst) {
[self.models removeAllObjects];
}
if (listArr.count == 0 ) {
self.isNoMoreData = YES;
}
NSArray *models = [WLStakeRecordModel mj_objectArrayWithKeyValuesArray:listArr];
[self.models addObjectsFromArray:models];
[_collectionView reloadData];
_pageIndex ++;
}
}
#pragma mark ====================== 代理方法 ======================
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
return CGSizeMake(APPW, 40);
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
UICollectionReusableView *foot = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot" forIndexPath:indexPath];
NSInteger footTag = 1001;
UIView *subview = [foot viewWithTag:footTag];
if (subview) {
[subview removeFromSuperview];
}
self.footerView.tag = footTag;
[foot addSubview:self.footerView];
return foot;
}
return nil;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.models.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
NSString *cellId = NSStringFromClass(WLStakeRecordCell.class);
WLStakeRecordCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
WLStakeRecordModel *model = self.models[indexPath.item];
cell.model = model;
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.item+6>self.models.count && !self.isNoMoreData) {
[self loadMore];
}
}
@end