From dd43f3dbf5063f10f4623345e2df266500491a45 Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 3 Jul 2024 19:27:25 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../com/tangem/pagination/BatchListSource.kt | 36 ++++++++++--------- .../tangem/pagination/BatchUpdateFetcher.kt | 2 +- .../{BatchFetchResult.kt => FetchResult.kt} | 8 ++--- ...chUpdateResult.kt => FetchUpdateResult.kt} | 8 ++--- .../com/tangem/pagination/PaginationStatus.kt | 4 +-- .../tangem/pagination/fetcher/BatchFetcher.kt | 8 ++--- .../fetcher/LimitOffsetBatchFetcher.kt | 14 ++++---- 7 files changed, 42 insertions(+), 38 deletions(-) rename core/pagination/src/main/java/com/tangem/pagination/{BatchFetchResult.kt => FetchResult.kt} (77%) rename core/pagination/src/main/java/com/tangem/pagination/{BatchFetchUpdateResult.kt => FetchUpdateResult.kt} (72%) diff --git a/core/pagination/src/main/java/com/tangem/pagination/BatchListSource.kt b/core/pagination/src/main/java/com/tangem/pagination/BatchListSource.kt index 7021d474c3..cfedfbcbf2 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/BatchListSource.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/BatchListSource.kt @@ -17,13 +17,13 @@ import kotlinx.coroutines.flow.* */ interface BatchListSource { val state: StateFlow> - val updateResults: SharedFlow>> + val updateResults: SharedFlow>> } /** * Creates a new [BatchListSource] with the provided configuration. * - * @param config Configuration for batching. + * @param ioDispatcher Dispatcher for IO operations. * @param context Context for batching. * @param generateNewKey Function to generate a new key for a batch. * @param batchFetcher Function to fetch a batch of data. @@ -32,15 +32,17 @@ interface BatchListSource { */ @Suppress("FunctionNaming") fun BatchListSource( + ioDispatcher : CoroutineDispatcher = Dispatchers.IO, context: BatchingContext, generateNewKey: suspend (List) -> TKey, batchFetcher: BatchFetcher, -): BatchListSource = BatchListSourceImpl(context, generateNewKey, batchFetcher, null) +): BatchListSource = + BatchListSourceImpl(ioDispatcher, context, generateNewKey, batchFetcher, null) /** * Creates a new [BatchListSource] with the provided configuration. * - * @param config Configuration for batching. + * @param ioDispatcher Dispatcher for IO operations. * @param context Context for batching. * @param generateNewKey Function to generate a new key for a batch. * @param batchFetcher Function to fetch a batch of data. @@ -50,14 +52,16 @@ fun BatchListSource( */ @Suppress("FunctionNaming") fun BatchListSource( + ioDispatcher: CoroutineDispatcher = Dispatchers.IO, context: BatchingContext, generateNewKey: suspend (List) -> TKey, batchFetcher: BatchFetcher, updateFetcher: BatchUpdateFetcher, ): BatchListSource = - BatchListSourceImpl(context, generateNewKey, batchFetcher, updateFetcher) + BatchListSourceImpl(ioDispatcher, context, generateNewKey, batchFetcher, updateFetcher) private class BatchListSourceImpl( + private val ioDispatcher: CoroutineDispatcher, private val context: BatchingContext, private val generateNewKey: suspend (List) -> TKey, private val batchFetcher: BatchFetcher, @@ -65,7 +69,7 @@ private class BatchListSourceImpl( ) : BatchListSource { override val state = MutableStateFlow(BatchListState(emptyList(), PaginationStatus.None)) - override val updateResults = MutableSharedFlow>>( + override val updateResults = MutableSharedFlow>>( extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST, ) @@ -75,7 +79,7 @@ private class BatchListSourceImpl( private val waitingUpdateJobs = MutableStateFlow, Job>>>(emptyList()) - private val lastRequestResult = MutableStateFlow?>(null) + private val lastRequestResult = MutableStateFlow?>(null) private var reloadActionJob: Job? = null private var loadMoreActionJob: Job? = null @@ -108,7 +112,7 @@ private class BatchListSourceImpl( loadMoreActionJob?.cancel() reloadActionJob?.cancel() stopAllUpdates() - reloadActionJob = scope.launch(Dispatchers.IO) { + reloadActionJob = scope.launch(ioDispatcher) { reloadTask(action) } } @@ -117,7 +121,7 @@ private class BatchListSourceImpl( return } - loadMoreActionJob = scope.launch(Dispatchers.IO) { + loadMoreActionJob = scope.launch(ioDispatcher) { reloadActionJob?.join() loadMoreTask(action) } @@ -125,7 +129,7 @@ private class BatchListSourceImpl( is BatchAction.UpdateBatches -> { if (updateFetcher == null) return - scope.launch(Dispatchers.IO) { + scope.launch(ioDispatcher) { val job = launch(start = CoroutineStart.LAZY) { updateBatchesTask(action) } @@ -176,9 +180,9 @@ private class BatchListSourceImpl( val res = runCatching { batchFetcher.fetchFirst(action.request) - }.getOrElse { BatchFetchResult.UnknownError(it) } + }.getOrElse { FetchResult.UnknownError(it) } - state.value = if (res is BatchFetchResult.Success) { + state.value = if (res is FetchResult.Success) { val key = generateNewKey(listOf()) val batch = Batch( key = key, @@ -196,7 +200,7 @@ private class BatchListSourceImpl( BatchListState( data = emptyList(), status = PaginationStatus.InitialLoadingError( - error = (res as? BatchFetchResult.Error)?.error, + error = (res as? FetchResult.Error)?.error, ), ) } @@ -216,12 +220,12 @@ private class BatchListSourceImpl( val res = runCatching { batchFetcher.fetchNext(action.request, lastResult) - }.getOrElse { BatchFetchResult.UnknownError(it) } + }.getOrElse { FetchResult.UnknownError(it) } lastRequestResult.value = lastResult state.update { currentState -> - if (res is BatchFetchResult.Success) { + if (res is FetchResult.Success) { val newBatch = Batch( key = generateNewKey(currentState.data.map { it.key }), data = res.data, @@ -254,7 +258,7 @@ private class BatchListSourceImpl( updateRequest = action.request, ) - if (result is BatchFetchUpdateResult.Success) { + if (result is FetchUpdateResult.Success) { state.update { currentState -> val resMap = result.data.associateBy { it.key } currentState.copy( diff --git a/core/pagination/src/main/java/com/tangem/pagination/BatchUpdateFetcher.kt b/core/pagination/src/main/java/com/tangem/pagination/BatchUpdateFetcher.kt index 71ddd43c76..1cf7e10a91 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/BatchUpdateFetcher.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/BatchUpdateFetcher.kt @@ -21,5 +21,5 @@ interface BatchUpdateFetcher { suspend fun fetchUpdate( toUpdate: List>, updateRequest: TUpdate, - ): BatchFetchUpdateResult + ): FetchUpdateResult } \ No newline at end of file diff --git a/core/pagination/src/main/java/com/tangem/pagination/BatchFetchResult.kt b/core/pagination/src/main/java/com/tangem/pagination/FetchResult.kt similarity index 77% rename from core/pagination/src/main/java/com/tangem/pagination/BatchFetchResult.kt rename to core/pagination/src/main/java/com/tangem/pagination/FetchResult.kt index c3b8b03ee2..d72c42ac1d 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/BatchFetchResult.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/FetchResult.kt @@ -7,7 +7,7 @@ package com.tangem.pagination * @param TData type of the data. * @param TError type of the error. */ -sealed class BatchFetchResult { +sealed class FetchResult { /** * Represents a successful result of a batch fetch request. @@ -18,14 +18,14 @@ sealed class BatchFetchResult { data class Success( val data: TData, val last: Boolean = false, - ) : BatchFetchResult() + ) : FetchResult() /** * Represents an error result of a batch fetch request. * * @param error error that occurred during the request. */ - data class Error(val error: TError) : BatchFetchResult() + data class Error(val error: TError) : FetchResult() /** * Represents an unknown error result of a batch fetch request. @@ -34,5 +34,5 @@ sealed class BatchFetchResult { * @param throwable throwable that occurred during the request. * @see com.tangem.pagination.fetcher.BatchFetcher */ - class UnknownError(val throwable: Throwable) : BatchFetchResult() + class UnknownError(val throwable: Throwable) : FetchResult() } \ No newline at end of file diff --git a/core/pagination/src/main/java/com/tangem/pagination/BatchFetchUpdateResult.kt b/core/pagination/src/main/java/com/tangem/pagination/FetchUpdateResult.kt similarity index 72% rename from core/pagination/src/main/java/com/tangem/pagination/BatchFetchUpdateResult.kt rename to core/pagination/src/main/java/com/tangem/pagination/FetchUpdateResult.kt index 7d47ed8142..d575044a1f 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/BatchFetchUpdateResult.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/FetchUpdateResult.kt @@ -8,7 +8,7 @@ package com.tangem.pagination * @param TData type of the data. * @param TError type of the error. */ -sealed class BatchFetchUpdateResult { +sealed class FetchUpdateResult { /** * Represents a successful result of a batch update operation. @@ -17,14 +17,14 @@ sealed class BatchFetchUpdateResult { */ data class Success( val data: List>, - ) : BatchFetchUpdateResult() + ) : FetchUpdateResult() /** * Represents an error result of a batch update operation. * * @param error error that occurred during the operation. */ - data class Error(val error: TError) : BatchFetchUpdateResult() + data class Error(val error: TError) : FetchUpdateResult() /** * Represents an unknown error result of a batch update operation. @@ -32,5 +32,5 @@ sealed class BatchFetchUpdateResult { * * @param throwable throwable that occurred during the operation. */ - class UnknownError(val throwable: Throwable) : BatchFetchUpdateResult() + class UnknownError(val throwable: Throwable) : FetchUpdateResult() } \ No newline at end of file diff --git a/core/pagination/src/main/java/com/tangem/pagination/PaginationStatus.kt b/core/pagination/src/main/java/com/tangem/pagination/PaginationStatus.kt index 054178ce19..ec30a2ea1e 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/PaginationStatus.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/PaginationStatus.kt @@ -34,12 +34,12 @@ sealed class PaginationStatus { /** * Represents that the last batch was loaded and * the source is ready to load the next one or reload previous if [lastResult] is an error. - * For the first batch, [lastResult] is always [BatchFetchResult.Success] + * For the first batch, [lastResult] is always [FetchResult.Success] * * @param lastResult result of the last batch fetch. */ data class Paginating( - val lastResult: BatchFetchResult, + val lastResult: FetchResult, ) : PaginationStatus() /** diff --git a/core/pagination/src/main/java/com/tangem/pagination/fetcher/BatchFetcher.kt b/core/pagination/src/main/java/com/tangem/pagination/fetcher/BatchFetcher.kt index 901d83f32b..2d9e50832e 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/fetcher/BatchFetcher.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/fetcher/BatchFetcher.kt @@ -1,6 +1,6 @@ package com.tangem.pagination.fetcher -import com.tangem.pagination.BatchFetchResult +import com.tangem.pagination.FetchResult import com.tangem.pagination.BatchListState /** @@ -20,7 +20,7 @@ interface BatchFetcher { * @param request initial request. Will be saved to be used in [fetchNext] requests. * @return result of the fetch operation. */ - suspend fun fetchFirst(request: TRequest): BatchFetchResult + suspend fun fetchFirst(request: TRequest): FetchResult /** * Fetches the next batch of data. @@ -32,6 +32,6 @@ interface BatchFetcher { */ suspend fun fetchNext( overrideRequest: TRequest?, - lastResult: BatchFetchResult, - ): BatchFetchResult + lastResult: FetchResult, + ): FetchResult } \ No newline at end of file diff --git a/core/pagination/src/main/java/com/tangem/pagination/fetcher/LimitOffsetBatchFetcher.kt b/core/pagination/src/main/java/com/tangem/pagination/fetcher/LimitOffsetBatchFetcher.kt index 11af45665d..858411b6a6 100644 --- a/core/pagination/src/main/java/com/tangem/pagination/fetcher/LimitOffsetBatchFetcher.kt +++ b/core/pagination/src/main/java/com/tangem/pagination/fetcher/LimitOffsetBatchFetcher.kt @@ -1,6 +1,6 @@ package com.tangem.pagination.fetcher -import com.tangem.pagination.BatchFetchResult +import com.tangem.pagination.FetchResult import com.tangem.pagination.exception.EndOfPaginationException import kotlinx.coroutines.flow.MutableStateFlow @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.MutableStateFlow class LimitOffsetBatchFetcher( private val prefetchDistance: Int, private val batchSize: Int, - private val fetch: (request: Request) -> BatchFetchResult, + private val fetch: (request: Request) -> FetchResult, ) : BatchFetcher { data class Request( @@ -29,7 +29,7 @@ class LimitOffsetBatchFetcher( private val lastRequest = MutableStateFlow?>(null) - override suspend fun fetchFirst(request: TRequest): BatchFetchResult { + override suspend fun fetchFirst(request: TRequest): FetchResult { val req = Request( offset = 0, limit = prefetchDistance, @@ -43,14 +43,14 @@ class LimitOffsetBatchFetcher( override suspend fun fetchNext( overrideRequest: TRequest?, - lastResult: BatchFetchResult, - ): BatchFetchResult { + lastResult: FetchResult, + ): FetchResult { val last = lastRequest.value requireNotNull(last) - val req = if (lastResult is BatchFetchResult.Success) { + val req = if (lastResult is FetchResult.Success) { if (lastResult.last && overrideRequest == null) { - return BatchFetchResult.UnknownError(EndOfPaginationException()) + return FetchResult.UnknownError(EndOfPaginationException()) } Request(