cloudroam
2025-03-13 c6adb1c42a76001de0978e99f73cd6f5678c685e
app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
@@ -23,6 +23,7 @@
import android.graphics.Color
import android.widget.GridLayout
import com.example.firstapp.model.DailyStat
import com.github.mikephil.charting.components.YAxis
class DashboardFragment : Fragment() {
@@ -163,7 +164,8 @@
                setDrawGridLines(false)
                granularity = 1f
                labelRotationAngle = 0f
                textSize = 12f
                textSize = 10f  //标签字体
                setExtraLeftOffset(5f)  // 减少左侧留白
                setExtraBottomOffset(15f)
            }
@@ -189,6 +191,8 @@
            setExtraOffsets(10f, 10f, 10f, 20f)
        }
        updateBarChartData()
    }
    private fun updateBarChartData() {
@@ -367,8 +371,38 @@
            currentDate.timeInMillis,
            currentDateType.name
        ).observe(viewLifecycleOwner) { packages ->
            when (currentDateType) {
                DateType.DAY -> {
                    binding.textPackageCount.text = "${packages.size}个"
                }
                DateType.WEEK -> {
                    // 获取本周统计
                    viewModel.getCurrentWeekStats(currentDate.timeInMillis)
                        .observe(viewLifecycleOwner) { stats ->
                            val weekTotal = stats.sumOf { it.count }
                            binding.textPackageCount.text = "${weekTotal}个"
                        }
                }
                DateType.MONTH -> {
                    // 获取本月统计
                    viewModel.getMonthlyStats(currentDate.timeInMillis)
                        .observe(viewLifecycleOwner) { stats ->
                            val monthTotal = stats.sumOf { it.count }
                            binding.textPackageCount.text = "${monthTotal}个"
                        }
                }
                DateType.YEAR -> {
                    // 获取本年统计
                    viewModel.getCurrentYearStats(currentDate.timeInMillis)
                        .observe(viewLifecycleOwner) { stats ->
                            val yearTotal = stats.sumOf { it.count }
                            binding.textPackageCount.text = "${yearTotal}个"
                        }
                }
            }
            packageAdapter.updatePackages(packages)
            binding.textPackageCount.text = "${packages.size}个"
            packageAdapter.updatePackages(packages)
//            binding.textPackageCount.text = "${packages.size}个"
        }
    }
    private fun setupHeatmap() {
@@ -393,24 +427,44 @@
            // 更新UI
            binding.layoutWeekStats.heatmapYearly.apply {
                // 清除现有的子视图
                removeAllViews()
                
                // 创建网格布局
                val gridLayout = GridLayout(context).apply {
                    rowCount = 7
                    columnCount = 52
                    rowCount = 8 // 增加一行用于显示月份
                    columnCount = 53 // 增加一列用于显示星期标签
                }
                // 添加日期标签
                val dayLabels = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六")
                for (i in 0..6) {
                // 添加月份标签
                val months = arrayOf("1月", "2月", "3月", "4月", "5月", "6月",
                                   "7月", "8月", "9月", "10月", "11月", "12月")
                months.forEachIndexed { index, month ->
                    val label = TextView(context).apply {
                        text = dayLabels[i]
                        text = month
                        textSize = 10f
                        setPadding(0, 0, 8, 0)
                        setPadding(0, 0, 8, 4)
                        // 计算每个月份标签的位置
                        val weekPosition = (index * 4.3).toInt()
                        val params = GridLayout.LayoutParams()
                        params.columnSpec = GridLayout.spec(weekPosition + 1)
                        params.rowSpec = GridLayout.spec(0)
                        layoutParams = params
                    }
                    gridLayout.addView(label)
                }
                // 添加星期标签
                val dayLabels = arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日")
                dayLabels.forEachIndexed { index, label ->
                    val textView = TextView(context).apply {
                        text = label
                        textSize = 10f
                        setPadding(4, 0, 8, 0)
                        val params = GridLayout.LayoutParams()
                        params.columnSpec = GridLayout.spec(0)
                        params.rowSpec = GridLayout.spec(index + 1)
                        layoutParams = params
                    }
                    gridLayout.addView(textView)
                }
                // 添加热力图单元格
@@ -418,12 +472,15 @@
                    for (week in 0..51) {
                        val count = heatmapMatrix[day][week]
                        val cell = View(context).apply {
                            layoutParams = ViewGroup.LayoutParams(
                                resources.getDimensionPixelSize(R.dimen.heatmap_cell_size),
                                resources.getDimensionPixelSize(R.dimen.heatmap_cell_size)
                            )
                            val params = GridLayout.LayoutParams().apply {
                                width = resources.getDimensionPixelSize(R.dimen.heatmap_cell_size)
                                height = resources.getDimensionPixelSize(R.dimen.heatmap_cell_size)
                                columnSpec = GridLayout.spec(week + 1)
                                rowSpec = GridLayout.spec(day + 1)
                                setMargins(1, 1, 1, 1)
                            }
                            layoutParams = params
                            setBackgroundColor(getHeatmapColor(count))
                            setPadding(1, 1, 1, 1)
                        }
                        gridLayout.addView(cell)
                    }
@@ -435,12 +492,11 @@
    }
    private fun getHeatmapColor(count: Int): Int {
        // 根据数量返回不同深浅的颜色
        return when {
            count == 0 -> Color.parseColor("#EBEDF0")
            count <= 2 -> Color.parseColor("#9BE9A8")
            count <= 4 -> Color.parseColor("#40C463")
            count <= 6 -> Color.parseColor("#30A14E")
            count == 1 -> Color.parseColor("#9BE9A8")
            count == 2 -> Color.parseColor("#40C463")
            count <= 4 -> Color.parseColor("#30A14E")
            else -> Color.parseColor("#216E39")
        }
    }
@@ -452,6 +508,7 @@
                binding.recyclerPackages.visibility = View.VISIBLE
                binding.layoutWeekStats.root.visibility = View.GONE
                binding.layoutYearStats.root.visibility = View.GONE
                binding.cardPackageStats.visibility = View.VISIBLE
            }
            DateType.WEEK, DateType.MONTH -> {
                // 周和月视图显示柱状图和饼图,隐藏包裹列表
@@ -459,7 +516,9 @@
                binding.layoutWeekStats.root.visibility = View.VISIBLE
                binding.layoutYearStats.root.visibility = View.GONE
                binding.layoutWeekStats.chartDailyPackages.visibility = View.VISIBLE
                (binding.layoutWeekStats.chartDailyPackages.parent as? View)?.visibility = View.VISIBLE
                binding.layoutWeekStats.heatmapYearly.visibility = View.GONE
                binding.cardPackageStats.visibility = View.VISIBLE
                updateBarChartData()
                updatePieChartData()
            }
@@ -468,10 +527,12 @@
                binding.recyclerPackages.visibility = View.GONE
                binding.layoutWeekStats.root.visibility = View.VISIBLE
                binding.layoutYearStats.root.visibility = View.VISIBLE
                binding.layoutWeekStats.chartDailyPackages.visibility = View.GONE
                (binding.layoutWeekStats.chartDailyPackages.parent as? View)?.visibility = View.GONE
                binding.layoutWeekStats.heatmapYearly.visibility = View.VISIBLE
                binding.cardPackageStats.visibility = View.GONE
                updateHeatmapData()
                updatePieChartData()
                updateYearlyStats()
            }
        }
    }