// 修改TableViewCell在编辑模式下选中按钮的图片
- (void)layoutSubviews
{
[super layoutSubviews];
for (UIControl *control in self.subviews) {
if (![control isMemberOfClass:NSClassFromString(@"UITableViewCellEditControl")]){
continue;
}
for (UIView *subView in control.subviews) {
if (![subView isKindOfClass: [UIImageView class]]) {
continue;
}
UIImageView *imageView = (UIImageView *)subView;
if (self.selected) {
imageView.image = [UIImage imageNamed:@"download_selected"]; // 选中时的图片
} else {
imageView.image = [UIImage imageNamed:@"download_normal"]; // 未选中时的图片
}
}
}
}
#import "HPMineDownloadVC.h"
#import "MCDownloader.h"
#import "HPRTNavigationController.h"
#import
#import
// v
#import "HPMineDownloadCell.h"
// vm
// vc
#import "HPTrainDetailVC2.h"
#import "XJVideoPlayerVC.h"
#import
static NSString *const reuseIdentifier = @"HPMineDownloadCell";
@interface HPDownloadSection : NSObject
@property (nonatomic,strong)NSString *title;
@property (nonatomic,strong)NSString *ID;
@property (nonatomic,strong)NSArray *datas;
@property (nonatomic,assign)BOOL open;
@end
@implementation HPDownloadSection
@end
@interface HPMineDownloadVC ()
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic , strong) NSMutableArray *dataSource;
@property (nonatomic,strong)UIView *blankView;
// 编辑模式
@property (nonatomic,strong) NSMutableArray *deleteArr;//存储删除的数据
// 底部功能键
@property (nonatomic,strong)UIView *bottomView;
@property (nonatomic,strong)UIButton *selectAllBtn;
@property (nonatomic,strong)UIButton *deleteBtn;
@property (nonatomic,strong)UILabel *deleteNumLb;
@end
@implementation HPMineDownloadVC
- (void)requestData {
_dataSource = [[NSMutableArray alloc] init];
NSMutableArray *sourceCopyData = [[MCDownloader sharedDownloader].allDownloadReceipts allValues].mutableCopy;
NSMutableArray *temp1 = @[].mutableCopy;
// 只有下载过的才能出现
for (MCDownloadReceipt *rec in sourceCopyData) {
if (rec.state != MCDownloadStateNone || rec.progress.fractionCompleted != 0.f) {
[temp1 addObject:rec];
}
// xjLog(rec.description);
}
// 根据 sectionId 的不同去分组
// 1.获取 index
NSArray *indexArray = [temp1 valueForKey:@"courseId"];
// 2.将 array 转换 NSSet
NSSet *indexSet = [NSSet setWithArray:indexArray];
// 3.新建 array 存放分组后的 array
NSMutableArray *resultArray = @[].mutableCopy;
// NSSet 去重 遍历
[indexSet enumerateObjectsUsingBlock:^(id _Nonnull obj, BOOL * _Nonnull stop) {
// 根据 NSPredicate 获取 array
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"courseId == %@",obj];
NSArray *indexArray = [temp1 filteredArrayUsingPredicate:predicate];
// 将查询结果加入到resultArray中
[resultArray addObject:indexArray];
}];
// 加入 open, title 属性
for (NSArray *arr in resultArray) {
if (arr.count) {
// 取出第一个
MCDownloadReceipt *rec = arr[0];
NSString *title = rec.courseTitle;
NSString *courseId = rec.courseId;
HPDownloadSection *section = [HPDownloadSection new];
section.title = title;
section.ID = courseId;
section.datas = arr.mutableCopy;
section.open = YES;
[_dataSource addObject:section];
}
}
[self.tableView reloadData];
[self isShowRightItem];
}
//- (void)viewWillAppear:(BOOL)animated {
//
// [super viewWillAppear:YES];
//
// Refresh = YES;
//}
-(void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 发出章节 reload 的通知
POST_NOTIFICATION(kNotificationDownloadFileChange);
}
- (void)viewDidLoad {
[super viewDidLoad];
self.deleteArr = @[].mutableCopy;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(editBtnAction)];
self.title = @"下载管理";
[self.view addSubview:self.tableView];
[self requestData];
[self isShowRightItem];
[self.tableView reloadData];
// present 进来的
if (self.rt_navigationController.rt_viewControllers.count == 1) {
[self addBackBarBtn];
}
// 下载完成
extern NSString * _Nonnull const MCDownloadFinishNotification;
ADD_NOTIFICATION_WITH_SEL_AND_NAME(@selector(downloadFinish:), MCDownloadFinishNotification);
}
- (void)removeAction {
for (MCDownloadReceipt *rec in self.deleteArr) {
[[MCDownloader sharedDownloader] remove:rec completed:^{
[self requestData];
}];
}
[self.deleteArr removeAllObjects];
[self.tableView reloadData];
[self editBtnAction];
}
- (void)isShowRightItem {
if([self getDataSourceCount]) {
[self.navigationItem.rightBarButtonItem setEnabled:YES];
[self.navigationItem.rightBarButtonItem setTitle:@"编辑"];
}else {
[self.navigationItem.rightBarButtonItem setEnabled:NO];
[self.navigationItem.rightBarButtonItem setTitle:@""];
}
// [self.tableView reloadData];
}
// MARK: - IBAction
// 导航栏按钮的点击
- (void)editBtnAction{
if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"编辑"]) {
//打开所有隐藏
[self openAllSections];
// 暂停所有下载
[[MCDownloader sharedDownloader] cancelAllDownloads];
// 初始化删除列表
_deleteArr = @[].mutableCopy;
// 显示功能键界面
[UIView animateWithDuration:0.3 animations:^{
self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(self.view.bounds) - XJ_TabbarHeight , XJ_ScreenWidth, 49);
[self.tableView setHeight:XJ_ScreenHeight - XJ_NavigationBarHeight - XJ_TabbarHeight];
}];
// 编辑模式的时候可以多选
self.tableView.allowsMultipleSelectionDuringEditing = YES;
self.tableView.editing = YES;
self.navigationItem.rightBarButtonItem.title = @"取消";
}else{
self.tableView.editing = NO;
self.navigationItem.rightBarButtonItem.title = @"编辑";
// 消失功能键界面
[UIView animateWithDuration:0.3 animations:^{
self.bottomView.frame = CGRectMake(0, CGRectGetMaxY(self.view.bounds), XJ_ScreenWidth, 49);
[self.tableView setHeight:XJ_ScreenHeight - XJ_NavigationBarHeight];
} completion:^(BOOL finished) {
[self.bottomView removeFromSuperview];
self.bottomView = nil;
}];
[self isShowRightItem];
}
}
- (void)selectAllAciton{
if ([_selectAllBtn.titleLabel.text isEqualToString:@"全部选中"]) {
[_selectAllBtn setTitle:@"全部取消" forState:0];
// 获得选中的所有行
for (int i = 0; i < _dataSource.count; i ++) {
HPDownloadSection *sec = _dataSource[i];
for (int k = 0; k < sec.datas.count; k ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:k inSection:i];
[self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}
for (MCDownloadReceipt *rec in sec.datas) {
if (![self.deleteArr containsObject:rec]) {
[self.deleteArr addObject:rec];
}
}
}
}else{
[_selectAllBtn setTitle:@"全部选中" forState:0];
for (int i = 0; i < _dataSource.count; i ++) {
HPDownloadSection *sec = _dataSource[i];
for (int k = 0; k < sec.datas.count; k ++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:k inSection:i];
[self.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
[self.deleteArr removeAllObjects];
}
[self setDeleteNumLb];
}
- (void)deleteBtnClick {
NSArray *btnItems = @[MMItemMake(@"确定", MMItemTypeHighlight, ^(NSInteger index) {
// 删除
[self removeAction];
}),MMItemMake(@"取消", MMItemTypeNormal, nil)];
[[[MMAlertView alloc] initWithTitle:@"提示" detail:[NSString stringWithFormat:@"即将删除选中的%zd个文件", self.deleteArr.count] items:btnItems] showWithBlock:nil];
}
// section header 的点击
- (void)buttonPress:(UIButton *)sender//headButton点击
{
// 编辑中不能转换
if (self.tableView.isEditing) {
return;
}
HPDownloadSection *model = self.dataSource[sender.tag];
UIImageView *imageView = objc_getAssociatedObject(sender,@"buttonKey");
if (model.open) {
[UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionTransitionNone animations:^{
CGAffineTransform currentTransform = imageView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, -M_PI/2); // 在现在的基础上旋转指定角度
imageView.transform = newTransform;
} completion:^(BOOL finished) {
}];
}else{
[UIView animateWithDuration:0.3 delay:0.0 options: UIViewAnimationOptionAllowUserInteraction |UIViewAnimationOptionCurveLinear animations:^{
CGAffineTransform currentTransform = imageView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI/2); // 在现在的基础上旋转指定角度
imageView.transform = newTransform;
} completion:^(BOOL finished) {
}];
}
model.open = !model.open;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}
- (void)openAllSections {
NSInteger i = 0;
for (HPDownloadSection *section in self.dataSource) {
if (section.open == NO) {
section.open = YES;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationAutomatic];
}
i++;
}
}
/**
* 设置数量
*/
-(void)setDeleteNumLb{
long int delectCount = self.deleteArr.count;
// 准备删除数量大于0
if (delectCount) {
self.deleteNumLb.hidden = NO;
self.deleteNumLb.text = [NSString stringWithFormat:@"%ld", delectCount];
self.deleteBtn.enabled = YES;
} else {
self.deleteNumLb.hidden = YES;
self.deleteNumLb.text = @"0";
self.deleteBtn.enabled = NO;
}
if (self.deleteArr.count != [self getDataSourceCount]) {
[_selectAllBtn setTitle:@"全部选中" forState:UIControlStateNormal];
[_deleteBtn setTitle:@"删除选中" forState:UIControlStateNormal];
}else {
[_selectAllBtn setTitle:@"全部取消" forState:UIControlStateNormal];
[_deleteBtn setTitle:@"删除全部" forState:UIControlStateNormal];
}
}
// MARK: - tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
if (self.dataSource.count) {
return self.dataSource.count;
}
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (self.dataSource.count) {
HPDownloadSection *model = self.dataSource[section];
NSInteger count = model.open ? model.datas.count : 0;
return count;
}
return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
HPMineDownloadCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier forIndexPath:indexPath];
HPDownloadSection *sec = self.dataSource[indexPath.section];
MCDownloadReceipt *rec = sec.datas[indexPath.row];
cell.receipt = rec;
return cell;
}
//先要设Cell可编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
//定义编辑样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//xjy:直接播放本地视频
// HPDownloadSection *sec = self.dataSource[indexPath.section];
// MCDownloadReceipt *rec = sec.datas[indexPath.row];
// UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
// MPMoviePlayerViewController *mpc = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:rec.filePath]];
// [vc presentViewController:mpc animated:YES completion:nil];
//
// return;
if (tableView.isEditing) {
HPDownloadSection *sec = self.dataSource[indexPath.section];
MCDownloadReceipt *rec = sec.datas[indexPath.row];
if (![self.deleteArr containsObject:rec]) {
[self.deleteArr addObject:rec];
}
// NSLog(@"deleteArr: %ld, dataSource: %ld", self.deleteArr.count, [self getDataSourceCount]);
[self setDeleteNumLb];
return;
}
// 不是编辑模式下才允许进入详情页面
[tableView deselectRowAtIndexPath:indexPath animated:YES];
//
// XJVideoPlayerVC *playVc = [[XJVideoPlayerVC alloc] init];
// playVc.videoURL = [NSURL URLWithString:rec.url];
// [self.navigationController pushViewController:playVc animated:YES];
HPMineDownloadCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.receipt.state == MCDownloadStateCompleted || cell.receipt.progress.fractionCompleted == 1) {
// 进入课程详情
HPTrainDetailVC2 *detail = [[HPTrainDetailVC2 alloc] initWithCourseId:cell.receipt.courseId courseGuid:@"" courseApplyId:nil];
detail.modelIDReadyToPlay = cell.receipt.modelID;
[self.navigationController pushViewController:detail animated:YES];
}else {
[cell downloadBtnClick];
}
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView.isEditing) {
HPDownloadSection *sec = self.dataSource[indexPath.section];
MCDownloadReceipt *rec = sec.datas[indexPath.row];
if ([self.deleteArr containsObject:rec]) {
[self.deleteArr removeObject:rec];
}
[self setDeleteNumLb];
}
}
//MARK: - 表头表尾视图
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (self.dataSource.count) {
return 44;
}
return 0;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (self.dataSource.count) {
UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, XJ_ScreenWidth, 44)];
sectionView.backgroundColor = [UIColor whiteColor];
HPDownloadSection *model = self.dataSource[section];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTag:section];
UIFont* font = [UIFont systemFontOfSize:18 weight:UIFontWeightSemibold];
[button.titleLabel setFont:font];
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:model.title forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:sectionView.bounds];
[sectionView addSubview:button];
UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(button.bounds), XJ_ScreenWidth, 0.6)];
line.backgroundColor = techLineColor();
[sectionView addSubview:line];
if (model.open) {
UIImageView * _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(XJ_ScreenWidth - 6 - 20, (44-6)/2, 11, 6)];
[_imgView setImage:[UIImage imageNamed:@"train_chapter_down"]];
[button addSubview:_imgView];
CGAffineTransform currentTransform = _imgView.transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform, M_PI); // 在现在的基础上旋转指定角度
_imgView.transform = newTransform;
objc_setAssociatedObject(button, @"buttonKey", _imgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}else{
UIImageView * _imgView = [[UIImageView alloc] initWithFrame:CGRectMake(XJ_ScreenWidth - 6 - 20, (44-6)/2, 11, 6)];
[_imgView setImage:[UIImage imageNamed:@"train_chapter_down"]];
[button addSubview:_imgView];
objc_setAssociatedObject(button, @"buttonKey", _imgView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return sectionView;
}
return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (!self.dataSource.count) {
return self.blankView;
}
return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
if (!self.dataSource.count) {
return XJ_ScreenHeight - XJ_NavigationBarHeight - XJ_safeBottomMargin;
}
return 0;
}
#pragma mark - setter
-(UIView *)blankView
{
if (!_blankView) {
_blankView = [[UIView alloc] initWithFrame:CGRectMake(0, XJ_NavigationBarHeight, XJ_ScreenWidth, XJ_ScreenHeight - XJ_NavigationBarHeight - XJ_safeBottomMargin)];
_blankView.backgroundColor = techBgColor();
UIImageView *imv = [[UIImageView alloc] init];
imv.image = [UIImage imageNamed:@"holder_blank_4"];
imv.contentMode = UIViewContentModeScaleAspectFill;
[_blankView addSubview:imv];
UILabel *tips = [[UILabel alloc] init];
tips.text = @"啥课程也没下载哦~";
tips.font = [UIFont systemFontOfSize:14];
tips.textColor = techTextColor2();
tips.textAlignment = NSTextAlignmentCenter;
[tips sizeToFit];
[_blankView addSubview:tips];
[imv mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(tips);
make.bottom.equalTo(tips.mas_top).offset(-20);
}];
[tips mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(_blankView);
make.centerY.equalTo(_blankView).offset(-40);
}];
}
return _blankView;
}
-(UITableView *)tableView {
if (!_tableView) {
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, XJ_NavigationBarHeight, XJ_ScreenWidth, XJ_ScreenHeight - XJ_NavigationBarHeight - XJ_safeBottomMargin)];
_tableView.backgroundColor = [UIColor whiteColor];
_tableView.showsVerticalScrollIndicator = NO;
_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView registerClass:[HPMineDownloadCell class] forCellReuseIdentifier:reuseIdentifier];
_tableView.estimatedRowHeight = 50;
_tableView.rowHeight = UITableViewAutomaticDimension;
}
return _tableView;
}
-(UIView *)bottomView {
if (!_bottomView) {
_bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, XJ_ScreenHeight , XJ_ScreenWidth, 49)];
_bottomView.backgroundColor = [UIColor whiteColor];
self.selectAllBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.selectAllBtn setTitle:@"全部选中" forState:0];
[self.selectAllBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
[self.selectAllBtn setTitleColor:[UIColor blackColor] forState:0];
[self.selectAllBtn setFrame:CGRectMake(0, 0, XJ_ScreenWidth / 2.0, 49)];
[self.selectAllBtn addTarget:self action:@selector(selectAllAciton) forControlEvents:UIControlEventTouchUpInside];
[_bottomView addSubview:self.selectAllBtn];
CALayer *bottomBorder = [CALayer layer];
float height = 1.0f;
float width = _bottomView.frame.size.width;
bottomBorder.frame = CGRectMake(0.0f, 0, width, height);
bottomBorder.backgroundColor = techLineColor().CGColor;
[_bottomView.layer addSublayer:bottomBorder];
self.deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[self.deleteBtn setTitle:@"删除选中" forState:0];
[self.deleteBtn.titleLabel setFont:[UIFont systemFontOfSize:16]];
[self.deleteBtn setTitleColor:themeColor() forState:0];
[self.deleteBtn setTitleColor:techTextColor2() forState:UIControlStateDisabled];
[self.deleteBtn setFrame:CGRectMake(XJ_ScreenWidth / 2.0, 0, XJ_ScreenWidth / 2.0, 49)];
[self.deleteBtn addTarget:self action:@selector(deleteBtnClick) forControlEvents:UIControlEventTouchUpInside];
self.deleteBtn.enabled = NO;
[_bottomView addSubview:self.deleteBtn];
self.deleteNumLb = [[UILabel alloc] init];
self.deleteNumLb.font = [UIFont systemFontOfSize:9];
self.deleteNumLb.text = @"0";
self.deleteNumLb.textColor = themeColor();
[self.deleteNumLb sizeToFit];
self.deleteNumLb.layer.borderColor = themeColor().CGColor;
self.deleteNumLb.layer.borderWidth = 0.6;
self.deleteNumLb.layer.cornerRadius = 6;
self.deleteNumLb.textAlignment = NSTextAlignmentCenter;
self.deleteNumLb.hidden = YES;
[self.deleteBtn addSubview:self.deleteNumLb];
[self.deleteNumLb mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.deleteBtn).offset(-5);
make.left.equalTo(self.deleteBtn.titleLabel.mas_right);
make.height.equalTo(@12);
make.width.greaterThanOrEqualTo(@15);
}];
[self.view addSubview:_bottomView];
}
return _bottomView;
}
- (NSInteger)getDataSourceCount {
NSInteger count = 0;
for (HPDownloadSection *model in self.dataSource) {
count += model.datas.count;
}
return count;
}
- (void)addBackBarBtn
{
UIButton *dismissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[dismissBtn setImage:[UIImage imageNamed:@"back-white"] forState:UIControlStateNormal];
[dismissBtn sizeToFit];
[dismissBtn addTarget:self
action:@selector(dismissClick)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:dismissBtn];
self.navigationItem.leftBarButtonItem = leftItem;
}
- (void)downloadFinish:(NSNotification *)ntf
{
[self.tableView reloadData];
}
- (void)dismissClick
{
[self dismissViewControllerAnimated:YES completion:nil];
}
@end