Marino Faggiana 6 years ago
parent
commit
d388727b47

+ 2 - 2
iOSClient/Activity/NCActivity.storyboard

@@ -58,7 +58,7 @@
                                                     </constraints>
                                                 </imageView>
                                                 <collectionView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" dataMode="prototypes" translatesAutoresizingMaskIntoConstraints="NO" id="KpO-no-BMl">
-                                                    <rect key="frame" x="0.0" y="39.666666666666657" width="375" height="50"/>
+                                                    <rect key="frame" x="50" y="39.666666666666657" width="325" height="50"/>
                                                     <color key="backgroundColor" red="0.59999999999999998" green="0.80000000000000004" blue="0.0" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                                                     <constraints>
                                                         <constraint firstAttribute="height" constant="50" id="deQ-wc-jNT"/>
@@ -99,7 +99,7 @@
                                                 <constraint firstAttribute="bottom" secondItem="KpO-no-BMl" secondAttribute="bottom" id="ULe-Tt-dBj"/>
                                                 <constraint firstItem="LQ8-cO-794" firstAttribute="leading" secondItem="xNG-sf-PnA" secondAttribute="trailing" constant="10" id="aqp-Wu-9Hk"/>
                                                 <constraint firstItem="fcO-YL-MuT" firstAttribute="top" secondItem="i35-U4-bEk" secondAttribute="top" id="faC-by-km5"/>
-                                                <constraint firstItem="KpO-no-BMl" firstAttribute="leading" secondItem="i35-U4-bEk" secondAttribute="leading" id="l0Y-89-eTm"/>
+                                                <constraint firstItem="KpO-no-BMl" firstAttribute="leading" secondItem="i35-U4-bEk" secondAttribute="leading" constant="50" id="l0Y-89-eTm"/>
                                                 <constraint firstAttribute="trailing" secondItem="KpO-no-BMl" secondAttribute="trailing" id="vWj-9H-JLc"/>
                                                 <constraint firstItem="KpO-no-BMl" firstAttribute="top" secondItem="fcO-YL-MuT" secondAttribute="bottom" constant="20" id="z1e-vv-qJb"/>
                                             </constraints>

+ 65 - 0
iOSClient/Activity/NCActivity.swift

@@ -296,12 +296,46 @@ class activityTableViewCell: UITableViewCell, UICollectionViewDelegate, UICollec
                                         cell.imageView.image = image
                                     }
                                 } else {
+                                    
+                                    NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: activityPreview.source, fileName: nil, width: 25, rewrite: false)
+                                    
+                                    /*
+                                    let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + (activityPreview.source as NSString).lastPathComponent
+                                    if FileManager.default.fileExists(atPath: fileNameLocalPath) {
+                                        if let image = UIImage(contentsOfFile: fileNameLocalPath) {
+                                            cell.avatar.image = image
+                                        }
+                                    } else {
+                                        DispatchQueue.global().async {
+                                            let url = self.appDelegate.activeUrl + k_avatar + activity.user + "/128"
+                                            let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
+                                            if let data = try? Data(contentsOf: URL(string: encodedString!)!) {
+                                                DispatchQueue.main.async {
+                                                    do {
+                                                        try data.write(to: fileNameLocalPath.url, options: .atomic)
+                                                    } catch { return }
+                                                    cell.avatar.image = UIImage(data: data)
+                                                }
+                                            }
+                                        }
+                                    }
+                                    */
+                                    
+                                    
+                                    
                                     // default icon
                                 }
                             })
                         }
                     }
                 }
+                
+            } else {
+                if let imageNamePath = NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: activityPreview.source, fileName: nil, width: 100, rewrite: false) {
+                    if let image = UIImage(contentsOfFile: imageNamePath) {
+                        cell.imageView.image = image
+                    }
+                }
             }
             
             return cell
@@ -320,6 +354,37 @@ class activityCollectionViewCell: UICollectionViewCell {
     }
 }
 
+// MARK: Utility
+
+// MARK: - Get Image from url
+
+func getDataFromUrl(url: URL, completion: @escaping (_ data: Data?, _  response: URLResponse?, _ error: Error?) -> Void) {
+    URLSession.shared.dataTask(with: url) {
+        (data, response, error) in
+        completion(data, response, error)
+        }.resume()
+}
+
+func downloadImage(url: URL) {
+    
+    print("Download Started")
+    getDataFromUrl(url: url) { (data, response, error)  in
+        guard let data = data, error == nil else { return }
+        let fileName = response?.suggestedFilename ?? url.lastPathComponent
+        print("Download Finished")
+        DispatchQueue.main.async() { () -> Void in
+            
+            do {
+                let pathFileName = CCUtility.getDirectoryUserData() + "/" + fileName
+                try data.write(to: URL(fileURLWithPath: pathFileName), options: .atomic)
+                
+            } catch {
+                print(error)
+            }
+        }
+    }
+}
+
 // MARK: Extension
 
 extension String

+ 1 - 1
iOSClient/Networking/NCService.swift

@@ -69,7 +69,7 @@ class NCService: NSObject {
                     
                     // Download Logo
                     let fileNameThemingLogo = CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-themingLogo.png"
-                    NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: capabilities!.themingLogo, fileName: fileNameThemingLogo, width: 40, rewrite: true)
+                    _ = NCUtility.sharedInstance.convertSVGtoPNGWriteToUserData(svgUrlString: capabilities!.themingLogo, fileName: fileNameThemingLogo, width: 40, rewrite: true)
                     
                     // Download Theming Background
                     DispatchQueue.global().async {

+ 12 - 8
iOSClient/Utility/NCUtility.swift

@@ -168,15 +168,15 @@ class NCUtility: NSObject {
         return (k_layout_list, "fileName", true, "none", true)
     }
     
-    func convertSVGtoPNGWriteToUserData(svgUrlString: String, fileName: String?, width: CGFloat?, rewrite: Bool) {
+    func convertSVGtoPNGWriteToUserData(svgUrlString: String, fileName: String?, width: CGFloat?, rewrite: Bool) -> String? {
         
         var fileNamePNG = ""
         
         guard let svgUrlString = svgUrlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
-            return
+            return nil
         }
         guard let iconURL = URL(string: svgUrlString) else {
-            return
+            return nil
         }
         
         if fileName == nil {
@@ -190,7 +190,7 @@ class NCUtility: NSObject {
         if !FileManager.default.fileExists(atPath: imageNamePath) || rewrite == true {
             
             guard let imageData = try? Data(contentsOf:iconURL) else {
-                return
+                return nil
             }
             
             if let image = UIImage.init(data: imageData) {
@@ -212,14 +212,14 @@ class NCUtility: NSObject {
                 }
                 
                 guard let pngImageData = newImage.pngData() else {
-                    return
+                    return nil
                 }
                 CCUtility.write(pngImageData, fileNamePath: imageNamePath)
                 
             } else {
                 
                 guard let svgImage: SVGKImage = SVGKImage(contentsOf: iconURL) else {
-                    return
+                    return nil
                 }
                 
                 if width != nil {
@@ -228,15 +228,19 @@ class NCUtility: NSObject {
                 }
                 
                 guard let image: UIImage = svgImage.uiImage else {
-                    return
+                    return nil
                 }
                 guard let pngImageData = image.pngData() else {
-                    return
+                    return nil
                 }
                 
                 CCUtility.write(pngImageData, fileNamePath: imageNamePath)
+                
+                return imageNamePath
             }
         }
+        
+        return imageNamePath
     }
     
     @objc func startActivityIndicator(view: UIView) {