fix: 修复月份选择器参数错误

- 将 MonthYearPicker 的 initialMonth 参数改为 selectedMonth
- 保持与组件定义一致
This commit is contained in:
yovinchen 2024-11-28 10:51:01 +08:00
parent 71deaaa288
commit 3c080fbc05

View File

@ -35,109 +35,115 @@ fun AnalysisScreen(
val categoryStats by viewModel.categoryStats.collectAsState() val categoryStats by viewModel.categoryStats.collectAsState()
var showMonthPicker by remember { mutableStateOf(false) } var showMonthPicker by remember { mutableStateOf(false) }
Column( LazyColumn(
modifier = modifier modifier = modifier
.fillMaxSize() .fillMaxSize()
.padding(16.dp) .padding(16.dp)
) { ) {
// 月份选择器 // 月份选择器
Row( item {
modifier = Modifier.fillMaxWidth(), Row(
horizontalArrangement = Arrangement.SpaceBetween, modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically horizontalArrangement = Arrangement.SpaceBetween,
) { verticalAlignment = Alignment.CenterVertically
IconButton(onClick = { ) {
viewModel.setSelectedMonth(selectedMonth.minusMonths(1)) IconButton(onClick = {
}) { viewModel.setSelectedMonth(selectedMonth.minusMonths(1))
Icon(Icons.AutoMirrored.Filled.KeyboardArrowLeft, "上个月") }) {
} Icon(Icons.AutoMirrored.Filled.KeyboardArrowLeft, "上个月")
}
Text(
text = "${selectedMonth.year}${selectedMonth.monthValue}",
style = MaterialTheme.typography.titleLarge,
modifier = Modifier.clickable { showMonthPicker = true }
)
IconButton(onClick = {
viewModel.setSelectedMonth(selectedMonth.plusMonths(1))
}) {
Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, "下个月")
}
}
Spacer(modifier = Modifier.height(16.dp))
// 分析类型选择
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceEvenly
) {
AnalysisType.values().forEach { type ->
FilterChip(
selected = selectedType == type,
onClick = { viewModel.setAnalysisType(type) },
label = {
Text(
when (type) {
AnalysisType.EXPENSE -> "支出分析"
AnalysisType.INCOME -> "收入分析"
AnalysisType.TREND -> "收支趋势"
}
)
}
)
}
}
Spacer(modifier = Modifier.height(16.dp))
// 统计内容
when (selectedType) {
AnalysisType.EXPENSE, AnalysisType.INCOME -> {
Text( Text(
text = if (selectedType == AnalysisType.EXPENSE) "支出分析" else "收入分析", text = "${selectedMonth.year}${selectedMonth.monthValue}",
style = MaterialTheme.typography.titleMedium, style = MaterialTheme.typography.titleLarge,
modifier = Modifier.padding(vertical = 8.dp) modifier = Modifier.clickable { showMonthPicker = true }
) )
if (categoryStats.isEmpty()) { IconButton(onClick = {
Box( viewModel.setSelectedMonth(selectedMonth.plusMonths(1))
modifier = Modifier }) {
.fillMaxWidth() Icon(Icons.AutoMirrored.Filled.KeyboardArrowRight, "下个月")
.height(300.dp), }
contentAlignment = Alignment.Center }
) {
Text( Spacer(modifier = Modifier.height(16.dp))
text = "暂无数据",
style = MaterialTheme.typography.bodyLarge, // 分析类型选择
color = MaterialTheme.colorScheme.onSurfaceVariant Row(
) modifier = Modifier.fillMaxWidth(),
} horizontalArrangement = Arrangement.SpaceEvenly
} else { ) {
// 添加饼图 AnalysisType.values().forEach { type ->
CategoryPieChart( FilterChip(
categoryData = categoryStats.map { selected = selectedType == type,
it.category to it.amount.toFloat() onClick = { viewModel.setAnalysisType(type) },
label = {
Text(
when (type) {
AnalysisType.EXPENSE -> "支出分析"
AnalysisType.INCOME -> "收入分析"
AnalysisType.TREND -> "收支趋势"
}
)
} }
) )
} }
}
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
// 分类列表 // 统计内容
LazyColumn { when (selectedType) {
items(categoryStats) { stat -> AnalysisType.EXPENSE, AnalysisType.INCOME -> {
CategoryStatItem(stat) Text(
text = if (selectedType == AnalysisType.EXPENSE) "支出分析" else "收入分析",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(vertical = 8.dp)
)
if (categoryStats.isNotEmpty()) {
val pieChartData = categoryStats.map { stat ->
stat.category to stat.percentage.toFloat()
}
CategoryPieChart(
categoryData = pieChartData,
modifier = Modifier
.fillMaxWidth()
.height(300.dp)
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "分类明细",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(vertical = 8.dp)
)
} else {
Text(
text = "暂无数据",
style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Center,
modifier = Modifier
.fillMaxWidth()
.padding(32.dp)
)
} }
} }
AnalysisType.TREND -> {
Text(
text = "收支趋势分析(开发中)",
style = MaterialTheme.typography.titleMedium,
modifier = Modifier.padding(vertical = 8.dp)
)
}
} }
AnalysisType.TREND -> { }
// TODO: 实现收支趋势图表
Text( // 分类统计列表
text = "收支趋势", if (selectedType != AnalysisType.TREND && categoryStats.isNotEmpty()) {
style = MaterialTheme.typography.titleMedium, items(categoryStats) { stat ->
modifier = Modifier.padding(vertical = 8.dp) CategoryStatItem(stat)
)
} }
} }
} }
@ -145,8 +151,8 @@ fun AnalysisScreen(
if (showMonthPicker) { if (showMonthPicker) {
MonthYearPicker( MonthYearPicker(
selectedMonth = selectedMonth, selectedMonth = selectedMonth,
onMonthSelected = { yearMonth -> onMonthSelected = { month ->
viewModel.setSelectedMonth(yearMonth) viewModel.setSelectedMonth(month)
showMonthPicker = false showMonthPicker = false
}, },
onDismiss = { showMonthPicker = false } onDismiss = { showMonthPicker = false }
@ -156,59 +162,51 @@ fun AnalysisScreen(
@Composable @Composable
fun CategoryStatItem(stat: CategoryStat) { fun CategoryStatItem(stat: CategoryStat) {
Card( Column(
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(vertical = 4.dp), .padding(vertical = 8.dp)
elevation = CardDefaults.cardElevation(defaultElevation = 2.dp)
) { ) {
Column( Row(
modifier = Modifier modifier = Modifier.fillMaxWidth(),
.fillMaxWidth() horizontalArrangement = Arrangement.SpaceBetween,
.padding(16.dp) verticalAlignment = Alignment.CenterVertically
) { ) {
Row( Text(
modifier = Modifier.fillMaxWidth(), text = stat.category,
horizontalArrangement = Arrangement.SpaceBetween, style = MaterialTheme.typography.bodyLarge
verticalAlignment = Alignment.CenterVertically )
) { Text(
Text( text = String.format("%.2f", stat.amount),
text = stat.category, style = MaterialTheme.typography.bodyLarge
style = MaterialTheme.typography.titleMedium )
) }
Text(
text = String.format("%.1f%%", stat.percentage), Spacer(modifier = Modifier.height(4.dp))
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.primary Row(
) modifier = Modifier.fillMaxWidth(),
} horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
Spacer(modifier = Modifier.height(8.dp)) ) {
LinearProgressIndicator(
// 进度条 progress = stat.percentage.toFloat() / 100f,
LinearProgressIndicator( modifier = Modifier
progress = { (stat.percentage / 100).toFloat() }, .weight(1f)
modifier = Modifier .height(8.dp)
.fillMaxWidth() .background(
.height(8.dp), MaterialTheme.colorScheme.surfaceVariant,
RoundedCornerShape(4.dp)
)
)
Spacer(modifier = Modifier.width(8.dp))
Text(
text = String.format("%.1f%%", stat.percentage),
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
) )
Spacer(modifier = Modifier.height(8.dp))
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.SpaceBetween
) {
Text(
text = "¥${String.format("%.2f", stat.amount)}",
style = MaterialTheme.typography.bodyMedium
)
Text(
text = "${stat.count}",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.outline
)
}
} }
} }
} }