// // TFNovelDownloadMenuView.m // TFReader // // Created by 谢腾飞 on 2020/12/16. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFNovelDownloadMenuView.h" #import "WXYZ_BookDownloadManager.h" #import "TFNovelDownloadTaskListModel.h" #import "TFNovelDownloadMenuCell.h" #import "WXYZ_ChapterBottomPayBar.h" #define DefaultBar_H 6 * Cell_Height + PUB_NAVBAR_OFFSET + 10 #define Cell_Height 40 #define animateDuration 0.4f @interface TFNovelDownloadMenuView () { UIView *menuView; } @property (nonatomic ,strong) TFNovelDownloadTaskListModel *downloadModel; @property (nonatomic ,strong) WXYZ_BookDownloadManager *bookDownloadManager; @property (nonatomic ,strong) UIActivityIndicatorView *activityIndicator; @property (nonatomic ,weak) UITableView *mainTableView; @end @implementation TFNovelDownloadMenuView - (instancetype)init { if (self = [super init]) { [self initialize]; } return self; } - (void)initialize { self.backgroundColor = kColorRGBA(0, 0, 0, 0.0); self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); self.bookDownloadManager = [WXYZ_BookDownloadManager sharedManager]; } - (void)showDownloadPayView { [self.activityIndicator startAnimating]; [self netRequest]; } - (void)hiddenDownloadPayView { if (self.menuBarDidHiddenBlock) { self.menuBarDidHiddenBlock(); } [self removeAllSubviews]; [self removeFromSuperview]; } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UIView *touchView = [[touches anyObject] view]; if (![touchView isEqual:menuView] && ![touchView isEqual:self.mainTableView] && ![touchView isKindOfClass:[NSClassFromString(@"UITableViewCellContentView") class]]) { [self hiddenDownloadPayView]; } } - (void)createSubViews { self.backgroundColor = kBlackTransparentColor; menuView = [[UIView alloc] init]; menuView.backgroundColor = [UIColor whiteColor]; [self addSubview:menuView]; [menuView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(self.mas_bottom); make.width.mas_equalTo(self.mas_width); make.height.mas_equalTo(DefaultBar_H); }]; self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleGray)]; self.activityIndicator.hidesWhenStopped = YES; [menuView addSubview:self.activityIndicator]; //设置小菊花的frame [self.activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.mas_equalTo(menuView.mas_centerX); make.centerY.mas_equalTo(menuView.mas_centerY); make.height.width.mas_equalTo(20); }]; } - (void)reloadViewData { [UIView animateWithDuration:animateDuration animations:^{ [self->menuView mas_remakeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.bottom.mas_equalTo(self.mas_bottom); make.width.mas_equalTo(self.mas_width); make.height.mas_equalTo(DefaultBar_H); }]; }]; UITableView *mainTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.mainTableView = mainTableView; mainTableView.delegate = self; mainTableView.dataSource = self; mainTableView.scrollEnabled = NO; mainTableView.backgroundColor = [UIColor whiteColor]; mainTableView.showsVerticalScrollIndicator = NO; mainTableView.showsHorizontalScrollIndicator = NO; mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone; [self addSubview:mainTableView]; [mainTableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.bottom.mas_equalTo(self.mas_bottom); make.width.mas_equalTo(self.mas_width); make.height.mas_equalTo(Cell_Height * (1 + self.downloadModel.task_list.count) + Cell_Height + PUB_TABBAR_OFFSET + 10); }]; if (@available(iOS 11.0, *)) { mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { // Fallback on earlier versions } [menuView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(Cell_Height * (1 + self.downloadModel.task_list.count) + Cell_Height + PUB_TABBAR_OFFSET + 10); }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.downloadModel.task_list.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { TFNovelDownloadMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFNovelDownloadMenuCell"]; if (!cell) { cell = [[TFNovelDownloadMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFNovelDownloadMenuCell"]; } cell.book_id = [self.book_id integerValue]; cell.optionModel = [self.downloadModel.task_list objectOrNilAtIndex:indexPath.row]; cell.hiddenEndLine = (self.downloadModel.task_list.count - 1 == indexPath.row); return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.mainTableView.userInteractionEnabled = NO; TFNovelDownloadMenuCell *cell = [tableView cellForRowAtIndexPath:indexPath]; [cell startDownloadLoading]; NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; path = [path stringByAppendingPathComponent:[TFUtilsHelper stringToMD5:@"book_catalog"]]; NSString *catalogName = [NSString stringWithFormat:@"%@_%@", self.book_id, @"catalog"]; NSString *fullPath = [path stringByAppendingFormat:@"/%@.plist", [TFUtilsHelper stringToMD5:catalogName]]; if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) { NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:fullPath]; TFProductionModel *t_model = [TFProductionModel modelWithDictionary:dict]; self.downloadModel.productionModel = t_model; [self downloadChapterWithTaskModel:[self.downloadModel.task_list objectAtIndex:indexPath.row]]; } else { WS(weakSelf) [TFNetworkTools POST:Book_Catalog parameters:@{@"book_id":self.book_id} model:TFProductionModel.class success:^(BOOL isSuccess, TFProductionModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { weakSelf.downloadModel.productionModel = t_model; [weakSelf downloadChapterWithTaskModel:[self.downloadModel.task_list objectAtIndex:indexPath.row]]; [weakSelf hiddenDownloadPayView]; } } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return Cell_Height; } //section头部间距 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return Cell_Height; } //section头部视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UILabel *titleLabel = [[UILabel alloc] init]; titleLabel.backgroundColor = [UIColor whiteColor]; titleLabel.frame = CGRectMake(0, 0, SCREEN_WIDTH, Cell_Height); titleLabel.text = TFLocalizedString(@"选择需要下载的章节"); titleLabel.textColor = kBlackColor; titleLabel.font = kMainFont; titleLabel.textAlignment = NSTextAlignmentCenter; return titleLabel; } //section底部间距 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return Cell_Height + PUB_TABBAR_OFFSET + 10; } //section底部视图 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [[UIView alloc] init]; view.frame = CGRectMake(0, 0, SCREEN_WIDTH, Cell_Height + PUB_TABBAR_OFFSET + 10); view.backgroundColor = kColorRGBA(235, 235, 241, 1); UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom]; downloadButton.frame = CGRectMake(0, 10, SCREEN_WIDTH, Cell_Height + PUB_TABBAR_OFFSET); downloadButton.backgroundColor = [UIColor whiteColor]; [downloadButton setTitle:TFLocalizedString(@"下载缓存") forState:UIControlStateNormal]; [downloadButton setTitleColor:kBlackColor forState:UIControlStateNormal]; [downloadButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, PUB_TABBAR_OFFSET / 2, 0)]; [downloadButton.titleLabel setFont:kMainFont]; [downloadButton addTarget:self action:@selector(downloadClick) forControlEvents:UIControlEventTouchUpInside]; [view addSubview:downloadButton]; return view; } - (void)downloadClick { [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Push_To_Download object:nil]; [self hiddenDownloadPayView]; } - (void)netRequest { WS(weakSelf) [TFNetworkTools POST:Book_Chapter_Download_Option parameters:@{@"book_id":self.book_id, @"chapter_id":self.chapter_id} model:nil success:^(BOOL isSuccess, NSDictionary * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { TFNovelDownloadTaskListModel *t_downloadModel = [TFNovelDownloadTaskListModel modelWithDictionary:[t_model objectForKey:@"data"]]; weakSelf.downloadModel = t_downloadModel; [weakSelf.activityIndicator stopAnimating]; [weakSelf createSubViews]; [weakSelf reloadViewData]; } else if (Compare_Json_isEqualTo(requestModel.code, 703)) { [TFPromptManager showPromptViewWithStatus:TFPromptStatusError promptTitle:requestModel.msg]; [weakSelf hiddenDownloadPayView]; } } failure:nil]; } - (void)downloadChapterWithTaskModel:(TFDownloadTaskModel *)taskModel { WS(weakSelf) taskModel.dateString = [TFUtilsHelper currentDateStringWithFormat:@"yyyy-MM-dd"]; taskModel.download_title = [NSString stringWithFormat:@"%@ - %@%@", [TFUtilsHelper formatStringWithInteger:taskModel.start_order], [TFUtilsHelper formatStringWithInteger:taskModel.end_order], TFLocalizedString(@"章")]; [self.bookDownloadManager downloadChaptersWithProductionModel:self.downloadModel.productionModel downloadTaskModel:taskModel production_id:[self.book_id integerValue] start_chapter_id:[taskModel.start_chapter_id integerValue] downloadNum:taskModel.down_num]; self.bookDownloadManager.downloadMissionStateChangeBlock = ^(WXYZ_DownloadMissionState state, NSInteger production_id, TFDownloadTaskModel * _Nonnull downloadTaskModel, NSArray * _Nullable chapterIDArray) { switch (state) { case WXYZ_DownloadStateMissionStart: { for (UIView *view in weakSelf.mainTableView.subviews) { if ([view isKindOfClass:[TFNovelDownloadMenuCell class]]) { TFNovelDownloadMenuCell *cell = (TFNovelDownloadMenuCell *)view; if (cell.optionModel.label == downloadTaskModel.label) { cell.missionState = state; [weakSelf hiddenDownloadPayView]; } } } } break; case WXYZ_DownloadStateMissionFinished: { [TFPromptManager showPromptViewWithStatus:TFPromptStatusSuccess promptTitle:TFLocalizedString(@"下载完成")]; // 更新本地目录 [TFNovelDownloadMenuView updateLocalCatalog:chapterIDArray]; for (UIView *view in weakSelf.mainTableView.subviews) { if ([view isKindOfClass:[TFNovelDownloadMenuCell class]]) { TFNovelDownloadMenuCell *cell = (TFNovelDownloadMenuCell *)view; cell.optionModel = cell.optionModel; } } } break; case WXYZ_DownloadStateMissionShouldPay: { SS(strongSelf) [weakSelf hiddenDownloadPayView]; TFProductionChapterModel *t_model = [[TFProductionChapterModel alloc] init]; t_model.production_id = production_id; t_model.chapter_id = [downloadTaskModel.start_chapter_id integerValue]; WXYZ_ChapterBottomPayBar *payBar = [[WXYZ_ChapterBottomPayBar alloc] initWithChapterModel:t_model barType:WXYZ_BottomPayBarTypeDownload productionType:TFProductionTypeNovel buyChapterNum:[[TFUtilsHelper formatStringWithInteger:taskModel.down_num] integerValue]]; payBar.paySuccessChaptersBlock = ^(NSArray * _Nonnull success_chapter_ids) { [strongSelf downloadChapterWithTaskModel:taskModel]; }; [payBar showBottomPayBar]; } break; default: break; } weakSelf.mainTableView.userInteractionEnabled = YES; }; } static NSString *_bookID; - (void)setBook_id:(NSString *)book_id { _book_id = book_id; _bookID = book_id; } // 下载成功后请求目录并更新本地目录 + (void)updateLocalCatalog:(NSArray *)catalogArr { [TFNetworkTools POST:Book_Catalog parameters:@{@"book_id" : _bookID} model:TFProductionModel.class success:^(BOOL isSuccess, TFProductionModel * _Nullable t_model, TFNetworkRequestModel * _Nonnull requestModel) { if (isSuccess) { NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject; path = [path stringByAppendingPathComponent:[TFUtilsHelper stringToMD5:@"book_catalog"]]; if (![[NSFileManager defaultManager] fileExistsAtPath:path]) { [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:@{} error:nil]; } NSString *catalogName = [NSString stringWithFormat:@"%@_%@", _bookID, @"catalog"]; NSString *fullPath = [path stringByAppendingFormat:@"/%@.plist", [TFUtilsHelper stringToMD5:catalogName]]; [requestModel.data writeToFile:fullPath atomically:YES]; } } failure:nil]; } @end