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

View file

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