| | |
| | | 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.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 |
| | | import com.example.firstapp.activity.ReminderListActivity |
| | | import android.graphics.Color |
| | | import android.view.Gravity |
| | | import android.widget.FrameLayout |
| | | |
| | | class HomeFragment : Fragment() { |
| | | |
| | |
| | | private lateinit var flightAdapter: FinanceAdapter |
| | | private lateinit var trainAdapter: FinanceAdapter |
| | | private lateinit var dataUpdateReceiver: BroadcastReceiver |
| | | private lateinit var reminderUpdateReceiver: BroadcastReceiver |
| | | private var reminderBadge: TextView? = null |
| | | |
| | | //onCreateView这个方法创建后被调用,通常是初始化视图组件和观察者 |
| | | override fun onCreateView( |
| | |
| | | setupTabSwitching() |
| | | setupObservers() |
| | | setupCategorySelector() |
| | | setupUnreadBadge() |
| | | } |
| | | |
| | | override fun onCreate(savedInstanceState: Bundle?) { |
| | |
| | | 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() |
| | | } |
| | | } |
| | | } |
| | |
| | | updateTabStyles(tabFinance) |
| | | homeViewModel.loadFinanceData() |
| | | } |
| | | |
| | | |
| | | // 其他标签点击事件需要检查会员状态 |
| | | val memberOnlyTabs = mapOf( |
| | | tabIncome to { homeViewModel.loadIncomeData() }, |
| | |
| | | val tabs = listOf(tabExpress, tabFinance, tabIncome, tabFlight, tabTrain) |
| | | 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 |
| | |
| | | trainAdapter.submitList(items) |
| | | } |
| | | |
| | | // 观察未读提醒数量变化 |
| | | homeViewModel.unreadReminderCount.observe(viewLifecycleOwner) { unreadCount -> |
| | | updateReminderBadge(unreadCount) |
| | | } |
| | | |
| | | // 观察可见分类的变化 |
| | | homeViewModel.visibleCategories.observe(viewLifecycleOwner) { categories: List<CategoryConfig> -> |
| | | binding.apply { |
| | |
| | | tabFlight.visibility = View.GONE |
| | | tabTrain.visibility = View.GONE |
| | | |
| | | // 非会员只显示快递和还款 |
| | | // 获取用户信息判断是否是会员 |
| | | val savedPhone = PreferencesManager.getPhone() |
| | | lifecycleScope.launch { |
| | | try { |
| | |
| | | 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() |
| | | } |
| | | } |
| | | } |
| | |
| | | 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.checkUnreadReminders() |
| | | } |
| | | |
| | | override fun onPause() { |
| | |
| | | try { |
| | | // 取消注册广播接收器 |
| | | requireContext().unregisterReceiver(dataUpdateReceiver) |
| | | requireContext().unregisterReceiver(reminderUpdateReceiver) |
| | | } catch (e: Exception) { |
| | | // 处理可能的异常 |
| | | e.printStackTrace() |
| | |
| | | _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 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.reminderButton.setOnClickListener { |
| | | // 跳转到提醒列表页面 |
| | | startActivity(Intent(requireContext(), ReminderListActivity::class.java)) |
| | | // 重新检查未读提醒数量 |
| | | homeViewModel.checkUnreadReminders() |
| | | } |
| | | } |
| | | |
| | | private fun showCategorySelectorDialog() { |
| | |
| | | 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 { |
| | |
| | | |
| | | 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 |
| | | } |
| | | } |
| | | } |
| | | } |