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

View file

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

View file

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

View file

@ -24,11 +24,14 @@ import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.view.updatePadding
@ -82,9 +85,15 @@ fun MangaCoverDialog(
}
if (onEditClick != null) {
Box {
val (expanded, onExpand) = remember { mutableStateOf(false) }
var expanded by remember { mutableStateOf(false) }
IconButton(
onClick = { if (isCustomCover) onExpand(true) else onEditClick(EditCoverAction.EDIT) },
onClick = {
if (isCustomCover) {
expanded = true
} else {
onEditClick(EditCoverAction.EDIT)
}
},
) {
Icon(
imageVector = Icons.Outlined.Edit,
@ -93,20 +102,21 @@ fun MangaCoverDialog(
}
DropdownMenu(
expanded = expanded,
onDismissRequest = { onExpand(false) },
onDismissRequest = { expanded = false },
offset = DpOffset(8.dp, 0.dp),
) {
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_edit)) },
onClick = {
onEditClick(EditCoverAction.EDIT)
onExpand(false)
expanded = false
},
)
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_delete)) },
onClick = {
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.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource
@ -157,15 +159,15 @@ fun MangaToolbar(
}
if (onClickEditCategory != null && onClickMigrate != null) {
val (moreExpanded, onMoreExpanded) = remember { mutableStateOf(false) }
var moreExpanded by remember { mutableStateOf(false) }
Box {
IconButton(onClick = { onMoreExpanded(!moreExpanded) }) {
IconButton(onClick = { moreExpanded = !moreExpanded }) {
Icon(
imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
)
}
val onDismissRequest = { onMoreExpanded(false) }
val onDismissRequest = { moreExpanded = false }
DropdownMenu(
expanded = moreExpanded,
onDismissRequest = onDismissRequest,

View file

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

View file

@ -12,8 +12,10 @@ import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
@ -36,18 +38,18 @@ fun <T> ListPreferenceWidget(
entries: Map<out T, String>,
onValueChange: (T) -> Unit,
) {
val (isDialogShown, showDialog) = remember { mutableStateOf(false) }
var isDialogShown by remember { mutableStateOf(false) }
TextPreferenceWidget(
title = title,
subtitle = subtitle,
icon = icon,
onPreferenceClick = { showDialog(true) },
onPreferenceClick = { isDialogShown = true },
)
if (isDialogShown) {
AlertDialog(
onDismissRequest = { showDialog(false) },
onDismissRequest = { isDialogShown = false },
title = { Text(text = title) },
text = {
Box {
@ -61,7 +63,7 @@ fun <T> ListPreferenceWidget(
isSelected = isSelected,
onSelected = {
onValueChange(current.key!!)
showDialog(false)
isDialogShown = false
},
)
}
@ -72,7 +74,7 @@ fun <T> ListPreferenceWidget(
}
},
confirmButton = {
TextButton(onClick = { showDialog(false) }) {
TextButton(onClick = { isDialogShown = false }) {
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.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -30,13 +32,13 @@ fun MultiSelectListPreferenceWidget(
values: Set<String>,
onValuesChange: (Set<String>) -> Unit,
) {
val (isDialogShown, showDialog) = remember { mutableStateOf(false) }
var isDialogShown by remember { mutableStateOf(false) }
TextPreferenceWidget(
title = preference.title,
subtitle = preference.subtitleProvider(values, preference.entries),
icon = preference.icon,
onPreferenceClick = { showDialog(true) },
onPreferenceClick = { isDialogShown = true },
)
if (isDialogShown) {
@ -46,7 +48,7 @@ fun MultiSelectListPreferenceWidget(
.toMutableStateList()
}
AlertDialog(
onDismissRequest = { showDialog(false) },
onDismissRequest = { isDialogShown = false },
title = { Text(text = preference.title) },
text = {
LazyColumn {
@ -91,14 +93,14 @@ fun MultiSelectListPreferenceWidget(
TextButton(
onClick = {
onValuesChange(selected.toMutableSet())
showDialog(false)
isDialogShown = false
},
) {
Text(text = stringResource(android.R.string.ok))
}
},
dismissButton = {
TextButton(onClick = { showDialog(false) }) {
TextButton(onClick = { isDialogShown = false }) {
Text(text = stringResource(R.string.action_cancel))
}
},

View file

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