XYWishCenterViewModel.m 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //
  2. // XYWishCenterViewModel.m
  3. // Timi
  4. //
  5. // Created by 翟玉磊 on 2021/10/19.
  6. //
  7. #import "XYWishCenterViewModel.h"
  8. #import "XYWishGiftModel.h"
  9. #import "XYWishAPIManager.h"
  10. @implementation XYWishCenterViewModel
  11. - (instancetype)init
  12. {
  13. self = [super init];
  14. if (self) {
  15. self.shouldPullDownToRefresh = YES;
  16. self.shouldPullUpToLoadMore = NO;
  17. }
  18. return self;
  19. }
  20. - (void)loadData:(SuccessHandler)success failure:(FailureHandler)failure {
  21. [[XYWishAPIManager new] getWishListSuccessHandler:^(ZYLResponseModel *responseModel) {
  22. [self.dataSource removeAllObjects];
  23. for (NSDictionary *dict in responseModel.data[@"list"]) {
  24. XYWishGiftModel *model = [XYWishGiftModel new];
  25. [model yy_modelSetWithDictionary:dict];
  26. [self.dataSource addObject:model];
  27. }
  28. !success?:success(self.dataSource);
  29. } failureHandler:^(ZYLNetworkError *error) {
  30. !failure?:failure(error);
  31. }];
  32. }
  33. - (void)setupWishGiftSwitchStatusWithModel:(XYWishGiftModel *)model successBlock:(void(^)(NSInteger oldIndex, NSInteger index))success failureBlock:(FailureHandler)failur {
  34. [[XYWishAPIManager new] setWishTaskSwitchWithWishTaskId:model.wishTaskId status:!model.status successHandler:^(ZYLResponseModel *responseModel) {
  35. NSInteger oldIndex = -1;
  36. NSInteger index = -1;
  37. for (NSInteger i = 0; i < self.dataSource.count; i++) {
  38. XYWishGiftModel *tempModel = self.dataSource[i];
  39. if ([model.wishTaskId isEqualToString:tempModel.wishTaskId]) {
  40. index = i;
  41. if (tempModel.status == 1) {
  42. tempModel.status = 0;
  43. }else {
  44. tempModel.status = 1;
  45. }
  46. }else {
  47. if (tempModel.status == 1) {
  48. oldIndex = i;
  49. tempModel.status = 0;
  50. }
  51. }
  52. }
  53. // 心愿任务状态变更通知
  54. [[NSNotificationCenter defaultCenter] postNotificationName:WISH_TASK_SWITCH_NOTIFICATION object:nil];
  55. !success?:success(oldIndex, index);
  56. } failureHandler:^(ZYLNetworkError *error) {
  57. !failur?:failur(error);
  58. }];
  59. }
  60. @end