refactor: extract paging helpers
This commit is contained in:
+14
-11
@@ -31,6 +31,10 @@ import dev.meloda.fast.common.extensions.orDots
|
||||
import dev.meloda.fast.common.extensions.removeIfCompat
|
||||
import dev.meloda.fast.common.extensions.setValue
|
||||
import dev.meloda.fast.common.provider.ResourceProvider
|
||||
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.data.State
|
||||
import dev.meloda.fast.data.UserConfig
|
||||
import dev.meloda.fast.data.VkUtils
|
||||
@@ -915,11 +919,7 @@ class MessagesHistoryViewModelImpl(
|
||||
error = ::handleError,
|
||||
success = { response ->
|
||||
val messages = response.messages
|
||||
val fullMessages = if (offset == 0) {
|
||||
messages
|
||||
} else {
|
||||
this.messages.value.plus(messages)
|
||||
}.sorted()
|
||||
val fullMessages = mergePage(this.messages.value, messages, offset).sorted()
|
||||
|
||||
val convos = response.convos
|
||||
|
||||
@@ -930,10 +930,12 @@ class MessagesHistoryViewModelImpl(
|
||||
messagesUseCase.storeMessages(messages)
|
||||
convoUseCase.storeConvos(convos)
|
||||
|
||||
val itemsCountSufficient = messages.size == MESSAGES_LOAD_COUNT
|
||||
|
||||
val paginationExhausted = !itemsCountSufficient &&
|
||||
this.messages.value.isNotEmpty()
|
||||
val itemsCountSufficient = canPaginatePage(MESSAGES_LOAD_COUNT, messages.size)
|
||||
val paginationExhausted = isPaginationExhaustedPage(
|
||||
pageSize = MESSAGES_LOAD_COUNT,
|
||||
loadedCount = messages.size,
|
||||
hasExistingItems = this.messages.value.isNotEmpty()
|
||||
)
|
||||
screenState.setValue { old ->
|
||||
old.copy(isPaginationExhausted = paginationExhausted)
|
||||
}
|
||||
@@ -944,10 +946,11 @@ class MessagesHistoryViewModelImpl(
|
||||
}
|
||||
)
|
||||
|
||||
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