zhujie
2025-04-03 fe04012057d024770e0180543483d393281a542f
app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
@@ -33,13 +33,12 @@
import com.example.firstapp.utils.PreferencesManager
import com.google.android.material.bottomsheet.BottomSheetDialog
import kotlinx.coroutines.launch
import com.example.firstapp.view.UnderlineTextView
class HomeFragment : Fragment() {
    private var _binding: FragmentHomeBinding? = null
    // This property is only valid between onCreateView and
    // onDestroyView.
    private val binding get() = _binding!!
    private lateinit var homeViewModel: HomeViewModel
@@ -274,13 +273,17 @@
        }
    }
    private fun updateTabStyles(selectedTab: TextView) {
    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) R.color.tab_selected else R.color.gray))
                    if (tab == selectedTab) android.R.color.black else R.color.gray))
                // 设置文字大小
                tab.textSize = if (tab == selectedTab) 16f else 14f
                // 设置下划线
                tab.setUnderlineVisible(tab == selectedTab)
            }
        }
    }
@@ -319,7 +322,7 @@
                tabFlight.visibility = View.GONE
                tabTrain.visibility = View.GONE
                // 非会员只显示快递和还款
                // 获取用户信息判断是否是会员
                val savedPhone = PreferencesManager.getPhone()
                lifecycleScope.launch {
                    try {
@@ -340,24 +343,24 @@
                            categories.forEach { category ->
                                when (category.name) {
                                    "快递" -> {
                                        tabExpress.visibility = View.VISIBLE
                                        if (categories.indexOf(category) == 0) tabExpress.performClick()
                                        tabExpress.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabExpress.performClick()
                                    }
                                    "还款" -> {
                                        tabFinance.visibility = View.VISIBLE
                                        if (categories.indexOf(category) == 0) tabFinance.performClick()
                                        tabFinance.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabFinance.performClick()
                                    }
                                    "收入" -> {
                                        tabIncome.visibility = View.VISIBLE
                                        if (categories.indexOf(category) == 0) tabIncome.performClick()
                                        tabIncome.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabIncome.performClick()
                                    }
                                    "航班" -> {
                                        tabFlight.visibility = View.VISIBLE
                                        if (categories.indexOf(category) == 0) tabFlight.performClick()
                                        tabFlight.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabFlight.performClick()
                                    }
                                    "火车票" -> {
                                        tabTrain.visibility = View.VISIBLE
                                        if (categories.indexOf(category) == 0) tabTrain.performClick()
                                        tabTrain.visibility = if (category.isEnabled) View.VISIBLE else View.GONE
                                        if (categories.indexOf(category) == 0 && category.isEnabled) tabTrain.performClick()
                                    }
                                }
                            }
@@ -458,9 +461,33 @@
            this.adapter = adapter
        }
        // 加载现有分类
        // 加载所有分类
        homeViewModel.categories.observe(viewLifecycleOwner) { categories ->
            adapter.setCategories(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) {
                        // 会员可以看到所有分类
                        adapter.setCategories(categories)
                    } 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 {