reduxkotlin / redux-kotlin-compose

Compose Multiplatform integration for Redux-Kotlin
https://reduxkotlin.github.io/redux-kotlin-compose
29 stars 1 forks source link

Dispatch from outside of Composable? #7

Closed parkodee closed 1 year ago

parkodee commented 1 year ago

How can I dispatch action change from outside of composable? Can't call this from outside composable val dispatch = rememberDispatcher()

Any solution ?

mpetuska commented 1 year ago

The intended usage is to remember dispatcher inside the composable and then use it via captured closures or explicit argument passing outside of the composition scope.

mpetuska commented 1 year ago

Similar to how one updates state from side effects for example

mpetuska commented 1 year ago

However this gave me some idea for some custom side effect wrappers that would explicitly provide you the dispatcher. Will implement them once We're unblocked with maven central release. cc: @patjackson52 what do you think about the idea?

parkodee commented 1 year ago

After spending some time with the library it works perfect. Here is what I did in case anyone wanna try it: My Android project is Jetpack Compose it's not multi-platform;

First the reducer/store:

` import android.bluetooth.BluetoothDevice import org.reduxkotlin.Reducer import org.reduxkotlin.createThreadSafeStore import org.reduxkotlin.reducerForActionType

data class MyAppState( val connectedDevice: BluetoothDevice? )

sealed interface Action { data class DeviceConnected(val connectedDevice: BluetoothDevice) : Action object DeviceDisConnected : Action }

val reducer: Reducer = reducerForActionType<MyAppState, Action> { state, action -> when (action) { is Action.DeviceConnected -> state.copy(connectedDevice = action.connectedDevice) is Action.DeviceDisConnected -> state.copy(connectedDevice = null) } }

val appStore = createThreadSafeStore(reducer, MyAppState(emptySet(),null)) `

in the main activity I imported the store and wrapped the content with : StoreProvider(appStore) { content }

outside the composable I import the store and dispatch like this:

appStore.dispatch(Action.DeviceConnected( device ))

also outside composable I use the imported store to get the new state:

appStore.state.connectedDevice .....................

Inside a composable I use these: to get updated state: val connectedDevice by selectState<MyAppState, Set<BluetoothDevice>> { connectedDevice }

and use the dispatch : appStore.dispatch(Action.DeviceConnected(device)) or val dispatch = rememberDispatcher() dispatch(Action.DeviceConnected(device))

and here is what I did to import the library: implementation "org.reduxkotlin:redux-kotlin-threadsafe-jvm:0.5.5"

and I copied the three files in the repository to my project.

mpetuska commented 1 year ago

That's a long way around it, but it works. Hopefully we can publish this artifact to maven central soon so people no longer have to jump through as many hoops.

parkodee commented 1 year ago

Yeah a long way but it did the job. hope you publish it to maven soon. Thankx