Skip to content

Modularize Image Provider Implementation for Improved Flexibility #409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Examples/Demo/Demo/ImageProvidersView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import MarkdownUI
import SDWebImageSwiftUI
import SwiftUI
import ImageProviders
import NetworkImageProvider

struct ImageProvidersView: View {
private let content = """
Expand Down
2 changes: 2 additions & 0 deletions Examples/Demo/Demo/ImagesView.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import MarkdownUI
import SwiftUI
import ImageProviders
import NetworkImageProvider

struct ImagesView: View {
private let content = """
Expand Down
1 change: 1 addition & 0 deletions Examples/Demo/Demo/LazyLoadingView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import ImageProviders
import MarkdownUI
import SwiftUI

Expand Down
22 changes: 21 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ let package = Package(
.library(
name: "MarkdownUI",
targets: ["MarkdownUI"]
),
.library(
name: "ImageProviders",
targets: ["ImageProviders"]
),
.library(
name: "NetworkImageProvider",
targets: ["NetworkImageProvider"]
)
],
dependencies: [
Expand All @@ -26,9 +34,21 @@ let package = Package(
.target(
name: "MarkdownUI",
dependencies: [
"ImageProviders",
"NetworkImageProvider",
.product(name: "cmark-gfm", package: "swift-cmark"),
.product(name: "cmark-gfm-extensions", package: "swift-cmark"),
.product(name: "NetworkImage", package: "NetworkImage"),
]
),
.target(
name: "ImageProviders",
dependencies: []
),
.target(
name: "NetworkImageProvider",
dependencies: [
"ImageProviders",
.product(name: "NetworkImage", package: "NetworkImage")
]
),
.testTarget(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ public protocol ImageProvider {
@ViewBuilder func makeImage(url: URL?) -> Body
}

struct AnyImageProvider: ImageProvider {
public struct AnyImageProvider: ImageProvider {
private let _makeImage: (URL?) -> AnyView

init<I: ImageProvider>(_ imageProvider: I) {
public init<I: ImageProvider>(_ imageProvider: I) {
self._makeImage = {
AnyView(imageProvider.makeImage(url: $0))
}
}

func makeImage(url: URL?) -> some View {
public func makeImage(url: URL?) -> some View {
self._makeImage(url)
}
}
}
14 changes: 14 additions & 0 deletions Sources/ImageProviders/InlineImageProvider.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import SwiftUI

/// A type that provides an image for an inline image in a Markdown view.
///
/// To configure the current inline image provider for a view hierarchy,
/// use the `markdownInlineImageProvider(_:)` modifier.
public protocol InlineImageProvider {
/// Returns an image for the given URL and label.
///
/// - Parameters:
/// - url: The URL of the image to load.
/// - label: The accessibility label for the image.
func image(with url: URL, label: String) async throws -> Image
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import SwiftUI

struct ResizeToFit<Content>: View where Content: View {
public struct ResizeToFit<Content>: View where Content: View {
private let idealSize: CGSize
private let content: Content

init(idealSize: CGSize, @ViewBuilder content: () -> Content) {
public init(idealSize: CGSize, @ViewBuilder content: () -> Content) {
self.idealSize = idealSize
self.content = content()
}

var body: some View {
public var body: some View {
if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) {
ResizeToFit2 { self.content }
} else {
Expand Down
16 changes: 0 additions & 16 deletions Sources/MarkdownUI/Extensibility/InlineImageProvider.swift

This file was deleted.

16 changes: 0 additions & 16 deletions Sources/MarkdownUI/Utility/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import SwiftUI
import ImageProviders
import NetworkImageProvider

extension View {
/// Sets the image provider for the Markdown images in a view hierarchy.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import ImageProviders

extension View {
/// Sets the inline image provider for the Markdown inline images in a view hierarchy.
Expand Down
1 change: 1 addition & 0 deletions Sources/MarkdownUI/Views/Inlines/ImageView.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import ImageProviders

struct ImageView: View {
@Environment(\.theme.image) private var image
Expand Down
1 change: 1 addition & 0 deletions Sources/MarkdownUI/Views/Inlines/InlineText.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import SwiftUI
import ImageProviders

struct InlineText: View {
@Environment(\.inlineImageProvider) private var inlineImageProvider
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NetworkImage
import SwiftUI
import ImageProviders

/// The default image provider, which loads images from the network.
public struct DefaultImageProvider: ImageProvider {
Expand All @@ -26,3 +27,12 @@ extension ImageProvider where Self == DefaultImageProvider {
.init()
}
}

// MARK: - Deprecated after 2.1.0:

extension DefaultImageProvider {
@available(*, deprecated, message: "Use the 'default' static property")
public init(urlSession: URLSession = .shared) {
self.init()
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NetworkImage
import SwiftUI
import ImageProviders

/// The default inline image provider, which loads images from the network.
public struct DefaultInlineImageProvider: InlineImageProvider {
Expand All @@ -21,3 +22,12 @@ extension InlineImageProvider where Self == DefaultInlineImageProvider {
.init()
}
}

// MARK: - Deprecated after 2.1.0:

extension DefaultInlineImageProvider {
@available(*, deprecated, message: "Use the 'default' static property")
public init(urlSession: URLSession = .shared) {
self.init()
}
}
1 change: 1 addition & 0 deletions Tests/MarkdownUITests/MarkdownImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import XCTest

import MarkdownUI
import ImageProviders

final class MarkdownImageTests: XCTestCase {
private let layout = SwiftUISnapshotLayout.device(config: .iPhone8)
Expand Down
1 change: 1 addition & 0 deletions Tests/MarkdownUITests/MarkdownTableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import XCTest

import MarkdownUI
import ImageProviders

final class MarkdownTableTests: XCTestCase {
private let layout = SwiftUISnapshotLayout.device(config: .iPhone8)
Expand Down
1 change: 1 addition & 0 deletions Tests/MarkdownUITests/ThemeDocCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import XCTest

import MarkdownUI
import ImageProviders

final class ThemeDocCTests: XCTestCase {
private let layout = SwiftUISnapshotLayout.device(config: .iPhone8)
Expand Down