From 3466799c94227c5ebba9fb201621e745058867ee Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期二, 15 四月 2025 13:18:34 +0800 Subject: [PATCH] add: 消息提醒时间设定;会员到期时间调整; --- app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt | 97 +++++++++++++++++++++++++++++------------------- 1 files changed, 59 insertions(+), 38 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 80483dc..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,59 +153,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}") } @@ -234,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