在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):freeletics/FlowRedux开源软件地址(OpenSource Url):https://github.com/freeletics/FlowRedux开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):FlowReduxBuilding async. running Kotlin Multiplatform state machine made easy with a DSL and coroutines. UsageFull documentation and best practices can be found here: https://freeletics.github.io/FlowRedux/ sealed interface State
object Loading : State
data class ContentState(val items : List<Item>) : State
data class Error(val error : Throwable) : State
sealed interface Action
object RetryLoadingAction : Action
class MyStateMachine : FlowReduxStateMachine<State, Action>(initialState = Loading){
init {
spec {
inState<Loading> {
onEnter { state : State<Loading> ->
// executes this block whenever we enter Loading state
try {
val items = loadItems() // suspending function / coroutine to load items
state.override { ContentState(items) } // Transition to ContentState
} catch (t : Throwable) {
state.override { Error(t) } // Transition to Error state
}
}
}
inState<Error> {
on<RetryLoadingAction> { action : RetryLoadingAction, state : State<Error> ->
// executes this block whenever Error state is current state and RetryLoadingAction is emitted
state.override { Loading } // Transition to Loading state which loads list again
}
}
inState<ContentState> {
collectWhileInState( flowOf(1,2,3) ) { value : Int, state : State<ContentState> ->
// observes the given flow as long as state is ContentState.
// Once state is changed to another state the flow will automatically
// stop emitting.
state.mutate {
copy( items = this.items + Item("New item $value"))
}
}
}
}
}
} val statemachine = MyStateMachine()
launch { // Launch a coroutine
statemachine.state.collect { state ->
// do something with new state like update UI
renderUI(state)
}
}
// emit an Action
launch { // Launch a coroutine
statemachine.dispatch(action)
} In an Android Application you could use it with AndroidX class MyViewModel @Inject constructor(private val stateMachine : MyStateMachine) : ViewModel() {
val state = MutableLiveData<State>()
init {
viewModelScope.launch { // automatically canceled once ViewModel lifecycle reached destroyed.
stateMachine.state.collect { newState ->
state.value = newState
}
}
}
fun dispatch(action : Action) {
viewModelScope.launch {
stateMachine.dispatch(action)
}
}
} DependenciesThere are two artifacts that you can include as dependencis:
JVM / Android onlyimplementation 'com.freeletics.flowredux:flowredux-jvm:1.0.0'
implementation 'com.freeletics.flowredux:compose:1.0.0' Multiplatformimplementation 'com.freeletics.flowredux:flowredux:1.0.0' JavaScriptNo javascript version released yet but it is on our roadmap. SnapshotLatest snapshot (directly published from allprojects {
repositories {
// Your repositories.
// ...
// Add url to snapshot repository
maven {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
}
} Then just use implementation 'com.freeletics.flowredux:flowredux:1.0.1-SNAPSHOT' |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论