developer
2023-10-31 c2c0b4f016502c4d1e3a3d63854d800521374f6b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//
//  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