YMSpoorViewModel.m 3.0 KB

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