feat: 将类别饼图添加到成员详细信息屏幕
- 在DeliverDetailView模型中添加类别数据状态流 - 从成员视图访问时,在DeliverDetailScreen中显示饼图 - 计算并显示会员记录的类别分布
This commit is contained in:
parent
b00e01dffb
commit
80ebddfc13
@ -33,6 +33,7 @@ import androidx.lifecycle.viewmodel.compose.viewModel
|
|||||||
import com.yovinchen.bookkeeping.data.Record
|
import com.yovinchen.bookkeeping.data.Record
|
||||||
import com.yovinchen.bookkeeping.model.AnalysisType
|
import com.yovinchen.bookkeeping.model.AnalysisType
|
||||||
import com.yovinchen.bookkeeping.model.TransactionType
|
import com.yovinchen.bookkeeping.model.TransactionType
|
||||||
|
import com.yovinchen.bookkeeping.ui.components.CategoryPieChart
|
||||||
import com.yovinchen.bookkeeping.ui.components.RecordItem
|
import com.yovinchen.bookkeeping.ui.components.RecordItem
|
||||||
import com.yovinchen.bookkeeping.viewmodel.MemberDetailViewModel
|
import com.yovinchen.bookkeeping.viewmodel.MemberDetailViewModel
|
||||||
import java.text.NumberFormat
|
import java.text.NumberFormat
|
||||||
@ -53,6 +54,7 @@ fun MemberDetailScreen(
|
|||||||
) {
|
) {
|
||||||
val records by viewModel.memberRecords.collectAsState(initial = emptyList())
|
val records by viewModel.memberRecords.collectAsState(initial = emptyList())
|
||||||
val totalAmount by viewModel.totalAmount.collectAsState(initial = 0.0)
|
val totalAmount by viewModel.totalAmount.collectAsState(initial = 0.0)
|
||||||
|
val categoryData by viewModel.categoryData.collectAsState(initial = emptyList())
|
||||||
|
|
||||||
LaunchedEffect(memberName, category, startMonth, endMonth, analysisType) {
|
LaunchedEffect(memberName, category, startMonth, endMonth, analysisType) {
|
||||||
viewModel.loadMemberRecords(
|
viewModel.loadMemberRecords(
|
||||||
@ -99,20 +101,54 @@ fun MemberDetailScreen(
|
|||||||
) {
|
) {
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.padding(16.dp),
|
.fillMaxWidth()
|
||||||
horizontalAlignment = Alignment.CenterHorizontally
|
.padding(16.dp)
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
text = if (records.isNotEmpty() && records.first().type == TransactionType.INCOME) "总收入" else "总支出",
|
text = "总金额",
|
||||||
style = MaterialTheme.typography.titleMedium
|
style = MaterialTheme.typography.titleMedium,
|
||||||
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
Spacer(modifier = Modifier.height(8.dp))
|
Spacer(modifier = Modifier.height(8.dp))
|
||||||
Text(
|
Text(
|
||||||
text = NumberFormat.getCurrencyInstance(Locale.CHINA)
|
text = NumberFormat.getCurrencyInstance(Locale.CHINA)
|
||||||
.format(totalAmount),
|
.format(totalAmount),
|
||||||
style = MaterialTheme.typography.headlineMedium,
|
style = MaterialTheme.typography.headlineMedium,
|
||||||
|
color = MaterialTheme.colorScheme.primary
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 当从成员视图进入时显示饼图
|
||||||
|
if (category.isEmpty()) {
|
||||||
|
item {
|
||||||
|
Card(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(16.dp),
|
||||||
|
elevation = CardDefaults.cardElevation(defaultElevation = 4.dp)
|
||||||
|
) {
|
||||||
|
Column(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.padding(16.dp)
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = "分类统计",
|
||||||
|
style = MaterialTheme.typography.titleMedium,
|
||||||
fontWeight = FontWeight.Bold
|
fontWeight = FontWeight.Bold
|
||||||
)
|
)
|
||||||
|
Spacer(modifier = Modifier.height(16.dp))
|
||||||
|
CategoryPieChart(
|
||||||
|
categoryData = categoryData,
|
||||||
|
memberData = emptyList(),
|
||||||
|
currentViewMode = false,
|
||||||
|
onCategoryClick = { selectedCategory ->
|
||||||
|
// 暂时不处理点击事件
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import com.yovinchen.bookkeeping.model.BookkeepingRecord
|
|||||||
import com.yovinchen.bookkeeping.model.AnalysisType
|
import com.yovinchen.bookkeeping.model.AnalysisType
|
||||||
import com.yovinchen.bookkeeping.model.TransactionType
|
import com.yovinchen.bookkeeping.model.TransactionType
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.flow.*
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import java.time.YearMonth
|
import java.time.YearMonth
|
||||||
import java.time.ZoneId
|
import java.time.ZoneId
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
@ -22,6 +23,9 @@ class MemberDetailViewModel(application: Application) : AndroidViewModel(applica
|
|||||||
private val _totalAmount = MutableStateFlow(0.0)
|
private val _totalAmount = MutableStateFlow(0.0)
|
||||||
val totalAmount: StateFlow<Double> = _totalAmount.asStateFlow()
|
val totalAmount: StateFlow<Double> = _totalAmount.asStateFlow()
|
||||||
|
|
||||||
|
private val _categoryData = MutableStateFlow<List<Pair<String, Float>>>(emptyList())
|
||||||
|
val categoryData: StateFlow<List<Pair<String, Float>>> = _categoryData.asStateFlow()
|
||||||
|
|
||||||
fun loadMemberRecords(
|
fun loadMemberRecords(
|
||||||
memberName: String,
|
memberName: String,
|
||||||
category: String,
|
category: String,
|
||||||
@ -62,11 +66,18 @@ class MemberDetailViewModel(application: Application) : AndroidViewModel(applica
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
recordsFlow
|
viewModelScope.launch {
|
||||||
.onEach { records ->
|
recordsFlow.collect { records ->
|
||||||
_memberRecords.value = records
|
_memberRecords.value = records
|
||||||
_totalAmount.value = records.sumOf { it.amount }
|
_totalAmount.value = records.sumOf { it.amount }
|
||||||
|
|
||||||
|
// 计算分类数据
|
||||||
|
val categoryAmounts = records.groupBy { it.category }
|
||||||
|
.mapValues { (_, records) -> records.sumOf { it.amount }.toFloat() }
|
||||||
|
.toList()
|
||||||
|
.sortedByDescending { it.second }
|
||||||
|
_categoryData.value = categoryAmounts
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.launchIn(viewModelScope)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user