| | |
| | | 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.model.IncomeGroup |
| | | import com.example.firstapp.model.IncomePackage |
| | | 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<IncomeGroup>>() |
| | | 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<IncomeGroup>> = _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 |
| | | private val _visibleCategories = MutableLiveData<List<CategoryConfig>>() |
| | | val visibleCategories: LiveData<List<CategoryConfig>> = _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() // 重新获取并更新数据 |
| | | private fun loadDataByType(type: String) { |
| | | viewModelScope.launch { |
| | | try { |
| | | // 获取该类型下的所有站点分组 |
| | | val stations = Core.code.getStationsByType(type) |
| | | |
| | | when (type) { |
| | | "快递" -> { |
| | | // 处理快递类型 |
| | | val groups = stations.map { station -> |
| | | val packages = Core.code.getPackagesByTypeAndStation(type, station.stationName).map { code -> |
| | | ExpressPackage( |
| | | id = code.id, |
| | | company = code.secondLevel, |
| | | trackingNumber = code.code, |
| | | createTime = code.createTime |
| | | ) |
| | | } |
| | | ExpressGroup(stationName = station.stationName, packages = packages) |
| | | } |
| | | _expressItems.postValue(groups) |
| | | } |
| | | "收入" -> { |
| | | // 特殊处理收入类型 |
| | | val groups = stations.map { station -> |
| | | val packages = Core.code.getPackagesByTypeAndStation(type, station.stationName).map { code -> |
| | | IncomePackage( |
| | | id = code.id, |
| | | company = code.secondLevel, // 交易对方 |
| | | trackingNumber = code.code, // 金额 |
| | | createTime = code.createTime, |
| | | balance = code.remarks.replace("余额", "") // 去掉"余额"前缀 |
| | | ) |
| | | } |
| | | IncomeGroup(stationName = station.stationName, packages = packages) |
| | | } |
| | | _incomeItems.postValue(groups) |
| | | } |
| | | else -> { |
| | | // 处理其他类型(还款、航班、火车票) |
| | | val groups = stations.map { station -> |
| | | val packages = Core.code.getPackagesByTypeAndStation(type, station.stationName).map { code -> |
| | | FinancePackage( |
| | | id = code.id, |
| | | company = code.secondLevel, |
| | | trackingNumber = code.code, |
| | | createTime = code.createTime |
| | | ) |
| | | } |
| | | FinanceGroup(stationName = station.stationName, packages = packages) |
| | | } |
| | | |
| | | // 根据类型更新对应的 LiveData |
| | | when (type) { |
| | | "还款" -> _financeItems.postValue(groups) |
| | | "航班" -> _flightItems.postValue(groups) |
| | | "火车票" -> _trainItems.postValue(groups) |
| | | } |
| | | } |
| | | } |
| | | } catch (e: Exception) { |
| | | Log.e("HomeViewModel", "Failed to load $type data: ${e.message}") |
| | | } |
| | | } |
| | | } |
| | | |
| | | fun loadExpressData() { |
| | | loadDataByType("快递") |
| | | } |
| | | |
| | | fun loadFinanceData() { |
| | | loadDataByType("还款") |
| | | } |
| | | |
| | | fun loadIncomeData() { |
| | | loadDataByType("收入") |
| | | } |
| | | |
| | | fun loadFlightData() { |
| | | loadDataByType("航班") |
| | | } |
| | | |
| | | fun loadTrainData() { |
| | | loadDataByType("火车票") |
| | | } |
| | | |
| | | fun loadCategories() { |
| | | viewModelScope.launch { |
| | | try { |
| | | // 先尝试从本地获取配置 |
| | | val localCategories = secureStorage.getCategories(currentUserId) |
| | | 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) |
| | | ) |
| | | _categories.value = defaultCategories |
| | | secureStorage.saveCategories(currentUserId, defaultCategories) |
| | | } |
| | | } |
| | | // 更新可见分类 |
| | | _categories.value?.let { updateVisibleCategories(it) } |
| | | } catch (e: Exception) { |
| | | Log.e("HomeViewModel", "Failed to load categories: ${e.message}") |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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>) { |
| | | _categories.value = categories |
| | | // 保存到本地存储 |
| | | secureStorage.saveCategories(currentUserId, categories) |
| | | // 同步到服务器 |
| | | syncCategoriesToServer(categories) |
| | | // 更新可见分类 |
| | | updateVisibleCategories(categories) |
| | | } |
| | | |
| | | private fun updateVisibleCategories(categories: List<CategoryConfig>) { |
| | | val visibleNames = categories |
| | | .filter { it.isEnabled } |
| | | .sortedBy { it.order } |
| | | .map { it.name } |
| | | |
| | | _visibleCategories.value = categories.filter { it.isEnabled } |
| | | } |
| | | |
| | | // 登出时不再清除本地数据 |
| | | fun logout() { |
| | | // 只清除内存中的数据 |
| | | _categories.value = emptyList() |
| | | } |
| | | |
| | | } |