1
0
mirror of https://github.com/mihonapp/mihon.git synced 2025-02-20 22:18:59 +01:00

migrate to AppBar

This commit is contained in:
Cuong-Tran 2024-10-23 23:49:05 +07:00
parent d61ec31624
commit 8aa2eccfaa
No known key found for this signature in database
GPG Key ID: 733AA7624B9315C2

@ -3,7 +3,6 @@ package eu.kanade.presentation.libraryUpdateError
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
@ -25,22 +24,17 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.ZeroCornerSize
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.outlined.ArrowBack
import androidx.compose.material.icons.outlined.ArrowDownward
import androidx.compose.material.icons.outlined.ArrowUpward
import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.FindReplace
import androidx.compose.material.icons.outlined.FlipToBack
import androidx.compose.material.icons.outlined.SelectAll
import androidx.compose.material.icons.outlined.VerticalAlignBottom
import androidx.compose.material.icons.outlined.VerticalAlignTop
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.material3.ripple
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
@ -54,11 +48,12 @@ import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import eu.kanade.presentation.components.AppBarTitle
import eu.kanade.presentation.components.AppBar
import eu.kanade.presentation.components.AppBarActions
import eu.kanade.presentation.libraryUpdateError.components.libraryUpdateErrorUiItems
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.ui.libraryUpdateError.LibraryUpdateErrorItem
import eu.kanade.tachiyomi.ui.libraryUpdateError.LibraryUpdateErrorScreenState
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
@ -107,7 +102,12 @@ fun LibraryUpdateErrorScreen(
MR.strings.label_library_update_errors,
state.items.size,
),
actionModeCounter = state.selected.size,
itemCnt = state.items.size,
navigateUp = navigateUp,
selectedCount = state.selected.size,
onClickUnselectAll = { onSelectAll(false) },
onClickSelectAll = { onSelectAll(true) },
onClickInvertSelection = onInvertSelection,
scrollBehavior = scrollBehavior,
)
},
@ -115,14 +115,9 @@ fun LibraryUpdateErrorScreen(
LibraryUpdateErrorsBottomBar(
modifier = modifier,
selected = state.selected,
itemCount = state.items.size,
onMultiMigrateClicked = onMultiMigrateClicked,
enableScrollToTop = enableScrollToTop,
enableScrollToBottom = enableScrollToBottom,
onMultiMigrateClicked = onMultiMigrateClicked,
onSelectAll = { onSelectAll(true) },
onInvertSelection = onInvertSelection,
onCancelActionMode = { onSelectAll(false) },
navigateUp = navigateUp,
scrollToTop = {
scope.launch {
listState.scrollToItem(0)
@ -165,35 +160,27 @@ fun LibraryUpdateErrorScreen(
private fun LibraryUpdateErrorsBottomBar(
modifier: Modifier = Modifier,
selected: List<LibraryUpdateErrorItem>,
itemCount: Int,
onMultiMigrateClicked: (() -> Unit),
enableScrollToTop: Boolean,
enableScrollToBottom: Boolean,
onMultiMigrateClicked: (() -> Unit),
onSelectAll: () -> Unit,
onInvertSelection: () -> Unit,
onCancelActionMode: () -> Unit,
navigateUp: () -> Unit,
scrollToTop: () -> Unit,
scrollToBottom: () -> Unit,
) {
val scope = rememberCoroutineScope()
val animatedElevation by animateDpAsState(if (selected.isNotEmpty()) 3.dp else 0.dp)
Surface(
modifier = modifier,
shape = MaterialTheme.shapes.large.copy(
bottomEnd = ZeroCornerSize,
bottomStart = ZeroCornerSize,
),
color = MaterialTheme.colorScheme.surfaceColorAtElevation(
elevation = animatedElevation,
),
color = MaterialTheme.colorScheme.surfaceContainerHigh,
) {
val haptic = LocalHapticFeedback.current
val confirm = remember { mutableStateListOf(false, false, false, false, false, false) }
val confirm = remember { mutableStateListOf(false, false, false) }
var resetJob: Job? = remember { null }
val onLongClickItem: (Int) -> Unit = { toConfirmIndex ->
haptic.performHapticFeedback(HapticFeedbackType.LongPress)
(0 until 6).forEach { i -> confirm[i] = i == toConfirmIndex }
(0 until 3).forEach { i -> confirm[i] = i == toConfirmIndex }
resetJob?.cancel()
resetJob = scope.launch {
delay(1.seconds)
@ -209,54 +196,11 @@ private fun LibraryUpdateErrorsBottomBar(
)
.padding(horizontal = 8.dp, vertical = 12.dp),
) {
if (selected.isNotEmpty()) {
Button(
title = stringResource(MR.strings.action_cancel),
icon = Icons.Outlined.Close,
toConfirm = confirm[0],
onLongClick = { onLongClickItem(0) },
onClick = onCancelActionMode,
enabled = true,
)
} else {
Button(
title = androidx.compose.ui.res.stringResource(R.string.abc_action_bar_up_description),
icon = Icons.AutoMirrored.Outlined.ArrowBack,
toConfirm = confirm[0],
onLongClick = { onLongClickItem(0) },
onClick = navigateUp,
enabled = true,
)
}
Button(
title = stringResource(MR.strings.action_select_all),
icon = Icons.Outlined.SelectAll,
toConfirm = confirm[1],
onLongClick = { onLongClickItem(1) },
onClick = if (selected.isEmpty() or (selected.size != itemCount)) {
onSelectAll
} else {
{}
},
enabled = selected.isEmpty() or (selected.size != itemCount),
)
Button(
title = stringResource(MR.strings.action_select_inverse),
icon = Icons.Outlined.FlipToBack,
toConfirm = confirm[2],
onLongClick = { onLongClickItem(2) },
onClick = if (selected.isNotEmpty()) {
onInvertSelection
} else {
{}
},
enabled = selected.isNotEmpty(),
)
Button(
title = stringResource(MR.strings.action_scroll_to_top),
icon = Icons.Outlined.ArrowUpward,
toConfirm = confirm[3],
onLongClick = { onLongClickItem(3) },
icon = Icons.Outlined.VerticalAlignTop,
toConfirm = confirm[0],
onLongClick = { onLongClickItem(0) },
onClick = if (enableScrollToTop) {
scrollToTop
} else {
@ -264,23 +208,11 @@ private fun LibraryUpdateErrorsBottomBar(
},
enabled = enableScrollToTop,
)
Button(
title = stringResource(MR.strings.action_scroll_to_bottom),
icon = Icons.Outlined.ArrowDownward,
toConfirm = confirm[4],
onLongClick = { onLongClickItem(4) },
onClick = if (enableScrollToBottom) {
scrollToBottom
} else {
{}
},
enabled = enableScrollToBottom,
)
Button(
title = stringResource(MR.strings.migrate),
icon = Icons.Outlined.FindReplace,
toConfirm = confirm[5],
onLongClick = { onLongClickItem(5) },
toConfirm = confirm[1],
onLongClick = { onLongClickItem(1) },
onClick = if (selected.isNotEmpty()) {
onMultiMigrateClicked
} else {
@ -288,6 +220,18 @@ private fun LibraryUpdateErrorsBottomBar(
},
enabled = selected.isNotEmpty(),
)
Button(
title = stringResource(MR.strings.action_scroll_to_bottom),
icon = Icons.Outlined.VerticalAlignBottom,
toConfirm = confirm[2],
onLongClick = { onLongClickItem(2) },
onClick = if (enableScrollToBottom) {
scrollToBottom
} else {
{}
},
enabled = enableScrollToBottom,
)
}
}
}
@ -349,33 +293,49 @@ private fun RowScope.Button(
@Composable
private fun LibraryUpdateErrorsAppBar(
modifier: Modifier = Modifier,
title: String,
actionModeCounter: Int,
itemCnt: Int,
navigateUp: () -> Unit,
selectedCount: Int,
onClickUnselectAll: () -> Unit,
onClickSelectAll: () -> Unit,
onClickInvertSelection: () -> Unit,
scrollBehavior: TopAppBarScrollBehavior,
) {
val isActionMode by remember(actionModeCounter) {
derivedStateOf { actionModeCounter > 0 }
}
Column(
modifier = modifier,
) {
TopAppBar(
title = {
if (isActionMode) {
AppBarTitle("$actionModeCounter selected")
} else {
AppBarTitle(title)
}
},
actions = {},
colors = TopAppBarDefaults.topAppBarColors(
containerColor = MaterialTheme.colorScheme.surfaceColorAtElevation(
elevation = if (isActionMode) 3.dp else 0.dp,
AppBar(
title = title,
navigateUp = navigateUp,
actions = {
if (itemCnt > 0) {
AppBarActions(
persistentListOf(
AppBar.Action(
title = stringResource(MR.strings.action_select_all),
icon = Icons.Outlined.SelectAll,
onClick = onClickSelectAll,
),
),
)
}
},
actionModeCounter = selectedCount,
onCancelActionMode = onClickUnselectAll,
actionModeActions = {
AppBarActions(
persistentListOf(
AppBar.Action(
title = stringResource(MR.strings.action_select_all),
icon = Icons.Outlined.SelectAll,
onClick = onClickSelectAll,
),
AppBar.Action(
title = stringResource(MR.strings.action_select_inverse),
icon = Icons.Outlined.FlipToBack,
onClick = onClickInvertSelection,
),
),
),
scrollBehavior = scrollBehavior,
)
}
)
},
scrollBehavior = scrollBehavior,
)
}