Browse Source

coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 2 years ago
parent
commit
b962742d4b

+ 2 - 2
iOSClient/Activity/NCActivity.swift

@@ -222,7 +222,7 @@ extension NCActivity: UITableViewDataSource {
 
         // Image
         let fileName = appDelegate.userBaseUrl + "-" + comment.actorId + ".png"
-        NCOperationQueue.shared.downloadAvatar(user: comment.actorId, dispalyName: comment.actorDisplayName, fileName: fileName, cell: cell, view: tableView)
+        NCOperationQueue.shared.downloadAvatar(user: comment.actorId, dispalyName: comment.actorDisplayName, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView)
         // Username
         cell.labelUser.text = comment.actorDisplayName
         cell.labelUser.textColor = NCBrandColor.shared.label
@@ -288,7 +288,7 @@ extension NCActivity: UITableViewDataSource {
 
             let fileName = appDelegate.userBaseUrl + "-" + activity.user + ".png"
 
-            NCOperationQueue.shared.downloadAvatar(user: activity.user, dispalyName: nil, fileName: fileName, cell: cell, view: tableView)
+            NCOperationQueue.shared.downloadAvatar(user: activity.user, dispalyName: nil, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView)
         }
 
         // subject

+ 41 - 2
iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

@@ -1332,7 +1332,40 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
                 default:
                     (cell as! NCCellProtocol).filePreviewImageView?.image = NCBrandColor.cacheImages.file
                 }
-                print(metadata.iconUrl)
+
+                //var urlString: String = ""
+                if !metadata.iconUrl.isEmpty {
+                    if let ownerId = NCUtility.shared.getAvatarFromIconUrl(metadata: metadata), let cell = cell as? NCCellProtocol {
+                        let fileName = metadata.userBaseUrl + "-" + ownerId + ".png"
+                        NCOperationQueue.shared.downloadAvatar(user: ownerId, dispalyName: nil, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.filePreviewImageView)
+                    }
+
+                    /*
+                    if metadata.iconUrl.starts(with: "/apps") {
+                        //urlString = metadata.urlBase + metadata.iconUrl
+                    } else if metadata.iconUrl.contains("http") && metadata.iconUrl.contains("avatar") {
+                        let splitIconUrl = metadata.iconUrl.components(separatedBy: "/")
+                        var found:Bool = false
+                        var ownerId: String = ""
+                        for item in splitIconUrl {
+                            if found {
+                                ownerId = item
+                                break
+                            }
+                            if item == "avatar" { found = true}
+                        }
+                        let fileName = metadata.userBaseUrl + "-" + ownerId + ".png"
+                        if let cell = cell as? NCCellProtocol {
+                            NCOperationQueue.shared.downloadAvatar(user: ownerId, dispalyName: nil, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.filePreviewImageView)
+                        }
+                    }
+//                    NCCommunication.shared.downloadContent(serverUrl: urlString) { _, data, errorCode, _ in
+//                        if errorCode == 0, let data = data, let image = UIImage(data: data) {
+//                            (cell as! NCCellProtocol).filePreviewImageView?.image = image
+//                        }
+//                    }
+                     */
+                }
             }
         }
 
@@ -1342,7 +1375,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
            appDelegate.account == metadata.account,
            let cell = cell as? NCCellProtocol {
             let fileName = metadata.userBaseUrl + "-" + metadata.ownerId + ".png"
-            NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView)
+            NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.fileAvatarImageView)
         }
     }
 
@@ -1562,6 +1595,9 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
             if metadata.classFile == NCCommunicationCommon.typeClassFile.url.rawValue {
                 cell.hideButtonShare(true)
                 cell.hideButtonMore(true)
+                if let ownerId = NCUtility.shared.getAvatarFromIconUrl(metadata: metadata) {
+                    cell.fileUser = ownerId
+                }
             }
 
             // Disable Share Button
@@ -1701,6 +1737,9 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
             // URL
             if metadata.classFile == NCCommunicationCommon.typeClassFile.url.rawValue {
                 cell.hideButtonMore(true)
+                if let ownerId = NCUtility.shared.getAvatarFromIconUrl(metadata: metadata) {
+                    cell.fileUser = ownerId
+                }
             }
 
             // Edit mode

+ 8 - 5
iOSClient/Networking/NCOperationQueue.swift

@@ -160,17 +160,18 @@ import NCCommunication
 
     // Download Avatar
 
-    func downloadAvatar(user: String, dispalyName: String?, fileName: String, cell: NCCellProtocol, view: UIView?) {
+    func downloadAvatar(user: String, dispalyName: String?, fileName: String, cell: NCCellProtocol, view: UIView?, cellImageView: UIImageView?) {
 
         let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
 
         if let image = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) {
+            cellImageView?.image = image
             cell.fileAvatarImageView?.image = image
             return
         }
 
         if let account = NCManageDatabase.shared.getActiveAccount() {
-            cell.fileAvatarImageView?.image = NCUtility.shared.loadUserImage(
+            cellImageView?.image = NCUtility.shared.loadUserImage(
                 for: user,
                    displayName: dispalyName,
                    userBaseUrl: account)
@@ -181,7 +182,7 @@ import NCCommunication
                 return
             }
         }
-        downloadAvatarQueue.addOperation(NCOperationDownloadAvatar(user: user, fileName: fileName, fileNameLocalPath: fileNameLocalPath, cell: cell, view: view))
+        downloadAvatarQueue.addOperation(NCOperationDownloadAvatar(user: user, fileName: fileName, fileNameLocalPath: fileNameLocalPath, cell: cell, view: view, cellImageView: cellImageView))
     }
 
     func cancelDownloadAvatar(user: String) {
@@ -467,14 +468,16 @@ class NCOperationDownloadAvatar: ConcurrentOperation {
     var fileNameLocalPath: String
     var cell: NCCellProtocol!
     var view: UIView?
+    var cellImageView: UIImageView?
 
-    init(user: String, fileName: String, fileNameLocalPath: String, cell: NCCellProtocol, view: UIView?) {
+    init(user: String, fileName: String, fileNameLocalPath: String, cell: NCCellProtocol, view: UIView?, cellImageView: UIImageView?) {
         self.user = user
         self.fileName = fileName
         self.fileNameLocalPath = fileNameLocalPath
         self.cell = cell
         self.view = view
         self.etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
+        self.cellImageView = cellImageView
     }
 
     override func start() {
@@ -490,7 +493,7 @@ class NCOperationDownloadAvatar: ConcurrentOperation {
 
                     DispatchQueue.main.async {
                         if self.user == self.cell.fileUser {
-                            if let avatarImageView = self.cell?.fileAvatarImageView {
+                            if let avatarImageView = self.cellImageView {
                                 UIView.transition(with: avatarImageView, duration: 0.75, options: .transitionCrossDissolve) {
                                     avatarImageView.image = imageAvatar
                                 } completion: { _ in

+ 1 - 1
iOSClient/Notification/NCNotification.swift

@@ -146,7 +146,7 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty
                 cell.avatar.image = image
             } else if !FileManager.default.fileExists(atPath: fileNameLocalPath) {
                 cell.fileUser = user
-                NCOperationQueue.shared.downloadAvatar(user: user, dispalyName: json["user"]?["name"].string, fileName: fileName, cell: cell, view: tableView)
+                NCOperationQueue.shared.downloadAvatar(user: user, dispalyName: json["user"]?["name"].string, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView)
             }
         }
 

+ 1 - 1
iOSClient/Select/NCSelect.swift

@@ -411,7 +411,7 @@ extension NCSelect: UICollectionViewDataSource {
            activeAccount.account == metadata.account,
            let cell = cell as? NCCellProtocol {
             let fileName = metadata.userBaseUrl + "-" + metadata.ownerId + ".png"
-            NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView)
+            NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView, cellImageView: cell.fileAvatarImageView)
         }
     }
 

+ 1 - 1
iOSClient/Share/NCShare.swift

@@ -338,7 +338,7 @@ extension NCShare: UITableViewDataSource {
                 cell.delegate = self
                 cell.setupCellUI(userId: appDelegate.userId)
                 let fileName = appDelegate.userBaseUrl + "-" + tableShare.shareWith + ".png"
-                NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView)
+                NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView)
                 return cell
             }
         }

+ 17 - 0
iOSClient/Utility/NCUtility.swift

@@ -889,6 +889,23 @@ class NCUtility: NSObject {
         return UIDevice.current.systemVersion.compare(version,
          options: NSString.CompareOptions.numeric) == ComparisonResult.orderedAscending
     }
+
+    func getAvatarFromIconUrl(metadata: tableMetadata) -> String? {
+
+        var ownerId: String?
+        if metadata.iconUrl.contains("http") && metadata.iconUrl.contains("avatar") {
+            let splitIconUrl = metadata.iconUrl.components(separatedBy: "/")
+            var found:Bool = false
+            for item in splitIconUrl {
+                if found {
+                    ownerId = item
+                    break
+                }
+                if item == "avatar" { found = true}
+            }
+        }
+        return ownerId
+    }
 }
 
 // MARK: -