Skip to content

Commit

Permalink
Adopt the NetworkImage package for image loading
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal committed Sep 9, 2023
1 parent f28a726 commit b9345d5
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 150 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
{
"pins" : [
{
"identity" : "networkimage",
"kind" : "remoteSourceControl",
"location" : "https://github.com/gonzalezreal/NetworkImage",
"state" : {
"revision" : "7aff8d1b31148d32c5933d75557d42f6323ee3d1",
"version" : "6.0.0"
}
},
{
"identity" : "swift-snapshot-testing",
"kind" : "remoteSourceControl",
Expand Down
9 changes: 7 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.10.0")
.package(
url: "https://github.com/gonzalezreal/NetworkImage", from: "6.0.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing", from: "1.10.0"),
],
targets: [
.target(name: "cmark-gfm"),
.target(
name: "MarkdownUI",
dependencies: ["cmark-gfm"]
dependencies: [
"cmark-gfm",
.product(name: "NetworkImage", package: "NetworkImage"),
]
),
.testTarget(
name: "MarkdownUITests",
Expand Down
16 changes: 16 additions & 0 deletions Sources/MarkdownUI/Extensibility/AssetImageProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,19 @@ extension ImageProvider where Self == AssetImageProvider {
.init()
}
}

#if canImport(UIKit)
private typealias PlatformImage = UIImage
#elseif os(macOS)
private typealias PlatformImage = NSImage
#endif

extension Image {
fileprivate init(platformImage: PlatformImage) {
#if canImport(UIKit)
self.init(uiImage: platformImage)
#elseif os(macOS)
self.init(nsImage: platformImage)
#endif
}
}
21 changes: 12 additions & 9 deletions Sources/MarkdownUI/Extensibility/DefaultImageProvider.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import NetworkImage
import SwiftUI

/// The default image provider, which loads images from the network.
public struct DefaultImageProvider: ImageProvider {
private let urlSession: URLSession

/// Creates a default image provider.
/// - Parameter urlSession: An `URLSession` instance to load images.
public init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

public func makeImage(url: URL?) -> some View {
DefaultImageView(url: url, urlSession: self.urlSession)
NetworkImage(url: url) { state in
switch state {
case .empty, .failure:
Color.clear
.frame(width: 0, height: 0)
case .success(let image, let idealSize):
ResizeToFit(idealSize: idealSize) {
image.resizable()
}
}
}
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

14 changes: 4 additions & 10 deletions Sources/MarkdownUI/Extensibility/DefaultInlineImageProvider.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
import NetworkImage
import SwiftUI

/// The default inline image provider, which loads images from the network.
public struct DefaultInlineImageProvider: InlineImageProvider {
private let urlSession: URLSession

/// Creates a default inline image provider.
/// - Parameter urlSession: An `URLSession` instance to load images.
public init(urlSession: URLSession = .shared) {
self.urlSession = urlSession
}

public func image(with url: URL, label: String) async throws -> Image {
try await Image(
platformImage: DefaultImageLoader.shared
.image(with: url, urlSession: self.urlSession)
DefaultNetworkImageLoader.shared.image(from: url),
scale: 1,
label: Text(label)
)
}
}
Expand Down
17 changes: 0 additions & 17 deletions Sources/MarkdownUI/Extensibility/Image+PlatformImage.swift

This file was deleted.

16 changes: 16 additions & 0 deletions Sources/MarkdownUI/Utility/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import SwiftUI

// MARK: - Deprecated after 2.1.0:

extension DefaultImageProvider {
@available(*, deprecated, message: "Use the 'default' static property")
public init(urlSession: URLSession = .shared) {
self.init()
}
}

extension DefaultInlineImageProvider {
@available(*, deprecated, message: "Use the 'default' static property")
public init(urlSession: URLSession = .shared) {
self.init()
}
}

// MARK: - Deprecated after 2.0.2:

extension BlockStyle where Configuration == BlockConfiguration {
Expand Down

0 comments on commit b9345d5

Please sign in to comment.