如何用kotlin流实施分页
我是Kotlin Flow的新手。我正在关注 this 在Android中设置UI层。
这是我的UI状态流,
private val _teamsUiState = MutableStateFlow(TeamsUiState())
val teamsUiState: StateFlow<TeamsUiState> = _teamsUiState.asStateFlow()
这是我的TeamSuistate数据类别:
data class TeamsUiState(
val teamItems: ArrayList<Team> = ArrayList(),
val error: String? = null
)
我正在向API中的API响应,并像这样处理它,
fetchTeamsJob = viewModelScope.launch {
fetchTeamsJob?.cancel()
response.collectLatest {
when (it) {
is Result.Success -> {
it.data?.payload?.let { payload ->
if (payload.teams != null) {
_teamsUiState.update { teamsState ->
// Problem here
teamsState.copy(teamItems = ArrayList(payload.teams))
}
}
val totalCount = payload.total_count?.toInt() ?: 0
val listSize = teamsUiState.value.teamItems.size
teamsNextPageAvailable = (listSize >= totalCount).not()
}
}
is Result.Loading -> {
toggleTeamProgress(isRefresh)
}
is Result.Error -> {
_showToast.emit(
it.exception.message ?: getApplication<Application>().getString(
R.string.something_wrong_try_again
)
)
toggleTeamProgress(isRefresh)
}
}
}
并且在片段中,我会这样观察到这样的片段:
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
teamsAdapter.submitList(it.teamItems)
}
}
由于我有分页,我需要附加团队。列表到已经存在的列表。
我唯一可以做到的方法是做这样的事情:
_teamsUiState.update { teamsState ->
val temp = ArrayList(teamsState.teamItems)
temp.addAll(payload.teams)
teamsState.copy(teamItems = temp)
}
有没有更好的方法来实现我想要的东西而不使用temp
变量?
I am new to kotlin flows. I am following this to set up the UI layer in Android.
This is my UI stateFlow
private val _teamsUiState = MutableStateFlow(TeamsUiState())
val teamsUiState: StateFlow<TeamsUiState> = _teamsUiState.asStateFlow()
This is my TeamsUiState data class:
data class TeamsUiState(
val teamItems: ArrayList<Team> = ArrayList(),
val error: String? = null
)
I am fetching the response from the API on a coroutine and handling it like this
fetchTeamsJob = viewModelScope.launch {
fetchTeamsJob?.cancel()
response.collectLatest {
when (it) {
is Result.Success -> {
it.data?.payload?.let { payload ->
if (payload.teams != null) {
_teamsUiState.update { teamsState ->
// Problem here
teamsState.copy(teamItems = ArrayList(payload.teams))
}
}
val totalCount = payload.total_count?.toInt() ?: 0
val listSize = teamsUiState.value.teamItems.size
teamsNextPageAvailable = (listSize >= totalCount).not()
}
}
is Result.Loading -> {
toggleTeamProgress(isRefresh)
}
is Result.Error -> {
_showToast.emit(
it.exception.message ?: getApplication<Application>().getString(
R.string.something_wrong_try_again
)
)
toggleTeamProgress(isRefresh)
}
}
}
And in the fragment I am observing it like this:
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
teamsAdapter.submitList(it.teamItems)
}
}
Since I have pagination, I need to append the teams list to the already existing list.
The only way i could do this is by doing something like this:
_teamsUiState.update { teamsState ->
val temp = ArrayList(teamsState.teamItems)
temp.addAll(payload.teams)
teamsState.copy(teamItems = temp)
}
Is there a better way to achieve what I want without using a temp
variable?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想有很多方法可以完成这种理想的行为,但是很多锅炉板也会随附,我认为这里最好的方法是实施分页3,以便您可以以一种非常简单,有效的方式处理分页。来源:
如果您对我使用此库进行的编码任务项目有疑问,您可以随时查看:
I guess there will be many ways to get this desired behaviour done, but a lot of boiler plate will come with as well, I think the best approach here would be to implement Paging 3 so you can handle pagination in a very easy and efficient way. Sources:
You can always have a look if you have doubts at a coding task project I made with this library:
您可以在
_teamSuistate
上使用getAndupDate {}
。You could use
getAndUpdate {}
on_teamsUiState
.