2022-07-18 10:17:40 +08:00
|
|
|
package eu.kanade.presentation.updates
|
|
|
|
|
|
|
|
import androidx.activity.compose.BackHandler
|
2022-07-30 23:50:00 +08:00
|
|
|
import androidx.compose.foundation.layout.PaddingValues
|
2022-07-18 10:17:40 +08:00
|
|
|
import androidx.compose.foundation.layout.WindowInsets
|
|
|
|
import androidx.compose.foundation.layout.WindowInsetsSides
|
|
|
|
import androidx.compose.foundation.layout.asPaddingValues
|
|
|
|
import androidx.compose.foundation.layout.calculateEndPadding
|
|
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
import androidx.compose.foundation.layout.navigationBars
|
|
|
|
import androidx.compose.foundation.layout.only
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
|
|
|
import androidx.compose.foundation.lazy.rememberLazyListState
|
|
|
|
import androidx.compose.material.icons.Icons
|
|
|
|
import androidx.compose.material.icons.filled.FlipToBack
|
|
|
|
import androidx.compose.material.icons.filled.Refresh
|
|
|
|
import androidx.compose.material.icons.filled.SelectAll
|
|
|
|
import androidx.compose.material3.Icon
|
|
|
|
import androidx.compose.material3.IconButton
|
2022-08-01 10:24:19 +08:00
|
|
|
import androidx.compose.material3.TopAppBarDefaults
|
|
|
|
import androidx.compose.material3.TopAppBarScrollBehavior
|
|
|
|
import androidx.compose.material3.rememberTopAppBarState
|
2022-07-18 10:17:40 +08:00
|
|
|
import androidx.compose.runtime.Composable
|
2022-07-30 23:50:00 +08:00
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
2022-07-18 10:17:40 +08:00
|
|
|
import androidx.compose.ui.Modifier
|
2022-08-01 10:24:19 +08:00
|
|
|
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
2022-07-30 23:50:00 +08:00
|
|
|
import androidx.compose.ui.platform.LocalContext
|
2022-07-18 10:17:40 +08:00
|
|
|
import androidx.compose.ui.platform.LocalLayoutDirection
|
|
|
|
import androidx.compose.ui.res.stringResource
|
|
|
|
import com.google.accompanist.swiperefresh.SwipeRefresh
|
|
|
|
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
|
2022-07-19 06:32:25 +08:00
|
|
|
import eu.kanade.presentation.components.AppBar
|
2022-07-18 10:17:40 +08:00
|
|
|
import eu.kanade.presentation.components.ChapterDownloadAction
|
|
|
|
import eu.kanade.presentation.components.EmptyScreen
|
|
|
|
import eu.kanade.presentation.components.MangaBottomActionMenu
|
|
|
|
import eu.kanade.presentation.components.Scaffold
|
|
|
|
import eu.kanade.presentation.components.SwipeRefreshIndicator
|
|
|
|
import eu.kanade.presentation.components.VerticalFastScroller
|
2022-07-23 10:44:05 +08:00
|
|
|
import eu.kanade.presentation.util.bottomNavPaddingValues
|
2022-07-18 10:17:40 +08:00
|
|
|
import eu.kanade.presentation.util.plus
|
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
|
import eu.kanade.tachiyomi.data.download.model.Download
|
2022-07-30 23:50:00 +08:00
|
|
|
import eu.kanade.tachiyomi.data.library.LibraryUpdateService
|
|
|
|
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
|
2022-07-18 10:17:40 +08:00
|
|
|
import eu.kanade.tachiyomi.ui.recent.updates.UpdatesItem
|
2022-07-30 23:50:00 +08:00
|
|
|
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.flow.collectLatest
|
2022-07-18 10:17:40 +08:00
|
|
|
import java.util.Date
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun UpdateScreen(
|
2022-07-30 23:50:00 +08:00
|
|
|
presenter: UpdatesPresenter,
|
2022-07-18 10:17:40 +08:00
|
|
|
onClickCover: (UpdatesItem) -> Unit,
|
|
|
|
onBackClicked: () -> Unit,
|
2022-07-30 23:50:00 +08:00
|
|
|
onDownloadChapter: (List<UpdatesItem>, ChapterDownloadAction) -> Unit,
|
2022-07-18 10:17:40 +08:00
|
|
|
) {
|
|
|
|
val updatesListState = rememberLazyListState()
|
2022-08-01 10:24:19 +08:00
|
|
|
val insetPaddingValue = WindowInsets.navigationBars
|
|
|
|
.only(WindowInsetsSides.Horizontal + WindowInsetsSides.Bottom)
|
|
|
|
.asPaddingValues()
|
2022-07-18 10:17:40 +08:00
|
|
|
|
|
|
|
val internalOnBackPressed = {
|
2022-07-30 23:50:00 +08:00
|
|
|
if (presenter.selectionMode) {
|
|
|
|
presenter.toggleAllSelection(false)
|
2022-07-18 10:17:40 +08:00
|
|
|
} else {
|
|
|
|
onBackClicked()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BackHandler(onBack = internalOnBackPressed)
|
|
|
|
|
2022-07-30 23:50:00 +08:00
|
|
|
val context = LocalContext.current
|
|
|
|
|
|
|
|
val onUpdateLibrary = {
|
|
|
|
if (LibraryUpdateService.start(context)) {
|
|
|
|
context.toast(R.string.updating_library)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 10:24:19 +08:00
|
|
|
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
|
2022-07-18 10:17:40 +08:00
|
|
|
Scaffold(
|
|
|
|
modifier = Modifier
|
2022-08-01 10:24:19 +08:00
|
|
|
.padding(insetPaddingValue)
|
|
|
|
.nestedScroll(scrollBehavior.nestedScrollConnection),
|
2022-07-18 10:17:40 +08:00
|
|
|
topBar = {
|
|
|
|
UpdatesAppBar(
|
2022-07-30 23:50:00 +08:00
|
|
|
incognitoMode = presenter.isIncognitoMode,
|
|
|
|
downloadedOnlyMode = presenter.isDownloadOnly,
|
2022-07-18 10:17:40 +08:00
|
|
|
onUpdateLibrary = onUpdateLibrary,
|
2022-07-30 23:50:00 +08:00
|
|
|
actionModeCounter = presenter.selected.size,
|
|
|
|
onSelectAll = { presenter.toggleAllSelection(true) },
|
|
|
|
onInvertSelection = { presenter.invertSelection() },
|
|
|
|
onCancelActionMode = { presenter.toggleAllSelection(false) },
|
2022-08-01 10:24:19 +08:00
|
|
|
scrollBehavior = scrollBehavior,
|
2022-07-18 10:17:40 +08:00
|
|
|
)
|
|
|
|
},
|
|
|
|
bottomBar = {
|
|
|
|
UpdatesBottomBar(
|
2022-07-30 23:50:00 +08:00
|
|
|
selected = presenter.selected,
|
2022-07-18 10:17:40 +08:00
|
|
|
onDownloadChapter = onDownloadChapter,
|
2022-07-30 23:50:00 +08:00
|
|
|
onMultiBookmarkClicked = presenter::bookmarkUpdates,
|
|
|
|
onMultiMarkAsReadClicked = presenter::markUpdatesRead,
|
|
|
|
onMultiDeleteClicked = {
|
|
|
|
val updateItems = presenter.selected.map { it.item }
|
|
|
|
presenter.dialog = Dialog.DeleteConfirmation(updateItems)
|
|
|
|
},
|
2022-07-18 10:17:40 +08:00
|
|
|
)
|
|
|
|
},
|
|
|
|
) { contentPadding ->
|
2022-07-30 23:50:00 +08:00
|
|
|
// During selection mode bottom nav is not visible
|
|
|
|
val contentPaddingWithNavBar = (if (presenter.selectionMode) PaddingValues() else bottomNavPaddingValues) +
|
|
|
|
contentPadding + WindowInsets.navigationBars.only(WindowInsetsSides.Bottom).asPaddingValues()
|
2022-07-18 10:17:40 +08:00
|
|
|
|
|
|
|
SwipeRefresh(
|
2022-07-30 23:50:00 +08:00
|
|
|
state = rememberSwipeRefreshState(isRefreshing = false),
|
2022-07-18 10:17:40 +08:00
|
|
|
onRefresh = onUpdateLibrary,
|
2022-07-30 23:50:00 +08:00
|
|
|
swipeEnabled = presenter.selectionMode.not(),
|
2022-07-18 10:17:40 +08:00
|
|
|
indicatorPadding = contentPaddingWithNavBar,
|
|
|
|
indicator = { s, trigger ->
|
|
|
|
SwipeRefreshIndicator(
|
|
|
|
state = s,
|
|
|
|
refreshTriggerDistance = trigger,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
) {
|
2022-07-30 23:50:00 +08:00
|
|
|
if (presenter.uiModels.isEmpty()) {
|
2022-07-18 10:17:40 +08:00
|
|
|
EmptyScreen(textResource = R.string.information_no_recent)
|
|
|
|
} else {
|
|
|
|
VerticalFastScroller(
|
|
|
|
listState = updatesListState,
|
|
|
|
topContentPadding = contentPaddingWithNavBar.calculateTopPadding(),
|
2022-07-23 10:44:05 +08:00
|
|
|
bottomContentPadding = contentPaddingWithNavBar.calculateBottomPadding(),
|
2022-07-18 10:17:40 +08:00
|
|
|
endContentPadding = contentPaddingWithNavBar.calculateEndPadding(LocalLayoutDirection.current),
|
|
|
|
) {
|
|
|
|
LazyColumn(
|
|
|
|
modifier = Modifier.fillMaxHeight(),
|
|
|
|
state = updatesListState,
|
|
|
|
contentPadding = contentPaddingWithNavBar,
|
|
|
|
) {
|
|
|
|
updatesUiItems(
|
2022-07-30 23:50:00 +08:00
|
|
|
uiModels = presenter.uiModels,
|
|
|
|
selectionMode = presenter.selectionMode,
|
|
|
|
onUpdateSelected = presenter::toggleSelection,
|
2022-07-18 10:17:40 +08:00
|
|
|
onClickCover = onClickCover,
|
2022-07-30 23:50:00 +08:00
|
|
|
onClickUpdate = {
|
|
|
|
val intent = ReaderActivity.newIntent(context, it.update.mangaId, it.update.chapterId)
|
|
|
|
context.startActivity(intent)
|
|
|
|
},
|
2022-07-18 10:17:40 +08:00
|
|
|
onDownloadChapter = onDownloadChapter,
|
2022-07-30 23:50:00 +08:00
|
|
|
relativeTime = presenter.relativeTime,
|
|
|
|
dateFormat = presenter.dateFormat,
|
2022-07-18 10:17:40 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-30 23:50:00 +08:00
|
|
|
|
|
|
|
val onDismissDialog = { presenter.dialog = null }
|
|
|
|
when (val dialog = presenter.dialog) {
|
|
|
|
is Dialog.DeleteConfirmation -> {
|
|
|
|
UpdatesDeleteConfirmationDialog(
|
|
|
|
onDismissRequest = onDismissDialog,
|
|
|
|
onConfirm = {
|
|
|
|
presenter.deleteChapters(dialog.toDelete)
|
|
|
|
presenter.toggleAllSelection(false)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
null -> {}
|
|
|
|
}
|
|
|
|
LaunchedEffect(Unit) {
|
|
|
|
presenter.events.collectLatest { event ->
|
|
|
|
when (event) {
|
|
|
|
Event.InternalError -> context.toast(R.string.internal_error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-18 10:17:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun UpdatesAppBar(
|
|
|
|
modifier: Modifier = Modifier,
|
|
|
|
incognitoMode: Boolean,
|
|
|
|
downloadedOnlyMode: Boolean,
|
|
|
|
onUpdateLibrary: () -> Unit,
|
|
|
|
// For action mode
|
|
|
|
actionModeCounter: Int,
|
|
|
|
onSelectAll: () -> Unit,
|
|
|
|
onInvertSelection: () -> Unit,
|
2022-07-30 23:50:00 +08:00
|
|
|
onCancelActionMode: () -> Unit,
|
2022-08-01 10:24:19 +08:00
|
|
|
scrollBehavior: TopAppBarScrollBehavior,
|
2022-07-18 10:17:40 +08:00
|
|
|
) {
|
2022-07-19 06:32:25 +08:00
|
|
|
AppBar(
|
|
|
|
modifier = modifier,
|
|
|
|
title = stringResource(R.string.label_recent_updates),
|
|
|
|
actions = {
|
|
|
|
IconButton(onClick = onUpdateLibrary) {
|
|
|
|
Icon(
|
|
|
|
imageVector = Icons.Default.Refresh,
|
|
|
|
contentDescription = stringResource(R.string.action_update_library),
|
2022-07-18 10:17:40 +08:00
|
|
|
)
|
2022-07-19 06:32:25 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
actionModeCounter = actionModeCounter,
|
2022-07-30 23:50:00 +08:00
|
|
|
onCancelActionMode = onCancelActionMode,
|
2022-07-19 06:32:25 +08:00
|
|
|
actionModeActions = {
|
|
|
|
IconButton(onClick = onSelectAll) {
|
|
|
|
Icon(
|
|
|
|
imageVector = Icons.Default.SelectAll,
|
|
|
|
contentDescription = stringResource(R.string.action_select_all),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
IconButton(onClick = onInvertSelection) {
|
|
|
|
Icon(
|
|
|
|
imageVector = Icons.Default.FlipToBack,
|
|
|
|
contentDescription = stringResource(R.string.action_select_inverse),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
downloadedOnlyMode = downloadedOnlyMode,
|
|
|
|
incognitoMode = incognitoMode,
|
2022-08-01 10:24:19 +08:00
|
|
|
scrollBehavior = scrollBehavior,
|
2022-07-19 06:32:25 +08:00
|
|
|
)
|
2022-07-18 10:17:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
fun UpdatesBottomBar(
|
2022-07-30 23:50:00 +08:00
|
|
|
selected: List<UpdatesUiModel.Item>,
|
2022-07-18 10:17:40 +08:00
|
|
|
onDownloadChapter: (List<UpdatesItem>, ChapterDownloadAction) -> Unit,
|
|
|
|
onMultiBookmarkClicked: (List<UpdatesItem>, bookmark: Boolean) -> Unit,
|
|
|
|
onMultiMarkAsReadClicked: (List<UpdatesItem>, read: Boolean) -> Unit,
|
|
|
|
onMultiDeleteClicked: (List<UpdatesItem>) -> Unit,
|
|
|
|
) {
|
|
|
|
MangaBottomActionMenu(
|
|
|
|
visible = selected.isNotEmpty(),
|
|
|
|
modifier = Modifier.fillMaxWidth(),
|
|
|
|
onBookmarkClicked = {
|
|
|
|
onMultiBookmarkClicked.invoke(selected.map { it.item }, true)
|
|
|
|
}.takeIf { selected.any { !it.item.update.bookmark } },
|
|
|
|
onRemoveBookmarkClicked = {
|
|
|
|
onMultiBookmarkClicked.invoke(selected.map { it.item }, false)
|
|
|
|
}.takeIf { selected.all { it.item.update.bookmark } },
|
|
|
|
onMarkAsReadClicked = {
|
|
|
|
onMultiMarkAsReadClicked(selected.map { it.item }, true)
|
|
|
|
}.takeIf { selected.any { !it.item.update.read } },
|
|
|
|
onMarkAsUnreadClicked = {
|
|
|
|
onMultiMarkAsReadClicked(selected.map { it.item }, false)
|
|
|
|
}.takeIf { selected.any { it.item.update.read } },
|
|
|
|
onDownloadClicked = {
|
|
|
|
onDownloadChapter(selected.map { it.item }, ChapterDownloadAction.START)
|
|
|
|
}.takeIf {
|
|
|
|
selected.any { it.item.downloadStateProvider() != Download.State.DOWNLOADED }
|
|
|
|
},
|
|
|
|
onDeleteClicked = {
|
|
|
|
onMultiDeleteClicked(selected.map { it.item })
|
|
|
|
}.takeIf { selected.any { it.item.downloadStateProvider() == Download.State.DOWNLOADED } },
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class UpdatesUiModel {
|
|
|
|
data class Header(val date: Date) : UpdatesUiModel()
|
|
|
|
data class Item(val item: UpdatesItem) : UpdatesUiModel()
|
|
|
|
}
|