From 225c06550ae8a07c4ae0c6468dcc79f7f512fc64 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Feb 2023 10:23:52 +0600 Subject: [PATCH] fixes in FileKeyValueRepo --- .../micro_utils/repos/FileKeyValueRepo.kt | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt index ef9ee50b713..92909c7949f 100644 --- a/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt +++ b/repos/common/src/jvmMain/kotlin/dev/inmo/micro_utils/repos/FileKeyValueRepo.kt @@ -32,11 +32,18 @@ class FileReadKeyValueRepo( val count = count() val resultPagination = if (reversed) pagination.reverse(count) else pagination val filesList = folder.list() - val filesPaths = filesList.copyOfRange(resultPagination.firstIndex, resultPagination.lastIndexExclusive.coerceAtMost(filesList.size)) ?: return emptyPaginationResult() - if (reversed) { - filesPaths.reverse() + val files: Array = if (resultPagination.firstIndex < count) { + val filesPaths = filesList.copyOfRange(resultPagination.firstIndex, resultPagination.lastIndexExclusive.coerceAtMost(filesList.size)) + + if (reversed) { + filesPaths.reversedArray() + } else { + filesPaths + } + } else { + emptyArray() } - return filesPaths.map { File(folder, it) }.createPaginationResult( + return files.map { File(folder, it) }.createPaginationResult( resultPagination, count ) @@ -46,11 +53,20 @@ class FileReadKeyValueRepo( val count = count() val resultPagination = if (reversed) pagination.reverse(count) else pagination val filesList = folder.list() - val filesPaths = filesList.copyOfRange(resultPagination.firstIndex, resultPagination.lastIndexExclusive.coerceAtMost(filesList.size)) ?: return emptyPaginationResult() - if (reversed) { - filesPaths.reverse() + + val files: Array = if (resultPagination.firstIndex < count) { + val filesPaths = filesList.copyOfRange(resultPagination.firstIndex, resultPagination.lastIndexExclusive.coerceAtMost(filesList.size)) + + if (reversed) { + filesPaths.reversedArray() + } else { + filesPaths + } + } else { + emptyArray() } - return filesPaths.toList().createPaginationResult( + + return files.toList().createPaginationResult( resultPagination, count )