diff --git a/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditScreen.kt b/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditScreen.kt index a59ba9a..f4509d5 100644 --- a/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditScreen.kt +++ b/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditScreen.kt @@ -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 @@ -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 ) { diff --git a/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditViewModel.kt b/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditViewModel.kt index a3fdbbb..b59c886 100644 --- a/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditViewModel.kt +++ b/app/src/main/java/dev/arkbuilders/arkretouch/edition/ui/main/EditViewModel.kt @@ -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 @@ -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() @@ -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.loadInto( - editManager: EditManager -) { - into(object : CustomTarget() { - override fun onResourceReady( - bitmap: Bitmap, - transition: Transition? - ) { - editManager.apply { - val image = bitmap.asImageBitmap() - backgroundImage.value = image - setOriginalBackgroundImage(image) - scaleToFit() - } - } - - override fun onLoadCleared(placeholder: Drawable?) {} - }) -} - fun resize( imageBitmap: ImageBitmap, maxWidth: Int, diff --git a/app/src/main/java/dev/arkbuilders/arkretouch/utils/ImageLoading.kt b/app/src/main/java/dev/arkbuilders/arkretouch/utils/ImageLoading.kt new file mode 100644 index 0000000..d8dce18 --- /dev/null +++ b/app/src/main/java/dev/arkbuilders/arkretouch/utils/ImageLoading.kt @@ -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.loadInto( + editManager: EditManager +) { + into(object : CustomTarget() { + override fun onResourceReady( + bitmap: Bitmap, + transition: Transition? + ) { + editManager.apply { + val image = bitmap.asImageBitmap() + backgroundImage.value = image + setOriginalBackgroundImage(image) + scaleToFit() + } + } + + override fun onLoadCleared(placeholder: Drawable?) {} + }) +} \ No newline at end of file