-
Notifications
You must be signed in to change notification settings - Fork 27
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
Pass parameters for loading data via action? #69
Comments
Hey Bodo, In general it's a good rule of thumb to make all information needed to restore the full state part of your State (then you get state restoration almost for free, i.e. restoring loading state from Bundle would then trigger onEnter again and load screen). I.e. I think you should have it as part of your data class Loading(
val refresh : Boolean,
internal val ref : Ref // make this private or internal so it is only accessible for your state machine, but not for your View
)
suspend fun loadScreen(getState: GetState<ActivityViewState>, setState: SetState<ActivityViewState>) {
val state = getState() as LoadingState
val ref = state.ref
loadData(ref)
...
} Does that help? |
Hey Hannes, thanks for the hint to put it in the state! whats best to the data (e.g. load the feed with other parameters, e.g. to the elements sorted desc). i think i will do it like this: init {
spec {
inState<Loading> {
onEnter(block = ::loadScreen)
}
inState<Content<Any>> {
on<ActivityReloadAction<Ref>> { _, _, setState ->
setState { Loading(refresh = true, ref = newRef) }
}
}
inState<Error> {
on<ActivityReloadAction<Ref>> { _, _, setState ->
setState { Loading(refresh = false, ref = newRef) }
}
}
}
} -> that means i will pass the new ref with the updated request data via the ActivityReloadAction to the Loading ViewState |
I'm sorry, I don't fully understand the relationship between other parameters, i.e. sort order, and |
So for example the inital load will be done with the following request: When the user wants to sort the list desc he has to call the feed with another parameter, so i dispatch the ActivityReloadAction(ref = Ref(sort = "desc") inState<Content<Any>> {
on<ActivityReloadAction<Ref>> { _, _, setState ->
setState { Loading(refresh = true, ref = newRef) }
}
} so the action contains the ref with the new url parameters to load the feed |
Yes, you can do it like that. One Anti-Pattern that you should avoid (not sure if it related to your example) is to not leak any state related information in via data class NextPageAction(
val nextPage : Int // Don't do this!
) : Action
inState<Content> {
on<NextPageAction> { action, _, setState ->
setState { Loading(page = action.nextPage) }
}
} The "anti pattern" here is that essentially an Action can "override" completely the state of your state machine. For example at some point, most likely in your UI, you have to create a Instead the current page should be part of your State: object NextPageAction : Action
sealed class State {
abstract val page : Int
data class Content( override page : Int) : State
data class Loading( override page : Int) : State
}
inState<Content> {
on<NextPageAction> { action, getState, setState ->
setState { Loading(page = getState().page + 1) } // Now state machine drives the state
}
} It sounds a little bit that Does that help? |
Yes this was very helpful, thanks. |
Hi @sockeqwe,
first of all: great work!
I love FlowRedux and already played a little bit with this library.
But I am facing and problem:
I am dispatching a loading action to the statemachine and this loading actions contains the data for loading feeds (e.g. id for a specific article). i looked at your best practices but i don't know how to get the data out of my action.
my current implementation:
Maybe you can tell me how i can get the action which triggered the state or how i can pass in paramters to load the data?
Thx
Bodo
The text was updated successfully, but these errors were encountered: