Переглянути джерело

create algo for select directory as OnDevice #706

Marino Faggiana 6 роки тому
батько
коміт
a8f5bf7288

+ 32 - 10
iOSClient/Main/CCMain.m

@@ -3482,13 +3482,24 @@
     if (self.metadata.directory) {
         
         BOOL lockDirectory = NO;
+        BOOL optionOnDevice = NO;
         NSString *dirServerUrl = [CCUtility stringAppendServerUrl:serverUrl addFileName:self.metadata.fileName];
+        NSString *firstServerUrl = [CCUtility firtsPathComponentFromServerUrl:dirServerUrl activeUrl:appDelegate.activeUrl];
         BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:[NSString stringWithFormat:@"%@/%@", self.serverUrl, self.metadata.fileName] account:appDelegate.activeAccount];
         
         // Directory bloccata ?
-        tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, dirServerUrl]];
-        
-        if (directory.lock && [[CCUtility getBlockCode] length] && appDelegate.sessionePasscodeLock == nil) lockDirectory = YES;
+        tableDirectory *directoryForLock = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", appDelegate.activeAccount, dirServerUrl]];
+        if (directoryForLock.lock && [[CCUtility getBlockCode] length] && appDelegate.sessionePasscodeLock == nil) lockDirectory = YES;
+        
+        // Directory set as offline ?
+        tableDirectory *directoryOnDevice = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl CONTAINS %@ AND onDevice == true", appDelegate.activeAccount, firstServerUrl]];
+        if (directoryOnDevice == nil) {
+            optionOnDevice = YES;
+        } else if (directoryOnDevice.serverUrl.length == dirServerUrl.length) {
+            optionOnDevice = YES;
+        } else if (directoryOnDevice.serverUrl.length > dirServerUrl.length) {
+            optionOnDevice = YES;
+        }
         
         [actionSheet addButtonWithTitle:self.metadata.fileNameView
                                   image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"folder"] multiplier:2 color:[NCBrandColor sharedInstance].brandElement]
@@ -3570,12 +3581,17 @@
                                     }];
         }
         
-        if (!lockDirectory && !isFolderEncrypted) {
+        if (!lockDirectory && !isFolderEncrypted && optionOnDevice) {
             
-            NSString *title, *serverUrl = [CCUtility stringAppendServerUrl:_serverUrl addFileName:self.metadata.fileName];
-            tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND onDevice == true", appDelegate.activeAccount, serverUrl]];
-            if (directory == nil) { title = NSLocalizedString(@"_set_available_offline_", nil); }
-            else { title = NSLocalizedString(@"_remove_available_offline_", nil); }
+            NSString *title;
+            
+            if (directoryOnDevice == nil) {
+                title = NSLocalizedString(@"_set_available_offline_", nil);
+            } else if (directoryOnDevice.serverUrl.length == dirServerUrl.length) {
+                title = NSLocalizedString(@"_remove_available_offline_", nil);
+            } else if (directoryOnDevice.serverUrl.length > dirServerUrl.length) {
+                title = NSLocalizedString(@"_set_available_offline_", nil);
+            }
             
             [actionSheet addButtonWithTitle:title
                                       image:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"onDevice"] multiplier:2 color:[NCBrandColor sharedInstance].icon]
@@ -3583,8 +3599,14 @@
                                      height:50.0
                                        type:AHKActionSheetButtonTypeDefault
                                     handler:^(AHKActionSheet *as) {
-                                        if (directory == nil) { [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:serverUrl onDevice:true];
-                                        } else { [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:serverUrl onDevice:false]; }
+                                        if (directoryOnDevice == nil) {
+                                            [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:dirServerUrl onDevice:true];
+                                        } else if (directoryOnDevice.serverUrl.length == dirServerUrl.length) {
+                                            [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:dirServerUrl onDevice:false];
+                                        } else if (directoryOnDevice.serverUrl.length > dirServerUrl.length) {
+                                            [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:directoryOnDevice.serverUrl onDevice:false];
+                                            [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:dirServerUrl onDevice:true];
+                                        }
                                     }];
         }
         

+ 1 - 2
iOSClient/OnDevice/NCOnDevice.swift

@@ -326,8 +326,7 @@ class NCOnDevice: UIViewController ,UICollectionViewDataSource, UICollectionView
     @objc func loadDatasource(withSynchronized: Bool = false) {
         
         let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND onDevice == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true)
-        
-        let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@", appDelegate.activeAccount), sorted: "fileNameView", ascending: true)
+        let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@", appDelegate.activeAccount), sorted: "fileName", ascending: true)
         
         collectionView.reloadData()
     }

+ 1 - 0
iOSClient/Utility/CCUtility.h

@@ -196,6 +196,7 @@
 + (void)removeAllFileID_UPLOAD_ActiveUser:(NSString *)activeUser activeUrl:(NSString *)activeUrl;
 
 + (NSString *)deletingLastPathComponentFromServerUrl:(NSString *)serverUrl;
++ (NSString *)firtsPathComponentFromServerUrl:(NSString *)serverUrl activeUrl:(NSString *)activeUrl;
 + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl activeUrl:(NSString *)activeUrl;
 
 + (NSArray *)createNameSubFolder:(PHFetchResult *)assets;

+ 16 - 2
iOSClient/Utility/CCUtility.m

@@ -1016,8 +1016,6 @@
 
 + (NSString *)deletingLastPathComponentFromServerUrl:(NSString *)serverUrl
 {
-    //NSURL *url = [[NSURL URLWithString:[serverUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] URLByDeletingLastPathComponent]; DEPRECATED iOS9
-    
     NSURL *url = [[NSURL URLWithString:[serverUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]]] URLByDeletingLastPathComponent];
     
     NSString *pather = [[url absoluteString] stringByRemovingPercentEncoding];
@@ -1025,6 +1023,22 @@
     return [pather substringToIndex: [pather length] - 1];
 }
 
++ (NSString *)firtsPathComponentFromServerUrl:(NSString *)serverUrl activeUrl:(NSString *)activeUrl
+{
+    NSString *firstPath = serverUrl;
+
+    NSURL *serverUrlURL = [NSURL URLWithString:serverUrl];
+    NSURL *activeUrlURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@/", activeUrl, k_webDAV]];
+    
+    while ([[serverUrlURL absoluteString] isEqualToString:[activeUrlURL absoluteString]] == false) {
+        firstPath = [serverUrlURL absoluteString];
+        serverUrlURL = [serverUrlURL URLByDeletingLastPathComponent];
+    }
+    
+    if ([firstPath hasSuffix:@"/"]) firstPath = [firstPath substringToIndex:[firstPath length] - 1];
+    return firstPath;
+}
+
 + (NSString *)returnFileNamePathFromFileName:(NSString *)metadataFileName serverUrl:(NSString *)serverUrl activeUrl:(NSString *)activeUrl
 {
     NSString *fileName = [NSString stringWithFormat:@"%@/%@", [serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:activeUrl] withString:@""], metadataFileName];