From 58470e2dcd56d335571ac22494d128e8cb52736b Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Fri, 30 Dec 2022 15:53:16 +0000 Subject: [PATCH] Add home reducer tests - reworks the multi state --- chat-engine | 2 +- features/home/build.gradle | 9 ++- .../kotlin/app/dapk/st/home/HomeModule.kt | 4 +- .../app/dapk/st/home/state/HomeAction.kt | 3 + .../{CreateHomeReducer.kt => HomeReducer.kt} | 58 +++++++++++-------- .../dapk/st/settings/state/SettingsReducer.kt | 1 - screen-state | 2 +- 7 files changed, 48 insertions(+), 31 deletions(-) rename features/home/src/main/kotlin/app/dapk/st/home/state/{CreateHomeReducer.kt => HomeReducer.kt} (76%) diff --git a/chat-engine b/chat-engine index cdf3e1b..9017fe3 160000 --- a/chat-engine +++ b/chat-engine @@ -1 +1 @@ -Subproject commit cdf3e1bffba4b69dd8f752c6cc7588b0e89a17af +Subproject commit 9017fe3963754199db7c2525ba38a3265ef5701d diff --git a/features/home/build.gradle b/features/home/build.gradle index df99340..4be7225 100644 --- a/features/home/build.gradle +++ b/features/home/build.gradle @@ -8,14 +8,21 @@ android { dependencies { implementation "chat-engine:chat-engine" + implementation 'screen-state:screen-android' implementation project(":features:directory") implementation project(":features:login") implementation project(":features:settings") implementation project(":features:profile") implementation project(":domains:android:compose-core") implementation project(':domains:store') - implementation 'screen-state:screen-android' implementation project(":core") implementation project(":design-library") 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")) } \ No newline at end of file diff --git a/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt b/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt index 9a716cd..8077f32 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/HomeModule.kt @@ -5,7 +5,7 @@ import app.dapk.st.core.ProvidableModule import app.dapk.st.directory.DirectoryModule import app.dapk.st.domain.StoreModule 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.profile.ProfileModule import app.dapk.st.state.State @@ -39,7 +39,7 @@ class HomeModule( } private fun homeReducerFactory(eventEmitter: suspend (Any) -> Unit) = - createHomeReducer(chatEngine, storeModule.cacheCleaner(), betaVersionUpgradeUseCase, JobBag(), eventEmitter) + homeReducer(chatEngine, storeModule.cacheCleaner(), betaVersionUpgradeUseCase, JobBag(), eventEmitter) } typealias DynamicState = State diff --git a/features/home/src/main/kotlin/app/dapk/st/home/state/HomeAction.kt b/features/home/src/main/kotlin/app/dapk/st/home/state/HomeAction.kt index 25bc9cd..fc58d2e 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/state/HomeAction.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/state/HomeAction.kt @@ -1,5 +1,6 @@ package app.dapk.st.home.state +import app.dapk.st.engine.Me import app.dapk.st.home.state.HomeScreenState.Page import app.dapk.state.Action @@ -12,7 +13,9 @@ sealed interface HomeAction : Action { object LoggedIn : HomeAction data class ChangePage(val page: Page) : HomeAction + data class ChangePageSideEffect(val page: Page) : HomeAction data class UpdateInvitesCount(val invitesCount: Int) : HomeAction + data class UpdateToSignedIn(val me: Me) : HomeAction data class UpdateState(val state: HomeScreenState) : HomeAction object InitialHome : HomeAction } diff --git a/features/home/src/main/kotlin/app/dapk/st/home/state/CreateHomeReducer.kt b/features/home/src/main/kotlin/app/dapk/st/home/state/HomeReducer.kt similarity index 76% rename from features/home/src/main/kotlin/app/dapk/st/home/state/CreateHomeReducer.kt rename to features/home/src/main/kotlin/app/dapk/st/home/state/HomeReducer.kt index f330c67..f788da1 100644 --- a/features/home/src/main/kotlin/app/dapk/st/home/state/CreateHomeReducer.kt +++ b/features/home/src/main/kotlin/app/dapk/st/home/state/HomeReducer.kt @@ -11,7 +11,7 @@ import app.dapk.state.* import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -fun createHomeReducer( +fun homeReducer( chatEngine: ChatEngine, cacheCleaner: StoreCleaner, betaVersionUpgradeUseCase: BetaVersionUpgradeUseCase, @@ -25,6 +25,15 @@ fun createHomeReducer( 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 -> when (state) { HomeScreenState.Loading -> state @@ -37,7 +46,6 @@ fun createHomeReducer( if (chatEngine.isSignedIn()) { eventEmitter.invoke(HomeEvent.OnShowContent) dispatch(HomeAction.InitialHome) - listenForInviteChanges(chatEngine, jobBag) } else { dispatch(HomeAction.UpdateState(HomeScreenState.SignedOut)) } @@ -45,18 +53,26 @@ fun createHomeReducer( async(HomeAction.InitialHome::class) { val me = chatEngine.me(forceRefresh = false) - val nextState = when (val current = getState()) { - HomeScreenState.Loading -> HomeScreenState.SignedIn(HomeScreenState.Page.Directory, me, invites = 0) - is HomeScreenState.SignedIn -> current.copy(me = me, invites = current.invites) - HomeScreenState.SignedOut -> HomeScreenState.SignedIn(HomeScreenState.Page.Directory, me, invites = 0) - } - dispatch(HomeAction.UpdateState(nextState)) + dispatch(HomeAction.UpdateToSignedIn(me)) + listenForInviteChanges(chatEngine, jobBag) }, async(HomeAction.LoggedIn::class) { dispatch(HomeAction.InitialHome) eventEmitter.invoke(HomeEvent.OnShowContent) - listenForInviteChanges(chatEngine, jobBag) + }, + + async(HomeAction.ChangePageSideEffect::class) { action -> + when (action.page) { + HomeScreenState.Page.Directory -> { + // do nothing + } + + HomeScreenState.Page.Profile -> { + dispatch(ComponentLifecycle.OnGone) + dispatch(ProfileAction.Reset) + } + } }, multi(HomeAction.ChangePage::class) { action -> @@ -64,38 +80,30 @@ fun createHomeReducer( when (state) { is HomeScreenState.SignedIn -> when (action.page) { state.page -> state - else -> state.copy(page = action.page).also { - async { - when (action.page) { - HomeScreenState.Page.Directory -> { - // do nothing - } - - HomeScreenState.Page.Profile -> { - dispatch(ComponentLifecycle.OnGone) - dispatch(ProfileAction.Reset) - } - } - } - } + else -> state.copy(page = action.page) } HomeScreenState.Loading -> 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) { dispatch(DirectorySideEffect.ScrollToTop) }, - sideEffect(HomeAction.ClearCache::class) { _, _ -> cacheCleaner.cleanCache(removeCredentials = false) betaVersionUpgradeUseCase.notifyUpgraded() eventEmitter.invoke(HomeEvent.Relaunch) - } + }, ) } diff --git a/features/settings/src/main/kotlin/app/dapk/st/settings/state/SettingsReducer.kt b/features/settings/src/main/kotlin/app/dapk/st/settings/state/SettingsReducer.kt index 075661c..5cfdaa8 100644 --- a/features/settings/src/main/kotlin/app/dapk/st/settings/state/SettingsReducer.kt +++ b/features/settings/src/main/kotlin/app/dapk/st/settings/state/SettingsReducer.kt @@ -144,7 +144,6 @@ internal fun settingsReducer( } Ignored -> { - nothing() } ToggleDynamicTheme -> async { diff --git a/screen-state b/screen-state index 9abb6b4..d596949 160000 --- a/screen-state +++ b/screen-state @@ -1 +1 @@ -Subproject commit 9abb6b4418f451d81f09c4ba2b26f2b1ffd19f55 +Subproject commit d596949ac2b923b02da55ddd78e2e26dc46af82a