mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-08 15:50:16 +02:00
Add locking for refreshes
This commit is contained in:
parent
0438b889b6
commit
724519a6fa
1 changed files with 77 additions and 73 deletions
|
|
@ -17,6 +17,8 @@ import com.github.damontecres.wholphin.util.LoadingState
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlinx.coroutines.withContext
|
||||
import org.jellyfin.sdk.api.client.ApiClient
|
||||
import org.jellyfin.sdk.api.client.extensions.imageApi
|
||||
|
|
@ -49,6 +51,7 @@ class LiveTvViewModel
|
|||
val start =
|
||||
LocalDateTime.now().truncatedTo(ChronoUnit.HOURS)
|
||||
private lateinit var channelsIdToIndex: Map<UUID, Int>
|
||||
private val mutex = Mutex()
|
||||
|
||||
val channels = MutableLiveData<List<TvChannel>>()
|
||||
val programs = MutableLiveData<List<TvProgram>>()
|
||||
|
|
@ -87,7 +90,8 @@ class LiveTvViewModel
|
|||
}
|
||||
}
|
||||
|
||||
private suspend fun fetchPrograms(channels: List<TvChannel>) {
|
||||
private suspend fun fetchPrograms(channels: List<TvChannel>) =
|
||||
mutex.withLock {
|
||||
val maxStartDate = start.plusHours(MAX_HOURS).minusMinutes(1)
|
||||
val minEndDate = start.plusMinutes(1L)
|
||||
val request =
|
||||
|
|
@ -248,7 +252,7 @@ class LiveTvViewModel
|
|||
suspend fun refreshProgram(
|
||||
programIndex: Int,
|
||||
programId: UUID,
|
||||
) {
|
||||
) = mutex.withLock {
|
||||
val program by api.liveTvApi.getProgram(programId.toServerString())
|
||||
val newProgram =
|
||||
programs.value?.getOrNull(programIndex)?.copy(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue