// // XYWishCenterViewModel.m // Timi // // Created by 翟玉磊 on 2021/10/19. // #import "XYWishCenterViewModel.h" #import "XYWishGiftModel.h" #import "XYWishAPIManager.h" @implementation XYWishCenterViewModel - (instancetype)init { self = [super init]; if (self) { self.shouldPullDownToRefresh = YES; self.shouldPullUpToLoadMore = NO; } return self; } - (void)loadData:(SuccessHandler)success failure:(FailureHandler)failure { [[XYWishAPIManager new] getWishListSuccessHandler:^(ZYLResponseModel *responseModel) { [self.dataSource removeAllObjects]; for (NSDictionary *dict in responseModel.data[@"list"]) { XYWishGiftModel *model = [XYWishGiftModel new]; [model yy_modelSetWithDictionary:dict]; [self.dataSource addObject:model]; } !success?:success(self.dataSource); } failureHandler:^(ZYLNetworkError *error) { !failure?:failure(error); }]; } - (void)setupWishGiftSwitchStatusWithModel:(XYWishGiftModel *)model successBlock:(void(^)(NSInteger oldIndex, NSInteger index))success failureBlock:(FailureHandler)failur { [[XYWishAPIManager new] setWishTaskSwitchWithWishTaskId:model.wishTaskId status:!model.status successHandler:^(ZYLResponseModel *responseModel) { NSInteger oldIndex = -1; NSInteger index = -1; for (NSInteger i = 0; i < self.dataSource.count; i++) { XYWishGiftModel *tempModel = self.dataSource[i]; if ([model.wishTaskId isEqualToString:tempModel.wishTaskId]) { index = i; if (tempModel.status == 1) { tempModel.status = 0; }else { tempModel.status = 1; } }else { if (tempModel.status == 1) { oldIndex = i; tempModel.status = 0; } } } // 心愿任务状态变更通知 [[NSNotificationCenter defaultCenter] postNotificationName:WISH_TASK_SWITCH_NOTIFICATION object:nil]; !success?:success(oldIndex, index); } failureHandler:^(ZYLNetworkError *error) { !failur?:failur(error); }]; } @end