YMPersonalDynamicViewModel.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // YMPersonalDynamicViewModel.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/3.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMPersonalDynamicViewModel.h"
  9. #import "YMDynamicListModel.h"
  10. @interface YMPersonalDynamicViewModel ()
  11. /// 当前数据数量
  12. @property (nonatomic, assign) NSInteger currentSize;
  13. /// 列表数据
  14. @property (nonatomic, strong, readwrite) NSMutableArray <YMPersonalDynamicCellViewModel*>*listDataArray;
  15. /// 用户Id
  16. @property (nonatomic, assign) NSInteger userId;
  17. @end
  18. @implementation YMPersonalDynamicViewModel
  19. - (void)ym_initialize{
  20. [super ym_initialize];
  21. self.customNavTitle = @"ta的动态";
  22. self.currentPage = 1;
  23. self.currentSize = 10;
  24. self.userId = [self.params integerValueForKey:ParamsId defaultValue:0];
  25. }
  26. - (void)getPersonalDynamicListData {
  27. @weakify(self)
  28. [ZCHUDHelper showWithStatus:@"加载中..."];
  29. [LCHttpHelper requestWithURLString:DynamicUser parameters:@{
  30. @"page":@(self.currentPage),
  31. @"user_id":@(self.userId)
  32. } needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  33. @strongify(self)
  34. NSDictionary* dict = (NSDictionary*)responseObject;
  35. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  36. if (code == 0) {
  37. [ZCHUDHelper dismiss];
  38. NSDictionary *data = [dict dictionaryValueForKey:@"data" defaultValue:@{}];
  39. NSArray *result = [data arrayValueForKey:@"list" defaultValue:@[]];
  40. if (self.currentPage == 1) {
  41. [self.listDataArray removeAllObjects];
  42. }
  43. NSArray *viewModelArr = [[NSArray yy_modelArrayWithClass:[YMDynamicListModel class] json:result].rac_sequence map:^(YMDynamicListModel * _Nullable model) {
  44. YMPersonalDynamicCellViewModel *viewModel = [[YMPersonalDynamicCellViewModel alloc]initWithParams:@{
  45. ParamsId:@(self.userId),
  46. ParamsModel:model,
  47. }];
  48. return viewModel;
  49. }].array;
  50. [self.listDataArray addObjectsFromArray:viewModelArr];
  51. if(self.currentPage > 0 && self.currentPage % 5 == 0){
  52. [[SDImageCache sharedImageCache] clearMemory];
  53. }
  54. [self.refreshUISubject sendNext:@(result.count < self.currentSize ? YMFooterRefresh_HasNoMoreData : YMFooterRefresh_HasMoreData)];
  55. }else{
  56. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  57. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  58. [self.refreshUISubject sendNext:@(YMRefreshError)];
  59. });
  60. }
  61. } failure:^(NSError *error) {
  62. if(self.currentPage != 1){
  63. self.currentPage--;
  64. }
  65. [ZCHUDHelper showTitle:error.localizedDescription];
  66. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  67. [self.refreshUISubject sendNext:@(YMRefreshError)];
  68. });
  69. }];
  70. }
  71. - (NSMutableArray<YMPersonalDynamicCellViewModel *> *)listDataArray{
  72. if (!_listDataArray) {
  73. _listDataArray = [NSMutableArray array];
  74. }
  75. return _listDataArray;
  76. }
  77. @end