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>
This commit is contained in:
AntsyLich 2022-08-12 22:21:05 +06:00 committed by GitHub
parent 441e7bf8b1
commit 1474c8ffb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 20 deletions

View File

@ -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 ->

View File

@ -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() {

View File

@ -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))