Skip to content

Commit

Permalink
Merge pull request #3 from dgomolka/master
Browse files Browse the repository at this point in the history
AdapterList fix
  • Loading branch information
mvarnagiris authored May 15, 2020
2 parents 3e2cf09 + 334c720 commit a9f10af
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
11 changes: 8 additions & 3 deletions compose-glide-image/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ android {
}

composeOptions {
kotlinCompilerExtensionVersion "0.1.0-dev10"
kotlinCompilerExtensionVersion "0.1.0-dev11"
kotlinCompilerVersion "1.3.70-dev-withExperimentalGoogleExtensions-20200424"
}
}

dependencies {
def compose_version = '0.1.0-dev10'
def compose_version = '0.1.0-dev11'
def coroutines_version = "1.3.6"

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "androidx.compose:compose-runtime:$compose_version"
implementation "androidx.ui:ui-framework:$compose_version"
implementation "androidx.ui:ui-core:$compose_version"
implementation "androidx.ui:ui-foundation:$compose_version"
api 'com.github.bumptech.glide:glide:4.11.0'

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutines_version"

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package com.koduok.compose.glideimage
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import androidx.compose.Composable
import androidx.compose.onCommit
import androidx.compose.FrameManager
import androidx.compose.onPreCommit
import androidx.compose.state
import androidx.ui.core.ContextAmbient
import androidx.ui.core.Modifier
Expand All @@ -12,62 +13,74 @@ import androidx.ui.foundation.Canvas
import androidx.ui.foundation.Image
import androidx.ui.graphics.ImageAsset
import androidx.ui.graphics.asImageAsset
import androidx.ui.graphics.painter.drawCanvas
import androidx.ui.layout.fillMaxSize
import androidx.ui.unit.IntPx
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.Target.SIZE_ORIGINAL
import com.bumptech.glide.request.transition.Transition
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun GlideImage(
model: Any,
onImageReady: (() -> Unit)? = null,
customize: RequestBuilder<Bitmap>.() -> RequestBuilder<Bitmap> = { this }
) {
WithConstraints { constraints, _ ->
WithConstraints {
val image = state<ImageAsset?> { null }
val drawable = state<Drawable?> { null }
val context = ContextAmbient.current

onCommit(model) {
val target = object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {
image.value = null
drawable.value = placeholder
}
onPreCommit(model) {
val glide = Glide.with(context)
var target: CustomTarget<Bitmap>? = null
val job = CoroutineScope(Dispatchers.Main).launch {
target = object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {
image.value = null
drawable.value = placeholder
}

override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
image.value = resource.asImageAsset()
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
FrameManager.framed {
image.value = resource.asImageAsset()
onImageReady?.invoke()
}
}
}
}

val width =
if (constraints.maxWidth > IntPx.Zero && constraints.maxWidth < IntPx.Infinity) {
constraints.maxWidth.value
} else {
SIZE_ORIGINAL
}
val width =
if (constraints.maxWidth > IntPx.Zero && constraints.maxWidth < IntPx.Infinity) {
constraints.maxWidth.value
} else {
SIZE_ORIGINAL
}

val height =
if (constraints.maxHeight > IntPx.Zero && constraints.maxHeight < IntPx.Infinity) {
constraints.maxHeight.value
} else {
SIZE_ORIGINAL
}
val height =
if (constraints.maxHeight > IntPx.Zero && constraints.maxHeight < IntPx.Infinity) {
constraints.maxHeight.value
} else {
SIZE_ORIGINAL
}

val glide = Glide.with(context)
glide
.asBitmap()
.load(model)
.override(width, height)
.let(customize)
.into(target)
glide
.asBitmap()
.load(model)
.override(width, height)
.let(customize)
.into(target!!)
}

onDispose {
image.value = null
drawable.value = null
glide.clear(target)
job.cancel()
}
}

Expand All @@ -76,7 +89,9 @@ fun GlideImage(
if (theImage != null) {
Image(asset = theImage)
} else if (theDrawable != null) {
Canvas(modifier = Modifier.fillMaxSize()) { theDrawable.draw(nativeCanvas) }
Canvas(modifier = Modifier.fillMaxSize()) {
drawCanvas { canvas, pxSize -> theDrawable.draw(canvas.nativeCanvas) }
}
}
}
}

0 comments on commit a9f10af

Please sign in to comment.