Skip to content

Commit 93efc8e

Browse files
committed
Minor improvements.
1 parent f9e25ad commit 93efc8e

5 files changed

Lines changed: 36 additions & 15 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@ init() {
106106
}
107107
```
108108

109+
### You can also read this value from within a view to access the image cache management
110+
111+
```swift
112+
struct MyView: View {
113+
@ImageCache private var imageCache
114+
115+
// ...
116+
}
117+
```
118+
109119
## Requirements
110120
- iOS 14.0 +
111121
- [SwiftUI](https://developer.apple.com/xcode/swiftui/)

Sources/CachedAsyncImage/PropertyWrappers/ImageCache.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,29 @@
88

99
import Foundation
1010

11+
/// A property wrapper type that reflects a value from `TemporaryImageCache`.
12+
///
13+
/// Read this value from within a view to access the image cache management.
14+
///
15+
/// struct MyView: View {
16+
/// @ImageCache private var imageCache
17+
///
18+
/// // ...
19+
/// }
20+
///
1121
@propertyWrapper
1222
public struct ImageCache {
1323
// MARK: - Public Properties
1424

25+
/// The wrapped value property provides primary access to the value’s data.
1526
public var wrappedValue: ImageCacheProtocol {
1627
get { storage.imageCache }
1728
nonmutating set { storage.imageCache = newValue }
1829
}
1930

2031
// MARK: - Private Properties
2132

22-
private var storage: FeatureStorage
33+
private let storage: FeatureStorage
2334

2435
// MARK: - Initializers
2536

Sources/CachedAsyncImage/PropertyWrappers/Network.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct Network {
1919

2020
// MARK: - Private Properties
2121

22-
private var storage: FeatureStorage
22+
private let storage: FeatureStorage
2323

2424
// MARK: - Initializers
2525

Sources/CachedAsyncImage/Services/ImageLoader.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ final class ImageLoader: ObservableObject {
6767
.map { CPImage(data: $0) }
6868
.catch { [weak self] error -> AnyPublisher<CPImage?, Never> in
6969
if let error = error as? NetworkError {
70-
DispatchQueue.main.async {
71-
self?.errorMessage = error.rawValue
72-
}
70+
self?.errorMessage(with: error.rawValue)
7371

7472
#if DEBUG
7573
print("**** CachedAsyncImage error: \(error.rawValue)")
@@ -104,22 +102,25 @@ final class ImageLoader: ObservableObject {
104102

105103
private func start() {
106104
isLoading = true
107-
108-
Task { @MainActor [weak self] in
109-
self?.errorMessage = nil
110-
}
105+
errorMessage(with: nil)
111106
}
112107

113108
private func finish() {
114109
isLoading = false
115110
}
116111

112+
private func cancel() {
113+
cancellables.forEach { $0.cancel() }
114+
}
115+
117116
private func cache(url: URL?, image: CPImage?) {
118117
guard let url = url else { return }
119118
image.map { imageCache[url] = $0 }
120119
}
121120

122-
private func cancel() {
123-
cancellables.forEach { $0.cancel() }
121+
private func errorMessage(with text: String?) {
122+
Task { @MainActor [weak self] in
123+
self?.errorMessage = text
124+
}
124125
}
125126
}

Sources/CachedAsyncImage/Services/TemporaryImageCache.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public protocol ImageCacheProtocol {
2828
func removeCache()
2929
}
3030

31-
/// Temporary image cache.
3231
struct TemporaryImageCache: ImageCacheProtocol {
3332
// MARK: - Private Properties
3433

@@ -39,7 +38,7 @@ struct TemporaryImageCache: ImageCacheProtocol {
3938

4039
// MARK: - Subscripts
4140

42-
public subscript(_ key: URL) -> CPImage? {
41+
subscript(_ key: URL) -> CPImage? {
4342
get { cache.object(forKey: key as NSURL) }
4443
set {
4544
newValue == nil
@@ -50,12 +49,12 @@ struct TemporaryImageCache: ImageCacheProtocol {
5049

5150
// MARK: - Public Methods
5251

53-
public func setCacheLimit(countLimit: Int = 0, totalCostLimit: Int = 0) {
52+
func setCacheLimit(countLimit: Int = 0, totalCostLimit: Int = 0) {
5453
cache.countLimit = countLimit
5554
cache.totalCostLimit = totalCostLimit
5655
}
5756

58-
public func removeCache() {
57+
func removeCache() {
5958
cache.removeAllObjects()
6059
}
6160
}

0 commit comments

Comments
 (0)