Add error handling to played and favorite buttons

This commit is contained in:
Jarne Demeulemeester 2022-01-01 17:57:49 +01:00
parent e9aca103d8
commit 8e3c4a3a37
No known key found for this signature in database
GPG key ID: B61B7B150DB6A6D2
2 changed files with 79 additions and 40 deletions

View file

@ -12,6 +12,7 @@ import dev.jdtech.jellyfin.utils.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.jellyfin.sdk.api.client.exception.ApiClientException
import org.jellyfin.sdk.model.api.BaseItemDto
import timber.log.Timber
import java.text.DateFormat
@ -108,28 +109,44 @@ constructor(
fun markAsPlayed(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.markAsPlayed(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
played = true
}
fun markAsUnplayed(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.markAsUnplayed(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
played = false
}
fun markAsFavorite(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.markAsFavorite(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
favorite = true
}
fun unmarkAsFavorite(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.unmarkAsFavorite(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
favorite = false
}

View file

@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jellyfin.sdk.api.client.exception.ApiClientException
import org.jellyfin.sdk.model.api.BaseItemDto
import org.jellyfin.sdk.model.api.BaseItemPerson
import timber.log.Timber
@ -32,7 +33,7 @@ class MediaInfoViewModel
constructor(
private val application: Application,
private val jellyfinRepository: JellyfinRepository
) : ViewModel() {
) : ViewModel() {
private val uiState = MutableStateFlow<UiState>(UiState.Loading)
sealed class UiState {
@ -51,6 +52,7 @@ constructor(
val favorite: Boolean,
val downloaded: Boolean,
) : UiState()
object Loading : UiState()
data class Error(val message: String?) : UiState()
}
@ -98,7 +100,8 @@ constructor(
nextUp = getNextUp(itemId)
seasons = jellyfinRepository.getSeasons(itemId)
}
uiState.emit(UiState.Normal(
uiState.emit(
UiState.Normal(
tempItem,
actors,
director,
@ -112,7 +115,8 @@ constructor(
played,
favorite,
downloaded
))
)
)
} catch (e: Exception) {
Timber.d(e)
Timber.d(itemId.toString())
@ -135,7 +139,8 @@ constructor(
dateString = ""
played = tempItem.userData?.played ?: false
favorite = tempItem.userData?.isFavorite ?: false
uiState.emit(UiState.Normal(
uiState.emit(
UiState.Normal(
tempItem,
actors,
director,
@ -149,7 +154,8 @@ constructor(
played,
favorite,
downloaded
))
)
)
}
}
@ -188,28 +194,44 @@ constructor(
fun markAsPlayed(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.markAsPlayed(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
played = true
}
fun markAsUnplayed(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.markAsUnplayed(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
played = false
}
fun markAsFavorite(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.markAsFavorite(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
favorite = true
}
fun unmarkAsFavorite(itemId: UUID) {
viewModelScope.launch {
try {
jellyfinRepository.unmarkAsFavorite(itemId)
} catch (e: ApiClientException) {
Timber.d(e)
}
}
favorite = false
}