From 731667db1ac658a6f6064ef328d04eb1d47c20ff Mon Sep 17 00:00:00 2001
From: cloudroam <cloudroam>
Date: 星期一, 31 三月 2025 13:53:02 +0800
Subject: [PATCH] fix 登录
---
app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt | 241 +++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 225 insertions(+), 16 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 630b6af..d8c92f2 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
@@ -1,36 +1,245 @@
package com.example.firstapp.ui.home
+import android.content.Context
+import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
+import androidx.lifecycle.viewModelScope
import com.example.firstapp.core.Core
-import com.example.firstapp.database.entity.Code
+import com.example.firstapp.database.service.RetrofitClient
+import com.example.firstapp.model.CategoryConfig
+import com.example.firstapp.model.CategoryConfigSync
+import com.example.firstapp.model.ExpressGroup
+import com.example.firstapp.model.ExpressPackage
+import com.example.firstapp.model.FinanceGroup
+import com.example.firstapp.model.FinancePackage
+import com.example.firstapp.util.SecureStorage
+import kotlinx.coroutines.launch
class HomeViewModel : ViewModel() {
- private val _text = MutableLiveData<String>().apply {
- value = "短信主页面"
- }
- val text: LiveData<String> = _text
+ private val _expressItems = MutableLiveData<List<ExpressGroup>>()
+ private val _financeItems = MutableLiveData<List<FinanceGroup>>()
+ private val _incomeItems = MutableLiveData<List<FinanceGroup>>()
+ private val _flightItems = MutableLiveData<List<FinanceGroup>>()
+ private val _trainItems = MutableLiveData<List<FinanceGroup>>()
+
+ val expressItems: LiveData<List<ExpressGroup>> = _expressItems
+ val financeItems: LiveData<List<FinanceGroup>> = _financeItems
+ val incomeItems: LiveData<List<FinanceGroup>> = _incomeItems
+ val flightItems: LiveData<List<FinanceGroup>> = _flightItems
+ val trainItems: LiveData<List<FinanceGroup>> = _trainItems
- private val _codeList = MutableLiveData<List<Code>>()
+ private val _categories = MutableLiveData<List<CategoryConfig>>()
+ val categories: LiveData<List<CategoryConfig>> = _categories
- val codeList: LiveData<List<Code>> get() = _codeList
+ // 添加可见分类的 LiveData
+ private val _visibleCategories = MutableLiveData<List<String>>()
+ val visibleCategories: LiveData<List<String>> = _visibleCategories
+
+ private lateinit var secureStorage: SecureStorage
+ private lateinit var currentUserId: String
init {
- // 初始化时加载数据
- loadData()
+ // 初始化时加载包裹列表数据
+ loadExpressData()
+ // 初始化时不加载财务列表数据 0317
+ // loadFinanceData()
}
- // 加载数据的方法
- fun loadData() {
- // 获取数据,并更新 LiveData
- _codeList.value = Core.code.getAllDesc() // 假设这是获取最新的 data 的方法
+ fun initialize(context: Context, userId: String) {
+ secureStorage = SecureStorage(context)
+ currentUserId = userId
+ loadCategories()
+ // 初始化时更新可见分类
+ _categories.value?.let { updateVisibleCategories(it) }
}
- // 如果需要手动更新数据,可以调用这个方法
- fun updateData() {
- _codeList.value = Core.code.getAllDesc() // 重新获取并更新数据
+ fun loadExpressData() {
+ viewModelScope.launch {
+ // 1. 获取所有驿站类型的提醒设置
+ val stations = Core.reminder.getByType("快递")
+
+ // 2. 按驿站分组获取包裹信息
+ val groups = stations.map { station ->
+ val packages = Core.code.getByKeyword(station.nickname).map { code ->
+ ExpressPackage(
+ id = code.id, //ID
+ company = code.secondLevel, //快递公司
+ trackingNumber = code.code, // 取件码
+ createTime = code.createTime //快递时间
+ )
+ }
+ ExpressGroup(
+ stationName = station.nickname, packages = packages
+ )
+ }
+
+ _expressItems.postValue(groups)
+ }
+ }
+
+ fun loadFinanceData() {
+ viewModelScope.launch {
+ // 1. 获取所有驿站类型的提醒设置
+ val stations = Core.reminder.getByType("还款")
+
+ // 2. 按驿站分组获取包裹信息
+ val groups = stations.map { station ->
+ val packages = Core.code.getByKeyword(station.nickname).map { code ->
+ FinancePackage(
+ id = code.id, //ID
+ company = code.secondLevel, //快递公司
+ trackingNumber = code.code, // 取件码
+ createTime = code.createTime //快递时间
+ )
+ }
+ FinanceGroup(
+ stationName = station.nickname, packages = packages
+ )
+ }
+
+ _financeItems.postValue(groups)
+ }
+ }
+
+ fun loadIncomeData() {
+ viewModelScope.launch {
+ val stations = Core.reminder.getByType("收入")
+ val groups = stations.map { station ->
+ val packages = Core.code.getByKeyword(station.nickname).map { code ->
+ FinancePackage(
+ id = code.id,
+ company = code.secondLevel,
+ trackingNumber = code.code,
+ createTime = code.createTime
+ )
+ }
+ FinanceGroup(stationName = station.nickname, packages = packages)
+ }
+ _incomeItems.postValue(groups)
+ }
+ }
+
+ fun loadFlightData() {
+ viewModelScope.launch {
+ val stations = Core.reminder.getByType("航班")
+ val groups = stations.map { station ->
+ val packages = Core.code.getByKeyword(station.nickname).map { code ->
+ FinancePackage(
+ id = code.id,
+ company = code.secondLevel,
+ trackingNumber = code.code,
+ createTime = code.createTime
+ )
+ }
+ FinanceGroup(stationName = station.nickname, packages = packages)
+ }
+ _flightItems.postValue(groups)
+ }
+ }
+
+ fun loadTrainData() {
+ viewModelScope.launch {
+ val stations = Core.reminder.getByType("火车票")
+ val groups = stations.map { station ->
+ val packages = Core.code.getByKeyword(station.nickname).map { code ->
+ FinancePackage(
+ id = code.id,
+ company = code.secondLevel,
+ trackingNumber = code.code,
+ createTime = code.createTime
+ )
+ }
+ FinanceGroup(stationName = station.nickname, packages = packages)
+ }
+ _trainItems.postValue(groups)
+ }
+ }
+
+ fun loadCategories() {
+ viewModelScope.launch {
+ try {
+ // 先尝试从服务器获取配置
+ val serverCategories = RetrofitClient.apiService.getUserCategories(currentUserId)
+ if (serverCategories.isNotEmpty()) {
+ _categories.value = serverCategories
+ secureStorage.saveCategories(currentUserId, serverCategories)
+ } 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)
+ )
+ _categories.value = defaultCategories
+ syncCategoriesToServer(defaultCategories)
+ } else {
+ _categories.value = localCategories
+ syncCategoriesToServer(localCategories)
+ }
+ }
+ } 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)
+ )
+ }
+ }
+ }
+ }
+
+ private fun syncCategoriesToServer(categories: List<CategoryConfig>) {
+ viewModelScope.launch {
+ try {
+ RetrofitClient.apiService.saveUserCategories(
+ CategoryConfigSync(currentUserId, categories)
+ )
+ } catch (e: Exception) {
+ // 同步失败,可以稍后重试或者显示提示
+ Log.e("CategorySync", "Failed to sync categories: ${e.message}")
+ }
+ }
+ }
+
+ fun saveCategories(categories: List<CategoryConfig>) {
+ viewModelScope.launch {
+ // 保存到本地
+ secureStorage.saveCategories(currentUserId, categories)
+ // 同步到服务器
+ syncCategoriesToServer(categories)
+ _categories.value = categories
+
+ // 更新可见分类
+ updateVisibleCategories(categories)
+ }
+ }
+
+ private fun updateVisibleCategories(categories: List<CategoryConfig>) {
+ val visibleNames = categories
+ .filter { it.isEnabled }
+ .sortedBy { it.order }
+ .map { it.name }
+
+ _visibleCategories.value = visibleNames
+ }
+
+ // 登出时不再清除本地数据
+ fun logout() {
+ // 只清除内存中的数据
+ _categories.value = emptyList()
}
}
\ No newline at end of file
--
Gitblit v1.9.3