From 964248d44c9bd41f10205b00bb09cf804413ffb2 Mon Sep 17 00:00:00 2001 From: Syer10 Date: Thu, 27 May 2021 13:59:29 -0400 Subject: [PATCH] Add refresh manga info --- .../kotlin/ca/gosyer/ui/manga/MangaMenu.kt | 6 ++++- .../ca/gosyer/ui/manga/MangaMenuViewModel.kt | 25 +++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ca/gosyer/ui/manga/MangaMenu.kt b/src/main/kotlin/ca/gosyer/ui/manga/MangaMenu.kt index 0cb61fdb..0961aedb 100644 --- a/src/main/kotlin/ca/gosyer/ui/manga/MangaMenu.kt +++ b/src/main/kotlin/ca/gosyer/ui/manga/MangaMenu.kt @@ -8,6 +8,7 @@ package ca.gosyer.ui.manga import androidx.compose.foundation.VerticalScrollbar import androidx.compose.foundation.background +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.Column @@ -77,10 +78,13 @@ fun MangaMenu(mangaId: Long, backStack: BackStack? = null) { Toolbar("Manga", backStack, backStack != null) Surface(Modifier.height(40.dp).fillMaxWidth()) { - Row { + Row(horizontalArrangement = Arrangement.SpaceBetween) { Button(onClick = vm::toggleFavorite) { Text(if (manga?.inLibrary == true) "UnFavorite" else "Favorite") } + Button(onClick = vm::refreshManga, enabled = !vm.isRefreshing.collectAsState().value) { + Text("Refresh manga") + } } } manga?.let { manga -> diff --git a/src/main/kotlin/ca/gosyer/ui/manga/MangaMenuViewModel.kt b/src/main/kotlin/ca/gosyer/ui/manga/MangaMenuViewModel.kt index 6eeb32b6..8ee7c4d3 100644 --- a/src/main/kotlin/ca/gosyer/ui/manga/MangaMenuViewModel.kt +++ b/src/main/kotlin/ca/gosyer/ui/manga/MangaMenuViewModel.kt @@ -14,8 +14,8 @@ import ca.gosyer.data.server.interactions.LibraryInteractionHandler import ca.gosyer.data.server.interactions.MangaInteractionHandler import ca.gosyer.data.ui.UiPreferences import ca.gosyer.ui.base.vm.ViewModel +import ca.gosyer.util.lang.throwIfCancellation import ca.gosyer.util.lang.withIOContext -import kotlinx.coroutines.CancellationException import kotlinx.coroutines.async import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow @@ -46,6 +46,9 @@ class MangaMenuViewModel @Inject constructor( private val _isLoading = MutableStateFlow(true) val isLoading = _isLoading.asStateFlow() + private val _isRefreshing = MutableStateFlow(false) + val isRefreshing = _isRefreshing.asStateFlow() + val dateTimeFormatter = uiPreferences.dateFormat().changes() .map { getDateFormat(it) @@ -59,22 +62,30 @@ class MangaMenuViewModel @Inject constructor( } } - private suspend fun refreshMangaAsync(mangaId: Long) = withIOContext { + fun refreshManga() { + scope.launch { + _isRefreshing.value = true + refreshMangaAsync(params.mangaId, true).await() to refreshChaptersAsync(params.mangaId, true).await() + _isRefreshing.value = false + } + } + + private suspend fun refreshMangaAsync(mangaId: Long, refresh: Boolean = false) = withIOContext { async { try { - _manga.value = mangaHandler.getManga(mangaId) + _manga.value = mangaHandler.getManga(mangaId, refresh) } catch (e: Exception) { - if (e is CancellationException) throw e + e.throwIfCancellation() } } } - private suspend fun refreshChaptersAsync(mangaId: Long) = withIOContext { + private suspend fun refreshChaptersAsync(mangaId: Long, refresh: Boolean = false) = withIOContext { async { try { - _chapters.value = chapterHandler.getChapters(mangaId) + _chapters.value = chapterHandler.getChapters(mangaId, refresh) } catch (e: Exception) { - if (e is CancellationException) throw e + e.throwIfCancellation() } } }