Moves audiothumbnail loader to the viewModel scope

This commit is contained in:
Vitor Pamplona
2023-10-02 14:43:49 -04:00
parent 227aac5b76
commit 4b36217699
2 changed files with 30 additions and 10 deletions
@@ -98,24 +98,23 @@ fun LoadThumbAndThenVideoView(
onDialog: ((Boolean) -> Unit)? = null
) {
var loadingFinished by remember { mutableStateOf<Pair<Boolean, Drawable?>>(Pair(false, null)) }
val context = LocalContext.current
LaunchedEffect(Unit) {
launch(Dispatchers.IO) {
try {
val request = ImageRequest.Builder(context).data(thumbUri).build()
val myCover = context.imageLoader.execute(request).drawable
if (myCover != null) {
loadingFinished = Pair(true, myCover)
accountViewModel.loadThumb(
context,
thumbUri,
onReady = {
if (it != null) {
loadingFinished = Pair(true, it)
} else {
loadingFinished = Pair(true, null)
}
} catch (e: Exception) {
Log.e("VideoView", "Fail to load cover $thumbUri", e)
},
onError = {
loadingFinished = Pair(true, null)
}
}
)
}
if (loadingFinished.first) {
@@ -1,6 +1,7 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn
import android.content.Context
import android.graphics.drawable.Drawable
import android.util.Log
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.Stable
@@ -11,6 +12,8 @@ import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.map
import androidx.lifecycle.viewModelScope
import coil.imageLoader
import coil.request.ImageRequest
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.AccountState
import com.vitorpamplona.amethyst.model.AddressableNote
@@ -919,6 +922,24 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao {
collectorJob?.cancel()
super.onCleared()
}
fun loadThumb(
context: Context,
thumbUri: String,
onReady: (Drawable?) -> Unit,
onError: (String?) -> Unit
) {
viewModelScope.launch(Dispatchers.IO) {
try {
val request = ImageRequest.Builder(context).data(thumbUri).build()
val myCover = context.imageLoader.execute(request).drawable
onReady(myCover)
} catch (e: Exception) {
Log.e("VideoView", "Fail to load cover $thumbUri", e)
onError(e.message)
}
}
}
}
class HasNotificationDot(bottomNavigationItems: ImmutableList<Route>, val account: Account) {