|
| 1 | +package com.aicode.feature.settings.presentation.component |
| 2 | + |
| 3 | +import androidx.compose.foundation.BorderStroke |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Column |
| 6 | +import androidx.compose.foundation.layout.fillMaxSize |
| 7 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 8 | +import androidx.compose.foundation.layout.padding |
| 9 | +import androidx.compose.foundation.rememberScrollState |
| 10 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 11 | +import androidx.compose.foundation.text.selection.SelectionContainer |
| 12 | +import androidx.compose.foundation.verticalScroll |
| 13 | +import androidx.compose.material3.Card |
| 14 | +import androidx.compose.material3.CardDefaults |
| 15 | +import androidx.compose.material3.FilterChip |
| 16 | +import androidx.compose.material3.MaterialTheme |
| 17 | +import androidx.compose.material3.Text |
| 18 | +import androidx.compose.runtime.Composable |
| 19 | +import androidx.compose.ui.Modifier |
| 20 | +import androidx.compose.ui.text.font.FontFamily |
| 21 | +import androidx.compose.ui.text.font.FontWeight |
| 22 | +import androidx.compose.ui.unit.dp |
| 23 | +import com.aicode.core.theme.Radius |
| 24 | +import com.aicode.core.theme.Spacing |
| 25 | +import com.aicode.core.util.LogLevel |
| 26 | +import com.aicode.feature.settings.presentation.LogViewerUiState |
| 27 | + |
| 28 | +@Composable |
| 29 | +internal fun LogSection( |
| 30 | + current: LogLevel, |
| 31 | + onSelect: (LogLevel) -> Unit |
| 32 | +) { |
| 33 | + Column( |
| 34 | + modifier = Modifier |
| 35 | + .fillMaxSize() |
| 36 | + .padding(Spacing.lg), |
| 37 | + verticalArrangement = Arrangement.spacedBy(Spacing.md) |
| 38 | + ) { |
| 39 | + LogLevelCard(current = current, onSelect = onSelect) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +@OptIn(androidx.compose.foundation.layout.ExperimentalLayoutApi::class) |
| 44 | +@Composable |
| 45 | +internal fun LogViewerSection( |
| 46 | + state: LogViewerUiState, |
| 47 | + onSelectFile: (String) -> Unit |
| 48 | +) { |
| 49 | + Column( |
| 50 | + modifier = Modifier |
| 51 | + .fillMaxSize() |
| 52 | + .padding(Spacing.lg), |
| 53 | + verticalArrangement = Arrangement.spacedBy(Spacing.md) |
| 54 | + ) { |
| 55 | + Card( |
| 56 | + modifier = Modifier.fillMaxWidth(), |
| 57 | + shape = RoundedCornerShape(Radius.md), |
| 58 | + colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), |
| 59 | + border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant) |
| 60 | + ) { |
| 61 | + Column(modifier = Modifier.padding(Spacing.lg)) { |
| 62 | + Text( |
| 63 | + text = state.filterServerName?.let { "MCP 日志:$it" } ?: "全部日志", |
| 64 | + style = MaterialTheme.typography.titleMedium, |
| 65 | + fontWeight = FontWeight.Normal, |
| 66 | + color = MaterialTheme.colorScheme.onSurface |
| 67 | + ) |
| 68 | + Text( |
| 69 | + text = logViewerSummary(state), |
| 70 | + style = MaterialTheme.typography.bodySmall, |
| 71 | + color = MaterialTheme.colorScheme.onSurfaceVariant, |
| 72 | + modifier = Modifier.padding(top = Spacing.xs) |
| 73 | + ) |
| 74 | + |
| 75 | + if (state.files.isNotEmpty()) { |
| 76 | + androidx.compose.foundation.layout.FlowRow( |
| 77 | + modifier = Modifier |
| 78 | + .fillMaxWidth() |
| 79 | + .padding(top = Spacing.sm), |
| 80 | + horizontalArrangement = Arrangement.spacedBy(Spacing.sm), |
| 81 | + verticalArrangement = Arrangement.spacedBy(Spacing.xs) |
| 82 | + ) { |
| 83 | + state.files.forEach { fileName -> |
| 84 | + FilterChip( |
| 85 | + selected = fileName == state.selectedFileName, |
| 86 | + onClick = { onSelectFile(fileName) }, |
| 87 | + label = { Text(fileName.removePrefix("log-").removeSuffix(".txt")) } |
| 88 | + ) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + Card( |
| 96 | + modifier = Modifier |
| 97 | + .fillMaxWidth() |
| 98 | + .weight(1f), |
| 99 | + shape = RoundedCornerShape(Radius.md), |
| 100 | + colors = CardDefaults.cardColors(containerColor = MaterialTheme.colorScheme.surface), |
| 101 | + border = BorderStroke(1.dp, MaterialTheme.colorScheme.outlineVariant) |
| 102 | + ) { |
| 103 | + val verticalScroll = rememberScrollState() |
| 104 | + val text = when { |
| 105 | + state.loading -> "正在读取日志..." |
| 106 | + state.error != null -> state.error |
| 107 | + state.content.isBlank() -> "当前筛选条件下没有日志" |
| 108 | + else -> state.content |
| 109 | + } |
| 110 | + |
| 111 | + SelectionContainer { |
| 112 | + Text( |
| 113 | + text = text, |
| 114 | + modifier = Modifier |
| 115 | + .fillMaxSize() |
| 116 | + .verticalScroll(verticalScroll) |
| 117 | + .padding(Spacing.md), |
| 118 | + style = MaterialTheme.typography.bodySmall.copy(fontFamily = FontFamily.Monospace), |
| 119 | + color = if (state.error == null) { |
| 120 | + MaterialTheme.colorScheme.onSurface |
| 121 | + } else { |
| 122 | + MaterialTheme.colorScheme.error |
| 123 | + } |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +private fun logViewerSummary(state: LogViewerUiState): String { |
| 131 | + val file = state.selectedFileName ?: "未选择文件" |
| 132 | + val scope = state.filterServerName?.let { "筛选:$it" } ?: "未筛选" |
| 133 | + val count = if (state.totalLines > state.shownLines) { |
| 134 | + "显示最后 ${state.shownLines}/${state.totalLines} 行" |
| 135 | + } else { |
| 136 | + "显示 ${state.shownLines} 行" |
| 137 | + } |
| 138 | + return "$file · $scope · $count" |
| 139 | +} |
0 commit comments