ReadyListController.m 7.3 KB
//
//  ReadyListController.m
//  DreamSleep
//
//  Created by peter on 2022/7/11.
//

#import "ReadyListController.h"
#import "SleepReadyRequestModel.h"
#import "RelaxItemsView.h"
#import "PrepareItemsCell.h"

@interface ReadyListController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) RelaxItemsView *relaxItemsView;
@property (nonatomic, strong) UITableView *prepareItemsView;
@property (nonatomic, strong) NSArray *prepare_items;
@property (nonatomic, strong) ExceptionDefaultView *exceptionView;
@end

@implementation ReadyListController

- (instancetype)initWithDelegate:(id<ReadyListControllerDelegate>)delegate {
    if (self = [super init]) {
        _delegate = delegate;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.dk_backgroundColorPicker = DKColorPickerWithKey(VCViewBG);
    
    self.naviTitle = @"设置睡前安排";
    self.naviBarAlpha = 1.0;
    self.dsNaviBar.dk_backgroundColorPicker = DKColorPickerWithColors(BrandColor, SubNaviDarkColor, BrandColor);
    [self.dsNaviBar addSubview:self.backBtn];
    self.titleLab.dk_textColorPicker = DKColorPickerWithKey(Sub_Navi_TITLE);
    [self.backBtn dk_setImage:DKImagePickerWithNames(@"sys_back_icon", @"sys_back_icon", @"sys_back_icon") forState:UIControlStateNormal];
        
    [self queryPreparePeaceListRequest];
    
    [DataStatisticsUtil event:SleepReadyEvent attributes:@{@"name":@"设置睡前安排页面"}];
}

- (void)queryPreparePeaceListRequest {
    WS(weakSelf);
    NSURLSessionDataTask *task = [SleepReadyRequestModel queryPreparePeaceListWithCompletion:^(SleepReadyRequestModel * _Nonnull requestModel) {
        if (requestModel.resCode == DSResCodeSuccess) {
            weakSelf.exceptionView.hidden = YES;
            
            weakSelf.prepare_items = requestModel.prepare_items;
            
            [weakSelf.view addSubview:weakSelf.relaxItemsView];
            [weakSelf.view addSubview:weakSelf.prepareItemsView];
            
            [weakSelf.relaxItemsView adjustRelax:requestModel.relax_items relaxTime:requestModel.relax_time];
        } else {
            weakSelf.exceptionView.hidden = NO;
            [weakSelf.exceptionView showServerErrInfo:requestModel.errMessage];
        }
    }];
    [self autoCancelRequestOnDealloc:task];
}

#pragma mark - UITableViewDataSource && UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.prepare_items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PrepareItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PrepareItemsCell class]) forIndexPath:indexPath];
    ReadyItem *item = self.prepare_items[indexPath.row];
    cell.item = item;
    // 根据item_id在已安排好列表中匹配获取下标
    [cell adjustSelectBtnOrder:[self getIndex:item.item_id]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70.0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PrepareItemsCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ReadyItem *item = self.prepare_items[indexPath.row];
    
    if (cell.selectBtn.selected == NO) {
        if (self.relaxItemsView.itemCount >= 3) {
            [DSProgressHUD showDetailInfo:@"最多选择3个,请先取消1个"];
            return;
        }
        [self.relaxItemsView addItem:item];
        cell.selectBtn.selected = YES;
        item.status = 1;
    } else {
        [self.relaxItemsView removeItem:item.item_id];
        cell.selectBtn.selected = NO;
        item.status = 0;
    }
    [cell adjustSelectBtnState];
    [self.prepareItemsView reloadData];
}

#pragma mark - lazy
- (RelaxItemsView *)relaxItemsView {
    if (!_relaxItemsView) {
        _relaxItemsView = [[RelaxItemsView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.dsNaviBar.frame) + 15, kScreenWidth - 30, 162)];
    }
    return _relaxItemsView;
}

- (UITableView *)prepareItemsView {
    if (!_prepareItemsView) {
        _prepareItemsView = [[UITableView alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(self.relaxItemsView.frame) + 15, kScreenWidth - 30, kScreenHeight - self.dsNaviBar.height - self.relaxItemsView.height - 30 - 24 - Bottom_SafeArea_Height) style:UITableViewStylePlain];
        _prepareItemsView.dk_backgroundColorPicker = DKColorPickerWithKey(TabBarBG);
        [_prepareItemsView cornerRadius:24.0];
        _prepareItemsView.showsVerticalScrollIndicator = NO;
        _prepareItemsView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _prepareItemsView.dataSource = self;
        _prepareItemsView.delegate = self;
        [_prepareItemsView registerClass:[PrepareItemsCell class] forCellReuseIdentifier:NSStringFromClass([PrepareItemsCell class])];
        _prepareItemsView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
        _prepareItemsView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 12)];
    }
    return _prepareItemsView;
}

- (NSArray *)prepare_items {
    if (!_prepare_items) {
        _prepare_items = [NSArray array];
    }
    return _prepare_items;
}

- (ExceptionDefaultView *)exceptionView {
    if (!_exceptionView) {
        WS(weakSelf);
        _exceptionView = [[ExceptionDefaultView alloc] initWithType:ExceptionTypeNet block:^{
            weakSelf.exceptionView.hidden = YES;
            [weakSelf queryPreparePeaceListRequest];
        } superView:self.view];
    }
    return _exceptionView;
}

#pragma mark - others
- (NSUInteger)getIndex:(int)itemID {
    NSArray *relax_items = self.relaxItemsView.has_relax_items;
    __block NSUInteger index = -1;
    [relax_items enumerateObjectsUsingBlock:^(ReadyItem * obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (itemID == obj.item_id) {
            *stop = YES;
            index = idx;
        }
    }];
    return index;
}

- (void)backAction {
    if (self.delegate) { // AI睡眠教练页面->安睡准备页面->安睡设置页面
        if (self.prepare_items.count == 0) {
            [super backAction];
            return;
        }
        [self userSetupPrepareItemsRequest:NO];
    } else { // AI睡眠教练页面->安睡设置页面
        [self userSetupPrepareItemsRequest:YES];
    }
}

- (void)userSetupPrepareItemsRequest:(BOOL)needUpdateAICoach {
    [DSProgressHUD showProgressHUDWithInfo:@""];
    [SleepReadyRequestModel userSetupPrepareItems:self.relaxItemsView.has_relax_items completion:^(SleepReadyRequestModel * _Nonnull requestModel) {
        [DSProgressHUD dissmissProgressHUD];
        if (requestModel.resCode == DSResCodeSuccess) {
            if (self.delegate && [self.delegate respondsToSelector:@selector(passAdjustedReadyItems:)]) {
                [self.delegate passAdjustedReadyItems:self.relaxItemsView.has_relax_items];
            }
            if (needUpdateAICoach) {
                // 刷新AI睡眠教练
                [[NSNotificationCenter defaultCenter] postNotificationName:NeedUpdateAICoach object:nil];
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

#pragma mark - 设置状态栏文字颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

@end