sample, add regular coil sample

This commit is contained in:
Dimitry Ivanov 2020-08-26 13:21:48 +03:00
parent 8cea2e0202
commit 2356dd4618
4 changed files with 178 additions and 33 deletions

View File

@ -3,8 +3,14 @@
# SNAPSHOT
#### Changed
* `image-glide`: update to `4.11.0` version
* `inline-parser`: revert parsing index when `InlineProcessor` returns `null` as result
* `image-glide` - update to `4.11.0` version
* `inline-parser` - revert parsing index when `InlineProcessor` returns `null` as result
* `image-coil` - update `Coil` to `0.12.0` ([Coil changelog](https://coil-kt.github.io/coil/changelog/)) ([#284])<br>Thanks [@magnusvs]
[#284]: https://github.com/noties/Markwon/pull/284
[@magnusvs]: https://github.com/magnusvs
# 4.5.1

View File

@ -1,4 +1,16 @@
[
{
"javaClassName": "io.noties.markwon.app.samples.image.CoilImageSample",
"id": "20200826101209",
"title": "Coil image",
"description": "",
"artifacts": [
"IMAGE_COIL"
],
"tags": [
"image"
]
},
{
"javaClassName": "io.noties.markwon.app.samples.JustifyModeSample",
"id": "20200826084338",

View File

@ -0,0 +1,93 @@
package io.noties.markwon.app.samples.image
import coil.ImageLoader
import coil.request.Disposable
import coil.request.ImageRequest
import coil.transform.CircleCropTransformation
import io.noties.markwon.Markwon
import io.noties.markwon.app.sample.Tags
import io.noties.markwon.app.sample.ui.MarkwonTextViewSample
import io.noties.markwon.image.AsyncDrawable
import io.noties.markwon.image.coil.CoilImagesPlugin
import io.noties.markwon.sample.annotations.MarkwonArtifact
import io.noties.markwon.sample.annotations.MarkwonSampleInfo
@MarkwonSampleInfo(
id = "20200826101209",
title = "Coil image",
artifacts = [MarkwonArtifact.IMAGE_COIL],
tags = [Tags.image]
)
class CoilImageSample : MarkwonTextViewSample() {
override fun render() {
val md = """
# H1
## H2
### H3
#### H4
##### H5
> a quote
+ one
- two
* three
1. one
1. two
1. three
---
# Images
![img](https://picsum.photos/id/237/1024/800)
""".trimIndent()
// pick one
val markwon = Markwon.builder(context)
// .usePlugin(coilPlugin1)
// .usePlugin(coilPlugin2)
.usePlugin(coilPlugin3)
.build()
markwon.setMarkdown(textView, md)
}
val coilPlugin1: CoilImagesPlugin
get() = CoilImagesPlugin.create(context)
val coilPlugin2: CoilImagesPlugin
get() = CoilImagesPlugin.create(context, imageLoader)
val coilPlugin3: CoilImagesPlugin
get() {
val loader = imageLoader
return CoilImagesPlugin.create(
object : CoilImagesPlugin.CoilStore {
override fun load(drawable: AsyncDrawable): ImageRequest {
return ImageRequest.Builder(context)
.defaults(loader.defaults)
.data(drawable.destination)
.crossfade(true)
.transformations(CircleCropTransformation())
.build()
}
override fun cancel(disposable: Disposable) {
disposable.dispose()
}
},
loader
)
}
val imageLoader: ImageLoader
get() = ImageLoader.Builder(context)
.apply {
availableMemoryPercentage(0.5)
bitmapPoolPercentage(0.5)
crossfade(true)
}
.build()
}

View File

@ -2,39 +2,73 @@
<MavenBadge4 :artifact="'image-coil'" />
Image loading based on `Coil` library.
Image loading based on [Coil](https://github.com/coil-kt/coil/) library.
There are 3 factory methods to obtain `CoilImagesPlugin`:
```kotlin
val markwon = Markwon.builder(context)
// automatically create Coil instance
.usePlugin(CoilImagesPlugin.create(context))
// use supplied ImageLoader instance
.usePlugin(CoilImagesPlugin.create(
context,
ImageLoader(context) {
availableMemoryPercentage(0.5)
bitmapPoolPercentage(0.5)
crossfade(true)
}
))
// if you need more control
.usePlugin(CoilImagesPlugin.create(object : CoilImagesPlugin.CoilStore {
override fun load(drawable: AsyncDrawable): LoadRequest {
return LoadRequest(context, customImageLoader.defaults) {
data(drawable.destination)
crossfade(true)
transformations(CircleCropTransformation())
}
}
override cancel(disposable: RequestDisposable) {
disposable.dispose()
}
}, customImageLoader))
.build()
val coilPlugin: CoilImagesPlugin
get() = CoilImagesPlugin.create(context)
```
:::warning
In order to use the `CoilImagesPlugin.create(Context)` factory method your
app must have **explicit** dependency on `coil` library
app must have **explicit** dependency on `coil` (`io.coil-kt:coil`) library. This artifact
relies on `io.coil-kt:coil-base` as per [Coil documentation](https://coil-kt.github.io/coil/getting_started/#artifacts)
:::
```kotlin
val coilPlugin: CoilImagesPlugin
get() = CoilImagesPlugin.create(context, imageLoader)
val imageLoader: ImageLoader
get() = ImageLoader.Builder(context)
.apply {
availableMemoryPercentage(0.5)
bitmapPoolPercentage(0.5)
crossfade(true)
}
.build()
```
```kotlin
val coilPlugin: CoilImagesPlugin
get() {
val loader = imageLoader
return CoilImagesPlugin.create(
object : CoilImagesPlugin.CoilStore {
override fun load(drawable: AsyncDrawable): ImageRequest {
return ImageRequest.Builder(context)
.defaults(loader.defaults)
.data(drawable.destination)
.crossfade(true)
.transformations(CircleCropTransformation())
.build()
}
override fun cancel(disposable: Disposable) {
disposable.dispose()
}
},
loader
)
}
val imageLoader: ImageLoader
get() = ImageLoader.Builder(context)
.apply {
availableMemoryPercentage(0.5)
bitmapPoolPercentage(0.5)
crossfade(true)
}
.build()
```
Finally, use as a regular plugin:
```kotlin
val markwon = Markwon.builder(context)
.usePlugin(coilPlugin)
.build()
```