Marino Faggiana 8 år sedan
förälder
incheckning
8309cc24e7
1 ändrade filer med 64 tillägg och 6 borttagningar
  1. 64 6
      Picker/DocumentPickerViewController.swift

+ 64 - 6
Picker/DocumentPickerViewController.swift

@@ -23,7 +23,7 @@
 
 import UIKit
 
-class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCNetworkingDelegate {
+class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCNetworkingDelegate, OCNetworkingDelegate {
     
     // MARK: - Properties
     
@@ -53,6 +53,8 @@ class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCN
         return queue
     }()
     
+    var hud : CCHud!
+    
     // MARK: - IBOutlets
     
     @IBOutlet weak var tableView: UITableView!
@@ -95,23 +97,79 @@ class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCN
             return
         }
         
+        // init Object
         CCNetworking.shared().settingDelegate(self)
+        hud = CCHud.init(view: self.navigationController?.view)
         
         // COLOR_SEPARATOR_TABLE
         self.tableView.separatorColor = UIColor(colorLiteralRed: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 0.2)
+        
+        readFolder()
+    }
+    
+    func readFolder() {
+        
+        let metadataNet = CCMetadataNet()
+        
+        metadataNet.action = actionReadFolder
+        metadataNet.serverUrl = self.localServerUrl;
+        metadataNet.selector = selectorReadFolder;
+        metadataNet.date = nil;
+        
+        let oc : OCnetworking = OCnetworking.init(delegate: self, metadataNet: metadataNet, withUser:activeUser , withPassword: activePassword, withUrl: activeUrl, withTypeCloud: typeCloud, oneByOne: true, activityIndicator: false)
+        networkingOperationQueue.addOperation(oc)
+        
+        hud.visibleIndeterminateHud()
     }
     
-    override func viewWillAppear(_ animated: Bool) {
+    func readFolderFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
+        
+        hud.hideHud()
         
-        super.viewWillAppear(animated)
+        print ("error")
+    }
+    
+    func readFolderSuccess(_ metadataNet: CCMetadataNet!, permissions: String!, rev: String!, metadatas: [Any]!) {
         
-        let directoryID : String? = CCCoreData.getDirectoryID(fromServerUrl: localServerUrl!, activeAccount: activeAccount!)
-        let predicate = NSPredicate(format: "(account == %@) AND (directoryID == %@)", activeAccount!, directoryID!)
+        // remove all record
+        let predicate = NSPredicate(format: "(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == ''))", activeAccount!, metadataNet.directoryID)
+        CCCoreData.deleteMetadata(with: predicate)
         
-        recordsTableMetadata = CCCoreData.getTableMetadata(with: predicate, fieldOrder: CCUtility.getOrderSettings()!, ascending: CCUtility.getAscendingSettings()) as? [TableMetadata]
+        for metadata in metadatas as! [CCMetadata] {
+            
+            // do not insert crypto file
+            if CCUtility.isCryptoString(metadata.fileName) {
+                continue
+            }
+            
+            // plist + crypto = completed ?
+            if CCUtility.isCryptoPlistString(metadata.fileName) && metadata.directory == false {
+                
+                var isCryptoComplete = false
                 
+                for completeMetadata in metadatas as! [CCMetadata] {
+                    if completeMetadata.fileName == CCUtility.trasformedFileNamePlist(inCrypto: metadata.fileName) {
+                        isCryptoComplete = true
+                    }
+                }
+
+                if isCryptoComplete == false {
+                    continue
+                }
+            }
+            
+            // Add record
+            CCCoreData.add(metadata, activeAccount: activeAccount, activeUrl: activeUrl, typeCloud: typeCloud, context: nil)
+        }
+        
+        // Get Datasource
+        recordsTableMetadata = CCCoreData.getTableMetadata(with: NSPredicate(format: "(account == %@) AND (directoryID == %@)", activeAccount!, metadataNet.directoryID), fieldOrder: CCUtility.getOrderSettings()!, ascending: CCUtility.getAscendingSettings()) as? [TableMetadata]
+        
         tableView.reloadData()
+        
+        hud.hideHud()
     }
+    
 }
 
 // MARK: - UITableViewDelegate