Skip to content
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

Add an convenient Artifact to use FlowReduxStateMachine in compose #236

Merged
merged 11 commits into from
Nov 23, 2021

Conversation

sockeqwe
Copy link
Collaborator

Adds a way to subscribe to state in compose:

  • MyFlowReduxStateMachine.asState()

and a way to get both, subscribe to state and dispatch actions:

  • val (state : State<MyState>, dispatch : (MyAction) -> Unit) = MyFlowReduxStateMachine.stateAndDispatch()

I have also added a compose code sample. For sure it can be made nicer or more idiomatic, but does the job so far I guess.

compileSdk : 30,
targetSdk : 30,
compileSdk : 31,
targetSdk : 31,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It also updates compile + target SDK to 31(then some other DependABot created PRs should compile)


val errorMessage = stringResource(R.string.unexpected_error)
if (showErrorSnackBar) {
LaunchedEffect(scaffoldState.snackbarHostState) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually not 100% sure if this works as intended because of the surrounding if (showErrorSnackBar) ... It seems to work, but here I don't understand how LaunchedEffect(key) learns that the key = scaffoldState.snackbarHostState has changed if it surrounded by an if(showErrorSnackBar) ...

}

if (loadMore) {
LaunchedEffect(loadMore) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


suspend fun loadPage(page: Int): PageResult {
delay(300)
delay(2000)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

increased to see things better in the Sample Application UI

).run {
// TODO rewrite this to use header Link
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will never happen 😄 therefore removed this comment 😺

* Instead the "wrappter class" [PaginationStateMachine] should be used which hides `Flow` etc.
* but uses traditional "callbacks". That way it is easier to use on iOS.
*/
class InternalPaginationStateMachine(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was to only expose publicly PaginationStateMachine and not this class InternalPaginationStateMachine because PaginationStateMachine provides traditional callback based listener so that from iOS you don't need to bridge Flow and suspend function.

but compose actually should access directly a FlowReduxStateMachine to be able to use extension functions like stateAndDispatch() on it. Therefore made it public.

private val logger: FlowReduxLogger? = null
) : StateMachine<S, A> {

val initialState: S by lazy(LazyThreadSafetyMode.NONE, initialStateSupplier)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So initial state will be kept forever. That ensures that it is always the same initial state for all state:Flow<MyState> collectors. Also with making this public, FlowReduxStateMachine.asState() Compose extension can access it and return type is State<MyState> instead of nullable State<MyState?> as it serves as the initial state in produceState {...}.

@sockeqwe
Copy link
Collaborator Author

Closes #139

@sockeqwe sockeqwe merged commit 5ca4d62 into main Nov 23, 2021
@sockeqwe sockeqwe deleted the compose branch November 23, 2021 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant