|
| 1 | +package dev.dimension.flare.ui.screen.settings |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.Arrangement |
| 4 | +import androidx.compose.foundation.layout.Row |
| 5 | +import androidx.compose.foundation.layout.Spacer |
| 6 | +import androidx.compose.foundation.layout.height |
| 7 | +import androidx.compose.foundation.layout.padding |
| 8 | +import androidx.compose.foundation.lazy.LazyColumn |
| 9 | +import androidx.compose.foundation.lazy.items |
| 10 | +import androidx.compose.runtime.Composable |
| 11 | +import androidx.compose.runtime.getValue |
| 12 | +import androidx.compose.runtime.mutableStateOf |
| 13 | +import androidx.compose.runtime.remember |
| 14 | +import androidx.compose.runtime.setValue |
| 15 | +import androidx.compose.ui.Alignment |
| 16 | +import androidx.compose.ui.Modifier |
| 17 | +import androidx.compose.ui.unit.dp |
| 18 | +import compose.icons.FontAwesomeIcons |
| 19 | +import compose.icons.fontawesomeicons.Solid |
| 20 | +import compose.icons.fontawesomeicons.solid.FloppyDisk |
| 21 | +import compose.icons.fontawesomeicons.solid.Trash |
| 22 | +import dev.dimension.flare.LocalWindowPadding |
| 23 | +import dev.dimension.flare.Res |
| 24 | +import dev.dimension.flare.settings_app_logging_clear |
| 25 | +import dev.dimension.flare.settings_app_logging_enable_network_logging |
| 26 | +import dev.dimension.flare.settings_app_logging_save |
| 27 | +import dev.dimension.flare.ui.component.FAIcon |
| 28 | +import dev.dimension.flare.ui.presenter.invoke |
| 29 | +import dev.dimension.flare.ui.presenter.settings.DevModePresenter |
| 30 | +import dev.dimension.flare.ui.theme.LocalComposeWindow |
| 31 | +import dev.dimension.flare.ui.theme.screenHorizontalPadding |
| 32 | +import io.github.composefluent.component.AccentButton |
| 33 | +import io.github.composefluent.component.Expander |
| 34 | +import io.github.composefluent.component.ExpanderItem |
| 35 | +import io.github.composefluent.component.SubtleButton |
| 36 | +import io.github.composefluent.component.Switcher |
| 37 | +import io.github.composefluent.component.Text |
| 38 | +import moe.tlaster.precompose.molecule.producePresenter |
| 39 | +import org.jetbrains.compose.resources.stringResource |
| 40 | +import java.awt.FileDialog |
| 41 | +import java.io.File |
| 42 | +import kotlin.time.Clock |
| 43 | +import kotlin.time.ExperimentalTime |
| 44 | + |
| 45 | +@OptIn(ExperimentalTime::class) |
| 46 | +@Composable |
| 47 | +internal fun AppLoggingScreen() { |
| 48 | + val window = LocalComposeWindow.current |
| 49 | + val state by producePresenter { presenter() } |
| 50 | + LazyColumn( |
| 51 | + modifier = |
| 52 | + Modifier |
| 53 | + .padding(horizontal = screenHorizontalPadding), |
| 54 | + verticalArrangement = Arrangement.spacedBy(2.dp), |
| 55 | + contentPadding = LocalWindowPadding.current, |
| 56 | + ) { |
| 57 | + item { |
| 58 | + Row( |
| 59 | + horizontalArrangement = Arrangement.spacedBy(8.dp, Alignment.End), |
| 60 | + modifier = Modifier.fillParentMaxWidth(), |
| 61 | + ) { |
| 62 | + SubtleButton( |
| 63 | + onClick = { |
| 64 | + FileDialog(window).apply { |
| 65 | + mode = FileDialog.SAVE |
| 66 | + file = "flare-log-${Clock.System.now().toEpochMilliseconds()}.txt" |
| 67 | + isVisible = true |
| 68 | + val dir = directory |
| 69 | + val file = file |
| 70 | + if (dir != null && file != null) { |
| 71 | + val data = state.printMessageToString() |
| 72 | + val file = File(dir, file) |
| 73 | + file.writeText(data) |
| 74 | + } |
| 75 | + } |
| 76 | + }, |
| 77 | + ) { |
| 78 | + FAIcon( |
| 79 | + FontAwesomeIcons.Solid.FloppyDisk, |
| 80 | + contentDescription = stringResource(Res.string.settings_app_logging_save), |
| 81 | + ) |
| 82 | + Text( |
| 83 | + stringResource(Res.string.settings_app_logging_save), |
| 84 | + ) |
| 85 | + } |
| 86 | + AccentButton( |
| 87 | + onClick = { |
| 88 | + state.clear() |
| 89 | + }, |
| 90 | + ) { |
| 91 | + FAIcon( |
| 92 | + imageVector = FontAwesomeIcons.Solid.Trash, |
| 93 | + contentDescription = stringResource(Res.string.settings_app_logging_clear), |
| 94 | + ) |
| 95 | + Text( |
| 96 | + text = stringResource(Res.string.settings_app_logging_clear), |
| 97 | + ) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + item { |
| 102 | + Spacer(modifier = Modifier.height(6.dp)) |
| 103 | + } |
| 104 | + item { |
| 105 | + ExpanderItem( |
| 106 | + heading = { |
| 107 | + Text(stringResource(Res.string.settings_app_logging_enable_network_logging)) |
| 108 | + }, |
| 109 | + trailing = { |
| 110 | + Switcher( |
| 111 | + checked = state.enabled, |
| 112 | + onCheckStateChange = { |
| 113 | + state.setEnabled(it) |
| 114 | + }, |
| 115 | + textBefore = true, |
| 116 | + ) |
| 117 | + }, |
| 118 | + ) |
| 119 | + } |
| 120 | + item { |
| 121 | + Spacer(modifier = Modifier.height(12.dp)) |
| 122 | + } |
| 123 | + items(state.messages) { it -> |
| 124 | + var isExpanded by remember { mutableStateOf(false) } |
| 125 | + Expander( |
| 126 | + expanded = isExpanded, |
| 127 | + onExpandedChanged = { |
| 128 | + isExpanded = it |
| 129 | + }, |
| 130 | + heading = { |
| 131 | + Text(it, maxLines = 3) |
| 132 | + }, |
| 133 | + expandContent = { |
| 134 | + Text( |
| 135 | + it, |
| 136 | + modifier = Modifier.padding(16.dp), |
| 137 | + ) |
| 138 | + }, |
| 139 | + ) |
| 140 | + } |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +@Composable |
| 145 | +private fun presenter() = |
| 146 | + run { |
| 147 | + remember { DevModePresenter() }.invoke() |
| 148 | + } |
0 commit comments