// // YMDynamicDetailViewModel.m // MSYOUPAI // // Created by YoMi on 2024/3/21. // Copyright © 2024 MS. All rights reserved. // #import "YMDynamicDetailViewModel.h" #import "YMDynamicDetailModel.h" #import "YMDynamicCommentModel.h" @interface YMDynamicDetailViewModel () /// 当前数据数量 @property (nonatomic, assign) NSInteger currentSize; /// 动态Id @property (nonatomic, assign) NSInteger dynamicId; /// 动态详情信息数据 @property (nonatomic, strong, readwrite) NSArray *infoDataArray; /// 动态详情评论数据 @property (nonatomic, strong, readwrite) NSMutableArray *commentDataArray; @end @implementation YMDynamicDetailViewModel - (void)ym_initialize{ [super ym_initialize]; self.customNavTitle = @"动态详情"; self.currentPage = 1; self.currentSize = 10; self.dynamicId = [self.params intValueForKey:ParamsId default:0]; } - (void)getDynamicDetailData{ @weakify(self) [ZCHUDHelper showWithStatus:@"加载中..."]; [LCHttpHelper requestWithURLString:DynamicDetail parameters:@{ @"dynamic_id":@(self.dynamicId), } needToken:YES type:HttpRequestTypePost success:^(id responseObject) { @strongify(self) [ZCHUDHelper dismiss]; NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) { YMDynamicDetailModel *model = [YMDynamicDetailModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]]; YMDynamicDetailInfoCellViewModel *viewModel = [[YMDynamicDetailInfoCellViewModel alloc]initWithParams:@{ ParamsModel:model }]; self.infoDataArray = @[viewModel]; [self.refreshUISubject sendNext:@(YMRefreshUI)]; }else{ [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]]; } } failure:^(NSError *error) { [ZCHUDHelper showTitle:error.localizedDescription]; }]; } - (void)getDynamicDetailCommentListData{ @weakify(self) [ZCHUDHelper showWithStatus:@"加载中..."]; [LCHttpHelper requestWithURLString:DynamicDetailCommentList parameters:@{ @"dynamic_id":@(self.dynamicId), @"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.commentDataArray removeAllObjects]; } NSArray *viewModelArr = [[NSArray yy_modelArrayWithClass:[YMDynamicCommentModel class] json:result].rac_sequence map:^(YMDynamicCommentModel * _Nullable model) { YMDynamicDetailCommentCellViewModel *viewModel = [[YMDynamicDetailCommentCellViewModel alloc]initWithParams:@{ ParamsModel:model }]; return viewModel; }].array; [self.commentDataArray 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 *)commentDataArray{ if (!_commentDataArray) { _commentDataArray = [NSMutableArray array]; } return _commentDataArray; } @end