123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- //
- // TasksViewController.m
- // TumblrDownloader
- //
- // Created by HonorLee on 16/1/17.
- // Copyright © 2016年 HonorLee. All rights reserved.
- //
- #import "TasksViewController.h"
- #import "ImageHandler.h"
- @interface TasksViewController ()
- @property NSUserDefaults *userDefaults;
- @property UIRefreshControl *uirefresh;
- @property NSMutableArray *TaskData;
- @end
- @implementation TasksViewController
- //UIRefreshControl *uirefresh;
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- _TaskTableView.delegate = self;
- _TaskTableView.dataSource = self;
- // _TaskTableView sets
- _uirefresh = [[UIRefreshControl alloc]init];
- _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefresh", nil)];
- [_TaskTableView addSubview:_uirefresh];
- [_uirefresh addTarget:self action:@selector(pullToRefresh) forControlEvents:UIControlEventValueChanged];
-
- _userDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.honorlee.TDGroup"];
-
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- -(void)viewDidAppear:(BOOL)animated{
- [self pullToRefresh];
- }
- -(void)updateSourceData{
- [_userDefaults setObject:_TaskData forKey:@"tasks"];
- [_userDefaults synchronize];
- }
- #pragma Table Refresh
- -(void)pullToRefresh{
- NSLog(@"Start refresh");
- _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshing", nil)];
- [_uirefresh beginRefreshing];
- [self reloadCellData];
- [self refreshEnd];
- }
- -(void)refreshEnd{
- _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshEnd", nil)];
- [_uirefresh endRefreshing];
- }
- -(void)reloadCellData{
- [_userDefaults synchronize];
- _TaskData = [NSMutableArray arrayWithArray:(NSMutableArray *)[_userDefaults objectForKey:@"tasks"]];
- [_TaskTableView reloadData];
- NSLog(@"Data loaded");
- }
- #pragma TableViewDelegate
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- UITableViewCell *cell = [_TaskTableView dequeueReusableCellWithIdentifier:@"TaskCellStyle"];
- // cell.textLabel.
- NSMutableDictionary *tumblrData = [NSMutableDictionary dictionaryWithDictionary:_TaskData[indexPath.row]];
- NSLog(@"%@",[tumblrData objectForKey:@"imgURL"]);
- NSString *newPath = [ImageHandler getAndSaveThumbImageFromURLandSetToImageView:[tumblrData objectForKey:@"imgURL"] saveToName:[tumblrData objectForKey:@"videoID"] setToImage:cell.imageView];
- // NSLog(@"%@",newPath);
- // dispatch_async(dispatch_get_main_queue(), ^{
- // NSLog(@"%@",[tumblrData objectForKey:@"imgURL"]);
- // UIImage *img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tumblrData objectForKey:@"imgURL"]]] scale:1];
- // UIGraphicsBeginImageContext(CGSizeMake(80, 80));
- // [img drawInRect:CGRectMake(0, 0, 80, 80)];
- // UIImage *newImg = UIGraphicsGetImageFromCurrentImageContext();
- // UIGraphicsEndImageContext();
- // [cell.imageView setImage:newImg];
- // NSString *jpgPath = [NSHomeDirectory() stringByAppendingString:[NSString stringWithFormat:@"/thumb/%@.jpg",[tumblrData objectForKey:@"videoID"]]];
- // NSLog(@"%@",jpgPath);
- // [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:jpgPath atomically:YES];
- //// [cell.imageView setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[tumblrData objectForKey:@"imgURL"]]]]];
- // });
-
- cell.textLabel.text = [tumblrData objectForKey:@"videoID"];
- [tumblrData setObject:newPath forKey:@"cachePath"];
- _TaskData[indexPath.row] = tumblrData;
- [self updateSourceData];
- return cell;
- }
- //-tabl
- -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
- return [_TaskData count];
- }
- -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
- return 1;
- }
- //-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
- //// /UITableViewRowActionStyle *style =
- // UITableViewRowAction *downloadAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"Download" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
- //
- // }];
- // downloadAction.backgroundColor = [UIColor colorWithRed:166/255.0 green:226/255.0 blue:105/255.0 alpha:1];
- // UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"Delete" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
- //
- // }];
- // return @[deleteAction,downloadAction];
- //}
- -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
- return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
- [[_TaskTableView cellForRowAtIndexPath:indexPath] setTintColor:[UIColor redColor]];
- }
- -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
- return 60;
- }
- #pragma Action
- - (IBAction)EditTaskList:(id)sender {
- if([sender tag]==1){
- [_TaskTableView setEditing:NO];
- [sender setTitle:@"编辑"];
- [_AllEditBtn setTitle:@"全部开始"];
- [sender setTag:0];
- [_AllEditBtn setTag:1];
- }else{
- [_TaskTableView setEditing:YES];
- [sender setTitle:@"完成"];
- [_AllEditBtn setTitle:@"删除"];
- [sender setTag:1];
- [_AllEditBtn setTag:2];
- }
- }
- - (IBAction)EditAll:(id)sender {
- if([sender tag]==2){
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"是否删除所有已选任务" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
- [alert show];
- }else{
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"所有任务加入下载队列" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
- [alert show];
- }
- }
- #pragma UIAlertView Delegate
- -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
- if([_AllEditBtn tag]==2){
- if(buttonIndex==1){
- // NSArray *selectedIndexPaths =
- [self deleteSelected:[_TaskTableView indexPathsForSelectedRows]];
- }
- }else{
-
- }
- }
- -(void)startAll{
- }
- -(void)deleteSelected:(NSArray *)indexPathsArray{
- NSMutableIndexSet *indexSets = [[NSMutableIndexSet alloc]init];
- NSFileManager *fs = [NSFileManager defaultManager];
- [indexPathsArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
- NSIndexPath *index = (NSIndexPath *)obj;
- [indexSets addIndex:index.row];
- NSDictionary *tumblrData = [NSDictionary dictionaryWithDictionary:_TaskData[index.row]];
- NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Library/Caches/%@.jpg",[tumblrData objectForKey:@"videoID"]]];
- if([fs fileExistsAtPath:filePath]){
- NSLog(@"Remove cache");
- NSError *err;
- [fs removeItemAtPath:filePath error:&err];
- NSLog(@"Remove error:%@",err);
- }
- }];
- [_TaskData removeObjectsAtIndexes:indexSets];
- // [_TaskTableView setContentOffset:CGPointMake(0, -200) animated:NO];
- #pragma reload
- [self updateSourceData];
- // [_uirefresh sendActionsForControlEvents:UIControlEventValueChanged];
- [self reloadCellData];
- }
- @end
|