Browse Source

remake favorite

marinofaggiana 5 years ago
parent
commit
2b6454c622

+ 1 - 1
iOSClient/CCGlobal.h

@@ -340,7 +340,7 @@
 #define k_notificationCenter_deleteFile                 @"deleteFile"
 #define k_notificationCenter_renameFile                 @"renameFile"
 #define k_notificationCenter_moveFile                   @"moveFile"
-
+#define k_notificationCenter_favoriteFile               @"favoriteFile"
 #define k_notificationCenter_menuSearchTextPDF          @"menuSearchTextPDF"
 #define k_notificationCenter_menuDownloadImage          @"menuDownloadImage"
 #define k_notificationCenter_menuSaveLivePhoto          @"menuSaveLivePhoto"

+ 0 - 1
iOSClient/Main/CCMain.h

@@ -95,7 +95,6 @@
 - (void)downloadSelectedFilesFolders;
 - (void)moveOpenWindow:(NSArray *)indexPaths;
 
-- (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite;
 - (void)minCharTextFieldDidChange:(UITextField *)sender;
 - (void)renameFile:(NSArray *)arguments;
 - (void)comandoLockPassword;

+ 48 - 60
iOSClient/Main/CCMain.m

@@ -130,6 +130,7 @@
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setTitle) name:k_notificationCenter_setTitleMain object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(triggerProgressTask:) name:k_notificationCenter_progressTask object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteFile:) name:k_notificationCenter_deleteFile object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(favoriteFile:) name:k_notificationCenter_favoriteFile object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:k_notificationCenter_changeTheming object:nil];
     
     // Search
@@ -453,6 +454,52 @@
     }
 }
 
+- (void)favoriteFile:(NSNotification *)notification
+{
+    if (self.view.window == nil) { return; }
+    
+    NSDictionary *userInfo = notification.userInfo;
+    tableMetadata *metadata = userInfo[@"metadata"];
+    NSInteger errorCode = [userInfo[@"errorCode"] integerValue];
+    BOOL favorite = [userInfo[@"favorite"] boolValue];
+    
+    if (errorCode == 0) {
+        _dateReadDataSource = nil;
+        if (self.searchController.isActive) {
+            [self readFolder:self.serverUrl];
+        } else {
+            [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.serverUrl ocId:metadata.ocId action:k_action_MOD];
+        }
+        
+        if (metadata.directory && favorite) {
+                       
+            NSString *selector;
+                       
+            if ([CCUtility getFavoriteOffline])
+                selector = selectorReadFolderWithDownload;
+            else
+                selector = selectorReadFolder;
+                       
+            [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:self.serverUrl addFileName:metadata.fileName] selector:selector account:appDelegate.activeAccount];
+        }
+                   
+        if (!metadata.directory && favorite && [CCUtility getFavoriteOffline]) {
+                       
+            metadata.favorite = favorite;
+            metadata.session = k_download_session;
+            metadata.sessionError = @"";
+            metadata.sessionSelector = selectorDownloadSynchronize;
+            metadata.status = k_metadataStatusWaitDownload;
+                           
+            // Add Metadata for Download
+            [[NCManageDatabase sharedInstance] addMetadata:metadata];
+            [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.serverUrl ocId:metadata.ocId action:k_action_MOD];
+                    
+            [appDelegate startLoadAutoDownloadUpload];
+        }
+    }
+}
+
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ==== DZNEmptyDataSetSource ====
 #pragma --------------------------------------------------------------------------------------------
@@ -1990,62 +2037,6 @@
     }
 }
 
-#pragma --------------------------------------------------------------------------------------------
-#pragma mark ===== Favorite =====
-#pragma --------------------------------------------------------------------------------------------
-
-- (void)settingFavorite:(tableMetadata *)metadata favorite:(BOOL)favorite
-{
-    NSString *fileNameServerUrl = [CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:self.serverUrl activeUrl:appDelegate.activeUrl];
-    
-    [[NCCommunication sharedInstance] setFavoriteWithServerUrl:appDelegate.activeUrl fileName:fileNameServerUrl favorite:favorite account:appDelegate.activeAccount completionHandler:^(NSString *account, NSInteger errorCode, NSString *errorDecription) {
-        
-        if (errorCode == 0 && [appDelegate.activeAccount isEqualToString:account]) {
-            
-            [[NCManageDatabase sharedInstance] setMetadataFavoriteWithOcId:metadata.ocId favorite:favorite];
-
-            _dateReadDataSource = nil;
-            if (self.searchController.isActive)
-                [self readFolder:self.serverUrl];
-            else
-                [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.serverUrl ocId:metadata.ocId action:k_action_MOD];
-            
-            if (metadata.directory && favorite) {
-                
-                NSString *selector;
-                
-                if ([CCUtility getFavoriteOffline])
-                    selector = selectorReadFolderWithDownload;
-                else
-                    selector = selectorReadFolder;
-                
-                [[CCSynchronize sharedSynchronize] readFolder:[CCUtility stringAppendServerUrl:self.serverUrl addFileName:metadata.fileName] selector:selector account:appDelegate.activeAccount];
-            }
-            
-            if (!metadata.directory && favorite && [CCUtility getFavoriteOffline]) {
-                
-                metadata.favorite = favorite;
-                metadata.session = k_download_session;
-                metadata.sessionError = @"";
-                metadata.sessionSelector = selectorDownloadSynchronize;
-                metadata.status = k_metadataStatusWaitDownload;
-                    
-                // Add Metadata for Download
-                [[NCManageDatabase sharedInstance] addMetadata:metadata];
-                [[NCMainCommon sharedInstance] reloadDatasourceWithServerUrl:self.serverUrl ocId:metadata.ocId action:k_action_MOD];
-                
-                [appDelegate startLoadAutoDownloadUpload];
-            }
-            
-        } else if (errorCode != 0) {
-            [[NCContentPresenter shared] messageNotification:@"_error_" description:errorDecription delay:k_dismissAfterSecond type:messageTypeError errorCode:errorCode];
-        } else {
-            NSLog(@"[LOG] It has been changed user during networking process, error.");
-        }
-        
-    }];
-}
-
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ==== Menu LOGO ====
 #pragma --------------------------------------------------------------------------------------------
@@ -2645,10 +2636,7 @@
     }
     
     if (direction == MGSwipeDirectionLeftToRight) {
-        if (self.metadata.favorite)
-            [self settingFavorite:self.metadata favorite:NO];
-        else
-            [self settingFavorite:self.metadata favorite:YES];
+        [[NCNetworking sharedInstance] favoriteMetadata:self.metadata url:appDelegate.activeUrl completion:^(NSInteger errorCode, NSString *errorDescription) { }];
     }
     
     return YES;

+ 2 - 2
iOSClient/Main/Menu/CCMain+Menu.swift

@@ -229,7 +229,7 @@ extension CCMain {
                     title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
                     icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
                     action: { menuAction in
-                        self.settingFavorite(metadata, favorite: !metadata.favorite)
+                        NCNetworking.sharedInstance.favoriteMetadata(metadata, url: appDelegate.activeUrl) { (errorCode, errorDescription) in }
                     }
                 )
             )
@@ -382,7 +382,7 @@ extension CCMain {
                     title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
                     icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
                     action: { menuAction in
-                        self.settingFavorite(metadata, favorite: !metadata.favorite)
+                        NCNetworking.sharedInstance.favoriteMetadata(metadata, url: appDelegate.activeUrl) { (errorCode, errorDescription) in }
                     }
                 )
             )

+ 13 - 1
iOSClient/Main/Menu/NCDetailNavigationController+Menu.swift

@@ -30,7 +30,19 @@ extension NCDetailNavigationController {
         var actions = [NCMenuAction]()
         let fileNameExtension = (metadata.fileNameView as NSString).pathExtension.uppercased()
         let directEditingCreators = NCManageDatabase.sharedInstance.getDirectEditingCreators(account: appDelegate.activeAccount)
-
+        
+        var titleFavorite = NSLocalizedString("_add_favorites_", comment: "")
+        if metadata.favorite { titleFavorite = NSLocalizedString("_remove_favorites_", comment: "") }
+        actions.append(
+            NCMenuAction(
+                title: titleFavorite,
+                icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
+                action: { menuAction in
+                    
+                }
+            )
+        )
+        
         actions.append(
             NCMenuAction(
                 title: NSLocalizedString("_details_", comment: ""),

+ 29 - 0
iOSClient/Networking/NCNetworking.swift

@@ -336,4 +336,33 @@ import NCCommunication
             }
         }
     }
+    
+    @objc func favoriteMetadata(_ metadata: tableMetadata, url: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
+        
+        let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: url)!
+        var favorite = true
+        if metadata.favorite { favorite = false }
+        
+        NCCommunication.sharedInstance.setFavorite(serverUrl: url, fileName: fileName, favorite: favorite, account: metadata.account) { (account, errorCode, errorDescription) in
+            
+            var description = ""
+            if errorDescription != nil { description = errorDescription! }
+            
+            if errorCode == 0 && metadata.account == account {
+                
+                NCManageDatabase.sharedInstance.setMetadataFavorite(ocId: metadata.ocId, favorite: favorite)
+                
+            } else if (errorCode != 0) {
+                NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
+            } else {
+                print("[LOG] It has been changed user during networking process, error.")
+
+            }
+            
+            let userInfo: [String : Any] = ["metadata": metadata, "errorCode": Int(errorCode), "errorDescription": description, "favorite": Bool(favorite)]
+            NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_favoriteFile), object: nil, userInfo: userInfo)
+            
+            completion(errorCode, description)
+        }
+    }
 }