| | |
| | | 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 |
| | | |
| | | class HomeViewModel : ViewModel() { |
| | |
| | | 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}") |
| | | } |