Closed parkodee closed 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.
Similar to how one updates state from side effects for example
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?
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
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.
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.
Yeah a long way but it did the job. hope you publish it to maven soon. Thankx
How can I dispatch action change from outside of composable? Can't call this from outside composable
val dispatch = rememberDispatcher()
Any solution ?