Yokan / rebuild
Course 1 8624561

AboutSettingsScreen.kt

composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings/about/AboutSettingsScreen.kt

implementation216 lines
1package eu.friedcutlet.yokan.ui.screens.settings.about23import androidx.compose.foundation.layout.Arrangement4import androidx.compose.foundation.layout.Column5import androidx.compose.foundation.layout.Row6import androidx.compose.foundation.layout.fillMaxWidth7import androidx.compose.foundation.layout.heightIn8import androidx.compose.foundation.layout.padding9import androidx.compose.foundation.layout.size10import androidx.compose.foundation.rememberScrollState11import androidx.compose.foundation.verticalScroll12import androidx.compose.material.icons.Icons13import androidx.compose.material.icons.automirrored.filled.MenuBook14import androidx.compose.material3.AlertDialog15import androidx.compose.material3.Icon16import androidx.compose.material3.MaterialTheme17import androidx.compose.material3.OutlinedButton18import androidx.compose.material3.Text19import androidx.compose.material3.TextButton20import androidx.compose.runtime.Composable21import androidx.compose.runtime.getValue22import androidx.compose.runtime.mutableStateOf23import androidx.compose.runtime.remember24import androidx.compose.runtime.setValue25import androidx.compose.ui.Alignment26import androidx.compose.ui.Modifier27import androidx.compose.ui.text.font.FontWeight28import androidx.compose.ui.unit.dp29import androidx.lifecycle.compose.collectAsStateWithLifecycle30import androidx.lifecycle.viewmodel.compose.viewModel31import eu.friedcutlet.yokan.app.YokanRuntime32import eu.friedcutlet.yokan.database.DbSettings33import eu.friedcutlet.yokan.services.logging.LogSink34import eu.friedcutlet.yokan.services.system.openSystemBrowser35import eu.friedcutlet.yokan.services.updates.ReleaseAsset36import eu.friedcutlet.yokan.services.updates.ReleaseChannel37import eu.friedcutlet.yokan.services.updates.YOKAN_GITHUB_URL38import eu.friedcutlet.yokan.services.updates.YOKAN_RELEASE_CHANNEL39import eu.friedcutlet.yokan.services.updates.YOKAN_VERSION40import eu.friedcutlet.yokan.ui.screens.settings.SettingTile41import eu.friedcutlet.yokan.ui.screens.settings.SettingsScaffold42import eu.friedcutlet.yokan.ui.screens.settings.SwitchTile43import eu.friedcutlet.yokan.ui.screens.settings.data.formatBytes44import org.jetbrains.compose.resources.stringResource45import yokan.composeapp.generated.resources.Res46import yokan.composeapp.generated.resources.delete47import yokan.composeapp.generated.resources.tile_check_for_app_updates48import yokan.composeapp.generated.resources.ui_close49import yokan.composeapp.generated.resources.version_title50import yokan.composeapp.generated.resources.version_channel_alpha51import yokan.composeapp.generated.resources.version_channel_beta52import yokan.composeapp.generated.resources.version_channel_stable53import yokan.composeapp.generated.resources.version_channel_label54import eu.friedcutlet.yokan.ui.theme.YokanHorizontalDivider as HorizontalDivider5556@Composable57internal fun AboutSettingsRoute(58    title: String,59    settings: DbSettings,60    onUpdate: ((DbSettings) -> DbSettings) -> Unit,61    onBack: () -> Unit,62) {63    val model = viewModel { AboutSettingsViewModel(YokanRuntime.services.http, YokanRuntime.localFiles) }64    val state by model.state.collectAsStateWithLifecycle()65    AboutSettingsScreen(66        title = title,67        settings = settings,68        state = state,69        onUpdate = onUpdate,70        onCheckForUpdate = model::checkForUpdate,71        onDownloadAsset = model::downloadAsset,72        onShareLog = model::shareLog,73        onShareCrashReport = model::shareCrashReport,74        onDeleteCrashReport = model::deleteCrashReport,75        onBack = onBack,76    )77}7879@Composable80private fun AboutSettingsScreen(81    title: String,82    settings: DbSettings,83    state: AboutSettingsState,84    onUpdate: ((DbSettings) -> DbSettings) -> Unit,85    onCheckForUpdate: () -> Unit,86    onDownloadAsset: (ReleaseAsset) -> Unit,87    onShareLog: (String) -> Unit,88    onShareCrashReport: () -> Unit,89    onDeleteCrashReport: () -> Unit,90    onBack: () -> Unit,91) {92    val crashReport = state.crashReport93    var crashReportOpen by remember { mutableStateOf(false) }94    if (crashReportOpen) {95        AlertDialog(96            onDismissRequest = { crashReportOpen = false },97            title = { Text("Last crash report") },98            text = { Text(crashReport.orEmpty(), modifier = Modifier.heightIn(max = 360.dp).verticalScroll(rememberScrollState())) },99            confirmButton = { TextButton(onClick = { crashReportOpen = false }) { Text(stringResource(Res.string.ui_close)) } },100        )101    }102    SettingsScaffold(title, onBack) {103        item {104            // App banner — centered icon + name + version, matching the Flutter "logo" tile.105            Column(106                modifier = Modifier.fillMaxWidth().padding(vertical = 24.dp),107                horizontalAlignment = Alignment.CenterHorizontally,108                verticalArrangement = Arrangement.spacedBy(8.dp),109            ) {110                Icon(111                    Icons.AutoMirrored.Filled.MenuBook,112                    contentDescription = null,113                    tint = MaterialTheme.colorScheme.primary,114                    modifier = Modifier.size(72.dp),115                )116                Text("Yokan", style = MaterialTheme.typography.headlineSmall, fontWeight = FontWeight.SemiBold)117            }118        }119        item { HorizontalDivider() }120        item(key = "version") {121            val channelLabel = stringResource(when (YOKAN_RELEASE_CHANNEL) {122                ReleaseChannel.Alpha -> Res.string.version_channel_alpha123                ReleaseChannel.Beta -> Res.string.version_channel_beta124                ReleaseChannel.Stable -> Res.string.version_channel_stable125            })126            SettingTile(127                stringResource(Res.string.version_title),128                stringResource(Res.string.version_channel_label, channelLabel, YOKAN_VERSION),129            )130        }131        item {132            SwitchTile(133                title = stringResource(Res.string.tile_check_for_app_updates),134                subtitle = null,135                value = settings.checkForAppUpdates,136                onValueChange = { v -> onUpdate { it.copy(checkForAppUpdates = v) } },137            )138        }139        item {140            SettingTile(141                title = "Check for update",142                subtitle = state.updateMessage ?: "Query GitHub Releases for the latest build",143                onClick = onCheckForUpdate,144            )145        }146        // Show downloadable assets right under the check-for-update row when a release was fetched.147        state.release?.let { info ->148            info.assets.take(ShownAssets).forEach { asset ->149                item(key = "asset:${asset.name}") {150                    val isDownloaded = asset.name in state.downloadedAssets151                    Row(152                        modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 6.dp),153                        horizontalArrangement = Arrangement.spacedBy(8.dp),154                        verticalAlignment = Alignment.CenterVertically,155                    ) {156                        Column(Modifier.weight(1f)) {157                            Text(asset.name, fontWeight = FontWeight.SemiBold)158                            Text(formatBytes(asset.sizeBytes), color = MaterialTheme.colorScheme.onSurfaceVariant, style = MaterialTheme.typography.bodySmall)159                        }160                        OutlinedButton(onClick = { onDownloadAsset(asset) }) { Text(if (isDownloaded) "Re-download" else "Download") }161                    }162                }163            }164        }165        item {166            SwitchTile(167                title = "Logs on",168                subtitle = "Append app logs to a file",169                value = settings.enableFileLogging,170                onValueChange = { v -> onUpdate { it.copy(enableFileLogging = v) } },171            )172        }173        if (settings.enableFileLogging) {174            val logPath = LogSink.logFilePath()175            item {176                SettingTile(177                    title = "Share app logs",178                    subtitle = logPath ?: "No log file yet",179                    enabled = logPath != null,180                    onClick = { logPath?.let(onShareLog) },181                )182            }183        }184        crashReport?.let { report ->185            item {186                SettingTile(187                    title = "Last crash report",188                    subtitle = "Stored locally; ${formatBytes(report.encodeToByteArray().size.toLong())}",189                    onClick = { crashReportOpen = true },190                )191            }192            item {193                Row(194                    modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp),195                    horizontalArrangement = Arrangement.End,196                ) {197                    TextButton(onClick = onShareCrashReport) { Text("Share") }198                    TextButton(onClick = onDeleteCrashReport) { Text(stringResource(Res.string.delete)) }199                }200            }201        }202        item {203            Row(204                modifier = Modifier.fillMaxWidth().padding(vertical = 16.dp),205                horizontalArrangement = Arrangement.SpaceEvenly,206            ) {207                OutlinedButton(onClick = {208                    openSystemBrowser(YOKAN_GITHUB_URL)209                }) { Text("GitHub") }210                OutlinedButton(onClick = {211                    openSystemBrowser("https://discord.gg/yokan")212                }) { Text("Discord") }213            }214        }215    }216}217

Search chapters, reference and source filenames.