From f4a4110d5ebd74cccd38eea6cdeed0bd96d80c5e Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期三, 02 四月 2025 11:22:03 +0800
Subject: [PATCH] fix : 分类缓存
---
app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt | 50 ++++++++++++----
app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt | 76 ++++++++++++------------
2 files changed, 75 insertions(+), 51 deletions(-)
diff --git a/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt b/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
index 2a10248..3fcc370 100644
--- a/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
+++ b/app/src/main/java/com/example/firstapp/ui/home/HomeFragment.kt
@@ -322,7 +322,7 @@
tabFlight.visibility = View.GONE
tabTrain.visibility = View.GONE
- // 非会员只显示快递和还款
+ // 获取用户信息判断是否是会员
val savedPhone = PreferencesManager.getPhone()
lifecycleScope.launch {
try {
@@ -343,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()
}
}
}
@@ -461,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 {
diff --git a/app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt b/app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt
index 80483dc..d40df61 100644
--- a/app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt
+++ b/app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt
@@ -17,6 +17,7 @@
import com.example.firstapp.model.IncomeGroup
import com.example.firstapp.model.IncomePackage
import com.example.firstapp.util.SecureStorage
+import com.example.firstapp.utils.PreferencesManager
import kotlinx.coroutines.launch
class HomeViewModel : ViewModel() {
@@ -143,59 +144,58 @@
loadDataByType("火车票")
}
- fun loadCategories() {
+ private fun loadCategories() {
viewModelScope.launch {
try {
// 先尝试从本地获取配置
val localCategories = secureStorage.getCategories(currentUserId)
+
+ // 默认完整分类列表
+ val fullCategories = listOf(
+ CategoryConfig(1, "快递", 0, true),
+ CategoryConfig(2, "还款", 1, true),
+ CategoryConfig(3, "收入", 2, true),
+ CategoryConfig(4, "航班", 3, true),
+ CategoryConfig(5, "火车票", 4, true)
+ )
+
+ // 基础分类(非会员可见)
+ val basicCategories = listOf(
+ CategoryConfig(1, "快递", 0, true),
+ CategoryConfig(2, "还款", 1, true)
+ )
+
if (localCategories.isNotEmpty()) {
// 如果本地有配置,直接使用本地配置
_categories.value = localCategories
- // 同时尝试从服务器更新
- try {
- val serverCategories = RetrofitClient.apiService.getUserCategories(currentUserId)
- if (serverCategories.isNotEmpty()) {
- _categories.value = serverCategories
- secureStorage.saveCategories(currentUserId, serverCategories)
- }
- } catch (e: Exception) {
- // 服务器获取失败,继续使用本地配置
- Log.e("HomeViewModel", "Failed to fetch server categories: ${e.message}")
- }
} else {
- // 如果本地没有配置,尝试从服务器获取
try {
- val serverCategories = RetrofitClient.apiService.getUserCategories(currentUserId)
- if (serverCategories.isNotEmpty()) {
- _categories.value = serverCategories
- secureStorage.saveCategories(currentUserId, serverCategories)
- } else {
- // 如果服务器也没有配置,使用默认配置
- val defaultCategories = listOf(
- CategoryConfig(1, "快递", 0),
- CategoryConfig(2, "还款", 1),
- CategoryConfig(3, "收入", 2),
- CategoryConfig(4, "航班", 3),
- CategoryConfig(5, "火车票", 4)
- )
- _categories.value = defaultCategories
- secureStorage.saveCategories(currentUserId, defaultCategories)
- }
- } catch (e: Exception) {
- // 如果服务器请求失败,使用默认配置
- val defaultCategories = listOf(
- CategoryConfig(1, "快递", 0),
- CategoryConfig(2, "还款", 1),
- CategoryConfig(3, "收入", 2),
- CategoryConfig(4, "航班", 3),
- CategoryConfig(5, "火车票", 4)
- )
+ // 尝试从服务器获取用户信息判断是否是会员
+ val savedPhone = PreferencesManager.getPhone()
+ val response = RetrofitClient.apiService.getUserInfo(savedPhone ?: "")
+ val isMember = response.code == "0" && response.data?.isMember == true
+
+ // 根据会员状态设置默认分类
+ val defaultCategories = if (isMember) fullCategories else basicCategories
_categories.value = defaultCategories
secureStorage.saveCategories(currentUserId, defaultCategories)
+
+ // 同步到服务器
+ try {
+ syncCategoriesToServer(defaultCategories)
+ } catch (e: Exception) {
+ Log.e("HomeViewModel", "Failed to sync categories: ${e.message}")
+ }
+ } catch (e: Exception) {
+ // 如果获取用户信息失败,使用基础分类
+ _categories.value = basicCategories
+ secureStorage.saveCategories(currentUserId, basicCategories)
}
}
+
// 更新可见分类
_categories.value?.let { updateVisibleCategories(it) }
+
} catch (e: Exception) {
Log.e("HomeViewModel", "Failed to load categories: ${e.message}")
}
--
Gitblit v1.9.3