From 1474c8ffb337f824bc7f30f1f0f3e70e27205254 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Fri, 12 Aug 2022 22:21:05 +0600 Subject: [PATCH] Some tweaks on Updates screen (#7729) Based on #7708, #7709 and #7717 Co-Authored-By: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Co-Authored-By: Andreas <6576096+ghostbear@users.noreply.github.com> Co-authored-by: Ivan Iskandar <12537387+ivaniskandar@users.noreply.github.com> Co-authored-by: Andreas <6576096+ghostbear@users.noreply.github.com> --- .../presentation/updates/UpdatesScreen.kt | 22 ++++++++++++-- .../ui/recent/updates/UpdatesController.kt | 9 ++++-- .../ui/recent/updates/UpdatesPresenter.kt | 29 +++++++++---------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt b/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt index ed61d0c78e..0ac33f6058 100644 --- a/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/updates/UpdatesScreen.kt @@ -24,6 +24,11 @@ import androidx.compose.material3.TopAppBarScrollBehavior import androidx.compose.material3.rememberTopAppBarState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.input.nestedscroll.nestedScroll import androidx.compose.ui.platform.LocalContext @@ -49,7 +54,9 @@ import eu.kanade.tachiyomi.ui.recent.updates.UpdatesPresenter import eu.kanade.tachiyomi.ui.recent.updates.UpdatesPresenter.Dialog import eu.kanade.tachiyomi.ui.recent.updates.UpdatesPresenter.Event import eu.kanade.tachiyomi.util.system.toast +import kotlinx.coroutines.delay import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.launch import java.util.Date @Composable @@ -115,9 +122,20 @@ fun UpdateScreen( val contentPaddingWithNavBar = (if (presenter.selectionMode) PaddingValues() else bottomNavPaddingValues) + contentPadding + WindowInsets.navigationBars.only(WindowInsetsSides.Bottom).asPaddingValues() + val scope = rememberCoroutineScope() + var isRefreshing by remember { mutableStateOf(false) } + SwipeRefresh( - state = rememberSwipeRefreshState(isRefreshing = false), - onRefresh = onUpdateLibrary, + state = rememberSwipeRefreshState(isRefreshing = isRefreshing), + onRefresh = { + onUpdateLibrary() + scope.launch { + // Fake refresh status but hide it after a second as it's a long running task + isRefreshing = true + delay(1000) + isRefreshing = false + } + }, swipeEnabled = presenter.selectionMode.not(), indicatorPadding = contentPaddingWithNavBar, indicator = { s, trigger -> diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt index 96b6ab40a9..4322a9175c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesController.kt @@ -54,8 +54,13 @@ class UpdatesController : // Let compose view handle this override fun handleBack(): Boolean { - (activity as? OnBackPressedDispatcherOwner)?.onBackPressedDispatcher?.onBackPressed() - return true + val dispatcher = (activity as? OnBackPressedDispatcherOwner)?.onBackPressedDispatcher ?: return false + return if (dispatcher.hasEnabledCallbacks()) { + dispatcher.onBackPressed() + true + } else { + false + } } private fun onBackClicked() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt index 9a3da5a171..a4aca7b730 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/recent/updates/UpdatesPresenter.kt @@ -157,13 +157,12 @@ class UpdatesPresenter( * @param download download object containing progress. */ private fun updateDownloadState(download: Download) { - val uiModels = state.uiModels - val modifiedIndex = uiModels.indexOfFirst { - it is UpdatesUiModel.Item && it.item.update.chapterId == download.chapter.id - } - if (modifiedIndex < 0) return - state.uiModels = uiModels.toMutableList().apply { + val modifiedIndex = uiModels.indexOfFirst { + it is UpdatesUiModel.Item && it.item.update.chapterId == download.chapter.id + } + if (modifiedIndex < 0) return@apply + var uiModel = removeAt(modifiedIndex) if (uiModel is UpdatesUiModel.Item) { val item = uiModel.item.copy( @@ -249,7 +248,6 @@ class UpdatesPresenter( downloadManager.deleteChapters(chapters, manga, source).mapNotNull { it.id } } - val uiModels = state.uiModels val deletedUpdates = uiModels.filter { it is UpdatesUiModel.Item && deletedIds.contains(it.item.update.chapterId) } @@ -279,16 +277,15 @@ class UpdatesPresenter( userSelected: Boolean = false, fromLongPress: Boolean = false, ) { - val uiModels = state.uiModels - val modifiedIndex = uiModels.indexOfFirst { - it is UpdatesUiModel.Item && it.item.update.chapterId == item.update.chapterId - } - if (modifiedIndex < 0) return - - val oldItem = (uiModels[modifiedIndex] as? UpdatesUiModel.Item)?.item ?: return - if ((oldItem.selected && selected) || (!oldItem.selected && !selected)) return - state.uiModels = uiModels.toMutableList().apply { + val modifiedIndex = indexOfFirst { + it is UpdatesUiModel.Item && it.item == item + } + if (modifiedIndex < 0) return@apply + + val oldItem = (get(modifiedIndex) as? UpdatesUiModel.Item)?.item ?: return@apply + if ((oldItem.selected && selected) || (!oldItem.selected && !selected)) return@apply + val firstSelection = none { it is UpdatesUiModel.Item && it.item.selected } var newItem = (removeAt(modifiedIndex) as? UpdatesUiModel.Item)?.item?.copy(selected = selected) ?: return@apply add(modifiedIndex, UpdatesUiModel.Item(newItem))