YMSearchUserViewModel.m 3.4 KB

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