Skip to content

Commit

Permalink
sample, add regular coil sample
Browse files Browse the repository at this point in the history
  • Loading branch information
noties committed Aug 26, 2020
1 parent 8cea2e0 commit 2356dd4
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 33 deletions.
12 changes: 9 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -16,7 +22,7 @@
* `ext-tables` - fix column width rounding issue

[#272]: https://github.com/noties/Markwon/issues/272
[#274]: https://github.com/noties/Markwon/issues/274
[#274]: https://github.com/noties/Markwon/issues/274


# 4.5.0
Expand Down
12 changes: 12 additions & 0 deletions app-sample/samples.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
94 changes: 64 additions & 30 deletions docs/docs/v4/image-coil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()

```

0 comments on commit 2356dd4

Please sign in to comment.