mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-12 20:19:05 +01:00
Migrate More screen to Compose (#6990)
This commit is contained in:
@@ -11,12 +11,14 @@ import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.widthIn
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Switch
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.graphics.painter.Painter
|
||||
import androidx.compose.ui.unit.dp
|
||||
import eu.kanade.core.prefs.PreferenceMutableState
|
||||
import eu.kanade.presentation.util.horizontalPadding
|
||||
|
||||
@Composable
|
||||
@@ -29,7 +31,7 @@ fun Divider() {
|
||||
@Composable
|
||||
fun PreferenceRow(
|
||||
title: String,
|
||||
icon: ImageVector? = null,
|
||||
painter: Painter? = null,
|
||||
onClick: () -> Unit = {},
|
||||
onLongClick: () -> Unit = {},
|
||||
subtitle: String? = null,
|
||||
@@ -50,18 +52,18 @@ fun PreferenceRow(
|
||||
.heightIn(min = height)
|
||||
.combinedClickable(
|
||||
onLongClick = onLongClick,
|
||||
onClick = onClick
|
||||
onClick = onClick,
|
||||
),
|
||||
verticalAlignment = Alignment.CenterVertically
|
||||
) {
|
||||
if (icon != null) {
|
||||
if (painter != null) {
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
painter = painter,
|
||||
modifier = Modifier
|
||||
.padding(horizontal = horizontalPadding)
|
||||
.size(24.dp),
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
contentDescription = null
|
||||
contentDescription = null,
|
||||
)
|
||||
}
|
||||
Column(
|
||||
@@ -88,3 +90,23 @@ fun PreferenceRow(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun SwitchPreference(
|
||||
preference: PreferenceMutableState<Boolean>,
|
||||
title: String,
|
||||
subtitle: String? = null,
|
||||
painter: Painter? = null,
|
||||
) {
|
||||
PreferenceRow(
|
||||
title = title,
|
||||
subtitle = subtitle,
|
||||
painter = painter,
|
||||
action = {
|
||||
Switch(checked = preference.value, onCheckedChange = null)
|
||||
// TODO: remove this once switch checked state is fixed: https://issuetracker.google.com/issues/228336571
|
||||
Text(preference.value.toString())
|
||||
},
|
||||
onClick = { preference.value = !preference.value },
|
||||
)
|
||||
}
|
||||
|
||||
131
app/src/main/java/eu/kanade/presentation/more/MoreScreen.kt
Normal file
131
app/src/main/java/eu/kanade/presentation/more/MoreScreen.kt
Normal file
@@ -0,0 +1,131 @@
|
||||
package eu.kanade.presentation.more
|
||||
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.outlined.CloudOff
|
||||
import androidx.compose.material.icons.outlined.GetApp
|
||||
import androidx.compose.material.icons.outlined.HelpOutline
|
||||
import androidx.compose.material.icons.outlined.Info
|
||||
import androidx.compose.material.icons.outlined.Label
|
||||
import androidx.compose.material.icons.outlined.Settings
|
||||
import androidx.compose.material.icons.outlined.SettingsBackupRestore
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.rememberVectorPainter
|
||||
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
|
||||
import androidx.compose.ui.input.nestedscroll.nestedScroll
|
||||
import androidx.compose.ui.platform.LocalUriHandler
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import eu.kanade.presentation.components.Divider
|
||||
import eu.kanade.presentation.components.PreferenceRow
|
||||
import eu.kanade.presentation.components.SwitchPreference
|
||||
import eu.kanade.presentation.util.quantityStringResource
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.ui.more.DownloadQueueState
|
||||
import eu.kanade.tachiyomi.ui.more.MoreController
|
||||
import eu.kanade.tachiyomi.ui.more.MorePresenter
|
||||
|
||||
@Composable
|
||||
fun MoreScreen(
|
||||
nestedScrollInterop: NestedScrollConnection,
|
||||
presenter: MorePresenter,
|
||||
onClickDownloadQueue: () -> Unit,
|
||||
onClickCategories: () -> Unit,
|
||||
onClickBackupAndRestore: () -> Unit,
|
||||
onClickSettings: () -> Unit,
|
||||
onClickAbout: () -> Unit,
|
||||
) {
|
||||
val uriHandler = LocalUriHandler.current
|
||||
val downloadQueueState by presenter.downloadQueueState.collectAsState()
|
||||
|
||||
LazyColumn(
|
||||
modifier = Modifier.nestedScroll(nestedScrollInterop),
|
||||
) {
|
||||
item {
|
||||
LogoHeader()
|
||||
}
|
||||
|
||||
item {
|
||||
SwitchPreference(
|
||||
preference = presenter.downloadedOnly,
|
||||
title = stringResource(R.string.label_downloaded_only),
|
||||
subtitle = stringResource(R.string.downloaded_only_summary),
|
||||
painter = rememberVectorPainter(Icons.Outlined.CloudOff),
|
||||
)
|
||||
}
|
||||
item {
|
||||
SwitchPreference(
|
||||
preference = presenter.incognitoMode,
|
||||
title = stringResource(R.string.pref_incognito_mode),
|
||||
subtitle = stringResource(R.string.pref_incognito_mode_summary),
|
||||
painter = painterResource(R.drawable.ic_glasses_24dp),
|
||||
)
|
||||
}
|
||||
|
||||
item { Divider() }
|
||||
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_download_queue),
|
||||
subtitle = when (downloadQueueState) {
|
||||
DownloadQueueState.Stopped -> null
|
||||
is DownloadQueueState.Paused -> {
|
||||
val pending = (downloadQueueState as DownloadQueueState.Paused).pending
|
||||
if (pending == 0) {
|
||||
stringResource(R.string.paused)
|
||||
} else {
|
||||
"${stringResource(R.string.paused)} • ${quantityStringResource(R.plurals.download_queue_summary, pending, pending)}"
|
||||
}
|
||||
}
|
||||
is DownloadQueueState.Downloading -> {
|
||||
val pending = (downloadQueueState as DownloadQueueState.Downloading).pending
|
||||
quantityStringResource(R.plurals.download_queue_summary, pending, pending)
|
||||
}
|
||||
},
|
||||
painter = rememberVectorPainter(Icons.Outlined.GetApp),
|
||||
onClick = { onClickDownloadQueue() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.categories),
|
||||
painter = rememberVectorPainter(Icons.Outlined.Label),
|
||||
onClick = { onClickCategories() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_backup),
|
||||
painter = rememberVectorPainter(Icons.Outlined.SettingsBackupRestore),
|
||||
onClick = { onClickBackupAndRestore() },
|
||||
)
|
||||
}
|
||||
|
||||
item { Divider() }
|
||||
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_settings),
|
||||
painter = rememberVectorPainter(Icons.Outlined.Settings),
|
||||
onClick = { onClickSettings() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.pref_category_about),
|
||||
painter = rememberVectorPainter(Icons.Outlined.Info),
|
||||
onClick = { onClickAbout() },
|
||||
)
|
||||
}
|
||||
item {
|
||||
PreferenceRow(
|
||||
title = stringResource(R.string.label_help),
|
||||
painter = rememberVectorPainter(Icons.Outlined.HelpOutline),
|
||||
onClick = { uriHandler.openUri(MoreController.URL_HELP) },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
32
app/src/main/java/eu/kanade/presentation/util/Resources.kt
Normal file
32
app/src/main/java/eu/kanade/presentation/util/Resources.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package eu.kanade.presentation.util
|
||||
|
||||
import androidx.annotation.PluralsRes
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
|
||||
/**
|
||||
* Load a quantity string resource.
|
||||
*
|
||||
* @param id the resource identifier
|
||||
* @param quantity The number used to get the string for the current language's plural rules.
|
||||
* @return the string data associated with the resource
|
||||
*/
|
||||
@Composable
|
||||
fun quantityStringResource(@PluralsRes id: Int, quantity: Int): String {
|
||||
val context = LocalContext.current
|
||||
return context.resources.getQuantityString(id, quantity, quantity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a quantity string resource with formatting.
|
||||
*
|
||||
* @param id the resource identifier
|
||||
* @param quantity The number used to get the string for the current language's plural rules.
|
||||
* @param formatArgs the format arguments
|
||||
* @return the string data associated with the resource
|
||||
*/
|
||||
@Composable
|
||||
fun quantityStringResource(@PluralsRes id: Int, quantity: Int, vararg formatArgs: Any): String {
|
||||
val context = LocalContext.current
|
||||
return context.resources.getQuantityString(id, quantity, *formatArgs)
|
||||
}
|
||||
Reference in New Issue
Block a user