From 4209a1f5d57190bb0903c51bdb8bfac968a4f526 Mon Sep 17 00:00:00 2001
From: tj <1378534974@qq.com>
Date: 星期二, 01 四月 2025 10:43:36 +0800
Subject: [PATCH] 1.vip续费页面-VIP会员服务协议
---
app/src/main/java/com/example/firstapp/ui/home/HomeViewModel.kt | 182 +++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 175 insertions(+), 7 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 c8c6c17..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,23 +1,45 @@
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 _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 _categories = MutableLiveData<List<CategoryConfig>>()
+ val categories: LiveData<List<CategoryConfig>> = _categories
+
+ // 添加可见分类的 LiveData
+ private val _visibleCategories = MutableLiveData<List<String>>()
+ val visibleCategories: LiveData<List<String>> = _visibleCategories
+
+ private lateinit var secureStorage: SecureStorage
+ private lateinit var currentUserId: String
init {
// 初始化时加载包裹列表数据
@@ -26,19 +48,27 @@
// loadFinanceData()
}
+ fun initialize(context: Context, userId: String) {
+ secureStorage = SecureStorage(context)
+ currentUserId = userId
+ loadCategories()
+ // 初始化时更新可见分类
+ _categories.value?.let { updateVisibleCategories(it) }
+ }
+
fun loadExpressData() {
viewModelScope.launch {
// 1. 获取所有驿站类型的提醒设置
- val stations = Core.reminder.getByType("驿站")
+ 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.name, //快递公司
+ company = code.secondLevel, //快递公司
trackingNumber = code.code, // 取件码
- createTime = code.createtime //快递时间
+ createTime = code.createTime //快递时间
)
}
ExpressGroup(
@@ -53,16 +83,16 @@
fun loadFinanceData() {
viewModelScope.launch {
// 1. 获取所有驿站类型的提醒设置
- val stations = Core.reminder.getByType("财务")
+ 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.name, //快递公司
+ company = code.secondLevel, //快递公司
trackingNumber = code.code, // 取件码
- createTime = code.createtime //快递时间
+ createTime = code.createTime //快递时间
)
}
FinanceGroup(
@@ -74,4 +104,142 @@
}
}
+ 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