Skip to content

Commit 595a488

Browse files
authored
Adopt the NetworkImage package for image loading (#258)
1 parent f28a726 commit 595a488

10 files changed

+63
-150
lines changed

Package.resolved

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@ let package = Package(
1717
)
1818
],
1919
dependencies: [
20-
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.10.0")
20+
.package(url: "https://github.com/gonzalezreal/NetworkImage", from: "6.0.0"),
21+
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.10.0"),
2122
],
2223
targets: [
2324
.target(name: "cmark-gfm"),
2425
.target(
2526
name: "MarkdownUI",
26-
dependencies: ["cmark-gfm"]
27+
dependencies: [
28+
"cmark-gfm",
29+
.product(name: "NetworkImage", package: "NetworkImage"),
30+
]
2731
),
2832
.testTarget(
2933
name: "MarkdownUITests",

Sources/MarkdownUI/Extensibility/AssetImageProvider.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,19 @@ extension ImageProvider where Self == AssetImageProvider {
5858
.init()
5959
}
6060
}
61+
62+
#if canImport(UIKit)
63+
private typealias PlatformImage = UIImage
64+
#elseif os(macOS)
65+
private typealias PlatformImage = NSImage
66+
#endif
67+
68+
extension Image {
69+
fileprivate init(platformImage: PlatformImage) {
70+
#if canImport(UIKit)
71+
self.init(uiImage: platformImage)
72+
#elseif os(macOS)
73+
self.init(nsImage: platformImage)
74+
#endif
75+
}
76+
}

Sources/MarkdownUI/Extensibility/DefaultImageProvider.swift

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import NetworkImage
12
import SwiftUI
23

34
/// The default image provider, which loads images from the network.
45
public struct DefaultImageProvider: ImageProvider {
5-
private let urlSession: URLSession
6-
7-
/// Creates a default image provider.
8-
/// - Parameter urlSession: An `URLSession` instance to load images.
9-
public init(urlSession: URLSession = .shared) {
10-
self.urlSession = urlSession
11-
}
12-
136
public func makeImage(url: URL?) -> some View {
14-
DefaultImageView(url: url, urlSession: self.urlSession)
7+
NetworkImage(url: url) { state in
8+
switch state {
9+
case .empty, .failure:
10+
Color.clear
11+
.frame(width: 0, height: 0)
12+
case .success(let image, let idealSize):
13+
ResizeToFit(idealSize: idealSize) {
14+
image.resizable()
15+
}
16+
}
17+
}
1518
}
1619
}
1720

Sources/MarkdownUI/Extensibility/DefaultImageView/DefaultImageLoader.swift

Lines changed: 0 additions & 56 deletions
This file was deleted.

Sources/MarkdownUI/Extensibility/DefaultImageView/DefaultImageView.swift

Lines changed: 0 additions & 23 deletions
This file was deleted.

Sources/MarkdownUI/Extensibility/DefaultImageView/DefaultImageViewModel.swift

Lines changed: 0 additions & 33 deletions
This file was deleted.

Sources/MarkdownUI/Extensibility/DefaultInlineImageProvider.swift

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1+
import NetworkImage
12
import SwiftUI
23

34
/// The default inline image provider, which loads images from the network.
45
public struct DefaultInlineImageProvider: InlineImageProvider {
5-
private let urlSession: URLSession
6-
7-
/// Creates a default inline image provider.
8-
/// - Parameter urlSession: An `URLSession` instance to load images.
9-
public init(urlSession: URLSession = .shared) {
10-
self.urlSession = urlSession
11-
}
12-
136
public func image(with url: URL, label: String) async throws -> Image {
147
try await Image(
15-
platformImage: DefaultImageLoader.shared
16-
.image(with: url, urlSession: self.urlSession)
8+
DefaultNetworkImageLoader.shared.image(from: url),
9+
scale: 1,
10+
label: Text(label)
1711
)
1812
}
1913
}

Sources/MarkdownUI/Extensibility/Image+PlatformImage.swift

Lines changed: 0 additions & 17 deletions
This file was deleted.

Sources/MarkdownUI/Utility/Deprecations.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
import SwiftUI
22

3+
// MARK: - Deprecated after 2.1.0:
4+
5+
extension DefaultImageProvider {
6+
@available(*, deprecated, message: "Use the 'default' static property")
7+
public init(urlSession: URLSession = .shared) {
8+
self.init()
9+
}
10+
}
11+
12+
extension DefaultInlineImageProvider {
13+
@available(*, deprecated, message: "Use the 'default' static property")
14+
public init(urlSession: URLSession = .shared) {
15+
self.init()
16+
}
17+
}
18+
319
// MARK: - Deprecated after 2.0.2:
420

521
extension BlockStyle where Configuration == BlockConfiguration {

0 commit comments

Comments
 (0)