Skip to content

Commit

Permalink
Allows configuration of the image loading mechanism
Browse files Browse the repository at this point in the history
Removes the dependency on Alamofire and AlamofireImage
  • Loading branch information
ejensen committed Dec 3, 2020
1 parent 0739dfc commit 00efa27
Show file tree
Hide file tree
Showing 27 changed files with 1,127 additions and 1,201 deletions.
3 changes: 1 addition & 2 deletions ContentfulRichTextRenderer.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Pod::Spec.new do |spec|

spec.source_files = "Sources/RichTextRenderer/**/*.swift"

spec.dependency 'AlamofireImage', '~> 4.1.0'
spec.dependency 'Contentful', '~> 5.2.0'
end

end
4 changes: 4 additions & 0 deletions Example-iOS/Example-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
6DCC7F96250009CF000EF0E7 /* StartViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DCC7F95250009CF000EF0E7 /* StartViewController.swift */; };
6DEB233424E531AF009D5879 /* CarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DEB233324E531AF009D5879 /* CarView.swift */; };
6DEB233624E531D3009D5879 /* SuggestedArticleView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DEB233524E531D3009D5879 /* SuggestedArticleView.swift */; };
97130AAC2578C0A100CC7618 /* AlamofireImageLoader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 97130AAB2578C0A100CC7618 /* AlamofireImageLoader.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -44,6 +45,7 @@
6DCC7F95250009CF000EF0E7 /* StartViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StartViewController.swift; sourceTree = "<group>"; };
6DEB233324E531AF009D5879 /* CarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarView.swift; sourceTree = "<group>"; };
6DEB233524E531D3009D5879 /* SuggestedArticleView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuggestedArticleView.swift; sourceTree = "<group>"; };
97130AAB2578C0A100CC7618 /* AlamofireImageLoader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AlamofireImageLoader.swift; sourceTree = "<group>"; };
D954E450246B71B288D7C891 /* Pods-Example-iOS.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Example-iOS.release.xcconfig"; path = "Target Support Files/Pods-Example-iOS/Pods-Example-iOS.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -96,6 +98,7 @@
isa = PBXGroup;
children = (
6DB2F48824C0302400A07D9B /* ContentfulService.swift */,
97130AAB2578C0A100CC7618 /* AlamofireImageLoader.swift */,
6DB2F48224C02DF100A07D9B /* BlockProviders */,
6DEB233224E5317A009D5879 /* InlineProviders */,
6DB2F48324C02DF100A07D9B /* Models */,
Expand Down Expand Up @@ -258,6 +261,7 @@
6DB2F46E24C02CB800A07D9B /* AppDelegate.swift in Sources */,
6DB2F48924C0302400A07D9B /* ContentfulService.swift in Sources */,
6DEB233424E531AF009D5879 /* CarView.swift in Sources */,
97130AAC2578C0A100CC7618 /* AlamofireImageLoader.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
19 changes: 19 additions & 0 deletions Example-iOS/Example-iOS/Contentful/AlamofireImageLoader.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// AlamofireImageLoader.swift

import AlamofireImage
import ContentfulRichTextRenderer

struct AlamofireImageLoader: ImageLoader {
var placeholderImage: UIImage? = nil
var transition: UIImageView.ImageTransition = .crossDissolve(0.5)
var runImageTransitionIfCached: Bool = true

func loadImage(with url: URL, into imageView: UIImageView) {
imageView.af.setImage(
withURL: url,
placeholderImage: placeholderImage,
imageTransition: transition,
runImageTransitionIfCached: runImageTransitionIfCached
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ final class CarView: UIView, ResourceLinkBlockViewRepresentable {
private let car: Car

var surroundingTextShouldWrap: Bool = false
var context: [CodingUserInfoKey : Any] = [:]
var context: [CodingUserInfoKey: Any]

public init(car: Car) {
public init(car: Car, context: [CodingUserInfoKey: Any] = [:]) {
self.car = car
self.context = context
super.init(frame: .zero)

let title = UILabel(frame: .zero)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ struct ExampleBlockViewProvider: ResourceLinkBlockViewProviding {
switch resource {
case .entryDecodable(let entryDecodable):
if let car = entryDecodable as? Car {
return CarView(car: car)
return CarView(car: car, context: context)
} else if let article = entryDecodable as? Article {
return SuggestedArticleView(article: article)
return SuggestedArticleView(article: article, context: context)
}

return nil
Expand All @@ -22,7 +22,7 @@ struct ExampleBlockViewProvider: ResourceLinkBlockViewProviding {
case .asset(let asset):
guard asset.file?.details?.imageInfo != nil else { return nil }

let imageView = ResourceLinkBlockImageView(asset: asset)
let imageView = ResourceLinkBlockImageView(asset: asset, context: context)

imageView.backgroundColor = .gray
imageView.setImageToNaturalHeight()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Example-iOS

import AlamofireImage
import ContentfulRichTextRenderer
import UIKit

Expand All @@ -13,10 +14,11 @@ final class SuggestedArticleView: UIView, ResourceLinkBlockViewRepresentable {
private var imageView: UIImageView!
private var title: UILabel!

var context: [CodingUserInfoKey : Any] = [:]
var context: [CodingUserInfoKey: Any]

public init(article: Article) {
public init(article: Article, context: [CodingUserInfoKey: Any] = [:]) {
self.article = article
self.context = context
super.init(frame: .init(x: 0, y: 0, width: 1, height: 1))

imageView = UIImageView(frame: .zero)
Expand Down Expand Up @@ -75,8 +77,8 @@ final class SuggestedArticleView: UIView, ResourceLinkBlockViewRepresentable {
layer.shadowRadius = 6
layer.shadowOpacity = 0.25

if let imageUrl = article.thumbnail?.url {
imageView.af.setImage(withURL: imageUrl)
if let imageUrl = article.thumbnail?.url, let configuration = context[.rendererConfiguration] as? RendererConfiguration {
configuration.imageLoader.loadImage(with: imageUrl, into: imageView)
}
}

Expand Down
8 changes: 5 additions & 3 deletions Example-iOS/Example-iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ class ViewController: RichTextViewController {
private let client = ContentfulService()

init() {
var configuration = DefaultRendererConfiguration()
configuration.resourceLinkBlockViewProvider = ExampleBlockViewProvider()
configuration.resourceLinkInlineStringProvider = ExampleInlineStringProvider()
let configuration = DefaultRendererConfiguration(
resourceLinkInlineStringProvider: ExampleInlineStringProvider(),
resourceLinkBlockViewProvider: ExampleBlockViewProvider(),
imageLoader: AlamofireImageLoader()
)

let renderersProvider = DefaultRenderersProvider()

Expand Down
18 changes: 0 additions & 18 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
{
"object": {
"pins": [
{
"package": "Alamofire",
"repositoryURL": "https://github.com/Alamofire/Alamofire.git",
"state": {
"branch": null,
"revision": "becd9a729a37bdbef5bc39dc3c702b99f9e3d046",
"version": "5.2.2"
}
},
{
"package": "AlamofireImage",
"repositoryURL": "https://github.com/Alamofire/AlamofireImage.git",
"state": {
"branch": null,
"revision": "3e8edbeb75227f8542aa87f90240cf0424d6362f",
"version": "4.1.0"
}
},
{
"package": "Contentful",
"repositoryURL": "https://github.com/contentful/contentful.swift.git",
Expand Down
7 changes: 1 addition & 6 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,12 @@ let package = Package(
name: "Contentful",
url: "https://github.com/contentful/contentful.swift.git",
from: "5.2.0"
),
.package(
name: "AlamofireImage",
url: "https://github.com/Alamofire/AlamofireImage.git",
from: "4.1.0"
)
],
targets: [
.target(
name: "RichTextRenderer",
dependencies: ["Contentful", "AlamofireImage"],
dependencies: ["Contentful"],
path: "Sources"
)
],
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ platform :ios, '11.0'
workspace 'RichTextRenderer.xcworkspace'

target 'RichTextRenderer' do
pod 'AlamofireImage'
pod 'Contentful'
end

target 'Example-iOS' do
project 'Example-iOS/Example-iOS.xcodeproj'
pod 'Contentful'
pod 'ContentfulRichTextRenderer', :path => 'ContentfulRichTextRenderer.podspec'
pod 'AlamofireImage'
end
7 changes: 3 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ PODS:
- Contentful (5.2.0):
- Contentful/ImageOptions (= 5.2.0)
- Contentful/ImageOptions (5.2.0)
- ContentfulRichTextRenderer (0.2.0):
- AlamofireImage (~> 4.1.0)
- ContentfulRichTextRenderer (0.2.1):
- Contentful (~> 5.2.0)

DEPENDENCIES:
Expand All @@ -28,8 +27,8 @@ SPEC CHECKSUMS:
Alamofire: 814429acc853c6c54ff123fc3d2ef66803823ce0
AlamofireImage: c4a2ba349885fb3064feb74d2e547bd42ce9be10
Contentful: 4973186d957d01c4868107fa4bda2149cd5ea107
ContentfulRichTextRenderer: c3ad3e3811139ec1da8c65632b5e135f86853cd5
ContentfulRichTextRenderer: 0d3b6b4c2c0c2e3fede36085f1b1d7e51bf22c1e

PODFILE CHECKSUM: 5f25e3c0ed7f21cbf643044d554ad42e477ccf5e
PODFILE CHECKSUM: 5e7224c36a1c05a1a1f009edea0ac852781555cd

COCOAPODS: 1.9.3
7 changes: 2 additions & 5 deletions Pods/Local Podspecs/ContentfulRichTextRenderer.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 00efa27

Please sign in to comment.