Skip to content

Commit

Permalink
Merge pull request #8 from matt-ramotar/core
Browse files Browse the repository at this point in the history
Simplify the core API
  • Loading branch information
matt-ramotar authored Jul 21, 2024
2 parents b83ca75 + 5368e8b commit a89c21f
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 92 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ class TimelinePagingSource : PagingSource<Cursor, TimelineRequest, Post> {
}
```

4. Build a `PagingScope`:
4. Build a `Pager`:

```kotlin
PagingScope
Pager
.builder<Cursor, TimelineRequest, Post>(pagingConfig)
.setIdExtractor(idExtractor)
.setPagingSource(TimelinePagingSource())
Expand All @@ -81,13 +81,13 @@ PagingScope
.build()
```

5. Provide the `PagingScope` to the compose environment at the appropriate level in the composition hierarchy:
5. Provide the `Pager` to the compose environment at the appropriate level in the composition hierarchy:

```kotlin
class TimelineScreenUi(private val pagingScope: PagingScope) : Ui<TimelineScreen.State> {
class TimelineScreenUi(private val pager: Pager) : Ui<TimelineScreen.State> {
@Composable
override fun Content(state: TimelineScreen.State, modifier: Modifier) {
PagingScope(pagingScope) {
PagingScope(pager) {
LazyUpdatingItems(state.ids, modifier) { model: UpdatingItem<Cursor, Post> ->
TimelinePostUi(model)
}
Expand All @@ -101,16 +101,15 @@ class TimelineScreenUi(private val pagingScope: PagingScope) : Ui<TimelineScreen
```kotlin
class TimelineScreenPresenter(
private val pager: Pager<Cursor>,
private val dispatcher: Dispatcher<GetFeedRequest>
) : Presenter<TimelineScreen.State> {
@Composable
override fun present(): TimelineScreen.State {
val pagingState = pager.flow.collectAsState()
val pagingState = pager.collectAsState()
return TimelineScreen.State(
ids = pagingState.ids,
eventSink = { event ->
when (event) {
TimelineScreen.Event.Refresh -> dispatcher.dispatch(Action.refresh())
TimelineScreen.Event.Refresh -> pager.dispatch(Action.refresh())
}
}
)
Expand Down Expand Up @@ -174,21 +173,21 @@ class SortForTimeRange(private val timeRange: TimeRange) :
class TimelineScreenPresenter(...) : Presenter<TimelineScreen.State> {
@Composable
override fun present(): TimelineScreen.State {
val pagingState = pager.flow.collectAsState()
val pagingState = pager.collectAsState()
var sortingMethod by remember { mutableStateOf<SortingMethod>(SortingMethod.New) }

LaunchedEffect(sortingMethod) {
val operation = when (sortingMethod) {
is Top -> SortForTimeRange(operation.timeRange)
}
dispatcher.dispatch(Action.UpdateOperations(operation))
pager.dispatch(Action.UpdateOperations(operation))
}

return TimelineScreen.State(
ids = pagingState.ids,
eventSink = { event ->
when (event) {
TimelineScreen.Event.Refresh -> dispatcher.dispatch(Action.refresh())
TimelineScreen.Event.Refresh -> pager.dispatch(Action.refresh())
TimelineEvent.UpdateSort -> sortingMethod = event.sortingMethod
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package org.mobilenativefoundation.storex.paging.runtime

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.StateFlow
import org.mobilenativefoundation.store.store5.Updater
import org.mobilenativefoundation.storex.paging.custom.FetchingStrategy
import org.mobilenativefoundation.storex.paging.custom.LaunchEffect
import org.mobilenativefoundation.storex.paging.custom.Middleware
import org.mobilenativefoundation.storex.paging.custom.SideEffect
import org.mobilenativefoundation.storex.paging.persistence.api.ItemPersistence
import org.mobilenativefoundation.storex.paging.persistence.api.PagePersistence
import org.mobilenativefoundation.storex.paging.runtime.internal.pagingScope.impl.PagerBuilder

interface Pager<ItemId : Any, PageRequestKey : Any, ItemValue : Any> {
val state: StateFlow<PagingState<ItemId, PageRequestKey, ItemValue>>
suspend fun dispatch(action: Action<ItemId, PageRequestKey, ItemValue>)
suspend fun getUpdatingItem(id: ItemId): UpdatingItem<ItemId, ItemValue>

interface Builder<ItemId : Any, PageRequestKey : Any, ItemValue : Any> {
fun setInitialState(state: PagingState<ItemId, PageRequestKey, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setInitialLoadParams(params: PagingSource.LoadParams<PageRequestKey>): Builder<ItemId, PageRequestKey, ItemValue>
fun setPagingSource(source: PagingSource<ItemId, PageRequestKey, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setCoroutineDispatcher(dispatcher: CoroutineDispatcher): Builder<ItemId, PageRequestKey, ItemValue>
fun addLaunchEffect(effect: LaunchEffect): Builder<ItemId, PageRequestKey, ItemValue>
fun addSideEffect(effect: SideEffect<ItemId, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun addMiddleware(mw: Middleware<PageRequestKey>): Builder<ItemId, PageRequestKey, ItemValue>
fun setInitialFetchingState(state: FetchingState<ItemId, PageRequestKey>): Builder<ItemId, PageRequestKey, ItemValue>
fun setFetchingStrategy(strategy: FetchingStrategy<ItemId, PageRequestKey, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setErrorHandlingStrategy(strategy: ErrorHandlingStrategy): Builder<ItemId, PageRequestKey, ItemValue>
fun setItemMemoryCache(cache: MutableMap<ItemId, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setPageMemoryCache(cache: MutableMap<PageRequestKey, PagingSource.LoadResult.Data<ItemId, PageRequestKey, ItemValue>>): Builder<ItemId, PageRequestKey, ItemValue>
fun setItemPersistence(persistence: ItemPersistence<ItemId, PageRequestKey, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setPagePersistence(persistence: PagePersistence<ItemId, PageRequestKey, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setItemUpdater(updater: Updater<ItemId, ItemValue, *>): Builder<ItemId, PageRequestKey, ItemValue>
fun setPlaceholderFactory(placeholderFactory: PlaceholderFactory<ItemId, PageRequestKey, ItemValue>): Builder<ItemId, PageRequestKey, ItemValue>
fun setInitialOperations(operations: List<Operation<ItemId, PageRequestKey, ItemValue>>): Builder<ItemId, PageRequestKey, ItemValue>
fun build(): Pager<ItemId, PageRequestKey, ItemValue>
}

companion object {
fun <Id : Any, K : Any, V : Any> builder(
pagingConfig: PagingConfig<Id, K>
): Builder<Id, K, V> = PagerBuilder(pagingConfig)
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ import kotlinx.coroutines.launch
import org.mobilenativefoundation.storex.paging.custom.FetchingStrategy
import org.mobilenativefoundation.storex.paging.custom.LaunchEffect
import org.mobilenativefoundation.storex.paging.runtime.Action
import org.mobilenativefoundation.storex.paging.runtime.Dispatcher
import org.mobilenativefoundation.storex.paging.runtime.FetchingState
import org.mobilenativefoundation.storex.paging.runtime.LoadDirection
import org.mobilenativefoundation.storex.paging.runtime.Pager
import org.mobilenativefoundation.storex.paging.runtime.PagingSource
import org.mobilenativefoundation.storex.paging.runtime.PagingState
import org.mobilenativefoundation.storex.paging.runtime.RecompositionMode
import org.mobilenativefoundation.storex.paging.runtime.UpdatingItem
import org.mobilenativefoundation.storex.paging.runtime.UpdatingItemProvider
import org.mobilenativefoundation.storex.paging.runtime.internal.logger.api.PagingLogger
import org.mobilenativefoundation.storex.paging.runtime.internal.pager.api.FetchingStateHolder
import org.mobilenativefoundation.storex.paging.runtime.internal.pager.api.LoadParamsQueue
Expand Down Expand Up @@ -47,7 +50,9 @@ internal class RealPager<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(
private val queueManager: QueueManager<PageRequestKey>,
private val loadingHandler: LoadingHandler<ItemId, PageRequestKey, ItemValue>,
private val coroutineScope: CoroutineScope,
private val mutableOperationPipeline: MutableOperationPipeline<ItemId, PageRequestKey, ItemValue>
private val mutableOperationPipeline: MutableOperationPipeline<ItemId, PageRequestKey, ItemValue>,
private val dispatcher: Dispatcher<ItemId, PageRequestKey, ItemValue>,
private val updatingItemProvider: UpdatingItemProvider<ItemId, ItemValue>
) : Pager<ItemId, PageRequestKey, ItemValue> {

init {
Expand All @@ -63,6 +68,14 @@ internal class RealPager<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(
pagingState(actions)
}

override suspend fun getUpdatingItem(id: ItemId): UpdatingItem<ItemId, ItemValue> {
return updatingItemProvider.get(id)
}

override suspend fun dispatch(action: Action<ItemId, PageRequestKey, ItemValue>) {
dispatcher.dispatch(action)
}

/**
* Composable function that manages the paging state.
* @param actions Flow of [Action] objects.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import org.mobilenativefoundation.storex.paging.runtime.IdExtractor
import org.mobilenativefoundation.storex.paging.runtime.LoadDirection
import org.mobilenativefoundation.storex.paging.runtime.LoadStrategy
import org.mobilenativefoundation.storex.paging.runtime.Operation
import org.mobilenativefoundation.storex.paging.runtime.Pager
import org.mobilenativefoundation.storex.paging.runtime.PagingConfig
import org.mobilenativefoundation.storex.paging.runtime.PagingScope
import org.mobilenativefoundation.storex.paging.runtime.PagingSource
import org.mobilenativefoundation.storex.paging.runtime.PagingState
import org.mobilenativefoundation.storex.paging.runtime.PlaceholderFactory
Expand Down Expand Up @@ -55,9 +55,9 @@ import org.mobilenativefoundation.storex.paging.runtime.internal.store.impl.Real
import org.mobilenativefoundation.storex.paging.runtime.internal.updatingItem.impl.RealUpdatingItemFactory
import org.mobilenativefoundation.storex.paging.runtime.internal.updatingItem.impl.RealUpdatingItemProvider

class PagingScopeBuilder<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(
class PagerBuilder<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(
private val pagingConfig: PagingConfig<ItemId, PageRequestKey>,
) : PagingScope.Builder<ItemId, PageRequestKey, ItemValue> {
) : Pager.Builder<ItemId, PageRequestKey, ItemValue> {

private val logger = RealPagingLogger(pagingConfig.logging)
private val actionsFlow = MutableSharedFlow<Action<ItemId, PageRequestKey, ItemValue>>(replay = 20)
Expand Down Expand Up @@ -111,7 +111,7 @@ class PagingScopeBuilder<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(
override fun setErrorHandlingStrategy(strategy: ErrorHandlingStrategy) =
apply { errorHandlingStrategy = strategy }

override fun setPlaceholderFactory(placeholderFactory: PlaceholderFactory<ItemId, PageRequestKey, ItemValue>): PagingScope.Builder<ItemId, PageRequestKey, ItemValue> =
override fun setPlaceholderFactory(placeholderFactory: PlaceholderFactory<ItemId, PageRequestKey, ItemValue>): Pager.Builder<ItemId, PageRequestKey, ItemValue> =
apply {
this.placeholderFactory = placeholderFactory
}
Expand All @@ -128,12 +128,12 @@ class PagingScopeBuilder<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(

override fun setItemUpdater(updater: Updater<ItemId, ItemValue, *>) = apply { itemUpdater = updater }

override fun setInitialOperations(operations: List<Operation<ItemId, PageRequestKey, ItemValue>>): PagingScope.Builder<ItemId, PageRequestKey, ItemValue> =
override fun setInitialOperations(operations: List<Operation<ItemId, PageRequestKey, ItemValue>>): Pager.Builder<ItemId, PageRequestKey, ItemValue> =
apply {
this.initialOperations = operations.toMutableList()
}

override fun build(): PagingScope<ItemId, PageRequestKey, ItemValue> {
override fun build(): Pager<ItemId, PageRequestKey, ItemValue> {
val fetchingStateHolder = ConcurrentFetchingStateHolder(initialFetchingState, itemIdComparator, pageRequestKeyComparator)

val dataPersistence = RealDataPersistence(itemPersistence, pagePersistence)
Expand Down Expand Up @@ -182,14 +182,12 @@ class PagingScopeBuilder<ItemId : Any, PageRequestKey : Any, ItemValue : Any>(
queueManager = queueManager,
loadingHandler = loadingHandler,
coroutineScope = coroutineScope,
mutableOperationPipeline = mutableOperationPipeline
)

return RealPagingScope(
pager = pager,
mutableOperationPipeline = mutableOperationPipeline,
dispatcher = dispatcher,
updatingItemProvider = updatingItemProvider
)

return pager
}

private fun buildStore(
Expand Down

This file was deleted.

0 comments on commit a89c21f

Please sign in to comment.