Skip to content

Commit

Permalink
Replace PoolsDialogComponent with PoolsComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroBrine1st committed Apr 2, 2024
1 parent 5bc45f8 commit 415e597
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ interface PoolsComponent {
@Stable
val loadingPools: Boolean

fun openPool(pool: Pool)
fun onClick(pool: Pool)

fun dismiss()
fun activate()

fun onDismiss()

sealed interface PoolState {
val id: PoolId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,38 @@ import ru.herobrine1st.e621.api.API
import ru.herobrine1st.e621.api.model.Pool
import ru.herobrine1st.e621.navigation.LifecycleScope
import ru.herobrine1st.e621.navigation.component.post.PoolsComponent.PoolState
import ru.herobrine1st.e621.util.ExceptionReporter

private const val TAG = "PoolsComponentImpl"

class PoolsComponentImpl(
postState: Value<PostState>,
private val api: API,
private val exceptionReporter: ExceptionReporter,
private val openPool: (Pool) -> Unit,
private val onActivateRequest: () -> Unit,
private val onDismissRequest: () -> Unit,
componentContext: ComponentContext,
) : PoolsComponent, ComponentContext by componentContext {
val lifecycleScope = LifecycleScope()

// TODO save state
// It is probably better to re-download all pools than caching them locally
override val pools = mutableStateListOf<PoolState>()

override var showPools by mutableStateOf(false)
private set
override var loadingPools by mutableStateOf(false)
private set

override fun openPool(pool: Pool) {
override fun onClick(pool: Pool) {
openPool.invoke(pool)
}

override fun dismiss() {
override fun activate() {
onActivateRequest()
}

override fun onDismiss() {
onDismissRequest()
}

Expand All @@ -71,6 +78,7 @@ class PoolsComponentImpl(
if (state !is PostState.Ready) return@observe
if (pools.map { it.id } == state.post.pools) return@observe

// Reuse old pools
val loadedPoolsCache = pools.toList()
.filterIsInstance<PoolState.Successful>()
.associateBy { it.id }
Expand All @@ -90,8 +98,11 @@ class PoolsComponentImpl(
"UB: Component is resumed before post information is available"
)
if (pools.isEmpty()) Log.wtf(TAG, "Component is resumed with no post pools")

showPools = pools.size > 1
downloadPools()

if (pools.all { it is PoolState.Successful }) goToSinglePool()
else downloadPools()
},
onPause = {
showPools = false
Expand All @@ -101,21 +112,29 @@ class PoolsComponentImpl(
}

private fun downloadPools() {
if (pools.none { it is PoolState.NotLoaded }) return
downloadJob?.cancel()
downloadJob = lifecycleScope.launch {
loadingPools = true
pools.forEachIndexed { index, poolState ->
if (poolState is PoolState.NotLoaded) {
if (poolState is PoolState.NotLoaded || poolState is PoolState.Error) {
pools[index] = api.getPool(poolState.id).map {
PoolState.Successful(it)
}.recover {
exceptionReporter.handleRequestException(it, dontShowSnackbar = true)
PoolState.Error(poolState.id)
}.getOrThrow()
}
}
loadingPools = false
downloadJob = null
goToSinglePool()
}
}

/**
* Automatically goes to pool if there's only one
*/
private fun goToSinglePool() {
(pools.singleOrNull() as? PoolState.Successful)?.pool?.let(openPool::invoke)
}
}

This file was deleted.

Loading

0 comments on commit 415e597

Please sign in to comment.