diff --git a/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/PagingAction.kt b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/PagingAction.kt new file mode 100644 index 000000000..04f7e45e7 --- /dev/null +++ b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/PagingAction.kt @@ -0,0 +1,71 @@ +package org.mobilenativefoundation.paging.core + +/** + * Defines the actions that can be dispatched to modify the paging state. + * + * @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. + * @param E The type of errors that can occur during the paging process. + * @param A The type of custom actions that can be dispatched to modify the paging state. + */ +sealed interface PagingAction, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any> { + + /** + * Defines user-initiated actions. + */ + sealed interface User, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any> : PagingAction { + + /** + * Represents a user-initiated action to load data for a specific page key. + * + * @param key The page key to load data for. + */ + data class Load, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any>( + val key: PagingKey, + ) : User + + /** + * Represents a custom user-initiated action. + * + * @param action The custom action payload. + */ + data class Custom, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any>( + val action: A + ) : User + } + + + /** + * Represents an app-initiated action to load data for a specific page key. + * + * @param key The page key to load data for. + */ + data class Load, K : Any, P : Any, D : Any, E : Any, A : Any>( + val key: PagingKey, + ) : PagingAction + + /** + * Represents an app-initiated action to update the paging state with loaded data. + * + * @param params The parameters associated with the loaded data. + * @param data The loaded data. + */ + data class UpdateData, K : Any, P : Any, D : Any, E : Any, A : Any>( + val params: PagingSource.LoadParams, + val data: PagingSource.LoadResult.Data + ) : PagingAction + + /** + * Represents an app-initiated action to update the paging state with an error. + * + * @param params The parameters associated with the error. + * @param error The error that occurred. + */ + data class UpdateError, K : Any, P : Any, D : Any, E : Any, A : Any>( + val params: PagingSource.LoadParams, + val error: PagingSource.LoadResult.Error + ) : PagingAction + +} \ No newline at end of file diff --git a/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/PagingState.kt b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/PagingState.kt new file mode 100644 index 000000000..4754fe59c --- /dev/null +++ b/paging/core/src/commonMain/kotlin/org/mobilenativefoundation/paging/core/PagingState.kt @@ -0,0 +1,79 @@ +package org.mobilenativefoundation.paging.core + +/** + * Represents the current state of the paging data. + * + * The paging state can be in different stages, such as [Initial], [Loading], [Error], or [Data]. + * It can contain the current key, prefetch position, errors, and data, such as loaded items and the next key. + * + * @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. + * @param E The type of errors that can occur during the paging process. + */ +sealed interface PagingState, out K : Any, out P : Any, out D : Any, out E : Any> { + val currentKey: PagingKey + val prefetchPosition: PagingKey? + + data class Initial, K : Any, P : Any, D : Any, E : Any>( + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : PagingState + + data class Loading, K : Any, P : Any, D : Any, E : Any>( + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : PagingState + + sealed interface Error, out K : Any, out P : Any, out D : Any, out E : Any, out RE : Any> : PagingState { + val error: RE + + data class Exception, K : Any, P : Any, D : Any, E : Any>( + override val error: Throwable, + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : Error + + data class Custom, K : Any, P : Any, D : Any, E : Any>( + override val error: E, + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : Error + } + + sealed interface Data, K : Any, P : Any, D : Any, E : Any> : PagingState { + val data: List> + val itemsBefore: Int? + val itemsAfter: Int? + val nextKey: PagingKey? + + data class Idle, K : Any, P : Any, D : Any, E : Any>( + override val data: List>, + override val itemsBefore: Int?, + override val itemsAfter: Int?, + override val nextKey: PagingKey?, + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : Data + + data class LoadingMore, K : Any, P : Any, D : Any, E : Any>( + override val data: List>, + override val itemsBefore: Int?, + override val itemsAfter: Int?, + override val nextKey: PagingKey?, + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : Data + + data class ErrorLoadingMore, K : Any, P : Any, D : Any, E : Any, RE : Any>( + override val error: RE, + override val data: List>, + override val itemsBefore: Int?, + override val itemsAfter: Int?, + override val nextKey: PagingKey?, + override val currentKey: PagingKey, + override val prefetchPosition: PagingKey? + ) : Data, Error + } +} \ No newline at end of file