Add home reducer tests

- reworks the multi state
This commit is contained in:
Adam Brown 2022-12-30 15:53:16 +00:00
parent 7a8c8ed88d
commit 58470e2dcd
7 changed files with 48 additions and 31 deletions

@ -1 +1 @@
Subproject commit cdf3e1bffba4b69dd8f752c6cc7588b0e89a17af Subproject commit 9017fe3963754199db7c2525ba38a3265ef5701d

View File

@ -8,14 +8,21 @@ android {
dependencies { dependencies {
implementation "chat-engine:chat-engine" implementation "chat-engine:chat-engine"
implementation 'screen-state:screen-android'
implementation project(":features:directory") implementation project(":features:directory")
implementation project(":features:login") implementation project(":features:login")
implementation project(":features:settings") implementation project(":features:settings")
implementation project(":features:profile") implementation project(":features:profile")
implementation project(":domains:android:compose-core") implementation project(":domains:android:compose-core")
implementation project(':domains:store') implementation project(':domains:store')
implementation 'screen-state:screen-android'
implementation project(":core") implementation project(":core")
implementation project(":design-library") implementation project(":design-library")
implementation libs.compose.coil implementation libs.compose.coil
kotlinTest(it)
testImplementation 'screen-state:state-test'
testImplementation 'chat-engine:chat-engine-test'
androidImportFixturesWorkaround(project, project(":core"))
androidImportFixturesWorkaround(project, project(":domains:android:stub"))
} }

View File

@ -5,7 +5,7 @@ import app.dapk.st.core.ProvidableModule
import app.dapk.st.directory.DirectoryModule import app.dapk.st.directory.DirectoryModule
import app.dapk.st.domain.StoreModule import app.dapk.st.domain.StoreModule
import app.dapk.st.engine.ChatEngine import app.dapk.st.engine.ChatEngine
import app.dapk.st.home.state.createHomeReducer import app.dapk.st.home.state.homeReducer
import app.dapk.st.login.LoginModule import app.dapk.st.login.LoginModule
import app.dapk.st.profile.ProfileModule import app.dapk.st.profile.ProfileModule
import app.dapk.st.state.State import app.dapk.st.state.State
@ -39,7 +39,7 @@ class HomeModule(
} }
private fun homeReducerFactory(eventEmitter: suspend (Any) -> Unit) = private fun homeReducerFactory(eventEmitter: suspend (Any) -> Unit) =
createHomeReducer(chatEngine, storeModule.cacheCleaner(), betaVersionUpgradeUseCase, JobBag(), eventEmitter) homeReducer(chatEngine, storeModule.cacheCleaner(), betaVersionUpgradeUseCase, JobBag(), eventEmitter)
} }
typealias DynamicState = State<DynamicReducers, Any> typealias DynamicState = State<DynamicReducers, Any>

View File

@ -1,5 +1,6 @@
package app.dapk.st.home.state package app.dapk.st.home.state
import app.dapk.st.engine.Me
import app.dapk.st.home.state.HomeScreenState.Page import app.dapk.st.home.state.HomeScreenState.Page
import app.dapk.state.Action import app.dapk.state.Action
@ -12,7 +13,9 @@ sealed interface HomeAction : Action {
object LoggedIn : HomeAction object LoggedIn : HomeAction
data class ChangePage(val page: Page) : HomeAction data class ChangePage(val page: Page) : HomeAction
data class ChangePageSideEffect(val page: Page) : HomeAction
data class UpdateInvitesCount(val invitesCount: Int) : HomeAction data class UpdateInvitesCount(val invitesCount: Int) : HomeAction
data class UpdateToSignedIn(val me: Me) : HomeAction
data class UpdateState(val state: HomeScreenState) : HomeAction data class UpdateState(val state: HomeScreenState) : HomeAction
object InitialHome : HomeAction object InitialHome : HomeAction
} }

View File

@ -11,7 +11,7 @@ import app.dapk.state.*
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
fun createHomeReducer( fun homeReducer(
chatEngine: ChatEngine, chatEngine: ChatEngine,
cacheCleaner: StoreCleaner, cacheCleaner: StoreCleaner,
betaVersionUpgradeUseCase: BetaVersionUpgradeUseCase, betaVersionUpgradeUseCase: BetaVersionUpgradeUseCase,
@ -25,6 +25,15 @@ fun createHomeReducer(
action.state action.state
}, },
change(HomeAction.UpdateToSignedIn::class) { action, state ->
val me = action.me
when (state) {
HomeScreenState.Loading -> HomeScreenState.SignedIn(HomeScreenState.Page.Directory, me, invites = 0)
is HomeScreenState.SignedIn -> state.copy(me = me, invites = state.invites)
HomeScreenState.SignedOut -> HomeScreenState.SignedIn(HomeScreenState.Page.Directory, me, invites = 0)
}
},
change(HomeAction.UpdateInvitesCount::class) { action, state -> change(HomeAction.UpdateInvitesCount::class) { action, state ->
when (state) { when (state) {
HomeScreenState.Loading -> state HomeScreenState.Loading -> state
@ -37,7 +46,6 @@ fun createHomeReducer(
if (chatEngine.isSignedIn()) { if (chatEngine.isSignedIn()) {
eventEmitter.invoke(HomeEvent.OnShowContent) eventEmitter.invoke(HomeEvent.OnShowContent)
dispatch(HomeAction.InitialHome) dispatch(HomeAction.InitialHome)
listenForInviteChanges(chatEngine, jobBag)
} else { } else {
dispatch(HomeAction.UpdateState(HomeScreenState.SignedOut)) dispatch(HomeAction.UpdateState(HomeScreenState.SignedOut))
} }
@ -45,27 +53,16 @@ fun createHomeReducer(
async(HomeAction.InitialHome::class) { async(HomeAction.InitialHome::class) {
val me = chatEngine.me(forceRefresh = false) val me = chatEngine.me(forceRefresh = false)
val nextState = when (val current = getState()) { dispatch(HomeAction.UpdateToSignedIn(me))
HomeScreenState.Loading -> HomeScreenState.SignedIn(HomeScreenState.Page.Directory, me, invites = 0) listenForInviteChanges(chatEngine, jobBag)
is HomeScreenState.SignedIn -> current.copy(me = me, invites = current.invites)
HomeScreenState.SignedOut -> HomeScreenState.SignedIn(HomeScreenState.Page.Directory, me, invites = 0)
}
dispatch(HomeAction.UpdateState(nextState))
}, },
async(HomeAction.LoggedIn::class) { async(HomeAction.LoggedIn::class) {
dispatch(HomeAction.InitialHome) dispatch(HomeAction.InitialHome)
eventEmitter.invoke(HomeEvent.OnShowContent) eventEmitter.invoke(HomeEvent.OnShowContent)
listenForInviteChanges(chatEngine, jobBag)
}, },
multi(HomeAction.ChangePage::class) { action -> async(HomeAction.ChangePageSideEffect::class) { action ->
change { _, state ->
when (state) {
is HomeScreenState.SignedIn -> when (action.page) {
state.page -> state
else -> state.copy(page = action.page).also {
async {
when (action.page) { when (action.page) {
HomeScreenState.Page.Directory -> { HomeScreenState.Page.Directory -> {
// do nothing // do nothing
@ -76,26 +73,37 @@ fun createHomeReducer(
dispatch(ProfileAction.Reset) dispatch(ProfileAction.Reset)
} }
} }
} },
}
multi(HomeAction.ChangePage::class) { action ->
change { _, state ->
when (state) {
is HomeScreenState.SignedIn -> when (action.page) {
state.page -> state
else -> state.copy(page = action.page)
} }
HomeScreenState.Loading -> state HomeScreenState.Loading -> state
HomeScreenState.SignedOut -> state HomeScreenState.SignedOut -> state
} }
} }
async {
val state = getState()
if (state is HomeScreenState.SignedIn && state.page != action.page) {
dispatch(HomeAction.ChangePageSideEffect(action.page))
}
}
}, },
async(HomeAction.ScrollToTop::class) { async(HomeAction.ScrollToTop::class) {
dispatch(DirectorySideEffect.ScrollToTop) dispatch(DirectorySideEffect.ScrollToTop)
}, },
sideEffect(HomeAction.ClearCache::class) { _, _ -> sideEffect(HomeAction.ClearCache::class) { _, _ ->
cacheCleaner.cleanCache(removeCredentials = false) cacheCleaner.cleanCache(removeCredentials = false)
betaVersionUpgradeUseCase.notifyUpgraded() betaVersionUpgradeUseCase.notifyUpgraded()
eventEmitter.invoke(HomeEvent.Relaunch) eventEmitter.invoke(HomeEvent.Relaunch)
} },
) )
} }

View File

@ -144,7 +144,6 @@ internal fun settingsReducer(
} }
Ignored -> { Ignored -> {
nothing()
} }
ToggleDynamicTheme -> async { ToggleDynamicTheme -> async {

@ -1 +1 @@
Subproject commit 9abb6b4418f451d81f09c4ba2b26f2b1ffd19f55 Subproject commit d596949ac2b923b02da55ddd78e2e26dc46af82a