|
1 | 1 | import SwiftUI |
2 | 2 |
|
3 | | -final class NetworkImageModel: ObservableObject { |
4 | | - struct Environment { |
5 | | - let transaction: Transaction |
6 | | - let imageLoader: NetworkImageLoader |
7 | | - } |
| 3 | +@MainActor final class NetworkImageModel: ObservableObject { |
| 4 | + @Published private(set) var source: ImageSource? |
| 5 | + @Published private(set) var image: NetworkImageState = .empty |
8 | 6 |
|
9 | | - struct State: Equatable { |
10 | | - var source: ImageSource? |
11 | | - var image: NetworkImageState = .empty |
12 | | - } |
| 7 | + private var transaction = Transaction() |
| 8 | + private var imageLoader: NetworkImageLoader = DefaultNetworkImageLoader.shared |
13 | 9 |
|
14 | | - @Published private(set) var state: State = .init() |
| 10 | + // MARK: Actions |
15 | 11 |
|
16 | | - @MainActor func onAppear(source: ImageSource?, environment: Environment) async { |
17 | | - guard source != self.state.source else { return } |
| 12 | + func sourceChanged(_ source: ImageSource?) async { |
| 13 | + guard source != self.source else { return } |
18 | 14 |
|
19 | | - guard let source else { |
20 | | - self.state = .init() |
21 | | - return |
| 15 | + self.source = source |
| 16 | + |
| 17 | + if let source { |
| 18 | + await loadImage(source: source) |
22 | 19 | } |
| 20 | + } |
23 | 21 |
|
24 | | - self.state.source = source |
| 22 | + func transactionChanged(_ transaction: Transaction) { |
| 23 | + self.transaction = transaction |
| 24 | + } |
25 | 25 |
|
26 | | - let image: NetworkImageState |
| 26 | + func imageLoaderChanged(_ imageLoader: NetworkImageLoader) { |
| 27 | + self.imageLoader = imageLoader |
| 28 | + } |
27 | 29 |
|
28 | | - do { |
29 | | - let cgImage = try await environment.imageLoader.image(from: source.url) |
30 | | - image = .success( |
31 | | - image: .init(decorative: cgImage, scale: source.scale), |
| 30 | + private func loadImageFinished(_ cgImage: CGImage, scale: CGFloat) { |
| 31 | + withTransaction(transaction) { |
| 32 | + self.image = .success( |
| 33 | + image: Image(decorative: cgImage, scale: scale), |
32 | 34 | idealSize: CGSize( |
33 | | - width: CGFloat(cgImage.width) / source.scale, |
34 | | - height: CGFloat(cgImage.height) / source.scale |
| 35 | + width: CGFloat(cgImage.width) / scale, |
| 36 | + height: CGFloat(cgImage.height) / scale |
35 | 37 | ) |
36 | 38 | ) |
37 | | - } catch { |
38 | | - image = .failure |
39 | 39 | } |
| 40 | + } |
| 41 | + |
| 42 | + private func loadImageFailed(_: Error) { |
| 43 | + self.image = .failure |
| 44 | + } |
40 | 45 |
|
41 | | - withTransaction(environment.transaction) { |
42 | | - self.state.image = image |
| 46 | + // MARK: Effects |
| 47 | + |
| 48 | + private func loadImage(source: ImageSource) async { |
| 49 | + do { |
| 50 | + let cgImage = try await imageLoader.image(from: source.url) |
| 51 | + loadImageFinished(cgImage, scale: source.scale) |
| 52 | + } catch { |
| 53 | + loadImageFailed(error) |
43 | 54 | } |
44 | 55 | } |
45 | 56 | } |
0 commit comments