From b8aa11a7b447fb8048115bdd156c2aa9e3b16a07 Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期二, 15 四月 2025 16:40:08 +0800
Subject: [PATCH] 1.3

---
 app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt |  189 +++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 158 insertions(+), 31 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
index 9a7060f..e552794 100644
--- a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
+++ b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt
@@ -21,17 +21,14 @@
 import java.util.*
 import java.text.SimpleDateFormat
 import android.graphics.Color
-import android.widget.Button
 import android.widget.GridLayout
 import android.widget.Toast
-import androidx.core.content.ContextCompat
+import androidx.cardview.widget.CardView
 import androidx.lifecycle.lifecycleScope
-import com.bumptech.glide.Glide
 import com.example.firstapp.database.response.UserInfo
 import com.example.firstapp.database.service.RetrofitClient
 import com.example.firstapp.model.DailyStat
 import com.example.firstapp.utils.PreferencesManager
-import com.github.mikephil.charting.components.YAxis
 import kotlinx.coroutines.launch
 
 class DashboardFragment : Fragment() {
@@ -43,8 +40,14 @@
     private var currentDateType = DateType.DAY
     private lateinit var barChart: BarChart
     private lateinit var pieChart: PieChart
+    private lateinit var chartCourierDistriBution:CardView
     private lateinit var heatmapView: View
     private var currentUserInfo: UserInfo? = null // 确保使用你的实际数据类
+
+    private var startDateCur:String = ""
+    private var endDateCur:String = ""
+    private lateinit var bar_title:TextView
+    private lateinit var pie_title:TextView
 
 
     enum class DateType {
@@ -84,7 +87,7 @@
         loadUserInfo()
 
         // 遮罩层点击时间
-        binding.viewOverlay.setOnClickListener {
+        binding.overlayContent.setOnClickListener {
             // 跳转到vipActivity
             val intent = android.content.Intent(requireContext(), com.example.firstapp.activity.VipActivity::class.java)
             startActivity(intent)
@@ -94,11 +97,14 @@
     private fun showOverlay() {
         binding.viewOverlay.visibility = View.VISIBLE
         binding.overlayContent.visibility = View.VISIBLE
+
     }
 
     private fun hiddleOverlay() {
         binding.viewOverlay.visibility = View.GONE
         binding.overlayContent.visibility = View.GONE
+
+
     }
 
 
@@ -110,6 +116,10 @@
     }
 
     private fun setupTabLayout() {
+        val weekStatsView = binding.layoutWeekStats.root
+        bar_title = weekStatsView.findViewById(R.id.bar_title)
+        pie_title = weekStatsView.findViewById(R.id.pie_title)
+
         binding.tabDateRange.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
             override fun onTabSelected(tab: TabLayout.Tab?) {
                 currentDateType = when(tab?.position) {
@@ -119,6 +129,40 @@
                     3 -> DateType.YEAR
                     else -> DateType.DAY
                 }
+
+                when (currentDateType) {
+                    DateType.DAY -> {
+                        binding.cardPackageStatsTitleText.text = "本日收到包裹总数"
+                        binding.layoutYearStatsTitleText.text = "包裹取件码记录"
+
+                        binding.cardPackageStatsTitleText.visibility = View.VISIBLE
+                        binding.layoutYearStatsTitleText.visibility =  View.VISIBLE
+                    }
+                    DateType.WEEK -> {
+                        binding.cardPackageStatsTitleText.text = "本周收到包裹总数"
+                        bar_title.text = "本周收到包裹数分布 ->"
+                        pie_title.text = "本周包裹物流公司分布 ->"
+                        binding.cardPackageStatsTitleText.visibility = View.VISIBLE
+                        binding.layoutYearStatsTitleText.visibility =  View.GONE
+
+                    }
+                    DateType.MONTH -> {
+                        binding.cardPackageStatsTitleText.text = "本月收到包裹总数"
+                        bar_title.text = "本月收到包裹数分布 ->"
+                        pie_title.text = "本月包裹物流公司分布 ->"
+                        binding.cardPackageStatsTitleText.visibility = View.VISIBLE
+                        binding.layoutYearStatsTitleText.visibility =  View.GONE
+                    }
+                    DateType.YEAR -> {
+                        bar_title.text = "本年收到包裹数分布 ->"
+                        pie_title.text = "本年包裹物流公司分布 ->"
+                        binding.cardPackageStatsTitleText.visibility = View.GONE
+                        binding.layoutYearStatsTitleText.visibility =  View.GONE
+                    }
+                }
+
+
+
                 updateDateDisplay()
                 updateCharts()
                 loadPackages()
@@ -157,10 +201,13 @@
                 val calendar = currentDate.clone() as Calendar
                 calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY)
                 val startDate = SimpleDateFormat("MM月dd日", Locale.getDefault()).format(calendar.time)
-                
+                startDateCur=SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
+
                 calendar.add(Calendar.DAY_OF_WEEK, 6)
                 val endDate = SimpleDateFormat("MM月dd日", Locale.getDefault()).format(calendar.time)
-                
+                endDateCur=SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(calendar.time)
+
+
                 "$startDate-$endDate"
             }
             DateType.MONTH -> "yyyy年MM月"
@@ -175,10 +222,16 @@
         }
     }
     private fun setupView(view: View) {
+
         val weekStatsView = binding.layoutWeekStats.root
         barChart = weekStatsView.findViewById(R.id.chart_daily_packages)
+        chartCourierDistriBution=weekStatsView.findViewById(R.id.chart_courier_card_view)
         pieChart = weekStatsView.findViewById(R.id.chart_courier_distribution)
         heatmapView = weekStatsView.findViewById(R.id.heatmap_yearly)
+
+        barChart.setViewPortOffsets(100f, 100f, 100f, 200f)
+
+//        barChart.invalidate()
         
         // 初始化时隐藏统计视图
         weekStatsView.visibility = View.GONE
@@ -187,6 +240,7 @@
         setupPieChart()
         setupHeatmap()
         updateCharts()
+
     }
     private fun setupBarChart() {
         barChart.apply {
@@ -237,7 +291,8 @@
     private fun updateBarChartData() {
         val statsFlow = when (currentDateType) {
             DateType.WEEK -> {
-                viewModel.getWeeklyStats(currentDate.timeInMillis, 6)
+//                viewModel.getWeeklyStats(currentDate.timeInMillis, 6)
+                viewModel.getWeeklyStatsChart(startDateCur,endDateCur)
             }
             DateType.MONTH -> {
                 viewModel.getYearMonthlyStats(currentDate.timeInMillis)
@@ -381,24 +436,84 @@
                 PieEntry(stat.count.toFloat(), "${stat.courierName}(${stat.count})")
             }
 
-            val dataSet = PieDataSet(entries, "快递公司分布")
-            dataSet.colors = listOf(
-                resources.getColor(R.color.purple_500),
-                resources.getColor(R.color.teal_200),
-                resources.getColor(R.color.purple_200),
-                resources.getColor(R.color.teal_700)
-            )
-            dataSet.valueTextSize = 14f // 增大数值文字大小
+            if (entries.isNotEmpty()) {
+                val dataSet = PieDataSet(entries, "快递公司分布")
+                dataSet.colors = listOf(
+                    resources.getColor(R.color.light_blue_600_1),
+                    resources.getColor(R.color.sunflower),
+                    resources.getColor(R.color.light_blue),
+                    resources.getColor(R.color.vermillion),
+                    resources.getColor(R.color.fish_belly_white),
+                    resources.getColor(R.color.light_green),
+                    resources.getColor(R.color.crimson),
+                    resources.getColor(R.color.sky_blue),
+                    resources.getColor(R.color.gold),
+                    resources.getColor(R.color.light_purple),
+                    resources.getColor(R.color.yellow),
+                    resources.getColor(R.color.canary_yellow),
+                    resources.getColor(R.color.red_purple),
+                    resources.getColor(R.color.light_cyan),
+                    resources.getColor(R.color.orange),
+                    resources.getColor(R.color.magenta),
+                    resources.getColor(R.color.light_purple_2),
+                    resources.getColor(R.color.bright_yellow),
+                    resources.getColor(R.color.emerald_green),
+                    resources.getColor(R.color.turmeric),
+                    resources.getColor(R.color.red_gold),
+                    resources.getColor(R.color.off_white),
+                    resources.getColor(R.color.tangerine),
+                    resources.getColor(R.color.aqua_blue),
+                    resources.getColor(R.color.frost),
+                    resources.getColor(R.color.wisteria),
+                    resources.getColor(R.color.cyan)
+                )
+                dataSet.valueTextSize = 14f // 增大数值文字大小
 
-            val pieData = PieData(dataSet)
-            pieData.setValueFormatter(object : ValueFormatter() {
-                override fun getFormattedValue(value: Float): String {
-                    return value.toInt().toString()
-                }
-            })
+                val pieData = PieData(dataSet)
+                pieData.setValueFormatter(object : ValueFormatter() {
+                    override fun getFormattedValue(value: Float): String {
+                        return value.toInt().toString()
+                    }
+                })
 
-            pieChart.data = pieData
-            pieChart.invalidate()
+                pieChart.data = pieData
+                pieChart.invalidate()
+
+                pieChart.visibility = View.VISIBLE  // 例如:隐藏 PieChart
+
+                pie_title.visibility = View.VISIBLE
+
+                chartCourierDistriBution.visibility =View.VISIBLE
+
+            } else {
+                // 如果 entries 为空,可以选择隐藏图表或设置一个默认显示
+                pieChart.visibility = View.GONE  // 例如:隐藏 PieChart
+
+                pie_title.visibility = View.GONE
+
+                chartCourierDistriBution.visibility =View.GONE
+
+            }
+
+
+//            val dataSet = PieDataSet(entries, "快递公司分布")
+//            dataSet.colors = listOf(
+//                resources.getColor(R.color.purple_500),
+//                resources.getColor(R.color.teal_200),
+//                resources.getColor(R.color.purple_200),
+//                resources.getColor(R.color.teal_700)
+//            )
+//            dataSet.valueTextSize = 14f // 增大数值文字大小
+//
+//            val pieData = PieData(dataSet)
+//            pieData.setValueFormatter(object : ValueFormatter() {
+//                override fun getFormattedValue(value: Float): String {
+//                    return value.toInt().toString()
+//                }
+//            })
+//
+//            pieChart.data = pieData
+//            pieChart.invalidate()
         }
     }
     private fun getDayLabels(): Array<String> {
@@ -413,14 +528,26 @@
             when (currentDateType) {
                 DateType.DAY -> {
                     binding.textPackageCount.text = "${packages.size}个"
+
+                    // 获取本周统计
+                    viewModel.getPackagesUnread(currentDate.timeInMillis,
+                        currentDateType.name)
+                        .observe(viewLifecycleOwner) { unpackages->
+                            // 只读取未取件的包裹
+                            packageAdapter.updatePackages(unpackages)
+                        }
+
                 }
                 DateType.WEEK -> {
                     // 获取本周统计
-                    viewModel.getCurrentWeekStats(currentDate.timeInMillis)
-                        .observe(viewLifecycleOwner) { stats ->
-                            val weekTotal = stats.sumOf { it.count }
-                            binding.textPackageCount.text = "${weekTotal}个"
-                        }
+                    viewModel.getCurrentWeekStats2(startDateCur,endDateCur).observe(viewLifecycleOwner) { stats ->
+                        binding.textPackageCount.text = "${stats}个"
+                    }
+//                    viewModel.getCurrentWeekStats(currentDate.timeInMillis)
+//                        .observe(viewLifecycleOwner) { stats ->
+//                            val weekTotal = stats.sumOf { it.count }
+//                            binding.textPackageCount.text = "${weekTotal}个"
+//                        }
                 }
                 DateType.MONTH -> {
                     // 获取本月统计
@@ -439,8 +566,8 @@
                         }
                 }
             }
-            packageAdapter.updatePackages(packages)
-            packageAdapter.updatePackages(packages)
+
+//            packageAdapter.updatePackages(packages)
 //            binding.textPackageCount.text = "${packages.size}个"
         }
     }

--
Gitblit v1.9.3