| | |
| | | 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 androidx.recyclerview.widget.RecyclerView |
| | | import com.bumptech.glide.Glide |
| | | import com.example.firstapp.R |
| | | import com.example.firstapp.activity.PickupActivity |
| | | import com.example.firstapp.adapter.ExpressAdapter |
| | | import com.example.firstapp.core.Core |
| | | 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.utils.PreferencesManager |
| | | import com.google.android.material.bottomsheet.BottomSheetDialog |
| | | import kotlinx.coroutines.launch |
| | | |
| | | class HomeFragment : Fragment() { |
| | | |
| | |
| | | |
| | | private lateinit var homeViewModel: HomeViewModel |
| | | private lateinit var expressAdapter: ExpressAdapter |
| | | // private lateinit var financeAdapter: FinanceAdapter |
| | | // private lateinit var memorialAdapter: MemorialAdapter |
| | | private lateinit var financeAdapter: FinanceAdapter |
| | | private lateinit var incomeAdapter: FinanceAdapter |
| | | private lateinit var flightAdapter: FinanceAdapter |
| | | private lateinit var trainAdapter: FinanceAdapter |
| | | private lateinit var dataUpdateReceiver: BroadcastReceiver |
| | | |
| | | //onCreateView这个方法创建后被调用,通常是初始化视图组件和观察者 |
| | |
| | | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| | | super.onViewCreated(view, savedInstanceState) |
| | | |
| | | //通过 ViewModelProvider 获取 HomeViewModel 的实例,以便在视图中使用。 |
| | | homeViewModel = ViewModelProvider(this).get(HomeViewModel::class.java) |
| | | |
| | | // 假设从某处获取用户ID |
| | | // val userId = getUserId() // 需要实现这个方法 |
| | | val userId ="123456" |
| | | homeViewModel.initialize(requireContext(), userId) |
| | | |
| | | //调用这个方法来设置 RecyclerView用于设置 RecyclerView 的布局和适配器。 |
| | | setupRecyclerViews() |
| | | setupTabSwitching() |
| | | //调用这个方法来观察 ViewModel 中的数据变化 |
| | | observeViewModelData() |
| | | setupCategorySelector() |
| | | } |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | |
| | | } |
| | | } |
| | | |
| | | // // 财务列表 |
| | | // binding.financeRecycler.apply { |
| | | // layoutManager = LinearLayoutManager(context) |
| | | // financeAdapter = FinanceAdapter() |
| | | // adapter = financeAdapter |
| | | // } |
| | | // |
| | | // // 纪念日列表 |
| | | // binding.memorialRecycler.apply { |
| | | // layoutManager = LinearLayoutManager(context) |
| | | // memorialAdapter = MemorialAdapter() |
| | | // adapter = memorialAdapter |
| | | // } |
| | | // 财务列表 |
| | | binding.financeRecycler.apply { |
| | | layoutManager = LinearLayoutManager(context) |
| | | financeAdapter = FinanceAdapter() |
| | | adapter = financeAdapter |
| | | |
| | | // 设置初始状态 - 添加这行 |
| | | binding.financeRecycler.visibility = View.GONE |
| | | |
| | | // 设置点击监听 |
| | | financeAdapter.setOnPackageClickListener { group, pack -> |
| | | // 跳转到取件页面 |
| | | val intent = Intent(requireContext(), PickupActivity::class.java).apply { |
| | | putExtra("station_name", group.stationName) |
| | | putExtra("company", pack.company) |
| | | } |
| | | startActivity(intent) |
| | | } |
| | | } |
| | | |
| | | // 添加新的 RecyclerView |
| | | binding.incomeRecycler.apply { |
| | | layoutManager = LinearLayoutManager(context) |
| | | incomeAdapter = FinanceAdapter() |
| | | adapter = incomeAdapter |
| | | visibility = View.GONE |
| | | } |
| | | |
| | | binding.flightRecycler.apply { |
| | | layoutManager = LinearLayoutManager(context) |
| | | flightAdapter = FinanceAdapter() |
| | | adapter = flightAdapter |
| | | visibility = View.GONE |
| | | } |
| | | |
| | | binding.trainRecycler.apply { |
| | | layoutManager = LinearLayoutManager(context) |
| | | trainAdapter = FinanceAdapter() |
| | | adapter = trainAdapter |
| | | visibility = View.GONE |
| | | } |
| | | } |
| | | |
| | | 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() |
| | | } |
| | | |
| | | tabIncome.setOnClickListener { |
| | | hideAllRecyclers() |
| | | incomeRecycler.visibility = View.VISIBLE |
| | | updateTabStyles(tabIncome) |
| | | homeViewModel.loadIncomeData() |
| | | } |
| | | |
| | | tabFlight.setOnClickListener { |
| | | hideAllRecyclers() |
| | | flightRecycler.visibility = View.VISIBLE |
| | | updateTabStyles(tabFlight) |
| | | homeViewModel.loadFlightData() |
| | | } |
| | | |
| | | tabTrain.setOnClickListener { |
| | | hideAllRecyclers() |
| | | trainRecycler.visibility = View.VISIBLE |
| | | updateTabStyles(tabTrain) |
| | | homeViewModel.loadTrainData() |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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: TextView) { |
| | | 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)) |
| | | tab.textSize = if (tab == selectedTab) 16f else 14f |
| | | } |
| | | } |
| | | } |
| | | |
| | | //这个方法用于观察 homeViewModel 中的 expressItems 数据。 |
| | |
| | | expressAdapter.submitList(items) |
| | | } |
| | | |
| | | // homeViewModel.financeItems.observe(viewLifecycleOwner) { items -> |
| | | // financeAdapter.submitList(items) |
| | | // } |
| | | // |
| | | // homeViewModel.memorialItems.observe(viewLifecycleOwner) { items -> |
| | | // memorialAdapter.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.visibleCategories.observe(viewLifecycleOwner) { categories: List<String> -> |
| | | binding.apply { |
| | | // 隐藏所有标签 |
| | | tabExpress.visibility = View.GONE |
| | | tabFinance.visibility = View.GONE |
| | | tabIncome.visibility = View.GONE |
| | | tabFlight.visibility = View.GONE |
| | | tabTrain.visibility = View.GONE |
| | | |
| | | // 根据选中的分类显示对应的标签 |
| | | categories.forEachIndexed { index: Int, categoryName: String -> |
| | | when (categoryName) { |
| | | "快递" -> { |
| | | tabExpress.visibility = View.VISIBLE |
| | | if (index == 0) tabExpress.performClick() |
| | | } |
| | | "还款" -> { |
| | | tabFinance.visibility = View.VISIBLE |
| | | if (index == 0) tabFinance.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() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | override fun onResume() { |
| | |
| | | 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 { |
| | | Toast.makeText(requireContext(), "该功能仅对会员开放", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } else { |
| | | Toast.makeText(requireContext(), "获取用户信息失败", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } catch (e: Exception) { |
| | | e.printStackTrace() |
| | | Toast.makeText(requireContext(), "网络错误,请稍后重试", Toast.LENGTH_SHORT).show() |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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 -> |
| | | adapter.setCategories(categories) |
| | | } |
| | | |
| | | dialogBinding.saveButton.setOnClickListener { |
| | | homeViewModel.saveCategories(adapter.getCategories()) |
| | | dialog.dismiss() |
| | | } |
| | | |
| | | dialog.show() |
| | | } |
| | | } |