| | |
| | | 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.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() { |
| | |
| | | private lateinit var pieChart: PieChart |
| | | private lateinit var heatmapView: View |
| | | private var currentUserInfo: UserInfo? = null // 确保使用你的实际数据类 |
| | | |
| | | private var startDateCur:String = "" |
| | | private var endDateCur:String = "" |
| | | |
| | | |
| | | enum class DateType { |
| | |
| | | } |
| | | |
| | | private fun setupTabLayout() { |
| | | val weekStatsView = binding.layoutWeekStats.root |
| | | val bar_title:TextView = weekStatsView.findViewById(R.id.bar_title) |
| | | val pie_title:TextView = weekStatsView.findViewById(R.id.pie_title) |
| | | |
| | | binding.tabDateRange.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener { |
| | | override fun onTabSelected(tab: TabLayout.Tab?) { |
| | | currentDateType = when(tab?.position) { |
| | |
| | | 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() |
| | |
| | | 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月" |
| | |
| | | } |
| | | } |
| | | private fun setupView(view: View) { |
| | | |
| | | val weekStatsView = binding.layoutWeekStats.root |
| | | barChart = weekStatsView.findViewById(R.id.chart_daily_packages) |
| | | 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 |
| | |
| | | setupPieChart() |
| | | setupHeatmap() |
| | | updateCharts() |
| | | |
| | | } |
| | | private fun setupBarChart() { |
| | | barChart.apply { |
| | |
| | | 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) |
| | |
| | | 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 -> { |
| | | // 获取本月统计 |
| | |
| | | } |
| | | } |
| | | } |
| | | packageAdapter.updatePackages(packages) |
| | | packageAdapter.updatePackages(packages) |
| | | |
| | | // packageAdapter.updatePackages(packages) |
| | | // binding.textPackageCount.text = "${packages.size}个" |
| | | } |
| | | } |