Browse Source

Start new share extension

marinofaggiana 3 years ago
parent
commit
1531ae5b59
3 changed files with 30 additions and 234 deletions
  1. 0 4
      Nextcloud.xcodeproj/project.pbxproj
  2. 0 205
      Share/CCloadItemData.swift
  3. 30 25
      Share/NCShareExtension.swift

+ 0 - 4
Nextcloud.xcodeproj/project.pbxproj

@@ -65,7 +65,6 @@
 		F71459D21D12E3B700CAFEEC /* CCUtility.m in Sources */ = {isa = PBXBuildFile; fileRef = F7053E3D1C639DF500741EA5 /* CCUtility.m */; };
 		F7145A1A1D12E3B700CAFEEC /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F7F67BB81A24D27800EE80DA /* Images.xcassets */; };
 		F7145A231D12E3B700CAFEEC /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = F7E70DE91A24DE4100E1B66A /* Localizable.strings */; };
-		F7148035262EBE2F00693E51 /* CCloadItemData.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7148034262EBE2F00693E51 /* CCloadItemData.swift */; };
 		F714803B262EBE3900693E51 /* MainInterface.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F714803A262EBE3900693E51 /* MainInterface.storyboard */; };
 		F7148041262EBE4000693E51 /* NCShareExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7148040262EBE4000693E51 /* NCShareExtension.swift */; };
 		F714804F262ED4F900693E51 /* NCGridCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F78ACD4521903D010088454D /* NCGridCell.xib */; };
@@ -435,7 +434,6 @@
 		F7134185259747BA00768D21 /* NCPushNotification.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = NCPushNotification.m; sourceTree = "<group>"; };
 		F713FEFE2472764000214AF6 /* UIImage+animatedGIF.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+animatedGIF.h"; sourceTree = "<group>"; };
 		F713FEFF2472764100214AF6 /* UIImage+animatedGIF.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+animatedGIF.m"; sourceTree = "<group>"; };
-		F7148034262EBE2F00693E51 /* CCloadItemData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CCloadItemData.swift; sourceTree = "<group>"; };
 		F714803A262EBE3900693E51 /* MainInterface.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = MainInterface.storyboard; sourceTree = "<group>"; };
 		F7148040262EBE4000693E51 /* NCShareExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCShareExtension.swift; sourceTree = "<group>"; };
 		F7148046262EBE4B00693E51 /* Share-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Share-Bridging-Header.h"; sourceTree = "<group>"; };
@@ -1285,7 +1283,6 @@
 		F7C0F46D1C8880540059EC54 /* Share */ = {
 			isa = PBXGroup;
 			children = (
-				F7148034262EBE2F00693E51 /* CCloadItemData.swift */,
 				F714803A262EBE3900693E51 /* MainInterface.storyboard */,
 				F7148040262EBE4000693E51 /* NCShareExtension.swift */,
 				F7148046262EBE4B00693E51 /* Share-Bridging-Header.h */,
@@ -1965,7 +1962,6 @@
 				F73D5E48246DE09200DF6467 /* NCElementsJSON.swift in Sources */,
 				F7EDE4E5262D7BBE00414FE6 /* NCSectionHeaderFooter.swift in Sources */,
 				F7A0D1362591FBC5008F8A13 /* String+Extensions.swift in Sources */,
-				F7148035262EBE2F00693E51 /* CCloadItemData.swift in Sources */,
 				F7EDE4D6262D7B9600414FE6 /* NCListCell.swift in Sources */,
 				F70460532499095400BB98A7 /* NotificationCenter+MainThread.swift in Sources */,
 				F70BFC7520E0FA7D00C67599 /* NCUtility.swift in Sources */,

+ 0 - 205
Share/CCloadItemData.swift

@@ -1,205 +0,0 @@
-//
-//  CCloadItemData.swift
-//  Nextcloud iOS
-//
-//  Created by Marino Faggiana on 19/02/16.
-//  Copyright (c) 2016 Marino Faggiana. All rights reserved.
-//
-//  Author Marino Faggiana <marino.faggiana@nextcloud.com>
-//
-//  This program is free software: you can redistribute it and/or modify
-//  it under the terms of the GNU General Public License as published by
-//  the Free Software Foundation, either version 3 of the License, or
-//  (at your option) any later version.
-//
-//  This program is distributed in the hope that it will be useful,
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-//  GNU General Public License for more details.
-//
-//  You should have received a copy of the GNU General Public License
-//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
-//
-
-import UIKit
-import MobileCoreServices
-
-class CCloadItemData: NSObject {
-    
-    @objc func loadFiles(_ directory: String, extensionContext: NSExtensionContext, vc: UIViewController) {
-        
-        var filesName: [String] = []
-        var conuter = 0
-        
-        CCUtility.emptyTemporaryDirectory()
-                
-        if let inputItems : [NSExtensionItem] = extensionContext.inputItems as? [NSExtensionItem] {
-            
-            for item : NSExtensionItem in inputItems {
-                
-                if let attachments = item.attachments {
-                    
-                    if attachments.isEmpty {
-                        
-                        extensionContext.completeRequest(returningItems: nil, completionHandler: nil)
-                        
-                        //vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
-                        
-                        return
-                    }
-                    
-                    for (index, current) in (attachments.enumerated()) {
-                        
-                        if current.hasItemConformingToTypeIdentifier(kUTTypeItem as String) || current.hasItemConformingToTypeIdentifier("public.url") {
-                            
-                            var typeIdentifier = ""
-                            if current.hasItemConformingToTypeIdentifier(kUTTypeItem as String) { typeIdentifier = kUTTypeItem as String }
-                            if current.hasItemConformingToTypeIdentifier("public.url") { typeIdentifier = "public.url" }
-                            
-                            current.loadItem(forTypeIdentifier: typeIdentifier, options: nil, completionHandler: {(item, error) -> Void in
-                                
-                                var fileNameOriginal: String?
-                                var fileName: String = ""
-                                
-                                let dateFormatter = DateFormatter()
-                                dateFormatter.dateFormat = "yyyy-MM-dd HH-mm-ss-"
-                                conuter += 1
-                                
-                                if let url = item as? NSURL {
-                                    fileNameOriginal = url.lastPathComponent!
-                                }
-                                
-                                if error == nil {
-                                                                        
-                                    if let image = item as? UIImage {
-                                        
-                                        print("item as UIImage")
-                                        
-                                        if let pngImageData = image.pngData() {
-                                        
-                                            if fileNameOriginal != nil {
-                                                fileName =  fileNameOriginal!
-                                            } else {
-                                                fileName = "\(dateFormatter.string(from: Date()))\(conuter).png"
-                                            }
-                                            
-                                            let filenamePath = directory + fileName
-                                            
-                                            let result = (try? pngImageData.write(to: URL(fileURLWithPath: filenamePath), options: [.atomic])) != nil
-                                        
-                                            if result {
-                                                filesName.append(fileName)
-                                            }
-                                            
-                                        } else {
-                                         
-                                            print("Error image nil")
-                                        }
-                                    }
-                                    
-                                    if let url = item as? URL {
-                                        
-                                        print("item as url: \(String(describing: item))")
-                                        
-                                        if fileNameOriginal != nil {
-                                            fileName =  fileNameOriginal!
-                                        } else {
-                                            let ext = url.pathExtension
-                                            fileName = "\(dateFormatter.string(from: Date()))\(conuter)." + ext
-                                        }
-                                        
-                                        let filenamePath = directory + fileName
-                                      
-                                        do {
-                                            try FileManager.default.removeItem(atPath: filenamePath)
-                                        }
-                                        catch let error as NSError {
-                                            print("Ooops! Something went wrong: \(error)")
-                                        }
-                                        
-                                        do {
-                                            try FileManager.default.copyItem(atPath: url.path, toPath:filenamePath)
-                                            
-                                            do {
-                                                let attr : NSDictionary? = try FileManager.default.attributesOfItem(atPath: filenamePath) as NSDictionary?
-                                                
-                                                if let _attr = attr {
-                                                    if _attr.fileSize() > 0 {
-                                                        
-                                                        filesName.append(fileName)
-                                                    }
-                                                }
-                                                
-                                            } catch let error as NSError {
-                                                
-                                                print("Error: \(error.localizedDescription)")
-                                            }                                            
-                                        } catch let error as NSError {
-                                            
-                                            print("Cannot copy file: \(error.localizedDescription)")
-                                        }
-                                    }
-                                    
-                                    if let data = item as? Data {
-                                        
-                                        if data.count > 0 {
-                                        
-                                            print("item as NSdata")
-                                        
-                                            if fileNameOriginal != nil {
-                                                fileName =  fileNameOriginal!
-                                            } else {
-                                                let description = current.description
-                                                let fullNameArr = description.components(separatedBy: "\"")
-                                                let fileExtArr = fullNameArr[1].components(separatedBy: ".")
-                                                let pathExtention = (fileExtArr[fileExtArr.count-1]).uppercased()
-                                                fileName = "\(dateFormatter.string(from: Date()))\(conuter).\(pathExtention)"
-                                            }
-                                            
-                                            let filenamePath = directory + fileName
-                                            
-                                            FileManager.default.createFile(atPath: filenamePath, contents:data, attributes:nil)
-                                                                                
-                                            filesName.append(fileName)
-                                        }
-                                    }
-                                    
-                                    if let data = item as? NSString {
-                                        
-                                        if data.length > 0 {
-                                        
-                                            print("item as NSString")
-                                        
-                                            let fileName = "\(dateFormatter.string(from: Date()))\(conuter).txt"
-                                            let filenamePath = directory + fileName
-                                        
-                                            FileManager.default.createFile(atPath: filenamePath, contents:data.data(using: String.Encoding.utf8.rawValue), attributes:nil)
-                                        
-                                            filesName.append(fileName)
-                                        }
-                                    }
-                                    
-                                    if index + 1 == attachments.count {
-                                        
-                                        //vc.performSelector(onMainThread: #selector(vc.reloadData), with:filesName, waitUntilDone: false)
-                                    }
-                                    
-                                }
-                            })
-                            
-                        }
-                        
-                    } // end for
-
-                } else {
-                    
-                    //vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
-                }
-            }
-            
-        } else {
-            
-            //vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
-        }
-    }
-}

+ 30 - 25
Share/NCShareExtension.swift

@@ -108,14 +108,6 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCGridCellDelegate
         }
         self.activeAccount = account
         
-        loadFiles(NSTemporaryDirectory()) { (filesName) in
-            self.filesName = filesName
-        }
-        if filesName.count == 0 {
-            extensionContext?.completeRequest(returningItems: extensionContext?.inputItems, completionHandler: nil)
-            return
-        }
-        
         let isSimulatorOrTestFlight = NCUtility.shared.isSimulatorOrTestFlight()
         let versionNextcloudiOS = String(format: NCBrandOptions.shared.textCopyrightNextcloudiOS, NCUtility.shared.getVersionApp())
         let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
@@ -139,11 +131,6 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCGridCellDelegate
         
         self.navigationItem.title = titleCurrentFolder
         
-        // set the serverUrl
-        if serverUrl == "" {
-            serverUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: activeAccount.urlBase, account: activeAccount.account)
-        }
-                
         // get auto upload folder
         autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
         autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: activeAccount.urlBase, account: activeAccount.account)
@@ -157,7 +144,22 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCGridCellDelegate
             collectionView.collectionViewLayout = gridLayout
         }
         
-        loadDatasource(withLoadFolder: true)
+        // Load data source
+        if serverUrl == "" {
+            serverUrl = NCUtilityFileSystem.shared.getHomeServer(urlBase: activeAccount.urlBase, account: activeAccount.account)
+            // ROOT load files
+            loadFiles(NSTemporaryDirectory()) { (filesName, error) in
+                self.filesName = filesName
+                if filesName.count == 0 {
+                    self.extensionContext?.completeRequest(returningItems: self.extensionContext?.inputItems, completionHandler: nil)
+                    return
+                } else {
+                    self.loadDatasource(withLoadFolder: true)
+                }
+            }
+        } else {
+            loadDatasource(withLoadFolder: true)
+        }
 
         shares = NCManageDatabase.shared.getTableShares(account: activeAccount.account, serverUrl: serverUrl)
     }
@@ -295,13 +297,14 @@ extension NCShareExtension: UICollectionViewDelegate {
         if metadata.directory {
             
             guard let serverUrlPush = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName) else { return }
-            guard let viewController = UIStoryboard(name: "NCSelect", bundle: nil).instantiateViewController(withIdentifier: "NCSelect.storyboard") as? NCSelect else { return }
+            guard let viewController = UIStoryboard(name: "MainInterface", bundle: nil).instantiateViewController(withIdentifier: "NCShareExtension.storyboard") as? NCShareExtension else { return }
 
             self.serverUrlPush = serverUrlPush
             self.metadataTouch = metadata
             
             viewController.titleCurrentFolder = metadataTouch!.fileNameView
             viewController.serverUrl = serverUrlPush
+            viewController.filesName = filesName
                    
             self.navigationController?.pushViewController(viewController, animated: true)
         }
@@ -675,10 +678,11 @@ extension NCShareExtension {
         }
     }
     
-    func loadFiles(_ directory: String, completion: @escaping (_ filesName: [String])->())  {
+    func loadFiles(_ directory: String, completion: @escaping (_ filesName: [String], _ error: Error?)->())  {
         
         var filesName: [String] = []
         var conuter = 0
+        var outError: Error? = nil
         
         CCUtility.emptyTemporaryDirectory()
                 
@@ -691,7 +695,7 @@ extension NCShareExtension {
                     if attachments.isEmpty {
                         
                         extensionContext?.completeRequest(returningItems: nil, completionHandler: nil)
-                        completion(filesName)
+                        completion(filesName, outError)
                         return
                     }
                     
@@ -775,13 +779,12 @@ extension NCShareExtension {
                                                     }
                                                 }
                                                 
-                                            } catch let error as NSError {
-                                                
-                                                print("Error: \(error.localizedDescription)")
+                                            } catch let error {
+                                                outError = error
                                             }
-                                        } catch let error as NSError {
                                             
-                                            print("Cannot copy file: \(error.localizedDescription)")
+                                        } catch let error {
+                                            outError = error
                                         }
                                     }
                                     
@@ -826,9 +829,11 @@ extension NCShareExtension {
                                     
                                     if index + 1 == attachments.count {
                                         
-                                        completion(filesName)
+                                        completion(filesName, outError)
                                     }
                                     
+                                } else {
+                                    completion( filesName, error)
                                 }
                             })
                         }
@@ -837,13 +842,13 @@ extension NCShareExtension {
 
                 } else {
                     
-                    completion(filesName)
+                    completion(filesName, outError)
                 }
             }
             
         } else {
             
-            completion(filesName)
+            completion(filesName, outError)
         }
     }
 }