Use remember var delegates in more places

This commit is contained in:
arkon 2022-10-29 16:14:44 -04:00
parent f9c25b350e
commit 0849111247
9 changed files with 56 additions and 36 deletions

View file

@ -6,8 +6,10 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.focus.focusRequester
@ -22,7 +24,7 @@ fun CategoryCreateDialog(
onDismissRequest: () -> Unit, onDismissRequest: () -> Unit,
onCreate: (String) -> Unit, onCreate: (String) -> Unit,
) { ) {
val (name, onNameChange) = remember { mutableStateOf("") } var name by remember { mutableStateOf("") }
val focusRequester = remember { FocusRequester() } val focusRequester = remember { FocusRequester() }
AlertDialog( AlertDialog(
@ -48,7 +50,7 @@ fun CategoryCreateDialog(
modifier = Modifier modifier = Modifier
.focusRequester(focusRequester), .focusRequester(focusRequester),
value = name, value = name,
onValueChange = onNameChange, onValueChange = { name = it },
label = { label = {
Text(text = stringResource(R.string.name)) Text(text = stringResource(R.string.name))
}, },
@ -70,7 +72,7 @@ fun CategoryRenameDialog(
onRename: (String) -> Unit, onRename: (String) -> Unit,
category: Category, category: Category,
) { ) {
val (name, onNameChange) = remember { mutableStateOf(category.name) } var name by remember { mutableStateOf(category.name) }
val focusRequester = remember { FocusRequester() } val focusRequester = remember { FocusRequester() }
AlertDialog( AlertDialog(
@ -96,7 +98,7 @@ fun CategoryRenameDialog(
modifier = Modifier modifier = Modifier
.focusRequester(focusRequester), .focusRequester(focusRequester),
value = name, value = name,
onValueChange = onNameChange, onValueChange = { name = it },
label = { label = {
Text(text = stringResource(R.string.name)) Text(text = stringResource(R.string.name))
}, },

View file

@ -22,6 +22,7 @@ fun DropdownMenu(
expanded: Boolean, expanded: Boolean,
onDismissRequest: () -> Unit, onDismissRequest: () -> Unit,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
offset: DpOffset = DpOffset(8.dp, (-56).dp),
properties: PopupProperties = PopupProperties(focusable = true), properties: PopupProperties = PopupProperties(focusable = true),
content: @Composable ColumnScope.() -> Unit, content: @Composable ColumnScope.() -> Unit,
) { ) {
@ -29,7 +30,7 @@ fun DropdownMenu(
expanded = expanded, expanded = expanded,
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,
modifier = modifier.sizeIn(minWidth = 196.dp, maxWidth = 196.dp), modifier = modifier.sizeIn(minWidth = 196.dp, maxWidth = 196.dp),
offset = DpOffset(8.dp, (-56).dp), offset = offset,
properties = properties, properties = properties,
content = content, content = content,
) )

View file

@ -35,6 +35,7 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.hapticfeedback.HapticFeedbackType import androidx.compose.ui.hapticfeedback.HapticFeedbackType
@ -410,7 +411,7 @@ fun MangaScreenLargeImpl(
val chapters = remember(state) { state.processedChapters.toList() } val chapters = remember(state) { state.processedChapters.toList() }
val insetPadding = WindowInsets.systemBars.only(WindowInsetsSides.Horizontal).asPaddingValues() val insetPadding = WindowInsets.systemBars.only(WindowInsetsSides.Horizontal).asPaddingValues()
val (topBarHeight, onTopBarHeightChanged) = remember { mutableStateOf(0) } var topBarHeight by remember { mutableStateOf(0) }
SwipeRefresh( SwipeRefresh(
refreshing = state.isRefreshingData, refreshing = state.isRefreshingData,
onRefresh = onRefresh, onRefresh = onRefresh,
@ -436,7 +437,7 @@ fun MangaScreenLargeImpl(
modifier = Modifier.padding(insetPadding), modifier = Modifier.padding(insetPadding),
topBar = { topBar = {
MangaToolbar( MangaToolbar(
modifier = Modifier.onSizeChanged { onTopBarHeightChanged(it.height) }, modifier = Modifier.onSizeChanged { topBarHeight = it.height },
title = state.manga.title, title = state.manga.title,
titleAlphaProvider = { if (chapters.any { it.selected }) 1f else 0f }, titleAlphaProvider = { if (chapters.any { it.selected }) 1f else 0f },
backgroundAlphaProvider = { 1f }, backgroundAlphaProvider = { 1f },

View file

@ -24,11 +24,14 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
@ -82,9 +85,15 @@ fun MangaCoverDialog(
} }
if (onEditClick != null) { if (onEditClick != null) {
Box { Box {
val (expanded, onExpand) = remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
IconButton( IconButton(
onClick = { if (isCustomCover) onExpand(true) else onEditClick(EditCoverAction.EDIT) }, onClick = {
if (isCustomCover) {
expanded = true
} else {
onEditClick(EditCoverAction.EDIT)
}
},
) { ) {
Icon( Icon(
imageVector = Icons.Outlined.Edit, imageVector = Icons.Outlined.Edit,
@ -93,20 +102,21 @@ fun MangaCoverDialog(
} }
DropdownMenu( DropdownMenu(
expanded = expanded, expanded = expanded,
onDismissRequest = { onExpand(false) }, onDismissRequest = { expanded = false },
offset = DpOffset(8.dp, 0.dp),
) { ) {
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_edit)) }, text = { Text(text = stringResource(R.string.action_edit)) },
onClick = { onClick = {
onEditClick(EditCoverAction.EDIT) onEditClick(EditCoverAction.EDIT)
onExpand(false) expanded = false
}, },
) )
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_delete)) }, text = { Text(text = stringResource(R.string.action_delete)) },
onClick = { onClick = {
onEditClick(EditCoverAction.DELETE) onEditClick(EditCoverAction.DELETE)
onExpand(false) expanded = false
}, },
) )
} }

View file

@ -22,8 +22,10 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.surfaceColorAtElevation import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
@ -157,15 +159,15 @@ fun MangaToolbar(
} }
if (onClickEditCategory != null && onClickMigrate != null) { if (onClickEditCategory != null && onClickMigrate != null) {
val (moreExpanded, onMoreExpanded) = remember { mutableStateOf(false) } var moreExpanded by remember { mutableStateOf(false) }
Box { Box {
IconButton(onClick = { onMoreExpanded(!moreExpanded) }) { IconButton(onClick = { moreExpanded = !moreExpanded }) {
Icon( Icon(
imageVector = Icons.Outlined.MoreVert, imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description), contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
) )
} }
val onDismissRequest = { onMoreExpanded(false) } val onDismissRequest = { moreExpanded = false }
DropdownMenu( DropdownMenu(
expanded = moreExpanded, expanded = moreExpanded,
onDismissRequest = onDismissRequest, onDismissRequest = onDismissRequest,

View file

@ -28,18 +28,18 @@ fun EditTextPreferenceWidget(
value: String, value: String,
onConfirm: suspend (String) -> Boolean, onConfirm: suspend (String) -> Boolean,
) { ) {
val (isDialogShown, showDialog) = remember { mutableStateOf(false) } var isDialogShown by remember { mutableStateOf(false) }
TextPreferenceWidget( TextPreferenceWidget(
title = title, title = title,
subtitle = subtitle?.format(value), subtitle = subtitle?.format(value),
icon = icon, icon = icon,
onPreferenceClick = { showDialog(true) }, onPreferenceClick = { isDialogShown = true },
) )
if (isDialogShown) { if (isDialogShown) {
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
val onDismissRequest = { showDialog(false) } val onDismissRequest = { isDialogShown = false }
var textFieldValue by rememberSaveable(stateSaver = TextFieldValue.Saver) { var textFieldValue by rememberSaveable(stateSaver = TextFieldValue.Saver) {
mutableStateOf(TextFieldValue(value)) mutableStateOf(TextFieldValue(value))
} }

View file

@ -12,8 +12,10 @@ import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
@ -36,18 +38,18 @@ fun <T> ListPreferenceWidget(
entries: Map<out T, String>, entries: Map<out T, String>,
onValueChange: (T) -> Unit, onValueChange: (T) -> Unit,
) { ) {
val (isDialogShown, showDialog) = remember { mutableStateOf(false) } var isDialogShown by remember { mutableStateOf(false) }
TextPreferenceWidget( TextPreferenceWidget(
title = title, title = title,
subtitle = subtitle, subtitle = subtitle,
icon = icon, icon = icon,
onPreferenceClick = { showDialog(true) }, onPreferenceClick = { isDialogShown = true },
) )
if (isDialogShown) { if (isDialogShown) {
AlertDialog( AlertDialog(
onDismissRequest = { showDialog(false) }, onDismissRequest = { isDialogShown = false },
title = { Text(text = title) }, title = { Text(text = title) },
text = { text = {
Box { Box {
@ -61,7 +63,7 @@ fun <T> ListPreferenceWidget(
isSelected = isSelected, isSelected = isSelected,
onSelected = { onSelected = {
onValueChange(current.key!!) onValueChange(current.key!!)
showDialog(false) isDialogShown = false
}, },
) )
} }
@ -72,7 +74,7 @@ fun <T> ListPreferenceWidget(
} }
}, },
confirmButton = { confirmButton = {
TextButton(onClick = { showDialog(false) }) { TextButton(onClick = { isDialogShown = false }) {
Text(text = stringResource(R.string.action_cancel)) Text(text = stringResource(R.string.action_cancel))
} }
}, },

View file

@ -11,8 +11,10 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.material3.TextButton import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
@ -30,13 +32,13 @@ fun MultiSelectListPreferenceWidget(
values: Set<String>, values: Set<String>,
onValuesChange: (Set<String>) -> Unit, onValuesChange: (Set<String>) -> Unit,
) { ) {
val (isDialogShown, showDialog) = remember { mutableStateOf(false) } var isDialogShown by remember { mutableStateOf(false) }
TextPreferenceWidget( TextPreferenceWidget(
title = preference.title, title = preference.title,
subtitle = preference.subtitleProvider(values, preference.entries), subtitle = preference.subtitleProvider(values, preference.entries),
icon = preference.icon, icon = preference.icon,
onPreferenceClick = { showDialog(true) }, onPreferenceClick = { isDialogShown = true },
) )
if (isDialogShown) { if (isDialogShown) {
@ -46,7 +48,7 @@ fun MultiSelectListPreferenceWidget(
.toMutableStateList() .toMutableStateList()
} }
AlertDialog( AlertDialog(
onDismissRequest = { showDialog(false) }, onDismissRequest = { isDialogShown = false },
title = { Text(text = preference.title) }, title = { Text(text = preference.title) },
text = { text = {
LazyColumn { LazyColumn {
@ -91,14 +93,14 @@ fun MultiSelectListPreferenceWidget(
TextButton( TextButton(
onClick = { onClick = {
onValuesChange(selected.toMutableSet()) onValuesChange(selected.toMutableSet())
showDialog(false) isDialogShown = false
}, },
) { ) {
Text(text = stringResource(android.R.string.ok)) Text(text = stringResource(android.R.string.ok))
} }
}, },
dismissButton = { dismissButton = {
TextButton(onClick = { showDialog(false) }) { TextButton(onClick = { isDialogShown = false }) {
Text(text = stringResource(R.string.action_cancel)) Text(text = stringResource(R.string.action_cancel))
} }
}, },

View file

@ -147,9 +147,9 @@ class DownloadController :
navigateUp = router::popCurrentController, navigateUp = router::popCurrentController,
actions = { actions = {
if (downloadList.isNotEmpty()) { if (downloadList.isNotEmpty()) {
val (expanded, onExpanded) = remember { mutableStateOf(false) } var expanded by remember { mutableStateOf(false) }
Box { Box {
IconButton(onClick = { onExpanded(!expanded) }) { IconButton(onClick = { expanded = !expanded }) {
Icon( Icon(
imageVector = Icons.Outlined.MoreVert, imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description), contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
@ -157,7 +157,7 @@ class DownloadController :
} }
CascadeDropdownMenu( CascadeDropdownMenu(
expanded = expanded, expanded = expanded,
onDismissRequest = { onExpanded(false) }, onDismissRequest = { expanded = false },
) { ) {
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_reorganize_by)) }, text = { Text(text = stringResource(R.string.action_reorganize_by)) },
@ -169,14 +169,14 @@ class DownloadController :
text = { Text(text = stringResource(R.string.action_newest)) }, text = { Text(text = stringResource(R.string.action_newest)) },
onClick = { onClick = {
reorderQueue({ it.download.chapter.date_upload }, true) reorderQueue({ it.download.chapter.date_upload }, true)
onExpanded(false) expanded = false
}, },
) )
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_oldest)) }, text = { Text(text = stringResource(R.string.action_oldest)) },
onClick = { onClick = {
reorderQueue({ it.download.chapter.date_upload }, false) reorderQueue({ it.download.chapter.date_upload }, false)
onExpanded(false) expanded = false
}, },
) )
}, },
@ -188,14 +188,14 @@ class DownloadController :
text = { Text(text = stringResource(R.string.action_asc)) }, text = { Text(text = stringResource(R.string.action_asc)) },
onClick = { onClick = {
reorderQueue({ it.download.chapter.chapter_number }, false) reorderQueue({ it.download.chapter.chapter_number }, false)
onExpanded(false) expanded = false
}, },
) )
DropdownMenuItem( DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_desc)) }, text = { Text(text = stringResource(R.string.action_desc)) },
onClick = { onClick = {
reorderQueue({ it.download.chapter.chapter_number }, true) reorderQueue({ it.download.chapter.chapter_number }, true)
onExpanded(false) expanded = false
}, },
) )
}, },
@ -206,7 +206,7 @@ class DownloadController :
text = { Text(text = stringResource(R.string.action_cancel_all)) }, text = { Text(text = stringResource(R.string.action_cancel_all)) },
onClick = { onClick = {
presenter.clearQueue(context) presenter.clearQueue(context)
onExpanded(false) expanded = false
}, },
) )
} }