From 93775e1f3d79a1e11b839abbf24201dafcd9e646 Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期五, 25 四月 2025 13:45:56 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt |  235 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 201 insertions(+), 34 deletions(-)

diff --git a/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt b/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
index a6782f5..9cfcf78 100644
--- a/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
+++ b/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
@@ -15,21 +15,19 @@
 import androidx.lifecycle.ViewModelProvider
 import androidx.lifecycle.lifecycleScope
 import androidx.recyclerview.widget.LinearLayoutManager
-import com.bumptech.glide.Glide
 import com.example.firstapp.R
-import com.example.firstapp.activity.ContentDetailActivity
 import com.example.firstapp.activity.PickupActivity
 import com.example.firstapp.activity.VipActivity
 import com.example.firstapp.adapter.ExpressAdapter
 import com.example.firstapp.adapter.FinanceAdapter
 import com.example.firstapp.adapter.CategorySelectorAdapter
 import com.example.firstapp.adapter.IncomeAdapter
+import com.example.firstapp.adapter.TrainAdapter
+import com.example.firstapp.adapter.FlightAdapter
 import com.example.firstapp.database.service.RetrofitClient
 import com.example.firstapp.databinding.FragmentHomeBinding
 import com.example.firstapp.databinding.DialogCategorySelectorBinding
 import com.example.firstapp.model.CategoryConfig
-import com.example.firstapp.model.IncomeGroup
-import com.example.firstapp.model.IncomePackage
 import com.example.firstapp.utils.PreferencesManager
 import com.google.android.material.bottomsheet.BottomSheetDialog
 import kotlinx.coroutines.launch
@@ -46,9 +44,11 @@
     private lateinit var expressAdapter: ExpressAdapter
     private lateinit var financeAdapter: FinanceAdapter
     private lateinit var incomeAdapter: IncomeAdapter
-    private lateinit var flightAdapter: FinanceAdapter
-    private lateinit var trainAdapter: FinanceAdapter
+    private lateinit var flightAdapter: FlightAdapter
+    private lateinit var trainAdapter: TrainAdapter
     private lateinit var dataUpdateReceiver: BroadcastReceiver
+    private lateinit var reminderUpdateReceiver: BroadcastReceiver
+    private var reminderBadge: TextView? = null
 
     //onCreateView这个方法创建后被调用,通常是初始化视图组件和观察者
     override fun onCreateView(
@@ -94,6 +94,7 @@
         setupTabSwitching()
         setupObservers()
         setupCategorySelector()
+        setupUnreadBadge()
     }
 
     override fun onCreate(savedInstanceState: Bundle?) {
@@ -108,7 +109,18 @@
                 }
             }
         }
+
+        // 创建提醒更新广播接收器
+        reminderUpdateReceiver = object : BroadcastReceiver() {
+            override fun onReceive(context: Context, intent: Intent) {
+                if (intent.action == "com.example.firstapp.REMINDER_UPDATED") {
+                    // 收到提醒更新广播时重新检查未读提醒数量
+                    homeViewModel.checkUnreadReminders()
+                }
+            }
+        }
     }
+
 
     private fun setupAdapters() {
         binding.expressRecycler.apply {
@@ -118,11 +130,24 @@
 
             // 设置点击监听
             expressAdapter.setOnPackageClickListener { group, pack ->
+                // 根据驿站名称设置相应的图标
+                val stationName = group.stationName.lowercase()
+                val stationIconResId = when {
+                    stationName.isEmpty() || stationName == "未知" -> R.drawable.express_normal
+                    stationName.contains("菜鸟") -> R.drawable.express_cainiao
+                    stationName.contains("多多") -> R.drawable.express_duoduo
+                    stationName.contains("兔喜") -> R.drawable.express_tuxi
+                    stationName.contains("妈妈") -> R.drawable.express_mama
+                    stationName.contains("小兵") -> R.drawable.express_xiaobing
+                    stationName.contains("蓝店") -> R.drawable.express_landian
+                    else -> R.drawable.express_normal
+                }
                 // 跳转到取件页面
                 val intent = Intent(requireContext(), PickupActivity::class.java).apply {
                     putExtra("station_name", group.stationName)
                     putExtra("company", pack.company)
                     putExtra("page_type", PickupActivity.TYPE_EXPRESS)
+                    putExtra("station_icon_res_id", stationIconResId) // 添加图标资源ID
                 }
                 startActivity(intent)
             }
@@ -139,11 +164,28 @@
 
             // 设置点击监听
             financeAdapter.setOnPackageClickListener { group, pack ->
+                val stationName = group.stationName.lowercase()
+                val stationIconResId = when {
+                    stationName.isEmpty() || stationName == "未知" -> R.drawable.finance_huankuan_normal
+                    stationName.contains("安逸花") -> R.drawable.finance_anyihua
+                    stationName.contains("房贷") -> R.drawable.finance_fangdai
+                    stationName.contains("还款") -> R.drawable.finance_huankuan
+                    stationName.contains("花呗") -> R.drawable.finance_huabei
+                    stationName.contains("借呗") -> R.drawable.finance_jiebei
+                    stationName.contains("京东白条") -> R.drawable.finance_jingdongbaitiao
+                    stationName.contains("京东金条") -> R.drawable.finance_jingdongjintiao
+                    stationName.contains("汽车分期") -> R.drawable.finance_qichefenqi
+                    stationName.contains("省呗") -> R.drawable.finance_shenbei
+                    stationName.contains("信用卡") -> R.drawable.finance_xingyongka
+                    stationName.contains("支付宝") -> R.drawable.finance_zhifubao
+                    else -> R.drawable.finance_huankuan_normal
+                }
                 // 跳转到取件页面
                 val intent = Intent(requireContext(), PickupActivity::class.java).apply {
                     putExtra("station_name", group.stationName)
                     putExtra("company", pack.company)
                     putExtra("page_type", PickupActivity.TYPE_REPAYMENT)
+                    putExtra("station_icon_res_id", stationIconResId) // 添加图标资源ID
 
                 }
                 startActivity(intent)
@@ -161,11 +203,38 @@
 
             // 设置点击监听
             incomeAdapter.setOnPackageClickListener { group, pack ->
+
+                val stationName = group.stationName.lowercase()
+                val stationIconResId = when {
+                    stationName.isEmpty() || stationName == "未知" -> R.drawable.finance_income_bank_normal
+                    stationName.contains("工商银行") -> R.drawable.bank_gongshang
+                    stationName.contains("光大银行") -> R.drawable.bank_guangda
+                    stationName.contains("国家开发银行") -> R.drawable.bank_guojiakaifa
+                    stationName.contains("恒丰银行") -> R.drawable.bank_hengfeng
+                    stationName.contains("华夏银行") -> R.drawable.bank_huaxia
+                    stationName.contains("花呗分期") -> R.drawable.bank_huabeifenqi
+                    stationName.contains("建设银行") -> R.drawable.bank_jianshe
+                    stationName.contains("交通银行") -> R.drawable.bank_jiaotong
+                    stationName.contains("民生银行") -> R.drawable.bank_mingsheng
+                    stationName.contains("南京银行") -> R.drawable.bank_nanjing
+                    stationName.contains("农商银行") -> R.drawable.bank_nongshang
+                    stationName.contains("农业银行") -> R.drawable.bank_nongye
+                    stationName.contains("平安银行") -> R.drawable.bank_pingan
+                    stationName.contains("浦发银行") -> R.drawable.bank_pufa
+                    stationName.contains("苏州银行") -> R.drawable.bank_suzhou
+                    stationName.contains("邮储银行") -> R.drawable.bank_youzheng
+                    stationName.contains("招商银行") -> R.drawable.bank_zhaoshang
+                    stationName.contains("中国银行") -> R.drawable.bank_zhongguo
+                    stationName.contains("中信银行") -> R.drawable.bank_zhongxin
+                    stationName.contains("中原银行") -> R.drawable.bank_zhongyuan
+                    else -> R.drawable.finance_income_bank_normal
+                }
                 // 跳转到取件页面
                 val intent = Intent(requireContext(), PickupActivity::class.java).apply {
                     putExtra("station_name", group.stationName)
                     putExtra("company", pack.company)
                     putExtra("page_type", PickupActivity.TYPE_INCOME)
+                    putExtra("station_icon_res_id", stationIconResId) // 添加图标资源ID
                 }
                 startActivity(intent)
             }
@@ -173,16 +242,38 @@
 
         binding.flightRecycler.apply {
             layoutManager = LinearLayoutManager(context)
-            flightAdapter = FinanceAdapter()
+            flightAdapter = FlightAdapter()
             adapter = flightAdapter
             visibility = View.GONE
+
+            // 设置点击监听
+            flightAdapter.setOnPackageClickListener { group, pack ->
+                // 跳转到航班处理页面
+                val intent = Intent(requireContext(), PickupActivity::class.java).apply {
+                    putExtra("station_name", group.stationName)
+                    putExtra("company", pack.company)
+                    putExtra("page_type", PickupActivity.TYPE_FLIGHT)
+                }
+                startActivity(intent)
+            }
         }
 
         binding.trainRecycler.apply {
             layoutManager = LinearLayoutManager(context)
-            trainAdapter = FinanceAdapter()
+            trainAdapter = TrainAdapter()
             adapter = trainAdapter
             visibility = View.GONE
+
+            // 设置点击监听
+            trainAdapter.setOnPackageClickListener { group, pack ->
+                // 跳转到火车票页面
+                val intent = Intent(requireContext(), PickupActivity::class.java).apply {
+                    putExtra("station_name", group.stationName)
+                    putExtra("company", pack.company)
+                    putExtra("page_type", PickupActivity.TYPE_TRAIN)
+                }
+                startActivity(intent)
+            }
         }
     }
 
@@ -207,12 +298,12 @@
                 updateTabStyles(tabFinance)
                 homeViewModel.loadFinanceData()
             }
-            
+
             // 其他标签点击事件需要检查会员状态
             val memberOnlyTabs = mapOf(
                 tabIncome to { homeViewModel.loadIncomeData() },
-                tabFlight to { homeViewModel.loadFlightData() },
-                tabTrain to { homeViewModel.loadTrainData() }
+//                tabFlight to { homeViewModel.loadFlightData() },
+//                tabTrain to { homeViewModel.loadTrainData() }
             )
 
             memberOnlyTabs.forEach { (tab, loadAction) ->
@@ -221,8 +312,8 @@
                         hideAllRecyclers()
                         when (tab) {
                             tabIncome -> incomeRecycler.visibility = View.VISIBLE
-                            tabFlight -> flightRecycler.visibility = View.VISIBLE
-                            tabTrain -> trainRecycler.visibility = View.VISIBLE
+//                            tabFlight -> flightRecycler.visibility = View.VISIBLE
+//                            tabTrain -> trainRecycler.visibility = View.VISIBLE
                         }
                         updateTabStyles(tab)
                         loadAction()
@@ -276,10 +367,11 @@
 
     private fun updateTabStyles(selectedTab: UnderlineTextView) {
         binding.apply {
-            val tabs = listOf(tabExpress, tabFinance, tabIncome, tabFlight, tabTrain)
+//            val tabs = listOf(tabExpress, tabFinance, tabIncome, tabFlight, tabTrain)
+            val tabs = listOf(tabExpress, tabFinance, tabIncome)
             tabs.forEach { tab ->
                 // 设置文字颜色为黑色或灰色
-                tab.setTextColor(ContextCompat.getColor(requireContext(), 
+                tab.setTextColor(ContextCompat.getColor(requireContext(),
                     if (tab == selectedTab) android.R.color.black else R.color.gray))
                 // 设置文字大小
                 tab.textSize = if (tab == selectedTab) 16f else 14f
@@ -313,6 +405,11 @@
             trainAdapter.submitList(items)
         }
 
+        // 观察未读提醒数量变化
+        homeViewModel.unreadReminderCount.observe(viewLifecycleOwner) { unreadCount ->
+            updateReminderBadge(unreadCount)
+        }
+
         // 观察可见分类的变化
         homeViewModel.visibleCategories.observe(viewLifecycleOwner) { categories: List<CategoryConfig> ->
             binding.apply {
@@ -320,8 +417,8 @@
                 tabExpress.visibility = View.GONE
                 tabFinance.visibility = View.GONE
                 tabIncome.visibility = View.GONE
-                tabFlight.visibility = View.GONE
-                tabTrain.visibility = View.GONE
+//                tabFlight.visibility = View.GONE
+//                tabTrain.visibility = View.GONE
 
                 // 获取用户信息判断是否是会员
                 val savedPhone = PreferencesManager.getPhone()
@@ -355,14 +452,14 @@
                                         tabIncome.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                         if (categories.indexOf(category) == 0 && category.isEnabled) tabIncome.performClick()
                                     }
-                                    "航班" -> {
-                                        tabFlight.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
-                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabFlight.performClick()
-                                    }
-                                    "火车票" -> {
-                                        tabTrain.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
-                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabTrain.performClick()
-                                    }
+//                                    "航班" -> {
+//                                        tabFlight.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
+//                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabFlight.performClick()
+//                                    }
+//                                    "火车票" -> {
+//                                        tabTrain.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
+//                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabTrain.performClick()
+//                                    }
                                 }
                             }
                         }
@@ -387,8 +484,26 @@
             IntentFilter("com.example.firstapp.DATA_UPDATED"),
             ContextCompat.RECEIVER_NOT_EXPORTED
         )
+
+        // 注册提醒更新广播接收器
+        ContextCompat.registerReceiver(
+            requireContext(),
+            reminderUpdateReceiver,
+            IntentFilter("com.example.firstapp.REMINDER_UPDATED"),
+            ContextCompat.RECEIVER_NOT_EXPORTED
+        )
+
         // 加载数据
-        homeViewModel.loadExpressData()
+        homeViewModel.loadAllCategoryData()
+//        homeViewModel.loadExpressData()
+//        homeViewModel.loadFinanceData()
+//        homeViewModel.loadIncomeData()
+//        homeViewModel.loadFlightData()
+//        homeViewModel.loadTrainData()
+
+        // 检查未读提醒数量
+        homeViewModel.checkUnreadReminders()
+
     }
 
     override fun onPause() {
@@ -396,6 +511,7 @@
         try {
             // 取消注册广播接收器
             requireContext().unregisterReceiver(dataUpdateReceiver)
+            requireContext().unregisterReceiver(reminderUpdateReceiver)
         } catch (e: Exception) {
             // 处理可能的异常
             e.printStackTrace()
@@ -451,9 +567,12 @@
         }
 
         // 添加提醒按钮点击事件
-//        binding.reminderButton.setOnClickListener {
-//            startActivity(Intent(requireContext(), ReminderListActivity::class.java))
-//        }
+        binding.reminderButton.setOnClickListener {
+            // 跳转到提醒列表页面
+            startActivity(Intent(requireContext(), ReminderListActivity::class.java))
+            // 重新检查未读提醒数量
+            homeViewModel.checkUnreadReminders()
+        }
     }
 
     private fun showCategorySelectorDialog() {
@@ -478,18 +597,48 @@
 
                     if (isMember) {
                         // 会员可以看到所有分类
-                        adapter.setCategories(categories)
+                        val serverCategories = RetrofitClient.apiService.getUserCategories(response.data?.id.toString())
+                        if(serverCategories.size==2 && serverCategories[0].name=="快递"&&serverCategories[1].name=="还款"){
+                            // 将 serverCategories 转换为可变列表
+                            val mutableServerCategories = serverCategories.toMutableList()
+
+                            // 添加多个新分类
+                            mutableServerCategories.add(CategoryConfig(
+                                id = 5,
+                                name = "火车票",
+                                order = 4,
+                                isEnabled = false
+                            ))
+                            mutableServerCategories.add(CategoryConfig(
+                                id = 4,
+                                name = "航班",
+                                order = 3,
+                                isEnabled = false
+                            ))
+                            mutableServerCategories.add(CategoryConfig(
+                                id = 3,
+                                name = "收入",
+                                order = 2,
+                                isEnabled = false
+                            ))
+
+                            // 设置适配器的分类列表
+                            adapter.setCategories(mutableServerCategories)
+
+                        }else {
+                            adapter.setCategories(serverCategories)
+                        }
                     } else {
                         // 非会员只能看到快递和还款
-                        val limitedCategories = categories.filter { 
-                            it.name == "快递" || it.name == "还款" 
+                        val limitedCategories = categories.filter {
+                            it.name == "快递" || it.name == "还款"
                         }
                         adapter.setCategories(limitedCategories)
                     }
                 } catch (e: Exception) {
                     // 发生错误时只显示基础分类
-                    val limitedCategories = categories.filter { 
-                        it.name == "快递" || it.name == "还款" 
+                    val limitedCategories = categories.filter {
+                        it.name == "快递" || it.name == "还款"
                     }
                     adapter.setCategories(limitedCategories)
                 }
@@ -503,4 +652,22 @@
 
         dialog.show()
     }
+
+    // 添加设置未读提醒徽章的方法
+    private fun setupUnreadBadge() {
+        // 直接使用布局中定义的小红点
+        reminderBadge = binding.reminderBadge
+    }
+
+    // 更新未读提醒徽章
+    private fun updateReminderBadge(unreadCount: Int) {
+        reminderBadge?.apply {
+            if (unreadCount > 0) {
+                text = if (unreadCount > 99) "99+" else unreadCount.toString()
+                visibility = View.VISIBLE
+            } else {
+                visibility = View.GONE
+            }
+        }
+    }
 }
\ No newline at end of file

--
Gitblit v1.9.3