-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matt Ramotar <[email protected]>
- Loading branch information
1 parent
dd6458c
commit 8514305
Showing
7 changed files
with
137 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
experimental/market/src/commonMain/kotlin/org/mobilenativefoundation/market/MarketPager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.mobilenativefoundation.market | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.launch | ||
import org.mobilenativefoundation.storex.paging.Identifiable | ||
import org.mobilenativefoundation.storex.paging.Pager | ||
import org.mobilenativefoundation.storex.paging.PagerBuilder | ||
import org.mobilenativefoundation.storex.paging.StoreX | ||
|
||
|
||
interface MarketPager<Id : Comparable<Id>> { | ||
|
||
fun contribute(anchorPosition: Flow<Id>) | ||
|
||
companion object { | ||
fun <Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any, A : Market.Action, D : Market.Dispatcher<A>> from( | ||
coroutineDispatcher: CoroutineDispatcher, | ||
marketDispatcher: D, | ||
actionFactory: (state: StoreX.Paging.State<Id, K, V, E>) -> A, | ||
pagerBuilder: PagerBuilder<Id, K, V, E>.() -> Pager<Id, K, V, E> | ||
): MarketPager<Id> { | ||
return RealMarketPager( | ||
coroutineDispatcher, pagerBuilder(PagerBuilder(coroutineDispatcher)), marketDispatcher, actionFactory | ||
) | ||
} | ||
} | ||
} | ||
|
||
class RealMarketPager<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any, A : Market.Action, D : Market.Dispatcher<A>>( | ||
coroutineDispatcher: CoroutineDispatcher, | ||
private val pager: Pager<Id, K, V, E>, | ||
private val marketDispatcher: D, | ||
private val actionFactory: (state: StoreX.Paging.State<Id, K, V, E>) -> A | ||
) : MarketPager<Id> { | ||
|
||
private val coroutineScope = CoroutineScope(coroutineDispatcher) | ||
|
||
override fun contribute(anchorPosition: Flow<Id>) { | ||
coroutineScope.launch { | ||
|
||
pager(anchorPosition) | ||
pager.flow.collect { | ||
val action = actionFactory(it) | ||
marketDispatcher.dispatch(action) | ||
} | ||
} | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
...mental/market/src/commonMain/kotlin/org/mobilenativefoundation/market/MemoizedSelector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...rimental/market/src/commonMain/kotlin/org/mobilenativefoundation/market/StatefulMarket.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.mobilenativefoundation.market | ||
|
||
import org.mobilenativefoundation.storex.paging.Identifiable | ||
|
||
interface StatefulMarket<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any, S : StatefulMarket.State<Id, K, V, E>> : | ||
Market<S> { | ||
|
||
interface State<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any> : Market.State { | ||
val subStates: Map<String, SubState<Id, K, V, E>> | ||
} | ||
|
||
sealed interface SubState<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any> | ||
|
||
data class NormalizedState<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any>( | ||
val allIds: List<Id>, | ||
val byId: Map<Id, ItemState<Id, K, V, E>> | ||
) : SubState<Id, K, V, E> | ||
|
||
|
||
sealed interface ItemState<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any> { | ||
data object Initial : ItemState<Nothing, Nothing, Nothing, Nothing> | ||
data object Loading : ItemState<Nothing, Nothing, Nothing, Nothing> | ||
data class Error<E : Any>(val error: E) : ItemState<Nothing, Nothing, Nothing, E> | ||
data class Data<Id : Comparable<Id>, V : Identifiable<Id>>( | ||
val value: V, | ||
val status: Status, | ||
val lastModified: Long, | ||
val lastRefreshed: Long, | ||
) : SubState<Id, Nothing, V, Nothing> { | ||
enum class Status { | ||
Idle, | ||
Downloading, | ||
Uploading, | ||
} | ||
} | ||
} | ||
|
||
|
||
sealed interface PagingState<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any> : SubState<Id, K, V, E> { | ||
val anchorPosition: Id? | ||
val prefetchPosition: Id? | ||
|
||
data class Initial<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any>( | ||
override val prefetchPosition: Id?, | ||
override val anchorPosition: Id?, | ||
) : PagingState<Id, K, V, E> | ||
|
||
data class Loading<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any>( | ||
override val prefetchPosition: Id?, | ||
override val anchorPosition: Id?, | ||
) : PagingState<Id, K, V, E> | ||
|
||
data class Error<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any>( | ||
val value: E, | ||
override val prefetchPosition: Id?, | ||
override val anchorPosition: Id?, | ||
) : PagingState<Id, K, V, E> | ||
|
||
data class Data<Id : Comparable<Id>, K : Any, V : Identifiable<Id>, E : Any>( | ||
val allIds: List<Id>, | ||
override val anchorPosition: Id?, | ||
override val prefetchPosition: Id?, | ||
val lastModified: Long, | ||
val lastRefreshed: Long, | ||
val status: Status | ||
) : PagingState<Id, K, V, E> { | ||
enum class Status { | ||
Idle, | ||
LoadingMore, | ||
ErrorLoadingMore, | ||
Refreshing | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...ain/story/api/src/commonMain/kotlin/monster/scoop/xplat/domain/story/api/StorySupplier.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package monster.scoop.xplat.domain.story.api | ||
|
||
import monster.scoop.xplat.foundation.networking.api.GetStoryQuery | ||
import org.mobilenativefoundation.market.MarketSupplier | ||
|
||
typealias StorySupplier = MarketSupplier<GetStoryQuery> |