Roman Podymov пре 3 година
родитељ
комит
14001ea735
2 измењених фајлова са 74 додато и 1 уклоњено
  1. 22 0
      Sources/Cache/ImageCache.swift
  2. 52 1
      Tests/KingfisherTests/ImageCacheTests.swift

+ 22 - 0
Sources/Cache/ImageCache.swift

@@ -816,6 +816,28 @@ open class ImageCache {
         }
     }
     
+    #if swift(>=5.5)
+    #if canImport(_Concurrency)
+    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
+    open var diskStorageSize: UInt {
+        get async throws {
+            try await withCheckedThrowingContinuation { continuation in
+                ioQueue.async {
+                    do {
+                        let size = try self.diskStorage.totalSize()
+                        Task { @MainActor in continuation.resume(returning: size) }
+                    } catch let error as KingfisherError {
+                        Task { @MainActor in continuation.resume(throwing: error) }
+                    } catch {
+                        assertionFailure("The internal thrown error should be a `KingfisherError`.")
+                    }
+                }
+            }
+        }
+    }
+    #endif
+    #endif
+    
     /// Gets the cache path for the key.
     /// It is useful for projects with web view or anyone that needs access to the local file path.
     ///

+ 52 - 1
Tests/KingfisherTests/ImageCacheTests.swift

@@ -466,8 +466,46 @@ class ImageCacheTests: XCTestCase {
         waitForExpectations(timeout: 3, handler: nil)
     }
 
+    func testCalculateDiskStorageSize() {
+        let exp = expectation(description: #function)
+        cache.calculateDiskStorageSize { result in
+            switch result {
+            case .success(let size):
+                XCTAssertEqual(size, 0)
+                self.storeMultipleImages {
+                    self.cache.calculateDiskStorageSize { result in
+                        switch result {
+                        case .success(let size):
+                            XCTAssertEqual(size, UInt(testImagePNGData.count * testKeys.count))
+                        case .failure:
+                            XCTAssert(false)
+                        }
+                        exp.fulfill()
+                    }
+                }
+            case .failure:
+                XCTAssert(false)
+                exp.fulfill()
+            }
+        }
+        waitForExpectations(timeout: 3, handler: nil)
+    }
+    
+    #if swift(>=5.5)
+    #if canImport(_Concurrency)
+    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
+    func testCalculateDiskStorageSizeAsync() async throws {
+            let size = try await cache.diskStorageSize
+            XCTAssertEqual(size, 0)
+            _ = await storeMultipleImagesAsync()
+            let sizeAfterStoreMultipleImages = try await cache.diskStorageSize
+            XCTAssertEqual(sizeAfterStoreMultipleImages, UInt(testImagePNGData.count * testKeys.count))
+    }
+    #endif
+    #endif
+    
     // MARK: - Helper
-    func storeMultipleImages(_ completionHandler: @escaping () -> Void) {
+    private func storeMultipleImages(_ completionHandler: @escaping () -> Void) {
         let group = DispatchGroup()
         testKeys.forEach {
             group.enter()
@@ -477,4 +515,17 @@ class ImageCacheTests: XCTestCase {
         }
         group.notify(queue: .main, execute: completionHandler)
     }
+
+    #if swift(>=5.5)
+    #if canImport(_Concurrency)
+    @available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
+    private func storeMultipleImagesAsync() async {
+        await withCheckedContinuation { continuation in
+            storeMultipleImages {
+                continuation.resume()
+            }
+        }
+    }
+    #endif
+    #endif
 }