Ver Fonte

Refactor getUserUrlBase into protocol extension

Protocol extensions aren't exposed to objc. But since the object is of that type, when passed back to Swift it work.

Signed-off-by: Henrik Storch <henrik.storch@nextcloud.com>
Henrik Storch há 3 anos atrás
pai
commit
66cd961862

+ 1 - 1
Share/NCShareExtension.swift

@@ -266,7 +266,7 @@ class NCShareExtension: UIViewController, NCListCellDelegate, NCEmptyDataSetDele
         let image = NCUtility.shared.loadUserImage(
             for: activeAccount.user,
                displayName: activeAccount.displayName,
-               userUrlBase: String(CCUtility.getUserUrlBase(activeAccount.user, urlBase: activeAccount.urlBase)))
+               userUrlBase: activeAccount)
 
         let profileButton = UIButton(type: .custom)
         profileButton.setImage(image, for: .normal)

+ 3 - 3
iOSClient/Activity/NCActivity.swift

@@ -93,7 +93,7 @@ class NCActivity: UIViewController {
             return
         }
         
-        let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + appDelegate.user + ".png"
+        let fileName = appDelegate.userUrlBase + "-" + appDelegate.user + ".png"
         let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
         if let image = UIImage(contentsOfFile: fileNameLocalPath) {
             imageItem.image = image
@@ -248,7 +248,7 @@ extension NCActivity: UITableViewDataSource {
         cell.sizeToFit()
         
         // Image
-        let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + comment.actorId + ".png"
+        let fileName = appDelegate.userUrlBase + "-" + comment.actorId + ".png"
         NCOperationQueue.shared.downloadAvatar(user: comment.actorId, dispalyName: comment.actorDisplayName, fileName: fileName, cell: cell, view: tableView)
         // Username
         cell.labelUser.text = comment.actorDisplayName
@@ -313,7 +313,7 @@ extension NCActivity: UITableViewDataSource {
             cell.avatar.isHidden = false
             cell.fileUser = activity.user
             
-            let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + activity.user + ".png"
+            let fileName = appDelegate.userUrlBase + "-" + activity.user + ".png"
             
             NCOperationQueue.shared.downloadAvatar(user: activity.user, dispalyName: nil, fileName: fileName, cell: cell, view: tableView)
         }

+ 2 - 2
iOSClient/AppDelegate.swift

@@ -30,7 +30,7 @@ import Firebase
 import IHProgressHUD
 
 @UIApplicationMain
-class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, TOPasscodeViewControllerDelegate, NCAccountRequestDelegate, NCViewCertificateDetailsDelegate {
+class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, TOPasscodeViewControllerDelegate, NCAccountRequestDelegate, NCViewCertificateDetailsDelegate, NCUserBaseUrl {
 
     var backgroundSessionCompletionHandler: (() -> Void)?
     var window: UIWindow?
@@ -40,7 +40,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     @objc var user: String = ""
     @objc var userId: String = ""
     @objc var password: String = ""
-    
+
     var activeAppConfigView: NCAppConfigView?
     var activeFiles: NCFiles?
     var activeFileViewInFolder: NCFileViewInFolder?

+ 6 - 2
iOSClient/Data/NCDatabase.swift

@@ -29,8 +29,12 @@ protocol DateCompareable {
     var dateKey: Date { get }
 }
 
-class tableAccount: Object {
+class tableAccount: Object, NCUserBaseUrl {
 
+//    @objc var userUrlBase: String {
+//        user + "-" + (URL(string: urlBase)?.host ?? "")
+//    }
+    
     @objc dynamic var account = ""
     @objc dynamic var active: Bool = false
     @objc dynamic var address = ""
@@ -351,7 +355,7 @@ class tableLocalFile: Object {
     }
 }
 
-class tableMetadata: Object {
+class tableMetadata: Object, NCUserBaseUrl {
     
     @objc dynamic var account = ""
     @objc dynamic var assetLocalIdentifier = ""

+ 1 - 1
iOSClient/Main/Account Request/NCAccountRequest.swift

@@ -239,7 +239,7 @@ extension NCAccountRequest: UITableViewDataSource {
             avatarImage?.image = NCUtility.shared.loadUserImage(
                 for: account.user,
                    displayName: account.displayName,
-                   userUrlBase: String(CCUtility.getUserUrlBase(account.user, urlBase: account.urlBase)))
+                   userUrlBase: account)
 
             if account.alias != "" {
                 userLabel?.text = account.alias.uppercased()

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

@@ -602,7 +602,7 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
                 let image = NCUtility.shared.loadUserImage(
                     for: appDelegate.user,
                        displayName: activeAccount?.displayName,
-                       userUrlBase: String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)))
+                       userUrlBase: appDelegate)
 
                 let button = UIButton(type: .custom)
                 button.setImage(image, for: .normal)
@@ -1341,7 +1341,7 @@ extension NCCollectionViewCommon: UICollectionViewDataSource {
            metadata.ownerId != appDelegate.userId,
            appDelegate.account == metadata.account,
            let cell = cell as? NCCellProtocol {
-            let fileName = String(CCUtility.getUserUrlBase(metadata.user, urlBase: metadata.urlBase)) + "-" + metadata.ownerId + ".png"
+            let fileName = metadata.userUrlBase + "-" + metadata.ownerId + ".png"
             NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView)
         }
     }

+ 1 - 1
iOSClient/Menu/NCLoginWeb+Menu.swift

@@ -40,7 +40,7 @@ extension NCLoginWeb {
             avatar = NCUtility.shared.loadUserImage(
                 for: account.user,
                    displayName: account.displayName,
-                   userUrlBase: String(CCUtility.getUserUrlBase(account.user, urlBase: account.urlBase)))
+                   userUrlBase: account)
 
             actions.append(
                 NCMenuAction(

+ 1 - 1
iOSClient/Menu/UIViewController+Menu.swift

@@ -71,7 +71,7 @@ extension UIViewController {
                 icon: NCUtility.shared.loadUserImage(
                     for: userId,
                        displayName: card.displayName,
-                       userUrlBase: String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase))),
+                       userUrlBase: appDelegate),
                 action: nil)
 
             let actions = card.actions.map { action -> NCMenuAction in

+ 1 - 1
iOSClient/More/NCMore.swift

@@ -327,7 +327,7 @@ class NCMore: UIViewController, UITableViewDelegate, UITableViewDataSource {
                 cell.avatar.image = NCUtility.shared.loadUserImage(
                     for: account.user,
                        displayName: account.displayName,
-                       userUrlBase: String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)))
+                       userUrlBase: appDelegate)
 
                 if account.alias == "" {
                     cell.displayName?.text = account.displayName

+ 1 - 1
iOSClient/Networking/NCOperationQueue.swift

@@ -174,7 +174,7 @@ import NCCommunication
             cell.fileAvatarImageView?.image = NCUtility.shared.loadUserImage(
                 for: user,
                    displayName: dispalyName,
-                   userUrlBase: String(CCUtility.getUserUrlBase(account.user, urlBase: account.urlBase)))
+                   userUrlBase: account)
         }
 
         for operation in downloadAvatarQueue.operations as! [NCOperationDownloadAvatar] {

+ 5 - 8
iOSClient/Networking/NCService.swift

@@ -92,20 +92,17 @@ class NCService: NSObject {
                     NCContentPresenter.shared.messageNotification("Account", description: "Internal error : account not found on DB",  delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError, priority: .max)
                     return
                 }
-            
-                let user = tableAccount.user
-                let url = tableAccount.urlBase
-                
+
                 self.appDelegate.settingAccount(tableAccount.account, urlBase: tableAccount.urlBase, user: tableAccount.user, userId: tableAccount.userId, password: CCUtility.getPassword(tableAccount.account))
-                   
+
                 // Synchronize favorite
                 NCNetworking.shared.listingFavoritescompletion(selector: NCGlobal.shared.selectorReadFile) { (_, _, _, _) in }
-            
+
                 // Synchronize Offline
                 self.synchronizeOffline(account: tableAccount.account)
-                
+
                 // Get Avatar
-                let fileName = String(CCUtility.getUserUrlBase(user, urlBase: url)) + "-" + self.appDelegate.user + ".png"
+                let fileName = tableAccount.userUrlBase + "-" + self.appDelegate.user + ".png"
                 let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
                 let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
 

+ 1 - 1
iOSClient/Notification/NCNotification.swift

@@ -137,7 +137,7 @@ class NCNotification: UITableViewController, NCNotificationCellDelegate, NCEmpty
             if let json = JSON(subjectRichParameters).dictionary {
                 if let user = json["user"]?["id"].stringValue {
                     
-                    let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + user + ".png"
+                    let fileName = appDelegate.userUrlBase + "-" + user + ".png"
                     let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
                     
                     if FileManager.default.fileExists(atPath: fileNameLocalPath) {

+ 1 - 1
iOSClient/Select/NCSelect.swift

@@ -419,7 +419,7 @@ extension NCSelect: UICollectionViewDataSource {
            metadata.ownerId != activeAccount.userId,
            activeAccount.account == metadata.account,
            let cell = cell as? NCCellProtocol {
-            let fileName = String(CCUtility.getUserUrlBase(metadata.user, urlBase: metadata.urlBase)) + "-" + metadata.ownerId + ".png"
+            let fileName = metadata.userUrlBase + "-" + metadata.ownerId + ".png"
             NCOperationQueue.shared.downloadAvatar(user: metadata.ownerId, dispalyName: metadata.ownerDisplayName, fileName: fileName, cell: cell, view: collectionView)
         }
     }

+ 1 - 2
iOSClient/Settings/CCManageAccount.m

@@ -56,8 +56,7 @@
         row = [XLFormRowDescriptor formRowDescriptorWithTag:account.account rowType:XLFormRowDescriptorTypeBooleanCheck title:title];
         
         // Avatar
-        NSString* userUrlBase = [CCUtility getUserUrlBase:account.user urlBase:account.urlBase];
-        UIImage *avatar = [NCUtility.shared loadUserImageFor:account.user displayName:account.displayName userUrlBase:userUrlBase original:YES];
+        UIImage *avatar = [[NCUtility shared] loadUserImageFor:account.user displayName:account.displayName userUrlBase:account];
         
         row.cellConfigAtConfigure[@"backgroundColor"] = NCBrandColor.shared.secondarySystemGroupedBackground;
         [row.cellConfig setObject:[UIFont systemFontOfSize:13.0] forKey:@"textLabel.font"];

+ 5 - 5
iOSClient/Share/NCShare.swift

@@ -101,7 +101,7 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareNetworkingD
             sharedWithYouByImage.image = NCUtility.shared.loadUserImage(
                 for: metadata.ownerId,
                    displayName: metadata.ownerDisplayName,
-                   userUrlBase: String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)))
+                   userUrlBase: appDelegate)
             let shareAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
             sharedWithYouByImage.addGestureRecognizer(shareAction)
             let shareLabelAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
@@ -121,7 +121,7 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareNetworkingD
             }
             
 
-            let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + metadata.ownerId + ".png"
+            let fileName = appDelegate.userUrlBase + "-" + metadata.ownerId + ".png"
 
             if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
                 let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
@@ -333,9 +333,9 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareNetworkingD
             cell.imageItem.image = NCUtility.shared.loadUserImage(
                 for: sharee.shareWith,
                    displayName: nil,
-                   userUrlBase: String(CCUtility.getUserUrlBase(self.appDelegate.user, urlBase: self.appDelegate.urlBase)))
+                   userUrlBase: self.appDelegate)
 
-            let fileName = String(CCUtility.getUserUrlBase(self.appDelegate.user, urlBase: self.appDelegate.urlBase)) + "-" + sharee.shareWith + ".png"
+            let fileName = self.appDelegate.userUrlBase + "-" + sharee.shareWith + ".png"
             if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
                 let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
                 let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
@@ -428,7 +428,7 @@ extension NCShare: UITableViewDataSource {
                 cell.imageStatus.image = status.onlineStatus
                 cell.status.text = status.statusMessage
                 
-                let fileName = String(CCUtility.getUserUrlBase(appDelegate.user, urlBase: appDelegate.urlBase)) + "-" + tableShare.shareWith + ".png"
+                let fileName = appDelegate.userUrlBase + "-" + tableShare.shareWith + ".png"
                
                 NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView)
                 

+ 0 - 1
iOSClient/Utility/CCUtility.h

@@ -202,7 +202,6 @@
 + (void)createDirectoryStandard;
 
 + (NSURL *)getDirectoryGroup;
-+ (NSString *)getUserUrlBase:(NSString *)user urlBase:(NSString *)urlBase;
 + (NSString *)getDirectoryDocuments;
 + (NSString *)getDirectoryReaderMetadata;
 + (NSString *)getDirectoryAudio;

+ 0 - 17
iOSClient/Utility/CCUtility.m

@@ -1041,23 +1041,6 @@
     return path;
 }
 
-+ (NSString *)getUserUrlBase:(NSString *)user urlBase:(NSString *)urlBase
-{
-    NSString *baseUrl = [urlBase lowercaseString];
-    NSString *dirUserBaseUrl = @"";
-
-    if ([user length] && [baseUrl length]) {
-        
-        if ([baseUrl hasPrefix:@"https://"]) baseUrl = [baseUrl substringFromIndex:8];
-        if ([baseUrl hasPrefix:@"http://"]) baseUrl = [baseUrl substringFromIndex:7];
-        
-        dirUserBaseUrl = [NSString stringWithFormat:@"%@-%@", user, baseUrl];
-        dirUserBaseUrl = [[self removeForbiddenCharactersFileSystem:dirUserBaseUrl] lowercaseString];
-    }
-    
-    return dirUserBaseUrl;
-}
-
 // Return the path of directory Documents -> NSDocumentDirectory
 + (NSString *)getDirectoryDocuments
 {

+ 19 - 3
iOSClient/Utility/NCUtility.swift

@@ -29,6 +29,22 @@ import PDFKit
 import Accelerate
 import CoreMedia
 
+
+// MARK: NCUserBaseUrl
+
+@objc public protocol NCUserBaseUrl {
+    var user: String { get }
+    var urlBase: String { get }
+}
+
+public extension NCUserBaseUrl {
+    var userUrlBase: String {
+        user + "-" + (URL(string: urlBase)?.host ?? "")
+    }
+}
+
+// MARK: - NCUtility
+
 class NCUtility: NSObject {
     @objc static let shared: NCUtility = {
         let instance = NCUtility()
@@ -496,13 +512,13 @@ class NCUtility: NSObject {
         return  UIImage(named: "file")!.image(color: color, size: size)
     }
     
-    @objc func loadUserImage(for user: String, displayName: String?, userUrlBase: String, original: Bool = false) -> UIImage {
+    @objc func loadUserImage(for user: String, displayName: String?, userUrlBase: NCUserBaseUrl) -> UIImage {
 
-        let fileName = userUrlBase + "-" + user + ".png"
+        let fileName = userUrlBase.userUrlBase + "-" + user + ".png"
         let localFilePath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
 
         if let localImage = UIImage(contentsOfFile: localFilePath) {
-            return NCUtility.shared.createAvatar(image: localImage, size: 30)
+            return createAvatar(image: localImage, size: 30)
         } else if let loadedAvatar = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) {
             return loadedAvatar
         } else if let displayName = displayName, !displayName.isEmpty, let avatarImg = createAvatar(displayName: displayName, size: 30) {