【发布时间】:2021-09-07 12:29:19
【问题描述】:
我正在构建一个应用程序,它有很多输入字段,所以对于这些输入字段,我必须在我的 viewModel 中管理很多状态。
因此,我的 viewModel 一团糟。谁能指导我用干净的代码有效地处理这些状态。
我使用的状态管理模式在android官方文档中有提到。
我的 viewModel 看起来像这样-
@HiltViewModel
class CardsViewModel @Inject constructor(
private val repository: CardsRoomRepository
): ViewModel() {
private val _results = MutableStateFlow<List<CardsItems>>(emptyList())
val results: StateFlow<List<CardsItems>> = _results
private val _resultsForFavorites = MutableStateFlow<List<CardsItems>>(emptyList())
val resultsForFavorites: StateFlow<List<CardsItems>> = _resultsForFavorites
private val _resultsForSearch = MutableStateFlow<List<CardsItems>>(emptyList())
val resultsForSearch: StateFlow<List<CardsItems>> = _resultsForSearch
private val _switch = mutableStateOf(false)
var switch: State<Boolean> = _switch
fun setSwitch(newText: Boolean) {
_switch.value = newText
}
private val _searchQuery = mutableStateOf("")
val searchQuery: State<String> = _searchQuery
fun setSearchQuery(newText: String){
_searchQuery.value = newText
}
init {
getAllLoginsItems()
getAllFavoriteCardsItems()
}
private val _title = mutableStateOf("")
var title: State<String> = _title
fun setTitle(newText: String) {
_title.value = newText
}
private val _category = mutableStateOf(0)
val category: State<Int> = _category
fun setCategory(newText: Int) {
_category.value = newText
}
private val _cardNumber = mutableStateOf("")
val cardNumber: State<String> = _cardNumber
fun setCardNumber(newText: String) {
_cardNumber.value = newText
}
private val _cardHolderName = mutableStateOf("")
var cardHolderName: State<String> = _cardHolderName
fun setCardHolderName(newText: String) {
_cardHolderName.value = newText
}
private val _pinNumber = mutableStateOf("")
var pinNumber: State<String> = _pinNumber
fun setPinNumber(newText: String) {
_pinNumber.value = newText
}
private val _cvvNumber = mutableStateOf("")
var cvvNumber: State<String> = _cvvNumber
fun setCVVNumber(newText: String) {
_cvvNumber.value = newText
}
private val _issueDate = mutableStateOf("")
var issueDate: State<String> = _issueDate
fun setIssueDate(newText: String) {
_issueDate.value = newText
}
private val _expiryDate = mutableStateOf("")
var expiryDate: State<String> = _expiryDate
fun setExpiryDate(newText: String) {
_expiryDate.value = newText
}
....
}
和
@HiltViewModel
class OthersViewModel @Inject constructor(
private val repository: OthersRoomRepository
) : ViewModel() {
private val _results = MutableStateFlow<List<OthersItems>>(emptyList())
val results: StateFlow<List<OthersItems>> = _results
private val _resultsForFavorites = MutableStateFlow<List<OthersItems>>(emptyList())
val resultsForFavorites: StateFlow<List<OthersItems>> = _resultsForFavorites
private val _resultsForSearch = MutableStateFlow<List<OthersItems>>(emptyList())
val resultsForSearch: StateFlow<List<OthersItems>> = _resultsForSearch
private val _switch = mutableStateOf(false)
var switch: State<Boolean> = _switch
fun setSwitch(newText: Boolean) {
_switch.value = newText
}
private val _searchQuery = mutableStateOf("")
val searchQuery: State<String> = _searchQuery
fun setSearchQuery(newText: String) {
_searchQuery.value = newText
}
init {
getAllOthersItems()
getAllFavoriteOthersItems()
}
private val _title = mutableStateOf("")
var title: State<String> = _title
fun setTitle(newText: String) {
_title.value = newText
}
private val _category = mutableStateOf(0)
val category: State<Int> = _category
fun setCategory(newText: Int) {
_category.value = newText
}
private val _userName = mutableStateOf("")
val userName: State<String> = _userName
fun setUserName(newText: String) {
_userName.value = newText
}
private val _password = mutableStateOf("")
val password: State<String> = _password
fun setPassword(newText: String) {
_password.value = newText
}
private val _description = mutableStateOf("")
val description: State<String> = _description
fun setDescription(newText: String) {
_description.value = newText
}
private val _macAddress = mutableStateOf("")
val macAddress: State<String> = _macAddress
fun setMacAddress(newText: String) {
_macAddress.value = newText
}
....
}
【问题讨论】:
-
它们都属于同一个屏幕吗?如果没有,您可以先按屏幕分隔它们
-
@PhilipDukhov 是的,它们都属于同一个屏幕。在我的 viewModel 中,我有输入字段、警报框等的状态。
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。
标签: android coding-style viewmodel android-jetpack-compose