feat: fix broken redux

This commit is contained in:
Yogesh Choudhary Paliyal
2025-05-18 17:04:23 +05:30
parent 79fc9b89c6
commit fbeb990f6b
4 changed files with 51 additions and 3 deletions

View File

@@ -72,7 +72,7 @@ import com.yogeshpaliyal.keypass.ui.style.KeyPassTheme
import dagger.hilt.android.AndroidEntryPoint
import org.reduxkotlin.compose.StoreProvider
import org.reduxkotlin.compose.rememberDispatcher
import org.reduxkotlin.compose.selectState
import com.yogeshpaliyal.keypass.ui.redux.selectState
val LocalUserSettings = compositionLocalOf { UserSettings() }

View File

@@ -14,11 +14,11 @@ import com.yogeshpaliyal.keypass.ui.nav.BottomNavViewModel
import com.yogeshpaliyal.keypass.ui.nav.NavigationModelItem
import com.yogeshpaliyal.keypass.ui.redux.actions.BottomSheetAction
import com.yogeshpaliyal.keypass.ui.redux.actions.NavigationAction
import com.yogeshpaliyal.keypass.ui.redux.selectState
import com.yogeshpaliyal.keypass.ui.redux.states.BottomSheetState
import com.yogeshpaliyal.keypass.ui.redux.states.HomeState
import com.yogeshpaliyal.keypass.ui.redux.states.KeyPassState
import org.reduxkotlin.compose.rememberDispatcher
import org.reduxkotlin.compose.selectState
@Composable
fun DashboardBottomSheet(viewModel: BottomNavViewModel) {

View File

@@ -29,7 +29,7 @@ import com.yogeshpaliyal.keypass.ui.redux.states.KeyPassState
import com.yogeshpaliyal.keypass.ui.redux.states.ScreenState
import com.yogeshpaliyal.keypass.ui.redux.states.SettingsState
import org.reduxkotlin.compose.rememberDispatcher
import org.reduxkotlin.compose.selectState
import com.yogeshpaliyal.keypass.ui.redux.selectState

View File

@@ -1,5 +1,10 @@
package com.yogeshpaliyal.keypass.ui.redux
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisallowComposableCalls
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import com.yogeshpaliyal.keypass.ui.redux.actions.BatchActions
import com.yogeshpaliyal.keypass.ui.redux.actions.BottomSheetAction
import com.yogeshpaliyal.keypass.ui.redux.actions.GoBackAction
@@ -16,7 +21,9 @@ import com.yogeshpaliyal.keypass.ui.redux.states.KeyPassState
import com.yogeshpaliyal.keypass.ui.redux.states.ScreenState
import com.yogeshpaliyal.keypass.ui.redux.states.generateDefaultState
import org.reduxkotlin.Reducer
import org.reduxkotlin.TypedStore
import org.reduxkotlin.applyMiddleware
import org.reduxkotlin.compose.rememberStore
import org.reduxkotlin.createStore
object KeyPassRedux {
@@ -100,3 +107,44 @@ object KeyPassRedux {
applyMiddleware(utilityMiddleware, intentNavigationMiddleware)
)
}
/**
* * These are function copied from
* * https://github.com/reduxkotlin/redux-kotlin-compose/blob/master/redux-kotlin-compose/src/commonMain/kotlin/org/reduxkotlin/compose/selectState.kt
* Selects a value from the local store.
* @param selector to extract the value
* @param State state type
* @param Slice extracted value type
* @return selected value
*/
@Composable
public inline fun <reified State, Slice> selectState(
crossinline selector: @DisallowComposableCalls State.() -> Slice
): androidx.compose.runtime.State<Slice> {
return rememberStore<State>().selectState(selector)
}
/**
* These are function copied from
* https://github.com/reduxkotlin/redux-kotlin-compose/blob/master/redux-kotlin-compose/src/commonMain/kotlin/org/reduxkotlin/compose/selectState.kt
*
* Selects a value from the local store.
* @receiver a store to extract the value from
* @param selector to extract the value
* @param State state type
* @param Slice extracted value type
* @return selected value
*/
@Composable
public inline fun <State, Slice> TypedStore<State, *>.selectState(
crossinline selector: @DisallowComposableCalls State.() -> Slice
): androidx.compose.runtime.State<Slice> {
val result = remember { mutableStateOf(state.selector()) }
DisposableEffect(result) {
val unsubscribe = subscribe { result.value = state.selector() }
onDispose(unsubscribe)
}
return result
}