refactor: extract paging helpers
This commit is contained in:
@@ -7,6 +7,10 @@ import coil.ImageLoader
|
||||
import coil.request.ImageRequest
|
||||
import dev.meloda.fast.common.extensions.listenValue
|
||||
import dev.meloda.fast.common.extensions.setValue
|
||||
import dev.meloda.fast.common.paging.canPaginate as canPaginatePage
|
||||
import dev.meloda.fast.common.paging.isPaginationExhausted as isPaginationExhaustedPage
|
||||
import dev.meloda.fast.common.paging.loadingFlags
|
||||
import dev.meloda.fast.common.paging.mergePage
|
||||
import dev.meloda.fast.convos.model.CreateChatScreenState
|
||||
import dev.meloda.fast.data.State
|
||||
import dev.meloda.fast.data.UserConfig
|
||||
@@ -154,13 +158,16 @@ class CreateChatViewModel(
|
||||
friendsUseCase.getFriends(count = LOAD_COUNT, offset = offset)
|
||||
.listenValue(viewModelScope) { state ->
|
||||
state.processState(
|
||||
error = ::handleError,
|
||||
success = { response ->
|
||||
val itemsCountSufficient = response.size == LOAD_COUNT
|
||||
_canPaginate.setValue { itemsCountSufficient }
|
||||
error = ::handleError,
|
||||
success = { response ->
|
||||
val itemsCountSufficient = canPaginatePage(LOAD_COUNT, response.size)
|
||||
_canPaginate.setValue { itemsCountSufficient }
|
||||
|
||||
val paginationExhausted = !itemsCountSufficient &&
|
||||
screenState.value.friends.isNotEmpty()
|
||||
val paginationExhausted = isPaginationExhaustedPage(
|
||||
pageSize = LOAD_COUNT,
|
||||
loadedCount = response.size,
|
||||
hasExistingItems = screenState.value.friends.isNotEmpty()
|
||||
)
|
||||
|
||||
val imagesToPreload =
|
||||
response.mapNotNull { it.photo100.takeIf { p -> !p.isNullOrEmpty() } }
|
||||
@@ -182,24 +189,19 @@ class CreateChatViewModel(
|
||||
val newState = screenState.value.copy(
|
||||
isPaginationExhausted = paginationExhausted
|
||||
)
|
||||
if (offset == 0) {
|
||||
_screenState.setValue {
|
||||
newState.copy(friends = loadedFriends)
|
||||
}
|
||||
} else {
|
||||
_screenState.setValue {
|
||||
newState.copy(
|
||||
friends = newState.friends.plus(loadedFriends)
|
||||
)
|
||||
}
|
||||
_screenState.setValue {
|
||||
newState.copy(
|
||||
friends = mergePage(newState.friends, loadedFriends, offset)
|
||||
)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
val flags = loadingFlags(offset, state.isLoading())
|
||||
_screenState.setValue { old ->
|
||||
old.copy(
|
||||
isLoading = offset == 0 && state.isLoading(),
|
||||
isPaginating = offset > 0 && state.isLoading()
|
||||
isLoading = flags.isLoading,
|
||||
isPaginating = flags.isPaginating
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user