-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebde3bb
commit 7492b1f
Showing
26 changed files
with
1,043 additions
and
446 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
93 changes: 0 additions & 93 deletions
93
paging/src/androidMain/kotlin/org/mobilenativefoundation/store/paging5/paging3.kt
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/Identifiable.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,35 @@ | ||
package org.mobilenativefoundation.store.paging5 | ||
|
||
|
||
/** | ||
* An interface that defines items that can be uniquely identified. | ||
* Every item that implements the [Identifiable] interface must have a means of identification. | ||
* This is useful in scenarios when data can be represented as singles or collections. | ||
*/ | ||
|
||
interface Identifiable<out Id : Any> { | ||
|
||
/** | ||
* Represents a single identifiable item. | ||
*/ | ||
interface Single<Id : Any> : Identifiable<Id> { | ||
val id: Id | ||
} | ||
|
||
/** | ||
* Represents a collection of identifiable items. | ||
*/ | ||
interface Collection<Id : Any, S : Single<Id>> : Identifiable<Id> { | ||
val items: List<S> | ||
|
||
/** | ||
* Returns a new collection with the updated items. | ||
*/ | ||
fun copyWith(items: List<S>): Collection<Id, S> | ||
|
||
/** | ||
* Inserts items to the existing collection and returns the updated collection. | ||
*/ | ||
fun insertItems(type: StoreKey.LoadType, items: List<S>): Collection<Id, S> | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/InitStoreStateFlow.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,65 @@ | ||
package org.mobilenativefoundation.store.paging5 | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import kotlinx.coroutines.launch | ||
import org.mobilenativefoundation.store.store5.ExperimentalStoreApi | ||
import org.mobilenativefoundation.store.store5.MutableStore | ||
import org.mobilenativefoundation.store.store5.Store | ||
|
||
|
||
/** | ||
* Initializes and returns a [StateFlow] that reflects the state of the Store, updating by a flow of provided keys. | ||
* @param scope A [CoroutineScope]. | ||
* @param keys A flow of keys that dictate how the Store should be updated. | ||
* @param updateStoreState A lambda that defines how the Store's state should be updated based on the current state and a key. | ||
* @return A read-only [StateFlow] reflecting the state of the Store. | ||
*/ | ||
private fun <Id : Any, Key : StoreKey<Id>, Output : Identifiable<Id>> initStoreStateFlow( | ||
scope: CoroutineScope, | ||
keys: Flow<Key>, | ||
updateStoreState: suspend (currentState: StoreState<Id, Output>, key: Key) -> StoreState<Id, Output> | ||
): StateFlow<StoreState<Id, Output>> { | ||
val stateFlow = MutableStateFlow<StoreState<Id, Output>>(StoreState.Loading) | ||
|
||
scope.launch { | ||
keys.collect { key -> | ||
println("KEY = $key") | ||
println("CURRENT STATE = ${stateFlow.value}") | ||
val updatedState = updateStoreState(stateFlow.value, key) | ||
stateFlow.emit(updatedState) | ||
} | ||
} | ||
|
||
return stateFlow.asStateFlow() | ||
} | ||
|
||
/** | ||
* Initializes and returns a [StateFlow] that reflects the state of the [Store], updating by a flow of provided keys. | ||
* @see [initStoreStateFlow]. | ||
*/ | ||
fun <Id : Any, Key : StoreKey<Id>, Output : Identifiable<Id>> Store<Key, Output>.initStoreStateFlow( | ||
scope: CoroutineScope, | ||
keys: Flow<Key>, | ||
): StateFlow<StoreState<Id, Output>> { | ||
return initStoreStateFlow(scope, keys) { currentState, key -> | ||
this.updateStoreState(currentState, key) | ||
} | ||
} | ||
|
||
/** | ||
* Initializes and returns a [StateFlow] that reflects the state of the [Store], updating by a flow of provided keys. | ||
* @see [initStoreStateFlow]. | ||
*/ | ||
@OptIn(ExperimentalStoreApi::class) | ||
fun <Id : Any, Key : StoreKey<Id>, Output : Identifiable<Id>> MutableStore<Key, Output>.initStoreStateFlow( | ||
scope: CoroutineScope, | ||
keys: Flow<Key>, | ||
): StateFlow<StoreState<Id, Output>> { | ||
return initStoreStateFlow(scope, keys) { currentState, key -> | ||
this.updateStoreState(currentState, key) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
paging/src/commonMain/kotlin/org/mobilenativefoundation/store/paging5/KeyProvider.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 org.mobilenativefoundation.store.paging5 | ||
|
||
interface KeyProvider<Id : Any, Single : Identifiable.Single<Id>> { | ||
fun from(key: StoreKey.Collection<Id>, value: Single): StoreKey.Single<Id> | ||
fun from(key: StoreKey.Single<Id>, value: Single): StoreKey.Collection<Id> | ||
} |
Oops, something went wrong.