XYMedalListViewModel.m 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //
  2. // XYMedalListViewModel.m
  3. // Starbuds
  4. //
  5. // Created by 翟玉磊 on 2020/1/13.
  6. // Copyright © 2020 翟玉磊. All rights reserved.
  7. //
  8. #import "XYMedalListViewModel.h"
  9. #import "XYMedalListCellModel.h"
  10. @implementation XYMedalListViewModel
  11. - (instancetype)init {
  12. if (self = [super init]) {
  13. self.shouldPullDownToRefresh = YES;
  14. self.shouldBeginRefreshing = NO;
  15. }
  16. return self;
  17. }
  18. - (void)loadData:(SuccessHandler)success failure:(FailureHandler)failure {
  19. self.pullDown?self.page=1:self.page++;
  20. [[XYUserSwitchManager sharedInstance] getUserSwitchInfoBlock:^(XYUserSwitchManager * _Nonnull manager) {
  21. [[XYUserAPIManager new] getMedalListSuccessHandler:^(ZYLResponseModel *responseModel) {
  22. if (self.page == 1) {
  23. [self.dataSource removeAllObjects];
  24. }
  25. XYMedalListCellModel *currentModel = nil;
  26. for (NSDictionary *dict in responseModel.data[@"list"]) {
  27. XYMedalListCellModel *model = [XYMedalListCellModel new];
  28. [model yy_modelSetWithDictionary:dict];
  29. if (manager.isMedal && [manager.medalId isEqualToString:model.itemId]) {
  30. model.useStatus = YES;
  31. currentModel = model;
  32. }else {
  33. model.useStatus = NO;
  34. [self.dataSource addObject:model];
  35. }
  36. }
  37. if (currentModel) {
  38. // 添加到首位
  39. [self.dataSource insertObject:currentModel atIndex:0];
  40. }
  41. !success?:success(responseModel);
  42. } failureHandler:^(ZYLNetworkError *error) {
  43. !failure?:failure(error);
  44. }];
  45. }];
  46. }
  47. - (void)selectedMedalWithIndex:(NSInteger)index success:(SuccessHandler)success failure:(FailureHandler)failure {
  48. XYMedalListCellModel *model = self.dataSource[index];
  49. [[XYUserSwitchManager sharedInstance] setSwitchWithType:504 switchStatus:!model.useStatus itemId:model.itemId completion:^(XYUserSwitchManager * _Nonnull manager, ZYLNetworkError *error) {
  50. if (error) {
  51. !failure?:failure(error);
  52. }else {
  53. NSMutableArray *temp = [NSMutableArray arrayWithArray:self.dataSource];
  54. [self.dataSource removeAllObjects];
  55. XYMedalListCellModel *currentModel = nil;
  56. for (XYMedalListCellModel *model in temp) {
  57. if (manager.isMedal && [manager.medalId isEqualToString:model.itemId]) {
  58. model.useStatus = YES;
  59. currentModel = model;
  60. }else {
  61. model.useStatus = NO;
  62. [self.dataSource addObject:model];
  63. }
  64. }
  65. if (currentModel) {
  66. // 添加到首位
  67. [self.dataSource insertObject:currentModel atIndex:0];
  68. }
  69. !success?:success(nil);
  70. }
  71. }];
  72. }
  73. @end