Refresh watching rows first

This commit is contained in:
Damontecres 2026-02-05 12:15:29 -05:00
parent 3f4ec24569
commit 70ad1ecdbf
No known key found for this signature in database

View file

@ -6,6 +6,7 @@ import androidx.lifecycle.viewModelScope
import com.github.damontecres.wholphin.data.NavDrawerItemRepository import com.github.damontecres.wholphin.data.NavDrawerItemRepository
import com.github.damontecres.wholphin.data.ServerRepository import com.github.damontecres.wholphin.data.ServerRepository
import com.github.damontecres.wholphin.data.model.BaseItem import com.github.damontecres.wholphin.data.model.BaseItem
import com.github.damontecres.wholphin.data.model.HomeRowConfig
import com.github.damontecres.wholphin.services.BackdropService import com.github.damontecres.wholphin.services.BackdropService
import com.github.damontecres.wholphin.services.DatePlayedService import com.github.damontecres.wholphin.services.DatePlayedService
import com.github.damontecres.wholphin.services.FavoriteWatchManager import com.github.damontecres.wholphin.services.FavoriteWatchManager
@ -56,7 +57,7 @@ class HomeViewModel
init { init {
datePlayedService.invalidateAll() datePlayedService.invalidateAll()
init() // init()
} }
fun init() { fun init() {
@ -86,18 +87,17 @@ class HomeViewModel
state.loadingState == LoadingState.Success && state.settings == settings state.loadingState == LoadingState.Success && state.settings == settings
val semaphore = Semaphore(4) val semaphore = Semaphore(4)
val loadingRows =
if (refresh) { val watchingRowIndexes =
state.homeRows settings.rows
} else { .mapIndexedNotNull { index, row ->
mutableListOf() if (isWatchingRow(row.config)) index else null
} }
val deferred = val deferred =
settings.rows.mapIndexed { index, row -> settings.rows
if (refresh) { // Load the watching rows first
(loadingRows as MutableList).add(HomeRowLoadingState.Loading(row.title)) .sortedByDescending { isWatchingRow(it.config) }
} .map { row ->
viewModelScope.async(Dispatchers.IO) { viewModelScope.async(Dispatchers.IO) {
semaphore.withPermit { semaphore.withPermit {
Timber.v("Fetching row: %s", row) Timber.v("Fetching row: %s", row)
@ -117,14 +117,24 @@ class HomeViewModel
} }
} }
} }
if (refresh) {
deferred.firstOrNull()?.await()?.let { if (refresh && state.homeRows.isNotEmpty() && watchingRowIndexes.isNotEmpty()) {
(loadingRows as MutableList)[0] = it // Replace watching rows first
} Timber.v("Refreshing rows: %s", watchingRowIndexes)
val rows =
deferred
.filterIndexed { index, _ -> index in watchingRowIndexes }
.awaitAll()
_state.update { _state.update {
val newRows =
it.homeRows.toMutableList().apply {
rows.forEachIndexed { index, row ->
set(watchingRowIndexes[index], row)
}
}
it.copy( it.copy(
loadingState = LoadingState.Success, loadingState = LoadingState.Success,
homeRows = loadingRows, homeRows = newRows,
) )
} }
} }
@ -194,3 +204,11 @@ data class HomeState(
) )
} }
} }
/**
* Whether a row is a "is watching" type
*/
private fun isWatchingRow(row: HomeRowConfig) =
row is HomeRowConfig.ContinueWatching ||
row is HomeRowConfig.NextUp ||
row is HomeRowConfig.ContinueWatchingCombined