-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract image loading from EditViewModel file
- Loading branch information
Showing
3 changed files
with
69 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
app/src/main/java/dev/arkbuilders/arkretouch/utils/ImageLoading.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?) {} | ||
}) | ||
} |