【发布时间】:2023-02-03 02:15:51
【问题描述】:
我试图显示一个加载微调器,但加载状态总是在 compose 函数上显示错误值。
我创建了一个自定义微调器,但它没有显示
@Composable
private fun MainContent(viewModel: SearchJourneyViewModel = hiltViewModel()) {
val state = viewModel.state
Loader(isDialogVisible = state.isLoading)
}
在 viewModel 加载状态正在刷新并返回我需要的值:
@HiltViewModel
class SearchJourneyViewModel @Inject constructor(
private val cityRepository: CityListRepository,
) : ViewModel() {
var state by mutableStateOf(SearchJourneyState().mock())
private set
init {
loadCityList()
}
private fun loadCityList() {
viewModelScope.launch {
cityRepository
.getCityList()
.collect { result ->
when (result) {
is Resource.Success -> {
state =
state.copy(
fromCity = //result,
toCity = //result,
isLoading = false,
error = null
)
}
}
is Resource.Error -> {
state =
state.copy(
fromCity = null,
toCity = null,
isLoading = false,
error = result.message
)
}
is Resource.Loading -> {
state =
state.copy(isLoading = result.isLoading)
}
}
}
}
}
}
这是我的状态:
data class SearchJourneyState(
val cityList: List<City>? = null,
val isLoading: Boolean = false,
val isCityLoading: Boolean = false,
)
【问题讨论】:
标签: android kotlin android-jetpack-compose jetpack