cloudroam
6 天以前 0cf0870889d65f5d11d0b2c8af7bca9442a7ba19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
package com.example.firstapp.ui.home
 
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.firstapp.R
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.utils.PreferencesManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlinx.coroutines.launch
import com.example.firstapp.view.UnderlineTextView
import com.example.firstapp.activity.ReminderListActivity
 
class HomeFragment : Fragment() {
 
    private var _binding: FragmentHomeBinding? = null
 
    private val binding get() = _binding!!
 
    private lateinit var homeViewModel: HomeViewModel
    private lateinit var expressAdapter: ExpressAdapter
    private lateinit var financeAdapter: FinanceAdapter
    private lateinit var incomeAdapter: IncomeAdapter
    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(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        return binding.root
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
 
        homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java)
        val userId = "123456"
        homeViewModel.initialize(requireContext(), userId)
 
        // 检查是否是首次安装
        val isFirstInstall = PreferencesManager.isFirstInstall()
        if (isFirstInstall) {
            // 首次安装,设置默认显示快递和还款
            val defaultCategories = listOf(
                CategoryConfig(
                    id = 1,
                    name = "快递",
                    order = 1,
                    isEnabled = true
                ),
                CategoryConfig(
                    id = 2,
                    name = "还款",
                    order = 2,
                    isEnabled = true
                )
            )
            homeViewModel.saveCategories(defaultCategories)
            // 标记为非首次安装
            PreferencesManager.setFirstInstall(false)
        }
 
        setupAdapters()
        setupTabSwitching()
        setupObservers()
        setupCategorySelector()
        setupUnreadBadge()
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
 
        // 创建广播接收器
        dataUpdateReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                if (intent.action == "com.example.firstapp.DATA_UPDATED") {
                    // 收到数据更新广播时重新加载数据
                    homeViewModel.loadExpressData()
                }
            }
        }
 
        // 创建提醒更新广播接收器
        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 {
            layoutManager = LinearLayoutManager(context)
            expressAdapter = ExpressAdapter()
            adapter = expressAdapter
 
            // 设置点击监听
            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)
            }
        }
 
        // 财务列表
        binding.financeRecycler.apply {
            layoutManager = LinearLayoutManager(context)
            financeAdapter = FinanceAdapter()
            adapter = financeAdapter
 
            // 设置初始状态 - 添加这行
            binding.financeRecycler.visibility = View.GONE
 
            // 设置点击监听
            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)
            }
        }
 
        // 添加新的 RecyclerView
        binding.incomeRecycler.apply {
            layoutManager = LinearLayoutManager(context)
            incomeAdapter = IncomeAdapter()
            adapter = incomeAdapter
 
            // 设置初始状态 - 添加这行
            binding.incomeRecycler.visibility = View.GONE
 
            // 设置点击监听
            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)
            }
        }
 
        binding.flightRecycler.apply {
            layoutManager = LinearLayoutManager(context)
            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 = 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)
            }
        }
    }
 
    private fun setupTabSwitching() {
        binding.apply {
            // 设置初始状态
            tabExpress.setTextColor(ContextCompat.getColor(requireContext(), R.color.tab_selected))
            tabFinance.setTextColor(ContextCompat.getColor(requireContext(), R.color.gray))
 
            // 快递标签点击事件
            tabExpress.setOnClickListener {
                hideAllRecyclers()
                expressRecycler.visibility = View.VISIBLE
                updateTabStyles(tabExpress)
                homeViewModel.loadExpressData()
            }
 
            // 还款标签点击事件 - 非会员也可以使用
            tabFinance.setOnClickListener {
                hideAllRecyclers()
                financeRecycler.visibility = View.VISIBLE
                updateTabStyles(tabFinance)
                homeViewModel.loadFinanceData()
            }
 
            // 其他标签点击事件需要检查会员状态
            val memberOnlyTabs = mapOf(
                tabIncome to { homeViewModel.loadIncomeData() },
                tabFlight to { homeViewModel.loadFlightData() },
                tabTrain to { homeViewModel.loadTrainData() }
            )
 
            memberOnlyTabs.forEach { (tab, loadAction) ->
                tab.setOnClickListener {
                    checkMembershipAndExecute(tab) {
                        hideAllRecyclers()
                        when (tab) {
                            tabIncome -> incomeRecycler.visibility = View.VISIBLE
                            tabFlight -> flightRecycler.visibility = View.VISIBLE
                            tabTrain -> trainRecycler.visibility = View.VISIBLE
                        }
                        updateTabStyles(tab)
                        loadAction()
                    }
                }
            }
        }
    }
 
    private fun checkMembershipAndExecute(tab: TextView, action: () -> Unit) {
        // 从本地获取保存的手机号
        val savedPhone = PreferencesManager.getPhone()
        if (savedPhone.isNullOrEmpty()) {
            Toast.makeText(requireContext(), "请先登录", Toast.LENGTH_SHORT).show()
            return
        }
 
        // 使用协程检查会员状态
        lifecycleScope.launch {
            try {
                val response = RetrofitClient.apiService.getUserInfo(savedPhone)
                if (response.code == "0" && response.data != null) {
                    if (response.data.isMember) {
                        action()
                    } else {
                        Toast.makeText(requireContext(), "该功能仅对会员开放", Toast.LENGTH_SHORT).show()
                        // 切回快递标签
                        binding.tabExpress.performClick()
                    }
                } else {
                    Toast.makeText(requireContext(), "获取用户信息失败", Toast.LENGTH_SHORT).show()
                    binding.tabExpress.performClick()
                }
            } catch (e: Exception) {
                e.printStackTrace()
                Toast.makeText(requireContext(), "网络错误,请稍后重试", Toast.LENGTH_SHORT).show()
                binding.tabExpress.performClick()
            }
        }
    }
 
    private fun hideAllRecyclers() {
        binding.apply {
            expressRecycler.visibility = View.GONE
            financeRecycler.visibility = View.GONE
            incomeRecycler.visibility = View.GONE
            flightRecycler.visibility = View.GONE
            trainRecycler.visibility = View.GONE
        }
    }
 
    private fun updateTabStyles(selectedTab: UnderlineTextView) {
        binding.apply {
            val tabs = listOf(tabExpress, tabFinance, tabIncome, tabFlight, tabTrain)
            tabs.forEach { tab ->
                // 设置文字颜色为黑色或灰色
                tab.setTextColor(ContextCompat.getColor(requireContext(),
                    if (tab == selectedTab) android.R.color.black else R.color.gray))
                // 设置文字大小
                tab.textSize = if (tab == selectedTab) 16f else 14f
                // 设置下划线
                tab.setUnderlineVisible(tab == selectedTab)
            }
        }
    }
 
    private fun setupObservers() {
        //当 expressItems 数据发生变化时,更新 RecyclerView 的数据。
        homeViewModel.expressItems.observe(viewLifecycleOwner) { items ->
            //将新的数据列表提交给适配器,以更新 RecyclerView 的显示内容。
            expressAdapter.submitList(items)
        }
 
        homeViewModel.financeItems.observe(viewLifecycleOwner) { items ->
            financeAdapter.submitList(items)
        }
 
        // 观察收入数据变化
        homeViewModel.incomeItems.observe(viewLifecycleOwner) { items ->
            incomeAdapter.submitList(items)
        }
 
        homeViewModel.flightItems.observe(viewLifecycleOwner) { items ->
            flightAdapter.submitList(items)
        }
 
        homeViewModel.trainItems.observe(viewLifecycleOwner) { items ->
            trainAdapter.submitList(items)
        }
 
        // 观察未读提醒数量变化
        homeViewModel.unreadReminderCount.observe(viewLifecycleOwner) { unreadCount ->
            updateReminderBadge(unreadCount)
        }
 
        // 观察可见分类的变化
        homeViewModel.visibleCategories.observe(viewLifecycleOwner) { categories: List<CategoryConfig> ->
            binding.apply {
                // 隐藏所有标签
                tabExpress.visibility = View.GONE
                tabFinance.visibility = View.GONE
                tabIncome.visibility = View.GONE
                tabFlight.visibility = View.GONE
                tabTrain.visibility = View.GONE
 
                // 获取用户信息判断是否是会员
                val savedPhone = PreferencesManager.getPhone()
                lifecycleScope.launch {
                    try {
                        val response = RetrofitClient.apiService.getUserInfo(savedPhone ?: "")
                        val isMember = response.code == "0" && response.data?.isMember == true
 
                        if (!isMember) {
                            // 非会员只显示快递和还款
                            tabExpress.visibility = View.VISIBLE
                            tabFinance.visibility = View.VISIBLE
                            if (categories.firstOrNull()?.name == "快递") {
                                tabExpress.performClick()
                            } else {
                                tabFinance.performClick()
                            }
                        } else {
                            // 会员显示所有选中的分类
                            categories.forEach { category ->
                                when (category.name) {
                                    "快递" -> {
                                        tabExpress.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabExpress.performClick()
                                    }
                                    "还款" -> {
                                        tabFinance.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabFinance.performClick()
                                    }
                                    "收入" -> {
                                        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()
                                    }
                                }
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                        // 发生错误时默认显示快递和还款
                        tabExpress.visibility = View.VISIBLE
                        tabFinance.visibility = View.VISIBLE
                        tabExpress.performClick()
                    }
                }
            }
        }
    }
 
    override fun onResume() {
        super.onResume()
        // 使用 ContextCompat 注册广播接收器,并指定 RECEIVER_NOT_EXPORTED 标志
        ContextCompat.registerReceiver(
            requireContext(),
            dataUpdateReceiver,
            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.loadAllCategoryData()
//        homeViewModel.loadExpressData()
//        homeViewModel.loadFinanceData()
//        homeViewModel.loadIncomeData()
//        homeViewModel.loadFlightData()
//        homeViewModel.loadTrainData()
 
        // 检查未读提醒数量
        homeViewModel.checkUnreadReminders()
 
    }
 
    override fun onPause() {
        super.onPause()
        try {
            // 取消注册广播接收器
            requireContext().unregisterReceiver(dataUpdateReceiver)
            requireContext().unregisterReceiver(reminderUpdateReceiver)
        } catch (e: Exception) {
            // 处理可能的异常
            e.printStackTrace()
        }
    }
 
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
 
//    private fun loadAdvertisements() {
//        // 使用 Glide 加载网络图片
//        Glide.with(this)
//            .load("http://192.168.1.235:9999/advertisement/up.png")
//            .into(binding.adBanner)
//
//        Glide.with(this)
//            .load("http://192.168.1.235:9999/advertisement/down.png")
//            .into(binding.bottomAdBanner)
//    }
 
    // 设置分类选择器 检查会员状态
    private fun setupCategorySelector() {
        binding.categoryButton.setOnClickListener {
            // 从本地获取保存的手机号
            val savedPhone = PreferencesManager.getPhone()
            if (savedPhone.isNullOrEmpty()) {
                Toast.makeText(requireContext(), "请先登录", Toast.LENGTH_SHORT).show()
                return@setOnClickListener
            }
 
            // 使用协程检查会员状态
            lifecycleScope.launch {
                try {
                    val response = RetrofitClient.apiService.getUserInfo(savedPhone)
                    if (response.code == "0" && response.data != null) {
                        if (response.data.isMember) {
                            showCategorySelectorDialog()
                        } else {
                            // 非会员跳转到VIP开通页面
                            val intent = Intent(requireContext(), VipActivity::class.java)
                            startActivity(intent)
                        }
                    } else {
                        Toast.makeText(requireContext(), "获取用户信息失败", Toast.LENGTH_SHORT).show()
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                    Toast.makeText(requireContext(), "网络错误,请稍后重试", Toast.LENGTH_SHORT).show()
                }
            }
        }
 
        // 添加提醒按钮点击事件
        binding.reminderButton.setOnClickListener {
            // 跳转到提醒列表页面
            startActivity(Intent(requireContext(), ReminderListActivity::class.java))
            // 重新检查未读提醒数量
            homeViewModel.checkUnreadReminders()
        }
    }
 
    private fun showCategorySelectorDialog() {
        val dialog = BottomSheetDialog(requireContext())
        val dialogBinding = DialogCategorySelectorBinding.inflate(layoutInflater)
        dialog.setContentView(dialogBinding.root)
 
        val adapter = CategorySelectorAdapter()
        dialogBinding.categoryRecyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            this.adapter = adapter
        }
 
        // 加载所有分类
        homeViewModel.categories.observe(viewLifecycleOwner) { categories ->
            // 如果是会员,显示所有分类供选择
            lifecycleScope.launch {
                try {
                    val savedPhone = PreferencesManager.getPhone()
                    val response = RetrofitClient.apiService.getUserInfo(savedPhone ?: "")
                    val isMember = response.code == "0" && response.data?.isMember == true
 
                    if (isMember) {
                        // 会员可以看到所有分类
                        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 == "还款"
                        }
                        adapter.setCategories(limitedCategories)
                    }
                } catch (e: Exception) {
                    // 发生错误时只显示基础分类
                    val limitedCategories = categories.filter {
                        it.name == "快递" || it.name == "还款"
                    }
                    adapter.setCategories(limitedCategories)
                }
            }
        }
 
        dialogBinding.saveButton.setOnClickListener {
            homeViewModel.saveCategories(adapter.getCategories())
            dialog.dismiss()
        }
 
        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
            }
        }
    }
}