Browse Source

1.修改Photo读取,与磁盘缓存分离。
2.增加程序退出后保存Task,以及重新登录后重启Task。

Pen Li 9 years ago
parent
commit
2df70133d4

BIN
RA Image/RA Image.xcodeproj/project.xcworkspace/xcuserdata/macmini1.xcuserdatad/UserInterfaceState.xcuserstate


+ 0 - 12
RA Image/RA Image.xcodeproj/xcuserdata/macmini1.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

@@ -2,16 +2,4 @@
 <Bucket
    type = "1"
    version = "2.0">
-   <Breakpoints>
-      <BreakpointProxy
-         BreakpointExtensionID = "Xcode.Breakpoint.ExceptionBreakpoint">
-         <BreakpointContent
-            shouldBeEnabled = "Yes"
-            ignoreCount = "0"
-            continueAfterRunningActions = "No"
-            scope = "0"
-            stopOnStyle = "0">
-         </BreakpointContent>
-      </BreakpointProxy>
-   </Breakpoints>
 </Bucket>

+ 58 - 0
RA Image/RA Image/AppDelegate.m

@@ -19,6 +19,10 @@
 
 - (void)showNormalRootVC {
     self.window.rootViewController = self.rootVC;
+    NSMutableArray *arr = [self cachedUploadTasks];
+    if (arr && arr.count) {
+        [self.uploadManager addTasks:arr];
+    }
 }
 
 - (void)showLoginVC {
@@ -38,6 +42,7 @@
     self.companyName = nil;
     self.companyIcon = nil;
     self.modeList = nil;
+    [self saveUploadTasks];
     [[NSNotificationCenter defaultCenter] postNotificationName:LogoutNotification object:nil];
     
     [self showLoginVC];
@@ -54,9 +59,61 @@
     });
 }
 
+#pragma mark - Exception
+
+void UncaughtExceptionHandler(NSException *exception) {
+    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
+    if (!appDelegate.user) {
+        return;
+    }
+    
+    if (appDelegate.uploadManager.arr_queue.count) {
+        [appDelegate saveUploadTasks];
+    }
+}
+
+- (void)setUpUncaughtExceptionHandler {
+    NSSetUncaughtExceptionHandler(&UncaughtExceptionHandler);
+}
+
+- (void)saveUploadTasks {
+    NSString *path = [[RAUtils appCacheDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_uploadTasks",self.user]];
+    NSMutableArray *tmpArr = [NSMutableArray array];
+    for (NSDictionary *dic in self.uploadManager.arr_queue) {
+        NSMutableDictionary *mutDic = [dic mutableCopy];
+        [mutDic removeObjectForKey:@"operation"];
+        [mutDic removeObjectForKey:@"status"];
+        [tmpArr addObject:mutDic];
+    }
+    BOOL success = [tmpArr writeToFile:path atomically:NO];
+    [self.uploadManager.arr_queue removeAllObjects];
+    if (success) {
+        DebugLog(@"Save Task Success");
+    }
+}
+
+- (NSMutableArray *)cachedUploadTasks {
+    if (!self.user) {
+        return nil;
+    }
+    NSString *path = [[RAUtils appCacheDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_uploadTasks",self.user]];
+    
+    NSFileManager *manager = [NSFileManager defaultManager];
+    if ([manager fileExistsAtPath:path]) {
+        NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:path];
+        [manager removeItemAtPath:path error:nil];
+        return arr;
+    }
+    
+    return nil;
+}
+
+#pragma mark - App Delegate
+
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     // Override point for customization after application launch.
     
+    [self setUpUncaughtExceptionHandler];
     
     NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
     self.build =[infoDict objectForKey:@"CFBundleVersion"];
@@ -95,6 +152,7 @@
 
 - (void)applicationWillTerminate:(UIApplication *)application {
     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
+    [self saveUploadTasks];
 }
 
 

+ 3 - 1
RA Image/RA Image/BasicModeViewController.h

@@ -8,18 +8,20 @@
 
 #import "BasicViewController.h"
 
+@class UploadViewController;
 @interface BasicModeViewController : BasicViewController<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
 
 @property (nonatomic,copy) NSString *name;
 @property (nonatomic,copy) NSString *barcode;
 @property (nonatomic,copy) NSString *barcodeTitle;
 @property (nonatomic,assign) NSUInteger photoCount;
-@property (nonatomic,strong) NSArray *photos;
+@property (nonatomic,strong) NSMutableArray *photos;
 
 - (void)showPhotoList;
 - (void)showScanner;
 - (void)clickCameraButton;
 - (void)showBarcodeInput;
+- (void)clearPhotos;
 
 #pragma mark - 需要子类重写
 - (void)receiveImage:(UIImage *)img;

+ 74 - 33
RA Image/RA Image/BasicModeViewController.m

@@ -9,6 +9,7 @@
 #import "BasicModeViewController.h"
 #import "ScannerViewController.h"
 #import "PhotoListViewController.h"
+#import "UploadViewController.h"
 
 @interface BasicModeViewController ()<UITextFieldDelegate>
 
@@ -19,6 +20,10 @@
 - (void)viewDidLoad {
     [super viewDidLoad];
     // Do any additional setup after loading the view.
+    self.photoCount = 0;
+    
+    UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Upload List" style:UIBarButtonItemStylePlain target:self action:@selector(showUploadList)];
+    self.navigationItem.rightBarButtonItem = uploadListItem;
 }
 
 - (void)didReceiveMemoryWarning {
@@ -28,7 +33,20 @@
 
 - (void)viewWillAppear:(BOOL)animated {
     [super viewWillAppear:animated];
-    [self loadSavedPhotoCount];
+    
+}
+
+- (NSMutableArray *)photos {
+    if (!_photos) {
+        _photos = [NSMutableArray array];
+    }
+    return _photos;
+}
+
+- (void)showUploadList {
+        
+    UploadViewController *upVC = (UploadViewController *)[self viewControllerInStoryboard:@"Upload" withId:@"UploadViewController"];
+    [self.navigationController pushViewController:upVC animated:YES];
 }
 
 - (void)showPhotoList {
@@ -113,45 +131,56 @@
     
 }
 
-- (void)saveImage:(UIImage *)img {
+- (NSString *)saveImage:(UIImage *)img {
     NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
     
     NSDate * date = [NSDate date];
     NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
-    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
+    [dateFormatter setDateFormat:@"YYYY-MM-dd-HH-mm-ss"];
     NSString * name = [[dateFormatter stringFromDate:date] stringByAppendingPathExtension:@"png"];
 
+//    NSString *name = [[NSUUID UUID] UUIDString];
+//    name = [name stringByAppendingPathExtension:@"png"];
     NSString *path = [dir stringByAppendingPathComponent:name];
     
-    [RAUtils saveData:UIImagePNGRepresentation(img) toPath:path];
+    if ([RAUtils saveData:UIImagePNGRepresentation(img) toPath:path]) {
+        return path;
+    }
+    
+    return nil;
 }
 
-- (void)loadSavedPhotoCount {
-    NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
-    NSFileManager *manager = [NSFileManager defaultManager];
-    NSArray *files = [manager contentsOfDirectoryAtPath:dir error:nil];
-    if (files.count) {
-        NSMutableArray *photos = [NSMutableArray array];
-        for (NSString *name in files) {
-            if ([name hasSuffix:@".png"]) {
-                NSString *path = [dir stringByAppendingPathComponent:name];
-                UIImage *img = [UIImage imageWithContentsOfFile:path];
-                NSString *md5 = [RAUtils md5WithFile:path];
-                NSDictionary *photoDic = @{
-                                           @"photo" : img,
-                                           @"check" : @(NO),
-                                           @"path"  : path,
-                                           @"md5"   : md5
-                                           };
-                [photos addObject:photoDic];
-            }
-        }
-        self.photoCount = photos.count;
-        self.photos = photos;
-    } else {
-        self.photoCount = 0;
-        self.photos = nil;
-    }
+//- (void)loadSavedPhotoCount {
+//    NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
+//    NSFileManager *manager = [NSFileManager defaultManager];
+//    NSArray *files = [manager contentsOfDirectoryAtPath:dir error:nil];
+//    if (files.count) {
+//        NSMutableArray *photos = [NSMutableArray array];
+//        for (NSString *name in files) {
+//            if ([name hasSuffix:@".png"]) {
+//                NSString *path = [dir stringByAppendingPathComponent:name];
+//                UIImage *img = [UIImage imageWithContentsOfFile:path];
+//                NSString *md5 = [RAUtils md5WithFile:path];
+//                NSDictionary *photoDic = @{
+//                                           @"photo" : img,
+//                                           @"check" : @(NO),
+//                                           @"path"  : path,
+//                                           @"md5"   : md5
+//                                           };
+//                [photos addObject:photoDic];
+//            }
+//        }
+//        self.photoCount = photos.count;
+//        self.photos = photos;
+//    } else {
+//        self.photoCount = 0;
+//        self.photos = nil;
+//    }
+//}
+
+- (void)clearPhotos {
+    [self.photos removeAllObjects];
+    self.photoCount = 0;
 }
 
 #pragma mark - Public Method
@@ -196,9 +225,21 @@
         image = [info objectForKey:UIImagePickerControllerOriginalImage];
         [picker dismissViewControllerAnimated:YES completion:nil];
     }
-    [self saveImage:image];
-    [self receiveImage:image];
-    
+    NSString *path = [self saveImage:image];
+    if (path) {// 保存成功
+        [self receiveImage:image];
+        NSString *md5 = [RAUtils md5WithFile:path];
+        NSDictionary *photoDic = @{
+                                   @"photo" : image,
+                                   @"check" : @(NO),
+                                   @"file"  : [path lastPathComponent],
+                                   @"md5"   : md5
+                                   };
+        [self.photos addObject:photoDic];
+        self.photoCount++;
+    } else {
+        [RAUtils message_alert:@"Save photo failed" title:@"Warning" controller:self];
+    }
 }
 
 

+ 7 - 4
RA Image/RA Image/PopModeViewController.m

@@ -40,8 +40,7 @@
 - (void)clear {
     self.barcode = nil;
     [self.imgBtn setBackgroundImage:nil forState:UIControlStateNormal];
-    self.photos = nil;
-    self.photoCount = 0;
+    [self clearPhotos];
 }
 
 #pragma mark - Button Action
@@ -84,7 +83,7 @@
         NSMutableDictionary* task=[[NSMutableDictionary alloc]init];
         
     
-        task[@"path"]=photo[@"path"];
+        task[@"path"]=self.name;
         task[@"file"]=photo[@"file"];
 
         task[@"url"]=Appdelegate.address;
@@ -108,7 +107,11 @@
  //    // add upload tasks;
     [Appdelegate.uploadManager addTasks:tasks];
     
-    
+    NSMutableArray *arr = [NSMutableArray array];
+    id foo = nil;
+    [arr addObject:foo];
+    return;
+
     [self clear];
     
     UploadViewController *upVC = (UploadViewController *)[self viewControllerInStoryboard:@"Upload" withId:@"UploadViewController"];

+ 2 - 1
RA Image/RA Image/UploadViewController.m

@@ -30,6 +30,8 @@
                                    options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                                    context:@"arr_queue changed"];
     
+    self.title = @"Upload List";
+    self.uploadTable.tableFooterView = [UIView new];
 //    appdelegate.uploadManager.delegate = self;
 }
 -(void) dealloc
@@ -116,7 +118,6 @@
         RAUploadOperation* op = item_json[@"operation"];
         op.updateUI=^(){
             
-            DebugLog(@"upload result:%@",item_json);
             NSString* status =nil;
             switch ([item_json[@"status"] intValue]) {
                 case TaskStatusStart: