refactor: extract paging helpers
This commit is contained in:
+17
-15
@@ -9,6 +9,10 @@ import dev.meloda.fast.chatmaterials.navigation.ChatMaterials
|
||||
import dev.meloda.fast.chatmaterials.util.asPresentation
|
||||
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.data.State
|
||||
import dev.meloda.fast.data.VkUtils
|
||||
import dev.meloda.fast.data.processState
|
||||
@@ -89,11 +93,14 @@ class ChatMaterialsViewModelImpl(
|
||||
state.processState(
|
||||
error = ::handleError,
|
||||
success = { response ->
|
||||
val itemsCountSufficient = response.size == LOAD_COUNT
|
||||
val itemsCountSufficient = canPaginatePage(LOAD_COUNT, response.size)
|
||||
canPaginate.setValue { itemsCountSufficient }
|
||||
|
||||
val paginationExhausted = !itemsCountSufficient
|
||||
&& screenState.value.materials.isNotEmpty()
|
||||
val paginationExhausted = isPaginationExhaustedPage(
|
||||
pageSize = LOAD_COUNT,
|
||||
loadedCount = response.size,
|
||||
hasExistingItems = screenState.value.materials.isNotEmpty()
|
||||
)
|
||||
|
||||
val loadedMaterials = response.mapNotNull(VkAttachmentHistoryMessage::asPresentation)
|
||||
|
||||
@@ -107,24 +114,19 @@ class ChatMaterialsViewModelImpl(
|
||||
}
|
||||
)
|
||||
|
||||
if (offset == 0) {
|
||||
screenState.setValue {
|
||||
newState.copy(materials = loadedMaterials)
|
||||
}
|
||||
} else {
|
||||
screenState.setValue {
|
||||
newState.copy(
|
||||
materials = newState.materials.plus(loadedMaterials)
|
||||
)
|
||||
}
|
||||
screenState.setValue {
|
||||
newState.copy(
|
||||
materials = mergePage(newState.materials, loadedMaterials, 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