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

Define PagingAction and PagingState #616

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -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<Id : Comparable<Id>, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any> {

/**
* Defines user-initiated actions.
*/
sealed interface User<Id : Comparable<Id>, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any> : PagingAction<Id, K, P, D, E, A> {

/**
* 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<Id : Comparable<Id>, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any>(
val key: PagingKey<K, P>,
) : User<Id, K, P, D, E, A>

/**
* Represents a custom user-initiated action.
*
* @param action The custom action payload.
*/
data class Custom<Id : Comparable<Id>, out K : Any, out P : Any, out D : Any, out E : Any, out A : Any>(
val action: A
) : User<Id, K, P, D, E, A>
}


/**
* 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<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, A : Any>(
val key: PagingKey<K, P>,
) : PagingAction<Id, K, P, D, E, A>

/**
* 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<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, A : Any>(
val params: PagingSource.LoadParams<K, P>,
val data: PagingSource.LoadResult.Data<Id, K, P, D>
) : PagingAction<Id, K, P, D, E, A>

/**
* 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<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, A : Any>(
val params: PagingSource.LoadParams<K, P>,
val error: PagingSource.LoadResult.Error<Id, K, P, D, E>
) : PagingAction<Id, K, P, D, E, A>

}
Original file line number Diff line number Diff line change
@@ -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<Id : Comparable<Id>, out K : Any, out P : Any, out D : Any, out E : Any> {
val currentKey: PagingKey<K, P>
val prefetchPosition: PagingKey<K, P>?

data class Initial<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any>(
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : PagingState<Id, K, P, D, E>

data class Loading<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any>(
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : PagingState<Id, K, P, D, E>

sealed interface Error<Id : Comparable<Id>, out K : Any, out P : Any, out D : Any, out E : Any, out RE : Any> : PagingState<Id, K, P, D, E> {
val error: RE

data class Exception<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any>(
override val error: Throwable,
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : Error<Id, K, P, D, E, Throwable>

data class Custom<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any>(
override val error: E,
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : Error<Id, K, P, D, E, E>
}

sealed interface Data<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any> : PagingState<Id, K, P, D, E> {
val data: List<PagingData.Single<Id, K, P, D>>
val itemsBefore: Int?
val itemsAfter: Int?
val nextKey: PagingKey<K, P>?

data class Idle<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any>(
override val data: List<PagingData.Single<Id, K, P, D>>,
override val itemsBefore: Int?,
override val itemsAfter: Int?,
override val nextKey: PagingKey<K, P>?,
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : Data<Id, K, P, D, E>

data class LoadingMore<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any>(
override val data: List<PagingData.Single<Id, K, P, D>>,
override val itemsBefore: Int?,
override val itemsAfter: Int?,
override val nextKey: PagingKey<K, P>?,
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : Data<Id, K, P, D, E>

data class ErrorLoadingMore<Id : Comparable<Id>, K : Any, P : Any, D : Any, E : Any, RE : Any>(
override val error: RE,
override val data: List<PagingData.Single<Id, K, P, D>>,
override val itemsBefore: Int?,
override val itemsAfter: Int?,
override val nextKey: PagingKey<K, P>?,
override val currentKey: PagingKey<K, P>,
override val prefetchPosition: PagingKey<K, P>?
) : Data<Id, K, P, D, E>, Error<Id, K, P, D, E, RE>
}
}
Loading