Эх сурвалжийг харах

refactoring + optimisation

Signed-off-by: Henrik Storch <thisisthefoxe@gmail.com>
Henrik Storch 3 жил өмнө
parent
commit
47afdb0e39

+ 20 - 7
iOSClient/Brand/NCBrand.swift

@@ -107,6 +107,7 @@ class NCBrandColor: NSObject {
     @objc static let shared: NCBrandColor = {
         let instance = NCBrandColor()
         instance.createImagesThemingColor()
+        instance.createUserColors()
         return instance
     }()
     
@@ -151,6 +152,8 @@ class NCBrandColor: NSObject {
     @objc public let gray:                  UIColor = UIColor(red: 104.0/255.0, green: 104.0/255.0, blue: 104.0/255.0, alpha: 1.0)
     @objc public let lightGray:             UIColor = UIColor(red: 229.0/255.0, green: 229.0/229.0, blue: 104.0/255.0, alpha: 1.0)
     @objc public let yellowFavorite:        UIColor = UIColor(red: 248.0/255.0, green: 205.0/255.0, blue: 70.0/255.0, alpha: 1.0)
+    
+    public var userColors: [CGColor] = []
 
     @objc public var systemBackground: UIColor {
         get {
@@ -307,7 +310,11 @@ class NCBrandColor: NSObject {
         self.brandElement = self.customer
         self.brandText = self.customerText        
     }
-    
+
+    private func createUserColors() {
+        self.userColors = generateColors()
+    }
+
     public func createImagesThemingColor() {
         
         let gray: UIColor = UIColor(red: 162.0/255.0, green: 162.0/255.0, blue: 162.0/255.0, alpha: 0.5)
@@ -427,16 +434,16 @@ class NCBrandColor: NSObject {
             NCBrandColor.shared.brandElement = NCBrandColor.shared.brand
         }
     }
-    
-    func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
+
+    private func stepCalc(steps: Int, color1: CGColor, color2: CGColor) -> [CGFloat] {
         var step = [CGFloat](repeating: 0, count: 3)
         step[0] = (color2.components![0] - color1.components![0]) / CGFloat(steps)
         step[1] = (color2.components![1] - color1.components![1]) / CGFloat(steps)
         step[2] = (color2.components![2] - color1.components![2]) / CGFloat(steps)
         return step
     }
-    
-    func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
+
+    private func mixPalette(steps: Int, color1: CGColor, color2: CGColor) -> [CGColor] {
         var palette = [color1]
         let step = stepCalc(steps: steps, color1: color1, color2: color2)
         
@@ -450,8 +457,14 @@ class NCBrandColor: NSObject {
         }
         return palette
     }
-    
-    func genColors(steps: Int = 6) -> [CGColor] {
+
+    /**
+     Generate colors from the official nextcloud color.
+     You can provide how many colors you want (multiplied by 3).
+     if `step` = 6,
+     3 colors \* 6 will result in 18 generated colors
+     */
+    func generateColors(steps: Int = 6) -> [CGColor] {
         let red = UIColor(red: 182/255, green: 70/255, blue: 157/255, alpha: 1).cgColor
         let yellow = UIColor(red: 221/255, green: 203/255, blue: 85/255, alpha: 1).cgColor
         let blue = UIColor(red: 0/255, green: 130/255, blue: 201/255, alpha: 1).cgColor

+ 11 - 10
iOSClient/NCGlobal.swift

@@ -33,11 +33,12 @@ class NCGlobal: NSObject {
         // Normalize hash
         let loweUserName = username.lowercased()
         var hash: String
-        
+
         let regex = try! NSRegularExpression(pattern: "^([0-9a-f]{4}-?){8}$")
-        let matches = regex.matches(in: username,
-                                    range: NSRange(username.startIndex..., in: username))
-        
+        let matches = regex.matches(
+            in: username,
+            range: NSRange(username.startIndex..., in: username))
+
         if (!matches.isEmpty) {
             // Already a md5 hash?
             // done, use as is.
@@ -47,19 +48,19 @@ class NCGlobal: NSObject {
         }
 
         hash = hash.replacingOccurrences(of: "[^0-9a-f]", with: "", options: .regularExpression)
-        
-        let steps = 6
-        let finalPalette = NCBrandColor.shared.genColors(steps: steps)
-        
-        return finalPalette[NCGlobal.hashToInt(hash: hash, maximum: steps * 3)]
+
+        // userColors has 18 colors by default
+        let userColorIx = NCGlobal.hashToInt(hash: hash, maximum: 18)
+        return NCBrandColor.shared.userColors[userColorIx]
     }
-    
+
     // Convert a string to an integer evenly
     // hash is hex string
     static func hashToInt(hash: String, maximum: Int) -> Int {
         let result = hash.compactMap(\.hexDigitValue)
         return result.reduce(0, { $0 + $1 }) % maximum
     }
+
     // Struct for Progress
     //
     struct progressType {