Skip to content

Commit

Permalink
Extract image loading from EditViewModel file
Browse files Browse the repository at this point in the history
  • Loading branch information
shubertm committed Jan 22, 2024
1 parent 7178dbb commit 6eba793
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ import dev.arkbuilders.arkretouch.presentation.edit.resize.delayHidingHint
import dev.arkbuilders.arkretouch.storage.Resolution
import dev.arkbuilders.arkretouch.ui.theme.Gray
import dev.arkbuilders.arkretouch.utils.getActivity
import dev.arkbuilders.arkretouch.utils.loadImageWithPath
import dev.arkbuilders.arkretouch.utils.loadImageWithUri
import dev.arkbuilders.arkretouch.utils.permission.isWritePermissionGranted
import dev.arkbuilders.arkretouch.utils.permission.requestWritePermissions
import org.koin.androidx.compose.koinViewModel
Expand Down Expand Up @@ -353,7 +355,10 @@ private fun DrawContainer(
}
}
}
viewModel.loadImage(context = context)
viewModel.loadImage(
loadByPath = { path, editManager -> loadImageWithPath(context, path, editManager) },
loadByUri = { uri, editManager -> loadImageWithUri(context, uri, editManager) }
)
},
contentAlignment = Alignment.Center
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@ import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.unit.IntSize
import androidx.compose.ui.unit.toSize
import androidx.core.net.toUri
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Matrix
import android.graphics.drawable.Drawable
import android.net.Uri
import android.view.MotionEvent
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import dev.arkbuilders.arkretouch.edition.manager.EditManager
import dev.arkbuilders.arkretouch.edition.model.EditionState
import dev.arkbuilders.arkretouch.edition.model.ImageViewParams
Expand Down Expand Up @@ -109,22 +101,14 @@ class EditViewModel(
editionState = editionState.copy(strokeSliderExpanded = isExpanded)
}

fun loadImage(context: Context) {
fun loadImage(loadByPath: (Path, EditManager) -> Unit, loadByUri: (String, EditManager) -> Unit) {
isLoaded = true
imagePath?.let {
loadImageWithPath(
context,
imagePath,
editManager
)
loadByPath(it, editManager)
return
}
imageUri?.let {
loadImageWithUri(
context,
imageUri,
editManager
)
loadByUri(it, editManager)
return
}
editManager.scaleToFit()
Expand Down Expand Up @@ -385,56 +369,6 @@ class EditViewModel(
}
}

// FIXME: Remove context or this function from here
private fun loadImageWithPath(
context: Context,
image: Path,
editManager: EditManager
) {
initGlideBuilder(context)
.load(image.toFile())
.loadInto(editManager)
}

// FIXME: Remove context or this function from here
private fun loadImageWithUri(
context: Context,
uri: String,
editManager: EditManager
) {
initGlideBuilder(context)
.load(uri.toUri())
.loadInto(editManager)
}

// FIXME: Remove context or this function from here
private fun initGlideBuilder(context: Context) = Glide
.with(context)
.asBitmap()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)

// FIXME: Remove context or this function from here
private fun RequestBuilder<Bitmap>.loadInto(
editManager: EditManager
) {
into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(
bitmap: Bitmap,
transition: Transition<in Bitmap>?
) {
editManager.apply {
val image = bitmap.asImageBitmap()
backgroundImage.value = image
setOriginalBackgroundImage(image)
scaleToFit()
}
}

override fun onLoadCleared(placeholder: Drawable?) {}
})
}

fun resize(
imageBitmap: ImageBitmap,
maxWidth: Int,
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkretouch/utils/ImageLoading.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package dev.arkbuilders.arkretouch.utils

import androidx.compose.ui.graphics.asImageBitmap
import androidx.core.net.toUri
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import dev.arkbuilders.arkretouch.edition.manager.EditManager
import java.nio.file.Path

fun loadImageWithPath(
context: Context,
image: Path,
editManager: EditManager
) {
initGlideBuilder(context)
.load(image.toFile())
.loadInto(editManager)
}

fun loadImageWithUri(
context: Context,
uri: String,
editManager: EditManager
) {
initGlideBuilder(context)
.load(uri.toUri())
.loadInto(editManager)
}

private fun initGlideBuilder(context: Context) = Glide
.with(context)
.asBitmap()
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.NONE)

private fun RequestBuilder<Bitmap>.loadInto(
editManager: EditManager
) {
into(object : CustomTarget<Bitmap>() {
override fun onResourceReady(
bitmap: Bitmap,
transition: Transition<in Bitmap>?
) {
editManager.apply {
val image = bitmap.asImageBitmap()
backgroundImage.value = image
setOriginalBackgroundImage(image)
scaleToFit()
}
}

override fun onLoadCleared(placeholder: Drawable?) {}
})
}

0 comments on commit 6eba793

Please sign in to comment.