//
|
// LookNoteController.m
|
// BuWanVideo2.0
|
//
|
// Created by weikou2016 on 16/8/12.
|
// Copyright © 2016年 com.yeshi.buwansheque.ios. All rights reserved.
|
//
|
|
#import "LookNoteController.h"
|
#import "NoteCell.h"
|
#import "XYRDetailViewController.h"
|
|
@interface LookNoteController ()<UITableViewDataSource,UITableViewDelegate>{
|
UITableView *mytableview;
|
|
UIButton *deleBtn; //导航栏删除按钮
|
|
UIView *allSelAndDelView; //删除视图
|
UIButton *deleteBut; //删除按钮
|
UIButton *allSelectBut; //全选按钮
|
|
UIView *prompt; //没有数据时,提示视图
|
|
NSMutableArray *historyListDataArray; //本地数据
|
NSMutableArray *_editDataAry; //需要删除的数据
|
}
|
|
@end
|
|
@implementation LookNoteController
|
|
- (void)viewDidLoad {
|
[super viewDidLoad];
|
self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
|
//获取本地的观看记录数据
|
historyListDataArray = [[NSMutableArray alloc]initWithContentsOfFile:HISTORYFILE];
|
|
//创建界面
|
[self initScene];
|
}
|
|
-(void)initScene{
|
self.view.backgroundColor = kGlobalBackgroundColor;
|
|
//设置导航栏
|
[self createNavgationBar];
|
|
//创建列表
|
[self createTableView];
|
|
//创建删除操作视图
|
[self CreateOperatingView];
|
|
//没有数据时的提示视图
|
[self promptView];
|
}
|
|
- (void)viewWillAppear:(BOOL)animated
|
{
|
[super viewWillAppear:animated];
|
[self.navigationController setNavigationBarHidden:NO animated:animated];
|
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
|
}
|
|
- (void)viewWillDisappear:(BOOL)animated
|
{
|
[super viewWillDisappear:animated];
|
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
|
}
|
|
/**
|
设置导航栏
|
*/
|
-(void)createNavgationBar{
|
self.navigationItem.title = @"观看记录";
|
self.navigationController.navigationBar.titleTextAttributes =@{NSFontAttributeName:[UIFont systemFontOfSize:18],NSForegroundColorAttributeName:[UIColor blackColor]};
|
|
//返回按钮
|
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
|
[button setImage:[[UIImage imageNamed:@"详情页面返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
[button addTarget:self action:@selector(back:)
|
forControlEvents:UIControlEventTouchUpInside];
|
button.frame = CGRectMake(0, 0, 32, 32);
|
UIBarButtonItem *iconBarItem=[[UIBarButtonItem alloc] initWithCustomView:button];
|
self.navigationItem.leftBarButtonItem = iconBarItem;
|
|
//删除按钮
|
deleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
|
deleBtn.frame = CGRectMake(0, 0, 40, 44);
|
|
//未选中状态
|
[deleBtn setTitle:@"" forState:UIControlStateNormal];
|
[deleBtn setImage:[[UIImage imageNamed:@"删除"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
//选中状态
|
[deleBtn setTitle:@"取消" forState:UIControlStateSelected];
|
|
deleBtn.titleLabel.font = [UIFont systemFontOfSize:15];
|
deleBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
|
[deleBtn addTarget:self action:@selector(deletebutton:) forControlEvents:UIControlEventTouchUpInside];
|
UIBarButtonItem *deleteBarItem=[[UIBarButtonItem alloc] initWithCustomView:deleBtn];
|
self.navigationItem.rightBarButtonItem = deleteBarItem;
|
}
|
|
/**
|
创建列表
|
*/
|
-(void)createTableView{
|
mytableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 5, KScreenW, KScreenH-5) style:UITableViewStylePlain];
|
mytableview.delegate = self;
|
mytableview.dataSource = self;
|
mytableview.backgroundColor = kGlobalBackgroundColor;
|
mytableview.showsVerticalScrollIndicator = NO;
|
mytableview.separatorStyle = UITableViewCellSeparatorStyleNone;
|
[self.view addSubview:mytableview];
|
[mytableview registerNib:[UINib nibWithNibName:@"NoteCell" bundle:nil] forCellReuseIdentifier:@"NoteCell"];
|
}
|
|
/**
|
创建操作视图(全选,删除)
|
*/
|
-(void)CreateOperatingView{
|
CGFloat height = 40;
|
if (KIsiPhoneX) {
|
height = 60;
|
}
|
allSelAndDelView = [[UIView alloc]initWithFrame:CGRectMake(0, KScreenH-height, KScreenW, 40)];
|
allSelAndDelView.hidden = YES;
|
allSelAndDelView.backgroundColor = [UIColor whiteColor];
|
|
deleteBut = [UIButton buttonWithType:UIButtonTypeSystem];
|
deleteBut.frame = CGRectMake(KScreenW*0.525, 0, KScreenW*0.45, 38);
|
[deleteBut setBackgroundColor:[UIColor lightTextColor]];
|
[deleteBut setTitle:@"删除" forState:UIControlStateNormal];
|
[deleteBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
[deleteBut addTarget:self action:@selector(deleteSelectDataClick) forControlEvents:UIControlEventTouchUpInside];
|
|
UIView *link = [[UIView alloc] initWithFrame:CGRectMake(KScreenW/2, 5, 1, 30)];
|
link.backgroundColor = kGlobalLightGreyColor_210;
|
|
UIView *link1 = [[UIView alloc] initWithFrame:CGRectMake(KScreenW-40, 5, KScreenH, 1)];
|
link.backgroundColor = kGlobalLightGreyColor_210;
|
|
allSelectBut = [UIButton buttonWithType:UIButtonTypeSystem];
|
allSelectBut.frame = CGRectMake(KScreenW*0.025, 0, KScreenW*0.45, 38);
|
[allSelectBut setBackgroundColor:[UIColor lightTextColor]];
|
[allSelectBut setTitle:@"全选" forState:UIControlStateNormal];
|
[allSelectBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
|
[allSelectBut addTarget:self action:@selector(allSelectOrCancelClick) forControlEvents:UIControlEventTouchUpInside];
|
|
[allSelAndDelView addSubview:deleteBut];
|
[allSelAndDelView addSubview:allSelectBut];
|
[allSelAndDelView addSubview:link];
|
[allSelAndDelView addSubview:link1];
|
[self.view addSubview:allSelAndDelView];
|
}
|
|
/**
|
没有数据时的提示视图
|
*/
|
-(void)promptView{
|
prompt = [[UIView alloc]initWithFrame:CGRectMake((self.view.frame.size.width - 155) /2, (self.view.frame.size.height - 190) /2, 155, 190)];
|
[prompt setHidden:YES];
|
[self.view addSubview:prompt];
|
|
UIImageView *buwanImag = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"登录娃娃"]];
|
buwanImag.frame = CGRectMake((prompt.frame.size.width - 102) / 2, 0, 102, 130);
|
[prompt addSubview:buwanImag];
|
|
UIImageView *tipMessage= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"观看记录提示"]];
|
tipMessage.frame = CGRectMake(0, buwanImag.frame.size.height + buwanImag.frame.origin.y + 18, 155, 42);
|
[prompt addSubview:tipMessage];
|
}
|
|
/**
|
* 全选
|
*/
|
-(void)allSelectOrCancelClick{
|
if ([allSelectBut.titleLabel.text isEqualToString:@"全选"]){
|
[allSelectBut setTitle:@"取消全选" forState:UIControlStateNormal];
|
for (int i = 0 ; i <historyListDataArray.count; ++i){
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
[mytableview selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
|
[_editDataAry addObject:[historyListDataArray objectAtIndex:i]];
|
}
|
[deleteBut setTitle:[NSString stringWithFormat:@"删除(%ld)",(unsigned long)_editDataAry.count] forState:UIControlStateNormal];
|
|
}else{
|
[allSelectBut setTitle:@"全选" forState:UIControlStateNormal];
|
[deleteBut setTitle:[NSString stringWithFormat:@"删除"] forState:UIControlStateNormal];
|
|
for (int i = 0 ; i < _editDataAry.count ; ++i){
|
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
|
[mytableview deselectRowAtIndexPath:indexPath animated:NO];
|
}
|
[_editDataAry removeAllObjects];
|
|
}
|
}
|
|
/**
|
* 删除
|
*/
|
-(void)deleteSelectDataClick{
|
if (_editDataAry.count == 0) {
|
[self autoDisappearAlertTime:0.5 msg:@"请选择要删除的记录"];
|
return;
|
}
|
[deleBtn setSelected:NO];
|
[deleBtn setImage:[[UIImage imageNamed:@"删除"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
allSelAndDelView.hidden = YES;
|
|
[historyListDataArray removeObjectsInArray:_editDataAry];
|
[_editDataAry removeAllObjects];
|
[deleteBut setTitle:[NSString stringWithFormat:@"删除"] forState:UIControlStateNormal];
|
|
//写入 HISTORYFILE
|
if (historyListDataArray.count){
|
[historyListDataArray writeToFile:HISTORYFILE atomically:YES];
|
}else{
|
NSFileManager* fileManager=[NSFileManager defaultManager];
|
[fileManager removeItemAtPath:HISTORYFILE error:nil];
|
}
|
[mytableview setEditing:NO];
|
[mytableview reloadData];
|
}
|
|
/**
|
返回
|
|
@param sender 返回按钮
|
*/
|
-(void)back:(id)sender{
|
[self.navigationController popViewControllerAnimated:YES];
|
}
|
|
/**
|
* 右上删除按钮
|
*/
|
-(void)deletebutton:(id)sender{
|
[deleteBut setTitle:[NSString stringWithFormat:@"删除"] forState:UIControlStateNormal];
|
|
if (mytableview.editing) {
|
[mytableview setEditing:NO];
|
[deleBtn setSelected:NO];
|
[deleBtn setImage:[[UIImage imageNamed:@"删除"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
allSelAndDelView.hidden = YES;
|
[allSelectBut setTitle:@"全选" forState:UIControlStateNormal];
|
}else{
|
if (historyListDataArray.count !=0 && historyListDataArray) {
|
_editDataAry = [[NSMutableArray alloc]initWithCapacity:0];
|
[mytableview setEditing:YES];
|
[deleBtn setSelected:YES];
|
[deleBtn setImage:[[UIImage imageNamed:@""] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
|
allSelAndDelView.hidden = NO;
|
}else{
|
allSelAndDelView.hidden = YES;
|
}
|
}
|
}
|
|
/**
|
* 计算字符串高度
|
*/
|
-(BOOL)autoHeiWithHeight:(NSString*)str WithWidth:(float)width{
|
UILabel *lb = [UILabel new];
|
lb.text = str;
|
lb.font = [UIFont systemFontOfSize:15];
|
lb.numberOfLines = 0;
|
CGSize size = [lb sizeThatFits:CGSizeMake(width, MAXFLOAT)];
|
if(size.height > 19){
|
return NO;
|
}else{
|
return YES;
|
}
|
}
|
|
#pragma mark -UITableViewDataSource
|
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
|
if(historyListDataArray.count>0){
|
prompt.hidden = YES;
|
}else{
|
prompt.hidden = NO;
|
}
|
return historyListDataArray.count;
|
}
|
|
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
|
NoteCell * cell = [tableView dequeueReusableCellWithIdentifier:@"NoteCell"];
|
NSMutableDictionary * dic = [historyListDataArray objectAtIndex:indexPath.row];
|
NSString *str = (NSString*)[dic objectForKey:@"Name"];
|
if ( [self autoHeiWithHeight:str WithWidth:cell.name.frame.size.width]){
|
str = [str stringByAppendingString:@"\n "];
|
}
|
cell.name.text = str;
|
NSData *data = [dic objectForKey:@"Hpicturedata"];
|
cell.image.image = [UIImage imageWithData:data];
|
NSString *Episode = (NSString*)[dic objectForKey:@"Tag"];
|
if(Episode.intValue < 100){
|
cell.collect.text = [NSString stringWithFormat:@"第%@集",[dic objectForKey:@"Tag"]];
|
}else if(Episode.length == 8){
|
cell.collect.text = [NSString stringWithFormat:@"第%@期",Episode];
|
}else{
|
cell.collect.text = Episode;
|
}
|
cell.imageWidth.constant=75/10*16;
|
return cell;
|
}
|
|
|
#pragma mark -UITableViewDelegate
|
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
|
return 89;
|
}
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
|
return CGFLOAT_MIN;
|
}
|
|
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
|
return 5;
|
}
|
|
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
|
if (mytableview.editing == NO) {
|
[tableView deselectRowAtIndexPath:indexPath animated:YES];
|
XYRDetailViewController *play = [[XYRDetailViewController alloc]init];
|
[YTHsharedManger startManger].preController = self;
|
NSDictionary *dic = historyListDataArray[indexPath.row];
|
play.modalPresentationStyle = 0;
|
play.Model = [XYRVideoInfoModel yy_modelWithDictionary:dic];
|
[self presentViewController:play animated:YES completion:^{
|
|
}];
|
}else{
|
[_editDataAry addObject:[historyListDataArray objectAtIndex:indexPath.row]];
|
if(_editDataAry.count==historyListDataArray.count){
|
[allSelectBut setTitle:@"取消全选" forState:UIControlStateNormal];
|
}
|
[deleteBut setTitle:_editDataAry.count==0?@"删除":[NSString stringWithFormat:@"删除(%lu)",(unsigned long)_editDataAry.count] forState:UIControlStateNormal];
|
}
|
}
|
|
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
|
if (mytableview.editing == YES){
|
[_editDataAry removeObject:[historyListDataArray objectAtIndex:indexPath.row]];
|
[allSelectBut setTitle:@"全选" forState:UIControlStateNormal];
|
[deleteBut setTitle:_editDataAry.count==0?@"删除":[NSString stringWithFormat:@"删除(%lu)",(unsigned long)_editDataAry.count] forState:UIControlStateNormal];
|
}
|
}
|
|
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
|
return YES;
|
}
|
|
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
|
return UITableViewCellEditingStyleDelete|UITableViewCellEditingStyleInsert;
|
}
|
|
@end
|