Skip to content

Commit

Permalink
Merge pull request #187 from gonzalezreal/fix-default-image-provider
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzalezreal authored Feb 5, 2023
2 parents bf1ec48 + 6482904 commit 5e15c0d
Show file tree
Hide file tree
Showing 17 changed files with 246 additions and 127 deletions.
4 changes: 4 additions & 0 deletions Examples/Demo/Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
991E39882950A80C00A3012A /* CodeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 991E39872950A80C00A3012A /* CodeView.swift */; };
991E398A2950B0DF00A3012A /* TablesView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 991E39892950B0DF00A3012A /* TablesView.swift */; };
994F5D87295821AF00B2BB51 /* SDWebImageSwiftUI in Frameworks */ = {isa = PBXBuildFile; productRef = 994F5D86295821AF00B2BB51 /* SDWebImageSwiftUI */; };
99939E2D298EC9B500E3337E /* LazyLoadingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99939E2C298EC9B500E3337E /* LazyLoadingView.swift */; };
99B59C9B29548FCF00390E9B /* Splash in Frameworks */ = {isa = PBXBuildFile; productRef = 99B59C9A29548FCF00390E9B /* Splash */; };
99B59C9E295490B300390E9B /* TextOutputFormat.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99B59C9D295490B300390E9B /* TextOutputFormat.swift */; };
99B59CA02954990B00390E9B /* SplashCodeSyntaxHighlighter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 99B59C9F2954990B00390E9B /* SplashCodeSyntaxHighlighter.swift */; };
Expand All @@ -36,6 +37,7 @@
991E39852950A73500A3012A /* QuotesView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = QuotesView.swift; sourceTree = "<group>"; };
991E39872950A80C00A3012A /* CodeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeView.swift; sourceTree = "<group>"; };
991E39892950B0DF00A3012A /* TablesView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TablesView.swift; sourceTree = "<group>"; };
99939E2C298EC9B500E3337E /* LazyLoadingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LazyLoadingView.swift; sourceTree = "<group>"; };
99B59C9D295490B300390E9B /* TextOutputFormat.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextOutputFormat.swift; sourceTree = "<group>"; };
99B59C9F2954990B00390E9B /* SplashCodeSyntaxHighlighter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashCodeSyntaxHighlighter.swift; sourceTree = "<group>"; };
99B59CA129549AB600390E9B /* CodeSyntaxHighlightView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodeSyntaxHighlightView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -107,6 +109,7 @@
99DE2A3C294F256A0025E332 /* HeadingsView.swift */,
9902F2B129571D5D003A2DCC /* ImagesView.swift */,
9902F2B629575183003A2DCC /* ImageProvidersView.swift */,
99939E2C298EC9B500E3337E /* LazyLoadingView.swift */,
99DE2A3E294F33030025E332 /* ListsView.swift */,
991E39852950A73500A3012A /* QuotesView.swift */,
99DE2A38294E1B700025E332 /* RepositoryReadmeView.swift */,
Expand Down Expand Up @@ -228,6 +231,7 @@
99DE2A39294E1B700025E332 /* RepositoryReadmeView.swift in Sources */,
9902F2B229571D5D003A2DCC /* ImagesView.swift in Sources */,
9902F2B729575183003A2DCC /* ImageProvidersView.swift in Sources */,
99939E2D298EC9B500E3337E /* LazyLoadingView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/CodeSyntaxHighlightView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ struct CodeSyntaxHighlightView: View {
Markdown(self.content)
.markdownCodeSyntaxHighlighter(.splash(theme: self.theme))
}
.navigationTitle("Syntax Highlighting")
}

private var theme: Splash.Theme {
Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/CodeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ struct CodeView: View {
DemoView {
Markdown(self.content)
}
.navigationTitle("Code")
}
}

Expand Down
29 changes: 29 additions & 0 deletions Examples/Demo/Demo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,63 +7,92 @@ struct ContentView: View {
Section("Formatting") {
NavigationLink {
HeadingsView()
.navigationTitle("Headings")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Headings", systemImage: "textformat.size")
}
NavigationLink {
ListsView()
.navigationTitle("Lists")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Lists", systemImage: "list.bullet")
}
NavigationLink {
TextStylesView()
.navigationTitle("Text Styles")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Text Styles", systemImage: "textformat.abc")
}
NavigationLink {
QuotesView()
.navigationTitle("Quotes")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Quotes", systemImage: "text.quote")
}
NavigationLink {
CodeView()
.navigationTitle("Code")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Code", systemImage: "curlybraces")
}
NavigationLink {
ImagesView()
.navigationTitle("Images")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Images", systemImage: "photo")
}
NavigationLink {
TablesView()
.navigationTitle("Tables")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Tables", systemImage: "tablecells")
}
}
Section("Extensibility") {
NavigationLink {
CodeSyntaxHighlightView()
.navigationTitle("Syntax Highlighting")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Syntax Highlighting", systemImage: "circle.grid.cross.left.filled")
}
NavigationLink {
ImageProvidersView()
.navigationTitle("Image Providers")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Image Providers", systemImage: "powerplug")
}
}
Section("Other") {
NavigationLink {
DingusView()
.navigationTitle("Dingus")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Dingus", systemImage: "character.cursor.ibeam")
}
NavigationLink {
RepositoryReadmeView()
.navigationTitle("Repository README")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Repository README", systemImage: "doc.text")
}
NavigationLink {
LazyLoadingView()
.navigationTitle("Lazy Loading")
.navigationBarTitleDisplayMode(.inline)
} label: {
Label("Lazy Loading", systemImage: "scroll")
}
}
}
.navigationTitle("MarkdownUI")
Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/DingusView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct DingusView: View {
Markdown(self.markdown)
}
}
.navigationTitle("Dingus")
}
}

Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/HeadingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ struct HeadingsView: View {
}
}
}
.navigationTitle("Headings")
}
}

Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/ImagesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ struct ImagesView: View {
.markdownMargin(top: .em(1.6), bottom: .em(1.6))
}
}
.navigationTitle("Images")
}
}

Expand Down
98 changes: 98 additions & 0 deletions Examples/Demo/Demo/LazyLoadingView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import MarkdownUI
import SwiftUI

struct LazyLoadingView: View {
struct Item: Identifiable {
let id = UUID()
let content = MarkdownContent {
Heading(.level2) {
"Try MarkdownUI"
}
Paragraph {
Strong("MarkdownUI")
" is a native Markdown renderer for SwiftUI"
" compatible with the "
InlineLink(
"GitHub Flavored Markdown Spec",
destination: URL(string: "https://github.github.com/gfm/")!
)
"."
}
Paragraph {
InlineImage(source: .randomImage())
}
}
}

private let about = """
This screen demonstrates how you can use the `Markdown` view inside a `LazyVStack` and
avoid re-layouts when scrolling up content. By using a custom `ImageProvider` that
shows a placeholder while the image is loading and fixing the height of the images,
you can avoid jumps and other weird effects caused by re-layouts when scrolling up.
> Note that this applies only when you plan to show Markdown content with images inside
> a `LazyVStack` or a `List`.
"""

let items = Array(repeating: (), count: 100).map(Item.init)

var body: some View {
ScrollView {
LazyVStack {
DisclosureGroup("About this demo") {
Markdown {
self.about
}
.frame(maxWidth: .infinity, alignment: .leading)
}
ForEach(self.items) { item in
Markdown(item.content)
.padding()
}
}
.padding()
}
.markdownTheme(.gitHub)
// Comment this line to see the effect of having re-layouts while scrolling up.
.markdownImageProvider(.lazyImage(aspectRatio: 4 / 3))
}
}

struct LazyLoadingView_Previews: PreviewProvider {
static var previews: some View {
LazyLoadingView()
}
}

struct LazyImageProvider: ImageProvider {
let aspectRatio: CGFloat

func makeImage(url: URL?) -> some View {
AsyncImage(url: url) { phase in
switch phase {
case .empty, .failure:
Color(.secondarySystemBackground)
case .success(let image):
image.resizable().scaledToFill()
@unknown default:
Color.clear
}
}
.aspectRatio(self.aspectRatio, contentMode: .fill)
}
}

extension ImageProvider where Self == LazyImageProvider {
static func lazyImage(aspectRatio: CGFloat) -> Self {
LazyImageProvider(aspectRatio: aspectRatio)
}
}

extension URL {
static func randomImage() -> URL {
let id: String = [
"11", "23", "26", "31", "34", "58", "63", "91", "103", "119",
].randomElement()!
return URL(string: "https://picsum.photos/id/\(id)/400/300")!
}
}
1 change: 0 additions & 1 deletion Examples/Demo/Demo/ListsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ struct ListsView: View {
.relativeFrame(minWidth: .em(1.5), alignment: .trailing)
}
}
.navigationTitle("Lists")
}
}

Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/QuotesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ struct QuotesView: View {
.background(Color.teal.opacity(0.5))
}
}
.navigationTitle("Quotes")
}
}

Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/RepositoryReadmeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ struct RepositoryReadmeView: View {
.autocapitalization(.none)
.disableAutocorrection(true)
}
.navigationTitle("Repository README")
}
}

Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/TablesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ struct TablesView: View {
DemoView {
Markdown(self.content)
}
.navigationTitle("Tables")
}
}

Expand Down
1 change: 0 additions & 1 deletion Examples/Demo/Demo/TextStylesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ struct TextStylesView: View {
UnderlineStyle(.init(pattern: .dot))
}
}
.navigationTitle("Text Styles")
}
}

Expand Down
Loading

0 comments on commit 5e15c0d

Please sign in to comment.