From cb99bd7dad1b305a434c5c6c99ca65e782eb0f34 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期五, 11 四月 2025 17:32:12 +0800 Subject: [PATCH] add: 消息提醒 --- app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt | 107 +++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 74 insertions(+), 33 deletions(-) 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 9699b1e..0601ec9 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,7 +17,10 @@ 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 +import com.example.firstapp.database.repository.ReminderRecordRepository +import com.example.firstapp.database.entity.ReminderRecord class HomeViewModel : ViewModel() { @@ -39,8 +42,12 @@ private val _visibleCategories = MutableLiveData<List<CategoryConfig>>() val visibleCategories: LiveData<List<CategoryConfig>> = _visibleCategories + private val _unreadReminderCount = MutableLiveData<Int>() + val unreadReminderCount: LiveData<Int> = _unreadReminderCount + private lateinit var secureStorage: SecureStorage private lateinit var currentUserId: String + private lateinit var reminderRecordRepository: ReminderRecordRepository init { // 初始化时加载包裹列表数据 @@ -52,9 +59,12 @@ fun initialize(context: Context, userId: String) { secureStorage = SecureStorage(context) currentUserId = userId + reminderRecordRepository = ReminderRecordRepository(context) loadCategories() // 初始化时更新可见分类 _categories.value?.let { updateVisibleCategories(it) } + // 加载未读提醒数量 + checkUnreadReminders() } private fun loadDataByType(type: String) { @@ -143,45 +153,60 @@ loadDataByType("火车票") } - fun loadCategories() { + private fun loadCategories() { viewModelScope.launch { try { - // 先尝试从服务器获取配置 - val serverCategories = RetrofitClient.apiService.getUserCategories(currentUserId) - if (serverCategories.isNotEmpty()) { - _categories.value = serverCategories - secureStorage.saveCategories(currentUserId, serverCategories) + // 先尝试从本地获取配置 + 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 } else { - // 如果服务器没有配置,尝试获取本地配置 - val localCategories = secureStorage.getCategories(currentUserId) - if (localCategories.isEmpty()) { - // 如果本地也没有配置,使用默认配置 - val defaultCategories = listOf( - CategoryConfig(1, "快递", 0), - CategoryConfig(2, "还款", 1), - CategoryConfig(3, "收入", 2), - CategoryConfig(4, "航班", 3), - CategoryConfig(5, "火车票", 4) - ) + try { + // 尝试从服务器获取用户信息判断是否是会员 + 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 - syncCategoriesToServer(defaultCategories) - } else { - _categories.value = localCategories - syncCategoriesToServer(localCategories) + 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) { - // 如果网络请求失败,使用本地数据 - val localCategories = secureStorage.getCategories(currentUserId) - _categories.value = localCategories.ifEmpty { - listOf( - CategoryConfig(1, "快递", 0), - CategoryConfig(2, "还款", 1), - CategoryConfig(3, "收入", 2), - CategoryConfig(4, "航班", 3), - CategoryConfig(5, "火车票", 4) - ) - } + Log.e("HomeViewModel", "Failed to load categories: ${e.message}") } } } @@ -201,8 +226,12 @@ fun saveCategories(categories: List<CategoryConfig>) { _categories.value = categories + // 保存到本地存储 + secureStorage.saveCategories(currentUserId, categories) + // 同步到服务器 + syncCategoriesToServer(categories) // 更新可见分类 - _visibleCategories.value = categories.filter { it.isEnabled } + updateVisibleCategories(categories) } private fun updateVisibleCategories(categories: List<CategoryConfig>) { @@ -214,6 +243,18 @@ _visibleCategories.value = categories.filter { it.isEnabled } } + // 添加检查未读提醒数量的方法 + fun checkUnreadReminders() { + viewModelScope.launch { + try { + val unreadCount = reminderRecordRepository.getUnreadCount(ReminderRecord.STATUS_UNREAD) + _unreadReminderCount.postValue(unreadCount) + } catch (e: Exception) { + Log.e("HomeViewModel", "Failed to get unread reminder count: ${e.message}") + } + } + } + // 登出时不再清除本地数据 fun logout() { // 只清除内存中的数据 -- Gitblit v1.9.3