// // YMInvitationBreakdownListViewModel.m // MSYOUPAI // // Created by YoMi on 2024/3/15. // Copyright © 2024 MS. All rights reserved. // #import "YMInvitationBreakdownListViewModel.h" #import "YMInvitingPersonsModel.h" #import "YMInvitingEarningsModel.h" @interface YMInvitationBreakdownListViewModel () /// 当前数据数量 @property (nonatomic, assign) NSInteger currentSize; /// 邀请明细分类类型 @property (nonatomic, assign, readwrite) YMInvitationBreakdownCategoryType categoryType; /// 列表数据 @property (nonatomic, strong, readwrite) NSMutableArray *listDataArray; /// 请求Api链接 @property (nonatomic, copy) NSString *requestApiUrl; @end @implementation YMInvitationBreakdownListViewModel - (void)ym_initialize{ [super ym_initialize]; self.currentPage = 1; self.currentSize = 10; self.categoryType = [self.params integerValueForKey:ParamsCategoryType defaultValue:YMInvitationBreakdownCategoryTypeInvitingPersons]; switch (self.categoryType) { case YMInvitationBreakdownCategoryTypeInvitingPersons: { self.requestApiUrl = InvitingPersonsList; } break; case YMInvitationBreakdownCategoryTypeInvitingEarnings: { self.requestApiUrl = InvitingEarningsList; } break; default: break; } } - (void)getInvitationBreakdownListData { @weakify(self) [ZCHUDHelper showWithStatus:@"加载中..."]; [LCHttpHelper requestWithURLString:self.requestApiUrl parameters:@{ @"page":@(self.currentPage), @"limit":@(self.currentSize) } needToken:YES type:HttpRequestTypePost success:^(id responseObject) { @strongify(self) NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) { [ZCHUDHelper dismiss]; NSDictionary *data = [dict dictionaryValueForKey:@"data" defaultValue:@{}]; NSArray *result = [data arrayValueForKey:@"list" defaultValue:@[]]; if (self.currentPage == 1) { [self.listDataArray removeAllObjects]; } NSArray *viewModelArr = @[]; switch (self.categoryType) { case YMInvitationBreakdownCategoryTypeInvitingPersons: { viewModelArr = [[NSArray yy_modelArrayWithClass:[YMInvitingPersonsModel class] json:result].rac_sequence map:^(YMInvitingPersonsModel * _Nullable model) { YMInvitationBreakdownListCellViewModel *viewModel = [[YMInvitationBreakdownListCellViewModel alloc]initWithParams:@{ ParamsModel:model, }]; return viewModel; }].array; } break; case YMInvitationBreakdownCategoryTypeInvitingEarnings: { viewModelArr = [[NSArray yy_modelArrayWithClass:[YMInvitingEarningsModel class] json:result].rac_sequence map:^(YMInvitingEarningsModel * _Nullable model) { YMInvitationBreakdownListCellViewModel *viewModel = [[YMInvitationBreakdownListCellViewModel alloc]initWithParams:@{ ParamsModel:model, }]; return viewModel; }].array; } break; default: break; } [self.listDataArray addObjectsFromArray:viewModelArr]; if(self.currentPage > 0 && self.currentPage % 5 == 0){ [[SDImageCache sharedImageCache] clearMemory]; } [self.refreshUISubject sendNext:@(result.count < self.currentSize ? YMFooterRefresh_HasNoMoreData : YMFooterRefresh_HasMoreData)]; }else{ [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.refreshUISubject sendNext:@(YMRefreshError)]; }); } } failure:^(NSError *error) { if(self.currentPage != 1){ self.currentPage--; } [ZCHUDHelper showTitle:error.localizedDescription]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self.refreshUISubject sendNext:@(YMRefreshError)]; }); }]; } - (NSMutableArray *)listDataArray{ if (!_listDataArray) { _listDataArray = [NSMutableArray array]; } return _listDataArray; } @end