Skip to content

Commit

Permalink
Fix code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Hussein Aladeen committed Jan 11, 2024
1 parent d447e5e commit 8bca98a
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 114 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[*.{kt,kts}]
ktlint_code_style=android_studio
ktlint_function_naming_ignore_when_annotated_with=Composable
11 changes: 6 additions & 5 deletions glide/src/main/java/com/husseinala/neon/glide/Glide.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ private fun context() = LocalContext.current
@Composable
fun ProvideGlideLoader(
requestManager: RequestManager = Glide.with(context()),
children: @Composable () -> Unit
children: @Composable () -> Unit,
) {
ProvideImageLoader(
imageLoader = GlideImageLoader(
requestManager
),
children = children
imageLoader =
GlideImageLoader(
requestManager,
),
children = children,
)
}
112 changes: 58 additions & 54 deletions glide/src/main/java/com/husseinala/neon/glide/GlideImageLoader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,43 +26,46 @@ import com.husseinala.neon.core.ScaleTypeTransformation
private typealias GlideTarget<T> = com.bumptech.glide.request.target.Target<T>

class GlideImageLoader(
private val requestManager: RequestManager
private val requestManager: RequestManager,
) : ImageLoader {

override fun getImage(
imageConfig: ImageConfig<*>,
onSuccess: (ImageBitmap) -> Unit,
onFailure: (Exception) -> Unit
onFailure: (Exception) -> Unit,
): Cancelable {
val size = if (imageConfig.size == IntSize.Zero) {
IntSize(GlideTarget.SIZE_ORIGINAL, GlideTarget.SIZE_ORIGINAL)
} else {
imageConfig.size
}
val size =
if (imageConfig.size == IntSize.Zero) {
IntSize(GlideTarget.SIZE_ORIGINAL, GlideTarget.SIZE_ORIGINAL)
} else {
imageConfig.size
}

val transformations = imageConfig.transformation.mapNotNull { value ->
when (value) {
is ScaleTypeTransformation -> when (value.scaleType) {
ScaleType.CENTER_CROP -> CenterCrop()
ScaleType.CENTER_INSIDE -> CenterInside()
val transformations =
imageConfig.transformation.mapNotNull { value ->
when (value) {
is ScaleTypeTransformation ->
when (value.scaleType) {
ScaleType.CENTER_CROP -> CenterCrop()
ScaleType.CENTER_INSIDE -> CenterInside()
}
is CircleCropTransformation -> CircleCrop()
is RoundedCornersTransformation -> RoundedCorners(value.radius)
else -> null
}
is CircleCropTransformation -> CircleCrop()
is RoundedCornersTransformation -> RoundedCorners(value.radius)
else -> null
}
}.toTypedArray()
}.toTypedArray()

val requestOptions = RequestOptions().transform(*transformations)

val future = requestManager
.asBitmap()
.load(imageConfig.id.value)
.apply(requestOptions)
.listener(
onSuccess = { onSuccess(it.asImageBitmap()) },
onFailure = onFailure
)
.submit(size.width, size.height)
val future =
requestManager
.asBitmap()
.load(imageConfig.id.value)
.apply(requestOptions)
.listener(
onSuccess = { onSuccess(it.asImageBitmap()) },
onFailure = onFailure,
)
.submit(size.width, size.height)

return Cancelable {
future.cancel(true)
Expand All @@ -72,36 +75,37 @@ class GlideImageLoader(

private inline fun <T> RequestBuilder<T>.listener(
crossinline onSuccess: (T) -> Unit,
crossinline onFailure: (Exception) -> Unit = { }
): RequestBuilder<T> = listener(
object : RequestListener<T> {
override fun onLoadFailed(
e: GlideException?,
model: Any,
target: com.bumptech.glide.request.target.Target<T>,
isFirstResource: Boolean
): Boolean {
runOnUiThread {
onFailure(e ?: Exception("Unknown Exception"))
crossinline onFailure: (Exception) -> Unit = { },
): RequestBuilder<T> =
listener(
object : RequestListener<T> {
override fun onLoadFailed(
e: GlideException?,
model: Any,
target: com.bumptech.glide.request.target.Target<T>,
isFirstResource: Boolean,
): Boolean {
runOnUiThread {
onFailure(e ?: Exception("Unknown Exception"))
}
return true
}
return true
}

override fun onResourceReady(
resource: T,
model: Any,
target: GlideTarget<T>,
dataSource: DataSource,
isFirstResource: Boolean
): Boolean {
runOnUiThread {
onSuccess(resource)
}
override fun onResourceReady(
resource: T,
model: Any,
target: GlideTarget<T>,
dataSource: DataSource,
isFirstResource: Boolean,
): Boolean {
runOnUiThread {
onSuccess(resource)
}

return true
}
}
)
return true
}
},
)

private fun runOnUiThread(action: () -> Unit) {
Handler(Looper.getMainLooper()).post { action() }
Expand Down
11 changes: 6 additions & 5 deletions picasso/src/main/java/com/husseinala/neon/picasso/Picasso.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import com.squareup.picasso.Picasso
@Composable
fun ProvidePicassoLoader(
picasso: Picasso = Picasso.get(),
children: @Composable () -> Unit
children: @Composable () -> Unit,
) {
ProvideImageLoader(
imageLoader = PicassoImageLoader(
picasso
),
children = children
imageLoader =
PicassoImageLoader(
picasso,
),
children = children,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,32 @@ import jp.wasabeef.picasso.transformations.CropCircleTransformation
import jp.wasabeef.picasso.transformations.RoundedCornersTransformation as PicassoRoundedCorners

class PicassoImageLoader(
private val picasso: Picasso
private val picasso: Picasso,
) : ImageLoader {

override fun getImage(
imageConfig: ImageConfig<*>,
onSuccess: (ImageBitmap) -> Unit,
onFailure: (Exception) -> Unit
onFailure: (Exception) -> Unit,
): Cancelable {
val id = imageConfig.id

val request = when (id) {
is ImageId.Path -> picasso.load(id.value)
is ImageId.Uri -> picasso.load(id.value)
is ImageId.File -> picasso.load(id.value)
is ImageId.Resource -> picasso.load(id.value)
}.apply {
if (imageConfig.size != IntSize.Zero) {
resize(imageConfig.size.width, imageConfig.size.height)
}
val request =
when (id) {
is ImageId.Path -> picasso.load(id.value)
is ImageId.Uri -> picasso.load(id.value)
is ImageId.File -> picasso.load(id.value)
is ImageId.Resource -> picasso.load(id.value)
}.apply {
if (imageConfig.size != IntSize.Zero) {
resize(imageConfig.size.width, imageConfig.size.height)
}

applyTransformations(imageConfig.transformation)
}.fetch(onSuccess = onSuccess, onFailure = onFailure)
applyTransformations(imageConfig.transformation)
}.fetch(onSuccess = onSuccess, onFailure = onFailure)

return Cancelable {
picasso.cancelRequest(
request
request,
)
}
}
Expand All @@ -55,38 +55,47 @@ class PicassoImageLoader(
private fun RequestCreator.applyTransformations(transformations: Transformation) {
transformations.forEach { transformation ->
when (transformation) {
is ScaleTypeTransformation -> when (transformation.scaleType) {
ScaleType.CENTER_CROP -> centerCrop()
ScaleType.CENTER_INSIDE -> centerInside()
}
is ScaleTypeTransformation ->
when (transformation.scaleType) {
ScaleType.CENTER_CROP -> centerCrop()
ScaleType.CENTER_INSIDE -> centerInside()
}
is CircleCropTransformation -> transform(CropCircleTransformation())
is RoundedCornersTransformation -> transform(
PicassoRoundedCorners(
transformation.radius,
0
is RoundedCornersTransformation ->
transform(
PicassoRoundedCorners(
transformation.radius,
0,
),
)
)
}
}
}

private fun RequestCreator.fetch(
onSuccess: (ImageBitmap) -> Unit,
onFailure: (Exception) -> Unit
onFailure: (Exception) -> Unit,
): Target {
val target = object : Target {
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
// not used
}
val target =
object : Target {
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
// not used
}

override fun onBitmapFailed(exception: Exception, errorDrawable: Drawable?) {
onFailure(exception)
}
override fun onBitmapFailed(
exception: Exception,
errorDrawable: Drawable?,
) {
onFailure(exception)
}

override fun onBitmapLoaded(bitmap: Bitmap, from: Picasso.LoadedFrom?) {
onSuccess(bitmap.asImageBitmap())
override fun onBitmapLoaded(
bitmap: Bitmap,
from: Picasso.LoadedFrom?,
) {
onSuccess(bitmap.asImageBitmap())
}
}
}

into(target)

Expand Down
13 changes: 7 additions & 6 deletions sample/src/main/java/com/husseinala/neon/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,19 @@ fun GlideSample(requestManager: RequestManager) {
@Composable
fun SampleScreen(title: String) {
Scaffold(
topBar = { TopAppBar(title = { Text(text = title) }) }
topBar = { TopAppBar(title = { Text(text = title) }) },
) {
LazyColumn {
items(Samples.images) { url ->
Neon(
url = url,
transformation = Transformation.centerCrop().roundedCorners(radius = 16.dp),
modifier = Modifier
.fillParentMaxWidth()
.aspectRatio(1.7f)
.padding(horizontal = 16.dp, vertical = 8.dp),
onLoading = { Center { CircularProgressIndicator() } }
modifier =
Modifier
.fillParentMaxWidth()
.aspectRatio(1.7f)
.padding(horizontal = 16.dp, vertical = 8.dp),
onLoading = { Center { CircularProgressIndicator() } },
)
}
}
Expand Down
19 changes: 10 additions & 9 deletions sample/src/main/java/com/husseinala/neon/sample/Samples.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.husseinala.neon.sample

object Samples {
val images = listOf(
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/3302324998.jpg",
"https://2.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/3189491900.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/0664614529.jpg",
"https://2.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/7303805396.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/6284642986.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/1007578251.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/3237211633.jpg"
)
val images =
listOf(
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/3302324998.jpg",
"https://2.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/3189491900.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/0664614529.jpg",
"https://2.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/7303805396.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/6284642986.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/1007578251.jpg",
"https://1.img-dpreview.com/files/p/TS1800x1200~sample_galleries/9229212315/3237211633.jpg",
)
}

0 comments on commit 8bca98a

Please sign in to comment.