Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paging #630

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Define PagingConfig and Paging Strategies (#619)
Signed-off-by: mramotar <[email protected]>
  • Loading branch information
matt-ramotar committed Mar 16, 2024
commit 705539ea57352c9e1c3979e00e541134d20bcb1b
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.mobilenativefoundation.paging.core

/**
* Represents a strategy for aggregating loaded pages of data into a single instance of [PagingItems].
*
* The [AggregatingStrategy] determines how the loaded pages of data should be combined and ordered to form a coherent list of [PagingData.Single] items.
* It takes into account the anchor position, prefetch position, paging configuration, and the current state of the paging buffer.
*
* @param Id The type of the unique identifier for each item in the paged data.
* @param K The type of the key used for paging.
* @param P The type of the parameters associated with each page of data.
* @param D The type of the data items.
*/
interface AggregatingStrategy<Id : Comparable<Id>, K : Any, P : Any, D : Any> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still prefer Key,Request,Response as the param names :-)


/**
* Aggregates the loaded pages of data into a single instance of [PagingItems].
*
* @param anchorPosition The current anchor position in the paged data.
* @param prefetchPosition The position to prefetch data from, or `null` if no prefetching is needed.
* @param pagingConfig The configuration of the pager, including page size and prefetch distance.
* @param pagingBuffer The current state of the paging buffer, containing the loaded data.
* @return The aggregated list of [PagingItems] representing the combined and ordered paging data.
*/
fun aggregate(
anchorPosition: PagingKey<K, P>,
prefetchPosition: PagingKey<K, P>?,
pagingConfig: PagingConfig,
pagingBuffer: PagingBuffer<Id, K, P, D>,
): PagingItems<Id, K, P, D>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.mobilenativefoundation.paging.core

/**
* Represents different strategies for handling errors during the paging process.
*/
sealed interface ErrorHandlingStrategy {
/**
* Ignores errors and continues with the previous state.
*/
data object Ignore : ErrorHandlingStrategy

/**
* Passes the error to the UI layer for handling.
*/
data object PassThrough : ErrorHandlingStrategy

/**
* Retries the last failed load operation.
*
* @property maxRetries The maximum number of retries before passing the error to the UI. Default is 3.
*/
data class RetryLast(
val maxRetries: Int = 3
) : ErrorHandlingStrategy
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.mobilenativefoundation.paging.core

/**
* Represents a strategy for determining whether to fetch more data based on the current state of the pager.
* The fetching strategy is responsible for deciding whether to fetch more data based on the anchor position,
* prefetch position, paging configuration, and the current state of the paging buffer.
*
* Implementing a custom [FetchingStrategy] allows you to define your own logic for when to fetch more data.
* For example, you can fetch more data when the user scrolls near the end of the currently loaded data, or when a certain number of items are remaining in the buffer.
*
* @param Id The type of the unique identifier for each item in the paged data.
* @param K The type of the key used for paging.
* @param P The type of the parameters associated with each page of data.
* @param D The type of the data items.
*/
interface FetchingStrategy<Id : Comparable<Id>, K : Any, P : Any, D : Any> {

/**
* Determines whether to fetch more data based on the current state of the pager.
* The [shouldFetch] implementation should determine whether more data should be fetched based on the provided parameters.
*
* @param anchorPosition The current anchor position in the paged data.
* @param prefetchPosition The position to prefetch data from, or `null` if no prefetching is needed.
* @param pagingConfig The configuration of the pager, including page size and prefetch distance.
* @param pagingBuffer The current state of the paging buffer, containing the loaded data.
* @return `true` if more data should be fetched, `false` otherwise.
*/
fun shouldFetch(
anchorPosition: PagingKey<K, P>,
prefetchPosition: PagingKey<K, P>?,
pagingConfig: PagingConfig,
pagingBuffer: PagingBuffer<Id, K, P, D>,
): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.mobilenativefoundation.paging.core

/**
* Represents the configuration for paging behavior.
*
* @property pageSize The number of items to load per page.
* @property prefetchDistance The distance from the edge of the loaded data at which to prefetch more data.
* @property insertionStrategy The strategy for inserting new data into the paging buffer.
*/
data class PagingConfig(
val pageSize: Int,
val prefetchDistance: Int,
val insertionStrategy: InsertionStrategy
) {
/**
* Represents different insertion strategies for adding new data to the paging buffer.
*/
enum class InsertionStrategy {
/**
* Appends new data to the end of the buffer.
*/
APPEND,

/**
* Prepends new data to the beginning of the buffer.
*/
PREPEND,

/**
* Replaces the existing data in the buffer with the new data.
*/
REPLACE,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.mobilenativefoundation.paging.core

/**
* Represents a list of paging items.
*
* @param Id The type of the unique identifier for each item in the paged data.
* @param K The type of the key used for paging.
* @param P The type of the parameters associated with each page of data.
* @param D The type of the data items.
* @property data The list of [PagingData.Single] items representing the paging data.
*/
data class PagingItems<Id : Comparable<Id>, K : Any, P : Any, D : Any>(
val data: List<PagingData.Single<Id, K, P, D>>
)
Loading