Jelajahi Sumber

Fix warnings for swiftlint

onevcat 10 tahun lalu
induk
melakukan
9973f08cd7
2 mengubah file dengan 51 tambahan dan 63 penghapusan
  1. 1 0
      .swiftlint.yml
  2. 50 63
      Sources/ImageCache.swift

+ 1 - 0
.swiftlint.yml

@@ -7,3 +7,4 @@ disabled_rules:
   - opening_brace
   - opening_brace
   - type_body_length
   - type_body_length
   - valid_docs
   - valid_docs
+  - function_parameter_count

+ 50 - 63
Sources/ImageCache.swift

@@ -404,47 +404,12 @@ extension ImageCache {
     - parameter completionHandler: Called after the operation completes.
     - parameter completionHandler: Called after the operation completes.
     */
     */
     public func cleanExpiredDiskCacheWithCompletionHander(completionHandler: (()->())?) {
     public func cleanExpiredDiskCacheWithCompletionHander(completionHandler: (()->())?) {
+        
         // Do things in cocurrent io queue
         // Do things in cocurrent io queue
         dispatch_async(ioQueue, { () -> Void in
         dispatch_async(ioQueue, { () -> Void in
-            let diskCacheURL = NSURL(fileURLWithPath: self.diskCachePath)
-            let resourceKeys = [NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]
-            let expiredDate = NSDate(timeIntervalSinceNow: -self.maxCachePeriodInSecond)
-            var cachedFiles = [NSURL: [NSObject: AnyObject]]()
-            var URLsToDelete = [NSURL]()
             
             
-            var diskCacheSize: UInt = 0
+            var (URLsToDelete, diskCacheSize, cachedFiles) = self.travelCachedFiles(onlyForCacheSize: false)
             
             
-            if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil),
-                             urls = fileEnumerator.allObjects as? [NSURL] {
-                for fileURL in urls {
-                            
-                    do {
-                        let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys)
-                        // If it is a Directory. Continue to next file URL.
-                        if let isDirectory = resourceValues[NSURLIsDirectoryKey] as? NSNumber {
-                            if isDirectory.boolValue {
-                                continue
-                            }
-                        }
-                            
-                        // If this file is expired, add it to URLsToDelete
-                        if let modificationDate = resourceValues[NSURLContentModificationDateKey] as? NSDate {
-                            if modificationDate.laterDate(expiredDate) == expiredDate {
-                                URLsToDelete.append(fileURL)
-                                continue
-                            }
-                        }
-                        
-                        if let fileSize = resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber {
-                            diskCacheSize += fileSize.unsignedLongValue
-                            cachedFiles[fileURL] = resourceValues
-                        }
-                    } catch _ {
-                    }
-                        
-                }
-            }
-                
             for fileURL in URLsToDelete {
             for fileURL in URLsToDelete {
                 do {
                 do {
                     try self.fileManager.removeItemAtURL(fileURL)
                     try self.fileManager.removeItemAtURL(fileURL)
@@ -502,6 +467,53 @@ extension ImageCache {
         })
         })
     }
     }
     
     
+    private func travelCachedFiles(onlyForCacheSize onlyForCacheSize: Bool) -> (URLsToDelete: [NSURL], diskCacheSize: UInt, cachedFiles: [NSURL: [NSObject: AnyObject]]) {
+        
+        let diskCacheURL = NSURL(fileURLWithPath: diskCachePath)
+        let resourceKeys = [NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey]
+        let expiredDate = NSDate(timeIntervalSinceNow: -self.maxCachePeriodInSecond)
+        
+        var cachedFiles = [NSURL: [NSObject: AnyObject]]()
+        var URLsToDelete = [NSURL]()
+        var diskCacheSize: UInt = 0
+        
+        if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil),
+            urls = fileEnumerator.allObjects as? [NSURL] {
+                for fileURL in urls {
+                    
+                    do {
+                        let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys)
+                        // If it is a Directory. Continue to next file URL.
+                        if let isDirectory = resourceValues[NSURLIsDirectoryKey] as? NSNumber {
+                            if isDirectory.boolValue {
+                                continue
+                            }
+                        }
+                        
+                        if !onlyForCacheSize {
+                            // If this file is expired, add it to URLsToDelete
+                            if let modificationDate = resourceValues[NSURLContentModificationDateKey] as? NSDate {
+                                if modificationDate.laterDate(expiredDate) == expiredDate {
+                                    URLsToDelete.append(fileURL)
+                                    continue
+                                }
+                            }
+                        }
+                        
+                        if let fileSize = resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber {
+                            diskCacheSize += fileSize.unsignedLongValue
+                            if !onlyForCacheSize {
+                                cachedFiles[fileURL] = resourceValues
+                            }
+                        }
+                    } catch _ {
+                    }
+                }
+        }
+        
+        return (URLsToDelete, diskCacheSize, cachedFiles)
+    }
+    
 #if !os(OSX) && !os(watchOS)
 #if !os(OSX) && !os(watchOS)
     /**
     /**
     Clean expired disk cache when app in background. This is an async operation.
     Clean expired disk cache when app in background. This is an async operation.
@@ -586,32 +598,7 @@ extension ImageCache {
     */
     */
     public func calculateDiskCacheSizeWithCompletionHandler(completionHandler: ((size: UInt) -> ())) {
     public func calculateDiskCacheSizeWithCompletionHandler(completionHandler: ((size: UInt) -> ())) {
         dispatch_async(ioQueue, { () -> Void in
         dispatch_async(ioQueue, { () -> Void in
-            let diskCacheURL = NSURL(fileURLWithPath: self.diskCachePath)
-                
-            let resourceKeys = [NSURLIsDirectoryKey, NSURLTotalFileAllocatedSizeKey]
-            var diskCacheSize: UInt = 0
-            
-            if let fileEnumerator = self.fileManager.enumeratorAtURL(diskCacheURL, includingPropertiesForKeys: resourceKeys, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles, errorHandler: nil),
-                             urls = fileEnumerator.allObjects as? [NSURL] {
-                    for fileURL in urls {
-                        do {
-                            let resourceValues = try fileURL.resourceValuesForKeys(resourceKeys)
-                            // If it is a Directory. Continue to next file URL.
-                            if let isDirectory = resourceValues[NSURLIsDirectoryKey]?.boolValue {
-                                if isDirectory {
-                                    continue
-                                }
-                            }
-                            
-                            if let fileSize = resourceValues[NSURLTotalFileAllocatedSizeKey] as? NSNumber {
-                                diskCacheSize += fileSize.unsignedLongValue
-                            }
-                        } catch _ {
-                        }
-                        
-                    }
-            }
-            
+            let (_, diskCacheSize, _) = self.travelCachedFiles(onlyForCacheSize: true)
             dispatch_async(dispatch_get_main_queue(), { () -> Void in
             dispatch_async(dispatch_get_main_queue(), { () -> Void in
                 completionHandler(size: diskCacheSize)
                 completionHandler(size: diskCacheSize)
             })
             })