YMGroupGreetingViewModel.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // YMGroupGreetingViewModel.m
  3. // MSYOUPAI
  4. //
  5. // Created by macmini on 2025/5/25.
  6. // Copyright © 2025 MS. All rights reserved.
  7. //
  8. #import "YMGroupGreetingViewModel.h"
  9. @implementation YMGroupGreetingViewModel
  10. - (void)ym_initialize{
  11. [super ym_initialize];
  12. self.customNavTitle = @"打招呼设置";
  13. self.currentPage = 0;
  14. self.currentSize = 6;
  15. }
  16. - (void)getGroupGreetingDataIsNew:(BOOL)isNew completion:(void(^)(BOOL isSuccess))completion {
  17. if (isNew) {
  18. self.currentPage = 0;
  19. }
  20. // 每次请求翻一页
  21. self.currentPage += 1;
  22. @weakify(self)
  23. [ZCHUDHelper showWithStatus:@"加载中..."];
  24. [LCHttpHelper requestWithURLString:HomeRecommend parameters:@{
  25. @"page":@(self.currentPage),
  26. @"limit":@(self.currentSize)
  27. } needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  28. @strongify(self)
  29. NSDictionary* dict = (NSDictionary*)responseObject;
  30. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  31. if (code == 0) {
  32. [ZCHUDHelper dismiss];
  33. NSDictionary *data = [dict dictionaryValueForKey:@"data" defaultValue:@{}];
  34. NSDictionary *result = [data dictionaryValueForKey:@"list" defaultValue:@{}];
  35. NSArray *userListData = [result arrayValueForKey:@"list" defaultValue:@[]];
  36. if (userListData.count == 0) {
  37. [ZCHUDHelper showWithStatus:@"没有更多数据~"];
  38. self.currentPage --;
  39. return;
  40. }
  41. if (userListData.count < self.currentSize) {
  42. [ZCHUDHelper showWithStatus:@"没有更多数据~"];
  43. self.currentPage --;
  44. }
  45. NSMutableArray<YMGroupGreetingUserModel *> *userList = [NSMutableArray array];
  46. // 解析用户数据
  47. for (NSDictionary *userDict in userListData) {
  48. YMGroupGreetingUserModel *user = [YMGroupGreetingUserModel mj_objectWithKeyValues:userDict];
  49. [userList addObject:user];
  50. }
  51. // 每次清空,再放进去
  52. [self.listDataArray removeAllObjects];
  53. // NSArray *viewModelArr = [[NSArray yy_modelArrayWithClass:[YMGroupGreetingUserModel class] json:result].rac_sequence map:^(YMGroupGreetingUserModel * _Nullable model) {
  54. // YMGroupGreetingUserModel *viewModel = [[YMGroupGreetingUserModel alloc] initWithParams:@{
  55. // ParamsModel:model
  56. // }];
  57. //
  58. // return viewModel;
  59. // }].array;
  60. //
  61. // [self.listDataArray addObjectsFromArray:viewModelArr];
  62. [self.listDataArray addObjectsFromArray:userList];
  63. // if(self.currentPage > 0 && self.currentPage % 6 == 0){
  64. // [[SDImageCache sharedImageCache] clearMemory];
  65. // }
  66. [self.refreshUISubject sendNext:@(result.count < self.currentSize ? YMFooterRefresh_HasNoMoreData : YMFooterRefresh_HasMoreData)];
  67. if (completion) {
  68. completion(YES);
  69. }
  70. }else{
  71. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  72. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  73. [self.refreshUISubject sendNext:@(YMRefreshError)];
  74. });
  75. if (completion) {
  76. completion(NO);
  77. }
  78. }
  79. } failure:^(NSError *error) {
  80. if(self.currentPage != 1){
  81. self.currentPage--;
  82. }
  83. [ZCHUDHelper showTitle:error.localizedDescription];
  84. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  85. [self.refreshUISubject sendNext:@(YMRefreshError)];
  86. });
  87. if (completion) {
  88. completion(NO);
  89. }
  90. }];
  91. }
  92. - (NSMutableArray<YMGroupGreetingUserModel *> *)listDataArray{
  93. if (!_listDataArray) {
  94. _listDataArray = [NSMutableArray array];
  95. }
  96. return _listDataArray;
  97. }
  98. @end