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.calculateEndPadding
|
|
|
|
import androidx.compose.foundation.layout.fillMaxHeight
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
2022-10-10 03:52:56 +08:00
|
|
|
import androidx.compose.foundation.layout.padding
|
2022-07-18 10:17:40 +08:00
|
|
|
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.TopAppBarScrollBehavior
|
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-08-13 00:21:05 +08:00
|
|
|
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
|
2022-07-18 10:17:40 +08:00
|
|
|
import androidx.compose.ui.Modifier
|
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
|
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
|
2022-08-14 22:12:31 +08:00
|
|
|
import eu.kanade.presentation.components.LazyColumn
|
2022-09-19 10:38:44 +08:00
|
|
|
import eu.kanade.presentation.components.LoadingScreen
|
2022-07-18 10:17:40 +08:00
|
|
|
import eu.kanade.presentation.components.MangaBottomActionMenu
|
|
|
|
import eu.kanade.presentation.components.Scaffold
|
2022-10-09 23:20:43 +08:00
|
|
|
import eu.kanade.presentation.components.SwipeRefresh
|
2022-07-18 10:17:40 +08:00
|
|
|
import eu.kanade.presentation.components.VerticalFastScroller
|
|
|
|
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
|
2022-10-10 03:52:56 +08:00
|
|
|
import eu.kanade.tachiyomi.widget.TachiyomiBottomNavigationView
|
2022-08-13 00:21:05 +08:00
|
|
|
import kotlinx.coroutines.delay
|
2022-07-30 23:50:00 +08:00
|
|
|
import kotlinx.coroutines.flow.collectLatest
|
2022-08-13 00:21:05 +08:00
|
|
|
import kotlinx.coroutines.launch
|
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,
|
|
|
|
) {
|
|
|
|
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 = {
|
2022-08-18 21:01:10 +08:00
|
|
|
val started = LibraryUpdateService.start(context)
|
|
|
|
context.toast(if (started) R.string.updating_library else R.string.update_already_running)
|
|
|
|
started
|
2022-07-30 23:50:00 +08:00
|
|
|
}
|
|
|
|
|
2022-07-18 10:17:40 +08:00
|
|
|
Scaffold(
|
2022-09-01 04:31:08 +08:00
|
|
|
topBar = { scrollBehavior ->
|
2022-07-18 10:17:40 +08:00
|
|
|
UpdatesAppBar(
|
2022-07-30 23:50:00 +08:00
|
|
|
incognitoMode = presenter.isIncognitoMode,
|
|
|
|
downloadedOnlyMode = presenter.isDownloadOnly,
|
2022-08-18 21:01:10 +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-09-19 10:38:44 +08:00
|
|
|
onDownloadChapter = presenter::downloadChapters,
|
2022-07-30 23:50:00 +08:00
|
|
|
onMultiBookmarkClicked = presenter::bookmarkUpdates,
|
|
|
|
onMultiMarkAsReadClicked = presenter::markUpdatesRead,
|
|
|
|
onMultiDeleteClicked = {
|
2022-09-20 11:56:28 +08:00
|
|
|
presenter.dialog = Dialog.DeleteConfirmation(it)
|
2022-07-30 23:50:00 +08:00
|
|
|
},
|
2022-07-18 10:17:40 +08:00
|
|
|
)
|
|
|
|
},
|
|
|
|
) { contentPadding ->
|
2022-10-10 03:52:56 +08:00
|
|
|
val contentPaddingWithNavBar = TachiyomiBottomNavigationView.withBottomNavPadding(contentPadding)
|
2022-09-19 10:38:44 +08:00
|
|
|
when {
|
|
|
|
presenter.isLoading -> LoadingScreen()
|
2022-10-10 03:52:56 +08:00
|
|
|
presenter.uiModels.isEmpty() -> EmptyScreen(
|
|
|
|
textResource = R.string.information_no_recent,
|
|
|
|
modifier = Modifier.padding(contentPaddingWithNavBar),
|
|
|
|
)
|
2022-09-19 10:38:44 +08:00
|
|
|
else -> {
|
|
|
|
UpdateScreenContent(
|
|
|
|
presenter = presenter,
|
2022-10-10 03:52:56 +08:00
|
|
|
contentPadding = contentPaddingWithNavBar,
|
2022-09-19 10:38:44 +08:00
|
|
|
onUpdateLibrary = onUpdateLibrary,
|
|
|
|
onClickCover = onClickCover,
|
|
|
|
)
|
2022-08-14 02:27:07 +08:00
|
|
|
}
|
2022-09-19 10:38:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
private fun UpdateScreenContent(
|
|
|
|
presenter: UpdatesPresenter,
|
|
|
|
contentPadding: PaddingValues,
|
|
|
|
onUpdateLibrary: () -> Boolean,
|
|
|
|
onClickCover: (UpdatesItem) -> Unit,
|
|
|
|
) {
|
|
|
|
val context = LocalContext.current
|
|
|
|
val updatesListState = rememberLazyListState()
|
|
|
|
val scope = rememberCoroutineScope()
|
|
|
|
var isRefreshing by remember { mutableStateOf(false) }
|
2022-09-01 03:56:08 +08:00
|
|
|
|
2022-09-19 10:38:44 +08:00
|
|
|
SwipeRefresh(
|
2022-10-09 23:20:43 +08:00
|
|
|
refreshing = isRefreshing,
|
2022-09-19 10:38:44 +08:00
|
|
|
onRefresh = {
|
|
|
|
val started = onUpdateLibrary()
|
|
|
|
if (!started) return@SwipeRefresh
|
|
|
|
scope.launch {
|
|
|
|
// Fake refresh status but hide it after a second as it's a long running task
|
|
|
|
isRefreshing = true
|
|
|
|
delay(1000)
|
|
|
|
isRefreshing = false
|
|
|
|
}
|
|
|
|
},
|
2022-10-09 23:20:43 +08:00
|
|
|
enabled = presenter.selectionMode.not(),
|
2022-10-10 03:52:56 +08:00
|
|
|
indicatorPadding = contentPadding,
|
2022-09-19 10:38:44 +08:00
|
|
|
) {
|
2022-10-10 03:52:56 +08:00
|
|
|
VerticalFastScroller(
|
|
|
|
listState = updatesListState,
|
|
|
|
topContentPadding = contentPadding.calculateTopPadding(),
|
|
|
|
endContentPadding = contentPadding.calculateEndPadding(LocalLayoutDirection.current),
|
|
|
|
) {
|
|
|
|
LazyColumn(
|
|
|
|
modifier = Modifier.fillMaxHeight(),
|
|
|
|
state = updatesListState,
|
|
|
|
contentPadding = contentPadding,
|
2022-09-19 10:38:44 +08:00
|
|
|
) {
|
2022-10-10 03:52:56 +08:00
|
|
|
if (presenter.lastUpdated > 0L) {
|
|
|
|
updatesLastUpdatedItem(presenter.lastUpdated)
|
2022-07-18 10:17:40 +08:00
|
|
|
}
|
2022-10-10 03:52:56 +08:00
|
|
|
|
|
|
|
updatesUiItems(
|
|
|
|
uiModels = presenter.uiModels,
|
|
|
|
selectionMode = presenter.selectionMode,
|
|
|
|
onUpdateSelected = presenter::toggleSelection,
|
|
|
|
onClickCover = onClickCover,
|
|
|
|
onClickUpdate = {
|
|
|
|
val intent = ReaderActivity.newIntent(context, it.update.mangaId, it.update.chapterId)
|
|
|
|
context.startActivity(intent)
|
|
|
|
},
|
|
|
|
onDownloadChapter = presenter::downloadChapters,
|
|
|
|
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.toggleAllSelection(false)
|
2022-09-28 05:44:41 +08:00
|
|
|
presenter.deleteChapters(dialog.toDelete)
|
2022-07-30 23:50:00 +08:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
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
|
2022-09-19 10:38:44 +08:00
|
|
|
private fun UpdatesAppBar(
|
2022-07-18 10:17:40 +08:00
|
|
|
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
|
2022-09-19 10:38:44 +08:00
|
|
|
private fun UpdatesBottomBar(
|
2022-09-20 11:56:28 +08:00
|
|
|
selected: List<UpdatesItem>,
|
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 = {
|
2022-09-20 11:56:28 +08:00
|
|
|
onMultiBookmarkClicked.invoke(selected, true)
|
|
|
|
}.takeIf { selected.any { !it.update.bookmark } },
|
2022-07-18 10:17:40 +08:00
|
|
|
onRemoveBookmarkClicked = {
|
2022-09-20 11:56:28 +08:00
|
|
|
onMultiBookmarkClicked.invoke(selected, false)
|
|
|
|
}.takeIf { selected.all { it.update.bookmark } },
|
2022-07-18 10:17:40 +08:00
|
|
|
onMarkAsReadClicked = {
|
2022-09-20 11:56:28 +08:00
|
|
|
onMultiMarkAsReadClicked(selected, true)
|
|
|
|
}.takeIf { selected.any { !it.update.read } },
|
2022-07-18 10:17:40 +08:00
|
|
|
onMarkAsUnreadClicked = {
|
2022-09-20 11:56:28 +08:00
|
|
|
onMultiMarkAsReadClicked(selected, false)
|
|
|
|
}.takeIf { selected.any { it.update.read } },
|
2022-07-18 10:17:40 +08:00
|
|
|
onDownloadClicked = {
|
2022-09-20 11:56:28 +08:00
|
|
|
onDownloadChapter(selected, ChapterDownloadAction.START)
|
2022-07-18 10:17:40 +08:00
|
|
|
}.takeIf {
|
2022-09-20 11:56:28 +08:00
|
|
|
selected.any { it.downloadStateProvider() != Download.State.DOWNLOADED }
|
2022-07-18 10:17:40 +08:00
|
|
|
},
|
|
|
|
onDeleteClicked = {
|
2022-09-20 11:56:28 +08:00
|
|
|
onMultiDeleteClicked(selected)
|
|
|
|
}.takeIf { selected.any { it.downloadStateProvider() == Download.State.DOWNLOADED } },
|
2022-07-18 10:17:40 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
sealed class UpdatesUiModel {
|
|
|
|
data class Header(val date: Date) : UpdatesUiModel()
|
|
|
|
data class Item(val item: UpdatesItem) : UpdatesUiModel()
|
|
|
|
}
|