mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-09 08:01:20 +02:00
Refresh watching rows first
This commit is contained in:
parent
3f4ec24569
commit
70ad1ecdbf
1 changed files with 51 additions and 33 deletions
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue