| | |
| | | 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.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 |
| | | 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 |
| | |
| | | super.onViewCreated(view, savedInstanceState) |
| | | |
| | | homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) |
| | | // 假设从某处获取用户ID |
| | | // val userId = getUserId() // 需要实现这个方法 |
| | | val userId ="123456" |
| | | 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() |
| | |
| | | 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( |
| | | tabFinance to { homeViewModel.loadFinanceData() }, |
| | | tabIncome to { homeViewModel.loadIncomeData() }, |
| | | tabFlight to { homeViewModel.loadFlightData() }, |
| | | tabTrain to { homeViewModel.loadTrainData() } |
| | |
| | | checkMembershipAndExecute(tab) { |
| | | hideAllRecyclers() |
| | | when (tab) { |
| | | tabFinance -> financeRecycler.visibility = View.VISIBLE |
| | | tabIncome -> incomeRecycler.visibility = View.VISIBLE |
| | | tabFlight -> flightRecycler.visibility = View.VISIBLE |
| | | tabTrain -> trainRecycler.visibility = View.VISIBLE |
| | |
| | | } |
| | | } |
| | | |
| | | 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) |
| | | } |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | // 观察可见分类的变化 |
| | | homeViewModel.visibleCategories.observe(viewLifecycleOwner) { categories: List<String> -> |
| | | homeViewModel.visibleCategories.observe(viewLifecycleOwner) { categories: List<CategoryConfig> -> |
| | | binding.apply { |
| | | // 隐藏所有标签 |
| | | tabExpress.visibility = View.GONE |
| | |
| | | tabFlight.visibility = View.GONE |
| | | tabTrain.visibility = View.GONE |
| | | |
| | | // 根据选中的分类显示对应的标签 |
| | | categories.forEachIndexed { index: Int, categoryName: String -> |
| | | when (categoryName) { |
| | | "快递" -> { |
| | | // 获取用户信息判断是否是会员 |
| | | 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 |
| | | if (index == 0) tabExpress.performClick() |
| | | } |
| | | "还款" -> { |
| | | tabFinance.visibility = View.VISIBLE |
| | | if (index == 0) tabFinance.performClick() |
| | | 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() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | "收入" -> { |
| | | tabIncome.visibility = View.VISIBLE |
| | | if (index == 0) tabIncome.performClick() |
| | | } |
| | | "航班" -> { |
| | | tabFlight.visibility = View.VISIBLE |
| | | if (index == 0) tabFlight.performClick() |
| | | } |
| | | "火车票" -> { |
| | | tabTrain.visibility = View.VISIBLE |
| | | if (index == 0) tabTrain.performClick() |
| | | } |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | // 发生错误时默认显示快递和还款 |
| | | tabExpress.visibility = View.VISIBLE |
| | | tabFinance.visibility = View.VISIBLE |
| | | tabExpress.performClick() |
| | | } |
| | | } |
| | | } |
| | |
| | | if (response.data.isMember) { |
| | | showCategorySelectorDialog() |
| | | } else { |
| | | Toast.makeText(requireContext(), "该功能仅对会员开放", Toast.LENGTH_SHORT).show() |
| | | // 非会员跳转到VIP开通页面 |
| | | val intent = Intent(requireContext(), VipActivity::class.java) |
| | | startActivity(intent) |
| | | } |
| | | } else { |
| | | Toast.makeText(requireContext(), "获取用户信息失败", Toast.LENGTH_SHORT).show() |
| | |
| | | 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 { |