// // WXYZ_AudioDownloadViewController.m // WXReader // // Created by Andrew on 2020/3/20. // Copyright © 2020 Andrew. All rights reserved. // #import "WXYZ_AudioDownloadViewController.h" #import "WXYZ_AudioDownloadTableViewCell.h" #import "WXYZ_AudioPlayPageMenuView.h" #import "WXYZ_AudioDownloadManager.h" #import "TFReadRecordManager.h" #import "WXYZ_ChapterBottomPayBar.h" @interface WXYZ_AudioDownloadViewController () @property (nonatomic, strong) TFProductionModel *audioModel; @property (nonatomic, assign) CGFloat totalMemorySize; @property (nonatomic, assign) BOOL payBarShowing; @property (nonatomic, strong) UIButton *downloadButton; @property (nonatomic, strong) UIActivityIndicatorView *downloadIndicatorView; @property (nonatomic, strong) WXYZ_AudioDownloadManager *audioDownloadManager; @property (nonatomic, weak) UILabel *headerTitleLabel; @end @implementation WXYZ_AudioDownloadViewController { UILabel *memoryBarView; TFButton *selectAllButton; } - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; [self initDownloadManager]; [self netRequest]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarDefaultStyle]; } - (void)initialize { [self setNavigationBarTitle:TFLocalizedString(@"批量下载")]; self.totalMemorySize = 0; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Login_Success object:nil]; } - (void)createSubviews { self.mainTableView.delegate = self; self.mainTableView.dataSource = self; [self.view addSubview:self.mainTableView]; [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(PUB_NAVBAR_HEIGHT); make.width.mas_equalTo(self.view.mas_width); make.height.mas_equalTo(self.view.mas_height).with.offset(- PUB_NAVBAR_HEIGHT - PUB_TABBAR_HEIGHT - 20); }]; memoryBarView = [[UILabel alloc] init]; memoryBarView.numberOfLines = 2; memoryBarView.backgroundColor = kColorRGB(251, 251, 251); memoryBarView.font = kFont10; memoryBarView.textAlignment = NSTextAlignmentCenter; memoryBarView.textColor = kGrayTextColor; memoryBarView.text =[NSString stringWithFormat:@"%@%@", TFLocalizedString(@"可用空间"), [TFUtilsHelper getRemainingMemorySpace]]; [self.view addSubview:memoryBarView]; [memoryBarView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(self.mainTableView.mas_bottom); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(20); }]; selectAllButton = [[TFButton alloc] initWithFrame:CGRectZero buttonTitle:TFLocalizedString(@"全选") buttonImageName:@"audio_download_unselect" buttonIndicator:TFButtonIndicatorImageLeftBothLeft]; selectAllButton.buttonTitleColor = kBlackColor; selectAllButton.buttonTitleFont = kMainFont; selectAllButton.graphicDistance = 10; selectAllButton.buttonImageScale = 0.4; selectAllButton.hidden = YES; selectAllButton.tag = 0; selectAllButton.backgroundColor = [UIColor whiteColor]; [selectAllButton addTarget:self action:@selector(checkallClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:selectAllButton]; [selectAllButton mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kMargin); make.top.equalTo(memoryBarView.mas_bottom); make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET); }]; self.downloadButton = [UIButton buttonWithType:UIButtonTypeCustom]; self.downloadButton.backgroundColor = kGrayTextLightColor; self.downloadButton.enabled = NO; [self.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal]; [self.downloadButton setTitleColor:kWhiteColor forState:UIControlStateNormal]; [self.downloadButton.titleLabel setFont:kMainFont]; [self.downloadButton addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:self.downloadButton]; [self.downloadButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.view.mas_right); make.top.mas_equalTo(selectAllButton.mas_top); make.width.mas_equalTo(SCREEN_WIDTH / 3); make.height.mas_equalTo(selectAllButton.mas_height); }]; [selectAllButton mas_updateConstraints:^(MASConstraintMaker *make) { make.right.equalTo(self.downloadButton.mas_left).offset(-kHalfMargin); }]; self.downloadIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)]; self.downloadIndicatorView.hidesWhenStopped = YES; [self.downloadButton addSubview:self.downloadIndicatorView]; [self.downloadIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(self.downloadButton.mas_centerX); make.centerY.mas_equalTo(self.downloadButton.mas_centerY); make.width.height.mas_equalTo(self.downloadButton.mas_height); }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSourceArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; static NSString *cellName = @"WXYZ_AudioDownloadTableViewCell"; WXYZ_AudioDownloadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName]; if (!cell) { cell = [[WXYZ_AudioDownloadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName]; } cell.chapterModel = chapterModel; cell.cellDownloadState = [[self.selectSourceDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapterModel.chapter_id]] integerValue]; cell.hiddenEndLine = self.dataSourceArray.count - 1 == indexPath.row; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } //section头间距 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 30; } //section头视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; view.backgroundColor = [UIColor clearColor]; UIView *mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)]; mainView.backgroundColor = kColorRGB(251, 251, 251); [view addSubview:mainView]; UILabel *headerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kMargin, 0, SCREEN_WIDTH / 2, 30)]; self.headerTitleLabel = headerTitleLabel; headerTitleLabel.backgroundColor = [UIColor clearColor]; headerTitleLabel.font = kFont12; // headerTitleLabel.text = [NSString stringWithFormat:@"%@ %zd %@", TFLocalizedString(@"已下载"), [[WXYZ_AudioDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:self.audioModel.production_id], TFLocalizedString(@"章")]; headerTitleLabel.text = [NSString stringWithFormat:@"%@ %zd %@", TFLocalizedString(@"共"), self.dataSourceArray.count, TFLocalizedString(@"章")]; headerTitleLabel.textColor = kGrayTextColor; [mainView addSubview:headerTitleLabel]; CGFloat width = [TFViewHelper getDynamicWidthWithLabelFont:kFont12 labelHeight:30.0 labelText:TFLocalizedString(@"选章标题") maxWidth:240.0]; width += kMargin; TFButton *selectionButton = [[TFButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - width - kHalfMargin, 0, width, 30) buttonTitle:TFLocalizedString(@"选章标题") buttonImageName:@"audio_selection" buttonIndicator:TFButtonIndicatorTitleRight]; selectionButton.tag = 0; selectionButton.buttonTintColor = kGrayTextColor; selectionButton.buttonTitleFont = kFont12; selectionButton.graphicDistance = 2; selectionButton.buttonImageScale = 0.4; [selectionButton addTarget:self action:@selector(selectionButtonClick) forControlEvents:UIControlEventTouchUpInside]; [mainView addSubview:selectionButton]; return view; } //section底部间距 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } //section底部视图 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, CGFLOAT_MIN)]; view.backgroundColor = [UIColor clearColor]; return view; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { TFProductionChapterModel *chapterModel = [self.dataSourceArray objectOrNilAtIndex:indexPath.row]; WXYZ_AudioDownloadTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; switch (cell.cellDownloadState) { case WXYZ_ProductionDownloadStateNormal: case WXYZ_ProductionDownloadStateFail: [self setCollectionCellDownloadStateWithChapter_id:chapterModel.chapter_id downloadState:WXYZ_ProductionDownloadStateSelected]; [self reloadToolBar]; break; case WXYZ_ProductionDownloadStateSelected: [self setCollectionCellDownloadStateWithChapter_id:chapterModel.chapter_id downloadState:WXYZ_ProductionDownloadStateNormal]; [self reloadToolBar]; break; default: break; } } - (void)checkallClick:(TFButton *)sender { self.totalMemorySize = 0; for (TFProductionChapterModel *chapterModel in self.dataSourceArray) { if ([[WXYZ_AudioDownloadManager sharedManager] getChapterDownloadStateWithProduction_id:chapterModel.production_id chapter_id:chapterModel.chapter_id] == WXYZ_ProductionDownloadStateNormal) { self.totalMemorySize = self.totalMemorySize + chapterModel.size; } } for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) { if (!sender.selected) { // 全选状态 if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateNormal || [[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateFail) { [self setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateSelected]; } } else { if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateSelected) { [self setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal]; } } } sender.selected = !sender.selected; [self reloadToolBar]; } - (void)selectionButtonClick { WS(weakSelf) WXYZ_AudioPlayPageMenuView *chooseMenuView = [[WXYZ_AudioPlayPageMenuView alloc] init]; chooseMenuView.menuListArray = self.dataSourceArray; chooseMenuView.totalChapter = self.audioModel.total_chapters; chooseMenuView.chooseMenuBlock = ^(WXYZ_MenuType menuType, NSInteger chooseIndex) { /* 滚动指定段的指定row 到 指定位置*/ [weakSelf.mainTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:chooseIndex * 30 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES]; }; [chooseMenuView showWithMenuType:WXYZ_MenuTypeAudioSelection]; } - (void)initDownloadManager { WS(weakSelf) self.audioDownloadManager = [WXYZ_AudioDownloadManager sharedManager]; self.audioDownloadManager.downloadChapterStateChangeBlock = ^(WXYZ_DownloadChapterState state, NSInteger production_id, NSInteger chapter_id) { dispatch_async(dispatch_get_main_queue(), ^{ switch (state) { case WXYZ_DownloadStateChapterDownloadFinished: // 章节下载完成 { [weakSelf setCollectionCellDownloadStateWithChapter_id:chapter_id downloadState:WXYZ_ProductionDownloadStateDownloaded]; } break; case WXYZ_DownloadStateChapterDownloadFail: // 下载失败 { [weakSelf setCollectionCellDownloadStateWithChapter_id:chapter_id downloadState:WXYZ_ProductionDownloadStateFail]; } break; case WXYZ_DownloadStateChapterDownloadStart: // 开始下载 { [weakSelf setCollectionCellDownloadStateWithChapter_id:chapter_id downloadState:WXYZ_ProductionDownloadStateDownloading]; } break; default: break; } [weakSelf reloadToolBar]; }); }; self.audioDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, NSArray * _Nonnull chapter_ids) { SS(strongSelf) switch (state) { // 任务开始 case WXYZ_DownloadStateMissionStart: { [weakSelf.downloadIndicatorView stopAnimating]; [weakSelf.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal]; for (NSString *t_chapter_id in chapter_ids) { [weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateDownloading]; } } break; // 任务失败 case WXYZ_DownloadStateMissionFail: { [weakSelf.downloadIndicatorView stopAnimating]; [weakSelf.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal]; for (NSString *t_chapter_id in chapter_ids) { [weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal]; } } break; case WXYZ_DownloadStateMissionShouldPay: { [weakSelf.downloadIndicatorView stopAnimating]; [weakSelf.downloadButton setTitle:TFLocalizedString(@"立即下载") forState:UIControlStateNormal]; if (!weakSelf.payBarShowing) { weakSelf.payBarShowing = YES; TFProductionChapterModel *chapterModel = [[TFProductionChapterModel alloc] init]; chapterModel.production_id = production_id; chapterModel.chapter_ids = chapter_ids; WXYZ_ChapterBottomPayBar *payBar = [[WXYZ_ChapterBottomPayBar alloc] initWithChapterModel:chapterModel barType:WXYZ_BottomPayBarTypeDownload productionType:TFProductionTypeAudio]; payBar.paySuccessChaptersBlock = ^(NSArray * _Nonnull success_chapter_ids) { [strongSelf downloadClick]; }; payBar.payFailChaptersBlock = ^(NSArray * _Nonnull fail_chapter_ids) { for (NSString *t_chapter_id in fail_chapter_ids) { [weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal]; } [weakSelf reloadToolBar]; [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:TFLocalizedString(@"购买失败")]; }; payBar.payCancleChapterBlock = ^(NSArray * _Nonnull fail_chapter_ids) { for (NSString *t_chapter_id in fail_chapter_ids) { [weakSelf setCollectionCellDownloadStateWithChapter_id:[t_chapter_id integerValue] downloadState:WXYZ_ProductionDownloadStateNormal]; } [weakSelf reloadToolBar]; }; SS(strongSelf) payBar.bottomPayBarHiddenBlock = ^{ strongSelf.payBarShowing = NO; }; [payBar showBottomPayBar]; } } break; case WXYZ_DownloadStateMissionFinished: [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"下载完成")]; weakSelf.headerTitleLabel.text = [NSString stringWithFormat:TFLocalizedString(@"已下载%zd章"), [[WXYZ_AudioDownloadManager sharedManager] getDownloadChapterCountWithProduction_id:weakSelf.audioModel.production_id]]; break; default: break; } weakSelf.downloadButton.userInteractionEnabled = YES; }; } - (void)downloadClick { self.downloadButton.userInteractionEnabled = NO; NSMutableArray *chapter_ids = [NSMutableArray array]; for (NSString *t_chapter_id in self.selectSourceDictionary.allKeys) { if ([[self.selectSourceDictionary objectForKey:t_chapter_id] integerValue] == WXYZ_ProductionDownloadStateSelected) { [chapter_ids addObject:t_chapter_id]; } } [self.downloadIndicatorView startAnimating]; [self.downloadButton setTitle:@"" forState:UIControlStateNormal]; [self reloadToolBar]; [self.audioDownloadManager downloadChaptersWithProductionModel:self.audioModel production_id:self.audioModel.production_id chapter_ids:chapter_ids]; } - (void)reloadToolBar { NSInteger normalIndex = 0; NSInteger selectIndex = 0; NSInteger downloadedIndex = 0; for (NSString *t_key in self.selectSourceDictionary) { switch ([[self.selectSourceDictionary objectForKey:t_key] integerValue]) { case WXYZ_ProductionDownloadStateNormal: normalIndex ++; break; case WXYZ_ProductionDownloadStateDownloaded: downloadedIndex ++; break; case WXYZ_ProductionDownloadStateSelected: selectIndex ++; break; default: break; } } if (selectIndex > 0) { memoryBarView.text = [NSString stringWithFormat:TFLocalizedString(@"已选择%@条声音,占用%@/可用空间%@"), [TFUtilsHelper formatStringWithInteger:selectIndex], [TFUtilsHelper convertFileSize:self.totalMemorySize * 1024], [TFUtilsHelper getRemainingMemorySpace]]; [memoryBarView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(memoryBarView.intrinsicContentSize.height); }]; } else { memoryBarView.text = [NSString stringWithFormat:@"%@%@", TFLocalizedString(@"可用空间"), [TFUtilsHelper getRemainingMemorySpace]]; [memoryBarView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(memoryBarView.intrinsicContentSize.height); }]; } if (downloadedIndex == self.dataSourceArray.count) { selectAllButton.hidden = YES; self.downloadButton.enabled = NO; self.downloadButton.selected = NO; self.downloadButton.backgroundColor = [UIColor whiteColor]; [self.downloadButton setTitle:TFLocalizedString(@"已全部下载") forState:UIControlStateNormal]; [self.downloadButton setTitleColor:kGrayTextLightColor forState:UIControlStateNormal]; [self.downloadButton mas_remakeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(self.view); make.top.mas_equalTo(selectAllButton.mas_top); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(selectAllButton.mas_height); }]; return; } self.downloadButton.enabled = NO; self.downloadButton.backgroundColor = kGrayTextLightColor; [self.downloadButton mas_updateConstraints:^(MASConstraintMaker *make) { make.width.mas_equalTo(SCREEN_WIDTH / 3); }]; selectAllButton.selected = NO; selectAllButton.enabled = YES; selectAllButton.hidden = NO; selectAllButton.buttonTitle = TFLocalizedString(@"全选"); selectAllButton.buttonImageName = @"audio_download_unselect"; if (selectIndex > 0 && normalIndex == 0) { // 有选中的文件并且没有未选中的文件 self.downloadButton.enabled = YES; self.downloadButton.backgroundColor = kMainColor; selectAllButton.selected = YES; selectAllButton.buttonTitle = TFLocalizedString(@"取消全选"); selectAllButton.buttonImageName = @"audio_download_select"; } else if (normalIndex > 0 && selectIndex > 0) { // 有未选中的文件 self.downloadButton.enabled = YES; self.downloadButton.backgroundColor = kMainColor; } } - (void)setCollectionCellDownloadStateWithChapter_id:(NSInteger)chapter_id downloadState:(WXYZ_ProductionDownloadState)downloadState { WXYZ_AudioDownloadTableViewCell *cell = (WXYZ_AudioDownloadTableViewCell *)[self.mainTableView cellForRowAtIndexPath:(NSIndexPath *)[self.cellIndexDictionary objectForKey:[TFUtilsHelper formatStringWithInteger:chapter_id]]]; cell.cellDownloadState = downloadState; [self.selectSourceDictionary setObject:[TFUtilsHelper formatStringWithInteger:downloadState] forKey:[TFUtilsHelper formatStringWithInteger:chapter_id]]; } - (void)netRequest { WS(weakSelf) [TFNetworkTools POST:Audio_Catalog parameters:@{@"audio_id":[TFUtilsHelper formatStringWithInteger:self.production_id]} model:TFProductionModel.class success:^(BOOL isSuccess, TFProductionModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { if (t_model.chapter_list.count > 0) { weakSelf.audioModel = t_model; weakSelf.dataSourceArray = [NSMutableArray arrayWithArray:t_model.chapter_list]; [weakSelf resetSelectSourceDicWithDataSourceArray:t_model.chapter_list productionType:TFProductionTypeAudio]; } } else { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg]; } [weakSelf.mainTableView reloadData]; [weakSelf reloadToolBar]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { [weakSelf.mainTableView reloadData]; }]; } @end