feat: 新增支付和个人资料ViewModel

- 新增PaymentViewModel处理支付和订单逻辑
- 新增ProfileViewModel处理用户资料逻辑
- 更新ViewModelFactory支持新的ViewModel
This commit is contained in:
徐在坤
2026-01-21 18:18:08 +08:00
parent 8a1659176f
commit 81f6c5e759
3 changed files with 193 additions and 3 deletions

View File

@@ -0,0 +1,98 @@
package com.huaga.life_echo.ui.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.huaga.life_echo.data.repository.PaymentRepository
import com.huaga.life_echo.network.models.OrderDto
import com.huaga.life_echo.network.models.PlanDto
import com.huaga.life_echo.network.models.QuotaCheckDto
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
class PaymentViewModel(
private val paymentRepository: PaymentRepository
) : ViewModel() {
private val _plans = MutableStateFlow<List<PlanDto>>(emptyList())
val plans: StateFlow<List<PlanDto>> = _plans
private val _currentPlan = MutableStateFlow<PlanDto?>(null)
val currentPlan: StateFlow<PlanDto?> = _currentPlan
private val _quota = MutableStateFlow<QuotaCheckDto?>(null)
val quota: StateFlow<QuotaCheckDto?> = _quota
private val _orders = MutableStateFlow<List<OrderDto>>(emptyList())
val orders: StateFlow<List<OrderDto>> = _orders
private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<Boolean> = _isLoading
private val _error = MutableStateFlow<String?>(null)
val error: StateFlow<String?> = _error
init {
loadPlans()
loadCurrentPlan()
checkQuota()
loadOrders()
}
fun loadPlans() {
viewModelScope.launch {
_isLoading.value = true
_error.value = null
paymentRepository.getPlans().fold(
onSuccess = { _plans.value = it },
onFailure = { _error.value = it.message }
)
_isLoading.value = false
}
}
fun loadCurrentPlan() {
viewModelScope.launch {
paymentRepository.getCurrentPlan().fold(
onSuccess = { _currentPlan.value = it },
onFailure = { }
)
}
}
fun checkQuota() {
viewModelScope.launch {
paymentRepository.checkQuota().fold(
onSuccess = { _quota.value = it },
onFailure = { }
)
}
}
fun createOrder(planId: String, onSuccess: (OrderDto) -> Unit, onError: (String) -> Unit) {
viewModelScope.launch {
_isLoading.value = true
_error.value = null
paymentRepository.createOrder(planId).fold(
onSuccess = {
_isLoading.value = false
onSuccess(it)
},
onFailure = {
_isLoading.value = false
_error.value = it.message
onError(it.message ?: "创建订单失败")
}
)
}
}
fun loadOrders() {
viewModelScope.launch {
paymentRepository.getOrders().fold(
onSuccess = { _orders.value = it },
onFailure = { }
)
}
}
}

View File

@@ -0,0 +1,74 @@
package com.huaga.life_echo.ui.viewmodel
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.huaga.life_echo.data.repository.ProfileRepository
import com.huaga.life_echo.network.models.FAQDto
import com.huaga.life_echo.network.models.UserProfileDto
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
class ProfileViewModel(
private val profileRepository: ProfileRepository
) : ViewModel() {
private val _userProfile = MutableStateFlow<UserProfileDto?>(null)
val userProfile: StateFlow<UserProfileDto?> = _userProfile
private val _faqs = MutableStateFlow<List<FAQDto>>(emptyList())
val faqs: StateFlow<List<FAQDto>> = _faqs
private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<Boolean> = _isLoading
private val _error = MutableStateFlow<String?>(null)
val error: StateFlow<String?> = _error
init {
loadUserProfile()
loadFAQs()
}
fun loadUserProfile() {
viewModelScope.launch {
_isLoading.value = true
profileRepository.getUserProfile().fold(
onSuccess = {
_userProfile.value = it
_isLoading.value = false
},
onFailure = {
_error.value = it.message
_isLoading.value = false
}
)
}
}
fun loadFAQs() {
viewModelScope.launch {
profileRepository.getFAQs().fold(
onSuccess = { _faqs.value = it },
onFailure = { }
)
}
}
fun submitFeedback(content: String, contact: String?, onSuccess: () -> Unit, onError: (String) -> Unit) {
viewModelScope.launch {
_isLoading.value = true
profileRepository.submitFeedback(content, contact).fold(
onSuccess = {
_isLoading.value = false
onSuccess()
},
onFailure = {
_isLoading.value = false
_error.value = it.message
onError(it.message ?: "提交失败")
}
)
}
}
}

View File

@@ -4,8 +4,7 @@ import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.huaga.life_echo.data.database.AppDatabase
import com.huaga.life_echo.data.repository.ChapterRepository
import com.huaga.life_echo.data.repository.ConversationRepository
import com.huaga.life_echo.data.repository.*
import com.huaga.life_echo.network.ApiService
class ViewModelFactory(private val context: Context) : ViewModelProvider.Factory {
@@ -14,7 +13,8 @@ class ViewModelFactory(private val context: Context) : ViewModelProvider.Factory
private val conversationRepository by lazy {
ConversationRepository(
conversationDao = database.conversationDao(),
segmentDao = database.conversationSegmentDao()
segmentDao = database.conversationSegmentDao(),
apiService = apiService
)
}
private val chapterRepository by lazy {
@@ -24,6 +24,14 @@ class ViewModelFactory(private val context: Context) : ViewModelProvider.Factory
}
private val apiService by lazy { ApiService() }
private val paymentRepository by lazy {
PaymentRepository(apiService = apiService)
}
private val profileRepository by lazy {
ProfileRepository(apiService = apiService)
}
@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return when {
@@ -48,6 +56,16 @@ class ViewModelFactory(private val context: Context) : ViewModelProvider.Factory
modelClass.isAssignableFrom(AuthViewModel::class.java) -> {
AuthViewModel(context = context) as T
}
modelClass.isAssignableFrom(PaymentViewModel::class.java) -> {
PaymentViewModel(
paymentRepository = paymentRepository
) as T
}
modelClass.isAssignableFrom(ProfileViewModel::class.java) -> {
ProfileViewModel(
profileRepository = profileRepository
) as T
}
else -> throw IllegalArgumentException("Unknown ViewModel class: ${modelClass.name}")
}
}