Marino Faggiana 7 年之前
父節點
當前提交
f56dca4ebf
共有 2 個文件被更改,包括 39 次插入72 次删除
  1. 28 69
      iOSClient/AutoUpload/NCAutoUpload.m
  2. 11 3
      iOSClient/Database/NCManageDatabase.swift

+ 28 - 69
iOSClient/AutoUpload/NCAutoUpload.m

@@ -350,17 +350,7 @@
     //[self getCameraRollNewItemsWithDatePhotoTEST:[NSDate distantPast] dateVideo:[NSDate distantPast] account:account];
     
     // Check Asset : NEW or FULL
-    if (assetsFull) {
-        
-        newAssetToUpload = [self getCameraRollNewItemsWithDatePhoto:[NSDate distantPast] dateVideo:[NSDate distantPast] account:account];
-        
-    } else {
-        
-        NSDate *databaseDatePhoto = account.autoUploadDatePhoto;
-        NSDate *databaseDateVideo = account.autoUploadDateVideo;
-        
-        newAssetToUpload = [self getCameraRollNewItemsWithDatePhoto:databaseDatePhoto dateVideo:databaseDateVideo account:account];
-    }
+    newAssetToUpload = [self getCameraRollAssets:account assetsFull:assetsFull];
     
     // News Assets ? if no verify if blocked Table Auto Upload -> Autostart
     if ([newAssetToUpload count] == 0) {
@@ -665,93 +655,62 @@
 #pragma mark ===== get Camera Roll new Asset ====
 #pragma --------------------------------------------------------------------------------------------
 
-- (PHFetchResult *)getCameraRollNewItemsWithDatePhotoOLD:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo account:(tableAccount *)account
+- (PHFetchResult *)getCameraRollAssets:(tableAccount *)account assetsFull:(BOOL)assetsFull
 {
     @synchronized(self) {
- 
+        
         if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
             
             PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
-                    
-            NSPredicate *predicateImage = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage], [NSPredicate predicateWithFormat:@"creationDate > %@", datePhoto]]];
             
-            NSPredicate *predicateVideo = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeVideo], [NSPredicate predicateWithFormat:@"creationDate > %@", dateVideo]]];
-                    
+            NSPredicate *predicateImage = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage];
+            NSPredicate *predicateVideo = [NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeVideo];
             NSPredicate *predicate;
+
+            BOOL image = NO;
+            BOOL video = NO;
+            
+            NSMutableArray *newAssets =[NSMutableArray new];
             
             if (account.autoUploadPhoto && account.autoUploadVideo) {
                 
                 predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateImage, predicateVideo]];
+                image = YES; video = YES;
                 
             } else if (account.autoUploadPhoto) {
                 
                 predicate = predicateImage;
+                image = YES;
                 
             } else if (account.autoUploadVideo) {
                 
                 predicate = predicateVideo;
+                video = YES;
             }
-                    
-            PHFetchOptions *newInstantUploadAssetsFetchOptions = [PHFetchOptions new];
-            newInstantUploadAssetsFetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
-            newInstantUploadAssetsFetchOptions.predicate = predicate;
             
-            PHAssetCollection *collection = result[0];
-            
-            PHFetchResult *newAssetToUpload = [PHAsset fetchAssetsInAssetCollection:collection options:newInstantUploadAssetsFetchOptions];
+            PHFetchOptions *fetchOptions = [PHFetchOptions new];
+            fetchOptions.predicate = predicate;
             
-            return newAssetToUpload;
-            
-        } else {
-            
-            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_access_photo_not_enabled_", nil) message:NSLocalizedString(@"_access_photo_not_enabled_msg_", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"_ok_", nil) otherButtonTitles:nil];
-            [alert show];
-        }
-    }
-    
-    return nil;
-}
-
-- (PHFetchResult *)getCameraRollNewItemsWithDatePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo account:(tableAccount *)account
-{
-    @synchronized(self) {
-        
-        if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusAuthorized) {
+            PHAssetCollection *collection = result[0];
             
-            PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
+            PHFetchResult *assets = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];
             
-            NSPredicate *predicateImage = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeImage], [NSPredicate predicateWithFormat:@"modificationDate > %@", datePhoto]]];
-            NSPredicate *predicateVideo = [NSCompoundPredicate andPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"mediaType = %i", PHAssetMediaTypeVideo], [NSPredicate predicateWithFormat:@"modificationDate > %@", dateVideo]]];
+            if (assetsFull == NO) {
             
-            NSPredicate *predicate;
-            
-            if (account.autoUploadPhoto && account.autoUploadVideo) {
-                
-                predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicateImage, predicateVideo]];
-                
-            } else if (account.autoUploadPhoto) {
+                NSArray *assetsLocalIdentifier = [[NCManageDatabase sharedInstance] getPhotoLibraryWithImage:image video:video];
                 
-                predicate = predicateImage;
+                for (PHAsset *asset in assets) {
+                    
+                    if (![assetsLocalIdentifier containsObject: asset.localIdentifier])
+                        [newAssets addObject:asset];
+                }
                 
-            } else if (account.autoUploadVideo) {
+                return (PHFetchResult *)newAssets;
                 
-                predicate = predicateVideo;
-            }
-            
-            PHFetchOptions *newInstantUploadAssetsFetchOptions = [PHFetchOptions new];
-            newInstantUploadAssetsFetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"modificationDate" ascending:YES]];
-            newInstantUploadAssetsFetchOptions.predicate = predicate;
-            
-            PHAssetCollection *collection = result[0];
-            
-            PHFetchResult *newAssetToUpload = [PHAsset fetchAssetsInAssetCollection:collection options:newInstantUploadAssetsFetchOptions];
-
-#ifdef DEBUG
-            for (PHAsset *asset in newAssetToUpload)
-                NSLog(@"%@ > %@ - %@", asset.modificationDate, datePhoto, asset);
-#endif
+            } else {
             
-            return newAssetToUpload;
+                return assets;
+            }
             
         } else {
             

+ 11 - 3
iOSClient/Database/NCManageDatabase.swift

@@ -1903,7 +1903,7 @@ class NCManageDatabase: NSObject {
         }
     }
     
-    func getPhotoLibrary(image: Bool, video: Bool) -> [tablePhotoLibrary]? {
+    func getPhotoLibrary(image: Bool, video: Bool) -> [String]? {
         
         let tableAccount = self.getAccountActive()
         if tableAccount == nil {
@@ -1922,14 +1922,22 @@ class NCManageDatabase: NSObject {
             
             predicate = NSPredicate(format: "account = %@ AND mediaType = 0", tableAccount!.account)
 
-        } else {
+        } else if (video) {
             
             predicate = NSPredicate(format: "account = %@ AND mediaType = 1", tableAccount!.account)
         }
         
         let results = realm.objects(tablePhotoLibrary.self).filter(predicate)
         
-        return Array(results)
+        // Get all assetLocalIdentifier
+        var assetsLocalIdentifier = [String]()
+        
+        for table in results {
+            
+            assetsLocalIdentifier.append(table.assetLocalIdentifier)
+        }
+
+        return assetsLocalIdentifier
     }
 
     //MARK: -