Browse Source

Begin download queen control

HonorLee 8 years ago
parent
commit
393dcba232

BIN
TumblrDownloader.xcodeproj/project.xcworkspace/xcuserdata/HonorLee.xcuserdatad/UserInterfaceState.xcuserstate


File diff suppressed because it is too large
+ 9918 - 2
TumblrDownloader/Base.lproj/Main.storyboard


+ 2 - 1
TumblrDownloader/DownloadQueenViewController.h

@@ -8,6 +8,7 @@
 
 #import <UIKit/UIKit.h>
 
-@interface DownloadQueenViewController : UIViewController
+@interface DownloadQueenViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
+@property (strong, nonatomic) IBOutlet UITableView *DownloadTableView;
 
 @end

+ 22 - 8
TumblrDownloader/DownloadQueenViewController.m

@@ -13,10 +13,12 @@
 @end
 
 @implementation DownloadQueenViewController
-
+@synthesize DownloadTableView;
 - (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view.
+    DownloadTableView.dataSource = self;
+    DownloadTableView.delegate = self;
 }
 
 - (void)didReceiveMemoryWarning {
@@ -24,14 +26,26 @@
     // Dispose of any resources that can be recreated.
 }
 
-/*
-#pragma mark - Navigation
 
-// In a storyboard-based application, you will often want to do a little preparation before navigation
-- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
-    // Get the new view controller using [segue destinationViewController].
-    // Pass the selected object to the new view controller.
+#pragma TableView
+-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
+    return 1;
 }
-*/
+-(void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender{
 
+}
+-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
+    return 1;
+}
+-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
+    UITableViewCell *cell = [DownloadTableView dequeueReusableCellWithIdentifier:@"DownloadQueenCell"];
+        [cell setFrame:CGRectMake(0, 0, DownloadTableView.frame.size.width, 40)];
+    UIProgressView *progressView = [[UIProgressView alloc]initWithFrame:CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height)];
+    [cell addSubview:progressView];
+    [progressView setTransform:CGAffineTransformMakeScale(1, 44)];
+    [progressView setTintColor:[UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1]];
+    [progressView setProgressViewStyle:UIProgressViewStyleBar];
+    [progressView setProgress:0.7 animated:YES];
+    return cell;
+}
 @end

+ 1 - 0
TumblrDownloader/ImageHandler.h

@@ -10,4 +10,5 @@
 #import <UIKit/UIKit.h>
 @interface ImageHandler : NSObject
 +(NSString *)getAndSaveThumbImageFromURLandSetToImageView:(NSString *)imagePath saveToName:(NSString *)fileName setToImage:(UIImageView *)imageView;
+//+(void)showAndCacheImage:(NSDictionary *)obj;
 @end

+ 18 - 16
TumblrDownloader/ImageHandler.m

@@ -14,22 +14,22 @@
     UIImage *newImg;
     NSData *imgData;
     NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"/Library/Caches/%@.jpg",fileName]];
-    NSLog(@"%@",filePath);
     BOOL needSave = NO;
     BOOL onError = NO;
-    NSError * error;
+//    NSError *error;
+    NSFileManager *fs = [NSFileManager defaultManager];
     dispatch_group_t disG = dispatch_group_create();
     
     dispatch_group_enter(disG);
-    if([imagePath containsString:@"http"]){
+    if([fs fileExistsAtPath:filePath]){
+        NSLog(@"Read File from cache");
+        imgData = [NSData dataWithContentsOfFile:filePath];
+    }else if([imagePath containsString:@"http"]){
+        NSLog(@"Read File from URL stream");
         needSave = YES;
         imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]];
     }else{
-        imgData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:&error];
-    }
-    if(error)
-    {
-        NSLog(@"%@",error);
+        onError = YES;
     }
 
     if(imgData){
@@ -40,7 +40,7 @@
             newImg = UIGraphicsGetImageFromCurrentImageContext();
             UIGraphicsEndImageContext();
         }else{
-            NSLog(@"%@",imgData);
+//            NSLog(@"%@",imgData);
             newImg = [UIImage imageWithData:imgData];
         }
     }else{
@@ -50,14 +50,11 @@
     
     
     dispatch_group_notify(disG, dispatch_get_main_queue(), ^{
-        if(!onError) [imageView setImage:newImg];
-        NSLog(@"123");
+        NSDictionary *obj = [NSDictionary dictionaryWithObjectsAndKeys:imageView,@"imageView",newImg,@"image", nil];
+//        if(!onError) [imageView setImage:newImg];
+        [NSThread detachNewThreadSelector:@selector(showImage:) toTarget:self withObject:obj];
         if(needSave){
-//            [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:filePath options:nil error:&error atomically:YES];
-            NSFileManager *fs = [NSFileManager defaultManager];
-            BOOL isDir;
-            [fs fileExistsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Caches"] isDirectory:&isDir];
-            NSLog(@"%@",isDir);
+            NSLog(@"Save file to cache");
             NSError *err;
             [UIImageJPEGRepresentation(newImg, 1.0) writeToFile:filePath options:NSDataWritingAtomic error:&err];
             NSLog(@"%@",err);
@@ -65,4 +62,9 @@
     });
     return filePath;
 }
++(void)showImage:(NSDictionary *)obj{
+    UIImageView *imageView = [obj objectForKey:@"imageView"];
+    UIImage *image = [obj objectForKey:@"image"];
+    [imageView setImage:image];
+}
 @end

+ 26 - 13
TumblrDownloader/TasksViewController.m

@@ -48,13 +48,14 @@
 -(void)pullToRefresh{
     NSLog(@"Start refresh");
     _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshing", nil)];
+    [_uirefresh beginRefreshing];
     [self reloadCellData];
     [self refreshEnd];
 }
 
 -(void)refreshEnd{
-    [_uirefresh endRefreshing];
     _uirefresh.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"PullToRefreshEnd", nil)];
+    [_uirefresh endRefreshing];
 }
 
 -(void)reloadCellData{
@@ -100,17 +101,17 @@
 -(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];
-}
+//-(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;
 }
@@ -118,7 +119,7 @@
     [[_TaskTableView cellForRowAtIndexPath:indexPath] setTintColor:[UIColor redColor]];
 }
 -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
-    return 80;
+    return 60;
 }
 
 #pragma Action
@@ -163,12 +164,24 @@
 }
 -(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