| | |
| | | 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() { |
| | | |
| | |
| | | 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 { |
| | | // 初始化时加载包裹列表数据 |
| | |
| | | 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) { |
| | |
| | | _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() { |
| | | // 只清除内存中的数据 |