Skip to content

Commit fb61051

Browse files
author
Christian Elies
committed
docs(readme): improvements
1 parent 1d49652 commit fb61051

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,21 @@ RemoteImage(type: .phAsset(localIdentifier: "541D4013-D51C-463C-AD85-0A1E4EA838F
5555
})
5656
```
5757

58-
## Custom `RemoteImageService`
58+
## Custom `RemoteImageURLDataPublisher`
5959

60-
If you want complete control over the service responsible for managing the state of the view and for fetching the image you could pass an object conforming to the `RemoteImageService` to the related initializer:
60+
Under the hood the `URLSession.shared` is used by default as the `RemoteImageURLDataPublisher` to fetch the image at the specified URL.
61+
You can specify a custom publisher through the`remoteImageURLDataPublisher` parameter.
62+
As an example that's how you could add support for low data mode to the `RemoteImage` view.
6163

6264
```swift
6365
let url = URL(string: "https://images.unsplash.com/photo-1524419986249-348e8fa6ad4a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80")!
6466

65-
RemoteImage(type: .url(url), service: yourCustomService, errorView: { error in
67+
RemoteImage(type: .url(url), remoteImageURLDataPublisher: {
68+
let configuration = URLSessionConfiguration.default
69+
// Enable low data mode support
70+
configuration.allowsConstrainedNetworkAccess = false
71+
return URLSession(configuration: configuration)
72+
}(), errorView: { error in
6673
Text(error.localizedDescription)
6774
}, imageView: { image in
6875
image
@@ -73,21 +80,36 @@ RemoteImage(type: ./service/http://github.com/url(url), service: yourCustomService, errorView: { error in
7380
})
7481
```
7582

76-
## Custom `RemoteImageURLDataPublisher`
83+
## Custom `RemoteImageService`
7784

78-
Under the hood by default the `URLSession.shared` is used as the `RemoteImageURLDataPublisher` to fetch the image at the specified URL.
79-
You can specify a custom publisher through the`remoteImageURLDataPublisher` parameter.
80-
As an example that's how you could add support for low data mode to the `RemoteImage` view.
85+
If you want complete control over the service responsible for managing the state of the view and for fetching the image you could pass an object conforming to the `RemoteImageService` protocol to the related initializer:
8186

8287
```swift
88+
final class CustomService: RemoteImageService { ... }
89+
8390
let url = URL(string: "https://images.unsplash.com/photo-1524419986249-348e8fa6ad4a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80")!
8491

85-
RemoteImage(type: .url(url), remoteImageURLDataPublisher: {
86-
let configuration = URLSessionConfiguration.default
87-
// Enable low data mode support
88-
configuration.allowsConstrainedNetworkAccess = false
89-
return URLSession(configuration: configuration)
90-
}(), errorView: { error in
92+
RemoteImage(type: .url(url), service: CustomService(), errorView: { error in
93+
Text(error.localizedDescription)
94+
}, imageView: { image in
95+
image
96+
.resizable()
97+
.aspectRatio(contentMode: .fit)
98+
}, loadingView: {
99+
Text("Loading ...")
100+
})
101+
```
102+
103+
In addition to that you could use the new `@StateObject` property wrapper introcuded in Swift by creating an instance of the default built-in `RemoteImageService` and using the above initializer:
104+
105+
```swift
106+
@StateObject var service = DefaultRemoteImageServiceFactory.makeDefaultRemoteImageService()
107+
// or
108+
@StateObject var service = DefaultRemoteImageServiceFactory.makeDefaultRemoteImageService(remoteImageURLDataPublisher: yourRemoteImageURLDataPublisher)
109+
110+
let url = URL(string: "https://images.unsplash.com/photo-1524419986249-348e8fa6ad4a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80")!
111+
112+
RemoteImage(type: .url(url), service: service, errorView: { error in
91113
Text(error.localizedDescription)
92114
}, imageView: { image in
93115
image

0 commit comments

Comments
 (0)