12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- //
- // XYMountListViewModel.m
- // Starbuds
- //
- // Created by 翟玉磊 on 2020/1/13.
- // Copyright © 2020 翟玉磊. All rights reserved.
- //
- #import "XYMountListViewModel.h"
- #import "XYMountListCellModel.h"
- @implementation XYMountListViewModel
- - (instancetype)init {
- if (self = [super init]) {
-
- self.shouldPullDownToRefresh = YES;
- self.shouldBeginRefreshing = NO;
- }
- return self;
- }
- - (void)loadData:(SuccessHandler)success failure:(FailureHandler)failure {
-
- self.pullDown?self.page=1:self.page++;
- [[XYUserSwitchManager sharedInstance] getUserSwitchInfoBlock:^(XYUserSwitchManager * _Nonnull manager) {
- [[XYUserAPIManager new] getMountsListWithUserId:@"" successHandler:^(ZYLResponseModel *responseModel) {
- if (self.page == 1) {
- [self.dataSource removeAllObjects];
- }
- XYMountListCellModel *currentModel = nil;
- for (NSDictionary *dict in responseModel.data[@"list"]) {
- XYMountListCellModel *model = [XYMountListCellModel new];
- [model yy_modelSetWithDictionary:dict];
- if (manager.isMountSwitch && [manager.mountId isEqualToString:model.itemId]) {
- model.useStatus = YES;
- currentModel = model;
- }else {
- model.useStatus = NO;
- [self.dataSource addObject:model];
- }
- }
- if (currentModel) {
- // 添加到首位
- [self.dataSource insertObject:currentModel atIndex:0];
- }
- !success?:success(responseModel);
- } failureHandler:^(ZYLNetworkError *error) {
- !failure?:failure(error);
- }];
- }];
- }
- - (void)selectedMountWithIndex:(NSInteger)index success:(SuccessHandler)success failure:(FailureHandler)failure {
-
- XYMountListCellModel *model = self.dataSource[index];
- [[XYUserSwitchManager sharedInstance] setSwitchWithType:501 switchStatus:!model.useStatus itemId:model.itemId completion:^(XYUserSwitchManager * _Nonnull manager, ZYLNetworkError *error) {
- if (error) {
- !failure?:failure(error);
- }else {
- NSMutableArray *temp = [NSMutableArray arrayWithArray:self.dataSource];
- [self.dataSource removeAllObjects];
- XYMountListCellModel *currentModel = nil;
- for (XYMountListCellModel *model in temp) {
- if (manager.isMountSwitch && [manager.mountId isEqualToString:model.itemId]) {
- model.useStatus = YES;
- currentModel = model;
- }else {
- model.useStatus = NO;
- [self.dataSource addObject:model];
- }
- }
- if (currentModel) {
- // 添加到首位
- [self.dataSource insertObject:currentModel atIndex:0];
- }
- !success?:success(nil);
- }
- }];
- }
- @end
|