-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aff8d1
commit 1b703f6
Showing
6 changed files
with
74 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// swift-tools-version:6.0 | ||
|
||
import PackageDescription | ||
|
||
let package = Package( | ||
name: "NetworkImage", | ||
platforms: [ | ||
.macOS(.v11), | ||
.iOS(.v14), | ||
.tvOS(.v14), | ||
.watchOS(.v7), | ||
], | ||
products: [ | ||
.library(name: "NetworkImage", targets: ["NetworkImage"]) | ||
], | ||
dependencies: [], | ||
targets: [ | ||
.target(name: "NetworkImage"), | ||
.testTarget( | ||
name: "NetworkImageTests", | ||
dependencies: ["NetworkImage"] | ||
), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import Foundation | ||
|
||
struct ImageSource: Hashable { | ||
struct ImageSource: Hashable, Sendable { | ||
let url: URL | ||
let scale: CGFloat | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,56 @@ | ||
import SwiftUI | ||
|
||
final class NetworkImageModel: ObservableObject { | ||
struct Environment { | ||
let transaction: Transaction | ||
let imageLoader: NetworkImageLoader | ||
} | ||
@MainActor final class NetworkImageModel: ObservableObject { | ||
@Published private(set) var source: ImageSource? | ||
@Published private(set) var image: NetworkImageState = .empty | ||
|
||
struct State: Equatable { | ||
var source: ImageSource? | ||
var image: NetworkImageState = .empty | ||
} | ||
private var transaction = Transaction() | ||
private var imageLoader: NetworkImageLoader = DefaultNetworkImageLoader.shared | ||
|
||
@Published private(set) var state: State = .init() | ||
// MARK: Actions | ||
|
||
@MainActor func onAppear(source: ImageSource?, environment: Environment) async { | ||
guard source != self.state.source else { return } | ||
func sourceChanged(_ source: ImageSource?) async { | ||
guard source != self.source else { return } | ||
|
||
guard let source else { | ||
self.state = .init() | ||
return | ||
self.source = source | ||
|
||
if let source { | ||
await loadImage(source: source) | ||
} | ||
} | ||
|
||
self.state.source = source | ||
func transactionChanged(_ transaction: Transaction) { | ||
self.transaction = transaction | ||
} | ||
|
||
let image: NetworkImageState | ||
func imageLoaderChanged(_ imageLoader: NetworkImageLoader) { | ||
self.imageLoader = imageLoader | ||
} | ||
|
||
do { | ||
let cgImage = try await environment.imageLoader.image(from: source.url) | ||
image = .success( | ||
image: .init(decorative: cgImage, scale: source.scale), | ||
private func loadImageFinished(_ cgImage: CGImage, scale: CGFloat) { | ||
withTransaction(transaction) { | ||
self.image = .success( | ||
image: Image(decorative: cgImage, scale: scale), | ||
idealSize: CGSize( | ||
width: CGFloat(cgImage.width) / source.scale, | ||
height: CGFloat(cgImage.height) / source.scale | ||
width: CGFloat(cgImage.width) / scale, | ||
height: CGFloat(cgImage.height) / scale | ||
) | ||
) | ||
} catch { | ||
image = .failure | ||
} | ||
} | ||
|
||
private func loadImageFailed(_: Error) { | ||
self.image = .failure | ||
} | ||
|
||
withTransaction(environment.transaction) { | ||
self.state.image = image | ||
// MARK: Effects | ||
|
||
private func loadImage(source: ImageSource) async { | ||
do { | ||
let cgImage = try await imageLoader.image(from: source.url) | ||
loadImageFinished(cgImage, scale: source.scale) | ||
} catch { | ||
loadImageFailed(error) | ||
} | ||
} | ||
} |