YMInvitationBreakdownListViewModel.m 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // YMInvitationBreakdownListViewModel.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/15.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMInvitationBreakdownListViewModel.h"
  9. #import "YMInvitingPersonsModel.h"
  10. #import "YMInvitingEarningsModel.h"
  11. @interface YMInvitationBreakdownListViewModel ()
  12. /// 当前数据数量
  13. @property (nonatomic, assign) NSInteger currentSize;
  14. /// 邀请明细分类类型
  15. @property (nonatomic, assign, readwrite) YMInvitationBreakdownCategoryType categoryType;
  16. /// 列表数据
  17. @property (nonatomic, strong, readwrite) NSMutableArray <YMInvitationBreakdownListCellViewModel*>*listDataArray;
  18. /// 请求Api链接
  19. @property (nonatomic, copy) NSString *requestApiUrl;
  20. @end
  21. @implementation YMInvitationBreakdownListViewModel
  22. - (void)ym_initialize{
  23. [super ym_initialize];
  24. self.currentPage = 1;
  25. self.currentSize = 10;
  26. self.categoryType = [self.params integerValueForKey:ParamsCategoryType defaultValue:YMInvitationBreakdownCategoryTypeInvitingPersons];
  27. switch (self.categoryType) {
  28. case YMInvitationBreakdownCategoryTypeInvitingPersons:
  29. {
  30. self.requestApiUrl = InvitingPersonsList;
  31. }
  32. break;
  33. case YMInvitationBreakdownCategoryTypeInvitingEarnings:
  34. {
  35. self.requestApiUrl = InvitingEarningsList;
  36. }
  37. break;
  38. default:
  39. break;
  40. }
  41. }
  42. - (void)getInvitationBreakdownListData {
  43. @weakify(self)
  44. [ZCHUDHelper showWithStatus:@"加载中..."];
  45. [LCHttpHelper requestWithURLString:self.requestApiUrl parameters:@{
  46. @"page":@(self.currentPage),
  47. @"limit":@(self.currentSize)
  48. } needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  49. @strongify(self)
  50. NSDictionary* dict = (NSDictionary*)responseObject;
  51. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  52. if (code == 0) {
  53. [ZCHUDHelper dismiss];
  54. NSDictionary *data = [dict dictionaryValueForKey:@"data" defaultValue:@{}];
  55. NSArray *result = [data arrayValueForKey:@"list" defaultValue:@[]];
  56. if (self.currentPage == 1) {
  57. [self.listDataArray removeAllObjects];
  58. }
  59. NSArray *viewModelArr = @[];
  60. switch (self.categoryType) {
  61. case YMInvitationBreakdownCategoryTypeInvitingPersons:
  62. {
  63. viewModelArr = [[NSArray yy_modelArrayWithClass:[YMInvitingPersonsModel class] json:result].rac_sequence map:^(YMInvitingPersonsModel * _Nullable model) {
  64. YMInvitationBreakdownListCellViewModel *viewModel = [[YMInvitationBreakdownListCellViewModel alloc]initWithParams:@{
  65. ParamsModel:model,
  66. }];
  67. return viewModel;
  68. }].array;
  69. }
  70. break;
  71. case YMInvitationBreakdownCategoryTypeInvitingEarnings:
  72. {
  73. viewModelArr = [[NSArray yy_modelArrayWithClass:[YMInvitingEarningsModel class] json:result].rac_sequence map:^(YMInvitingEarningsModel * _Nullable model) {
  74. YMInvitationBreakdownListCellViewModel *viewModel = [[YMInvitationBreakdownListCellViewModel alloc]initWithParams:@{
  75. ParamsModel:model,
  76. }];
  77. return viewModel;
  78. }].array;
  79. }
  80. break;
  81. default:
  82. break;
  83. }
  84. [self.listDataArray addObjectsFromArray:viewModelArr];
  85. if(self.currentPage > 0 && self.currentPage % 5 == 0){
  86. [[SDImageCache sharedImageCache] clearMemory];
  87. }
  88. [self.refreshUISubject sendNext:@(result.count < self.currentSize ? YMFooterRefresh_HasNoMoreData : YMFooterRefresh_HasMoreData)];
  89. }else{
  90. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  91. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  92. [self.refreshUISubject sendNext:@(YMRefreshError)];
  93. });
  94. }
  95. } failure:^(NSError *error) {
  96. if(self.currentPage != 1){
  97. self.currentPage--;
  98. }
  99. [ZCHUDHelper showTitle:error.localizedDescription];
  100. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  101. [self.refreshUISubject sendNext:@(YMRefreshError)];
  102. });
  103. }];
  104. }
  105. - (NSMutableArray<YMInvitationBreakdownListCellViewModel *> *)listDataArray{
  106. if (!_listDataArray) {
  107. _listDataArray = [NSMutableArray array];
  108. }
  109. return _listDataArray;
  110. }
  111. @end