YMGroupGreetingViewModel.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 = 5;
  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. NSMutableArray<YMGroupGreetingUserModel *> *userList = [NSMutableArray array];
  37. // 解析用户数据
  38. for (NSDictionary *userDict in userListData) {
  39. YMGroupGreetingUserModel *user = [YMGroupGreetingUserModel mj_objectWithKeyValues:userDict];
  40. [userList addObject:user];
  41. }
  42. // 每次清空,再放进去
  43. [self.listDataArray removeAllObjects];
  44. // NSArray *viewModelArr = [[NSArray yy_modelArrayWithClass:[YMGroupGreetingUserModel class] json:result].rac_sequence map:^(YMGroupGreetingUserModel * _Nullable model) {
  45. // YMGroupGreetingUserModel *viewModel = [[YMGroupGreetingUserModel alloc] initWithParams:@{
  46. // ParamsModel:model
  47. // }];
  48. //
  49. // return viewModel;
  50. // }].array;
  51. //
  52. // [self.listDataArray addObjectsFromArray:viewModelArr];
  53. [self.listDataArray addObjectsFromArray:userList];
  54. if(self.currentPage > 0 && self.currentPage % 5 == 0){
  55. [[SDImageCache sharedImageCache] clearMemory];
  56. }
  57. [self.refreshUISubject sendNext:@(result.count < self.currentSize ? YMFooterRefresh_HasNoMoreData : YMFooterRefresh_HasMoreData)];
  58. if (completion) {
  59. completion(YES);
  60. }
  61. }else{
  62. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  63. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  64. [self.refreshUISubject sendNext:@(YMRefreshError)];
  65. });
  66. if (completion) {
  67. completion(NO);
  68. }
  69. }
  70. } failure:^(NSError *error) {
  71. if(self.currentPage != 1){
  72. self.currentPage--;
  73. }
  74. [ZCHUDHelper showTitle:error.localizedDescription];
  75. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  76. [self.refreshUISubject sendNext:@(YMRefreshError)];
  77. });
  78. if (completion) {
  79. completion(NO);
  80. }
  81. }];
  82. }
  83. - (NSMutableArray<YMGroupGreetingUserModel *> *)listDataArray{
  84. if (!_listDataArray) {
  85. _listDataArray = [NSMutableArray array];
  86. }
  87. return _listDataArray;
  88. }
  89. @end