Repository navigation
fix(app): keep the onboarding view model in the activity's store #1246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/cx/aswin/boxlore/ui/OnboardingViewModelScope.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package cx.aswin.boxlore.ui | ||
|
|
||
| import androidx.compose.runtime.Composable | ||
| import androidx.lifecycle.ViewModel | ||
| import androidx.lifecycle.ViewModelProvider | ||
| import androidx.lifecycle.ViewModelStoreOwner | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| import cx.aswin.boxlore.feature.onboarding.OnboardingViewModel | ||
|
|
||
| /** The key the activity's onboarding view model is stored under. */ | ||
| internal const val ONBOARDING_VIEW_MODEL_KEY = "boxlore-onboarding" | ||
|
|
||
| /** | ||
| * The activity's one [OnboardingViewModel], kept in [owner]'s view model store rather than in | ||
| * composition. | ||
| * | ||
| * Every onboarding path shares this instance: search, Echo (AI), genre picks, the import sheet, | ||
| * Home's AI onboarding entry and the sign-in detour through Settings. Shows picked in one flow | ||
| * therefore carry over to the others, and the instance survives the activity being recreated. | ||
| * | ||
| * Because the store owns it, `onCleared` runs when the activity finishes for good: Echo requests | ||
| * are cancelled, deck audio is released, and an onboarding left unfinished is reported as | ||
| * abandoned. Created with `remember`, it was never cleared, so none of that happened. | ||
| * | ||
| * [build] runs once per store; later calls return the stored instance. | ||
| */ | ||
| @Composable | ||
| internal fun rememberOnboardingViewModel( | ||
| owner: ViewModelStoreOwner, | ||
| build: () -> OnboardingViewModel, | ||
| ): OnboardingViewModel = viewModel( | ||
| viewModelStoreOwner = owner, | ||
| key = ONBOARDING_VIEW_MODEL_KEY, | ||
| factory = object : ViewModelProvider.Factory { | ||
| @Suppress("UNCHECKED_CAST") | ||
| override fun <T : ViewModel> create(modelClass: Class<T>): T = build() as T | ||
| }, | ||
| ) |
111 changes: 111 additions & 0 deletions
111
app/src/test/java/cx/aswin/boxlore/ui/OnboardingViewModelRecreationTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package cx.aswin.boxlore.ui | ||
|
|
||
| import android.app.Application | ||
| import android.os.Looper | ||
| import androidx.activity.ComponentActivity | ||
| import androidx.activity.compose.setContent | ||
| import androidx.room.Room | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import cx.aswin.boxlore.core.analytics.AnalyticsHelper | ||
| import cx.aswin.boxlore.core.catalog.PodcastRepository | ||
| import cx.aswin.boxlore.core.catalog.SubscriptionRepository | ||
| import cx.aswin.boxlore.core.database.BoxLoreDatabase | ||
| import cx.aswin.boxlore.core.prefs.BoxcastPrefs | ||
| import cx.aswin.boxlore.core.prefs.UserPreferencesRepository | ||
| import cx.aswin.boxlore.core.rss.RssPodcastRepository | ||
| import cx.aswin.boxlore.core.testing.TestFixtures | ||
| import cx.aswin.boxlore.feature.onboarding.OnboardingViewModel | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertNotNull | ||
| import org.junit.Assert.assertSame | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.Robolectric | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.Shadows.shadowOf | ||
| import org.robolectric.android.controller.ActivityController | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| /** | ||
| * A real activity recreated mid-onboarding keeps its onboarding view model and the shows picked so | ||
| * far, and reports nothing; only finishing it for good clears the view model and reports the | ||
| * unfinished onboarding as abandoned. | ||
| */ | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [34], application = Application::class) | ||
| class OnboardingViewModelRecreationTest { | ||
| private val context: Application = ApplicationProvider.getApplicationContext() | ||
| private lateinit var database: BoxLoreDatabase | ||
| private lateinit var controller: ActivityController<ComponentActivity> | ||
| private val events = mutableListOf<Pair<String, Map<String, Any>>>() | ||
| private lateinit var restoreAnalytics: () -> Unit | ||
| private var builds = 0 | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| database = Room.inMemoryDatabaseBuilder(context, BoxLoreDatabase::class.java).allowMainThreadQueries().build() | ||
| RssPodcastRepository.clearInstanceForTests() | ||
| BoxcastPrefs(context).setOnboardingCompleted(false) | ||
| restoreAnalytics = AnalyticsHelper.installRecordingSink(events) | ||
| controller = Robolectric.buildActivity(ComponentActivity::class.java).setup() | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| restoreAnalytics() | ||
| database.close() | ||
| RssPodcastRepository.clearInstanceForTests() | ||
| } | ||
|
|
||
| private fun build(): OnboardingViewModel { | ||
| builds++ | ||
| val podcastRepository = | ||
| PodcastRepository( | ||
| baseUrl = "http://localhost/", | ||
| publicKey = "test-app-key", | ||
| context = context, | ||
| rssRepository = RssPodcastRepository.createForTests(context = context, database = database), | ||
| ) | ||
| return OnboardingViewModel(context, podcastRepository, SubscriptionRepository(database.podcastDao()), UserPreferencesRepository(context)) | ||
| } | ||
|
|
||
| /** Sets the activity's content the way the app root does and returns the view model it gets. */ | ||
| private fun showOnboarding(): OnboardingViewModel { | ||
| var viewModel: OnboardingViewModel? = null | ||
| val activity = controller.get() | ||
| activity.setContent { viewModel = rememberOnboardingViewModel(owner = activity, build = ::build) } | ||
| shadowOf(Looper.getMainLooper()).idle() | ||
| assertNotNull(viewModel) | ||
| return viewModel!! | ||
| } | ||
|
|
||
| @Test | ||
| fun `recreating the activity mid-onboarding keeps the view model and its picks and reports nothing`() { | ||
| val before = showOnboarding() | ||
| before.onWelcomeScreenViewed() | ||
| before.setShowsSelected(listOf(TestFixtures.podcast(id = "show-1"), TestFixtures.podcast(id = "show-2")), selected = true) | ||
|
|
||
| controller.recreate() | ||
| val after = showOnboarding() | ||
|
|
||
| assertSame(before, after) | ||
| assertEquals(1, builds) | ||
| assertEquals(setOf("show-1", "show-2"), after.uiState.value.selectedPodcasts.keys) | ||
| assertTrue(events.none { it.first == "onboarding_abandoned" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `finishing the activity after a recreation reports the unfinished onboarding once`() { | ||
| showOnboarding().onWelcomeScreenViewed() | ||
| controller.recreate() | ||
| showOnboarding() | ||
|
|
||
| controller.get().finish() | ||
| controller.pause().stop().destroy() | ||
|
|
||
| assertEquals(1, events.count { it.first == "onboarding_abandoned" }) | ||
| } | ||
| } |
142 changes: 142 additions & 0 deletions
142
app/src/test/java/cx/aswin/boxlore/ui/OnboardingViewModelScopeTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package cx.aswin.boxlore.ui | ||
|
|
||
| import android.app.Application | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.test.junit4.createComposeRule | ||
| import androidx.lifecycle.ViewModelStore | ||
| import androidx.lifecycle.ViewModelStoreOwner | ||
| import androidx.lifecycle.viewModelScope | ||
| import androidx.room.Room | ||
| import androidx.test.core.app.ApplicationProvider | ||
| import cx.aswin.boxlore.core.analytics.AnalyticsHelper | ||
| import cx.aswin.boxlore.core.catalog.PodcastRepository | ||
| import cx.aswin.boxlore.core.catalog.SubscriptionRepository | ||
| import cx.aswin.boxlore.core.database.BoxLoreDatabase | ||
| import cx.aswin.boxlore.core.prefs.BoxcastPrefs | ||
| import cx.aswin.boxlore.core.prefs.UserPreferencesRepository | ||
| import cx.aswin.boxlore.core.rss.RssPodcastRepository | ||
| import cx.aswin.boxlore.feature.onboarding.OnboardingViewModel | ||
| import cx.aswin.boxlore.feature.onboarding.markOnboardingCompletedSilent | ||
| import kotlinx.coroutines.isActive | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertSame | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Rule | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
| import org.robolectric.annotation.Config | ||
|
|
||
| /** | ||
| * The onboarding view model lives in the activity's view model store: every onboarding flow shares | ||
| * one instance (so picks carry over between search, Echo and the import sheet), and finishing the | ||
| * activity clears it. Created with `remember` it was never cleared, so an unfinished onboarding was | ||
| * never reported as abandoned and its Echo work was never cancelled. | ||
| */ | ||
| @RunWith(RobolectricTestRunner::class) | ||
| @Config(sdk = [34], application = Application::class) | ||
| class OnboardingViewModelScopeTest { | ||
| @get:Rule val composeRule = createComposeRule() | ||
|
|
||
| private val context: Application = ApplicationProvider.getApplicationContext() | ||
| private lateinit var database: BoxLoreDatabase | ||
| private val store = ViewModelStore() | ||
| private val activityOwner = object : ViewModelStoreOwner { | ||
| override val viewModelStore: ViewModelStore = store | ||
| } | ||
| private val events = mutableListOf<Pair<String, Map<String, Any>>>() | ||
| private lateinit var restoreAnalytics: () -> Unit | ||
| private var builds = 0 | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| database = Room.inMemoryDatabaseBuilder(context, BoxLoreDatabase::class.java).allowMainThreadQueries().build() | ||
| RssPodcastRepository.clearInstanceForTests() | ||
| BoxcastPrefs(context).setOnboardingCompleted(false) | ||
| restoreAnalytics = AnalyticsHelper.installRecordingSink(events) | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| store.clear() | ||
| restoreAnalytics() | ||
| database.close() | ||
| RssPodcastRepository.clearInstanceForTests() | ||
| } | ||
|
|
||
| private fun build(): OnboardingViewModel { | ||
| builds++ | ||
| val podcastRepository = | ||
| PodcastRepository( | ||
| baseUrl = "http://localhost/", | ||
| publicKey = "test-app-key", | ||
| context = context, | ||
| rssRepository = RssPodcastRepository.createForTests(context = context, database = database), | ||
| ) | ||
| return OnboardingViewModel(context, podcastRepository, SubscriptionRepository(database.podcastDao()), UserPreferencesRepository(context)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `every onboarding flow gets the same instance while the activity lives`() { | ||
| var showImportSheet by mutableStateOf(false) | ||
| var showOnboarding by mutableStateOf(true) | ||
| val seen = mutableListOf<OnboardingViewModel>() | ||
| composeRule.setContent { | ||
| // Separate call sites stand for the onboarding route, the import sheet and Home's AI entry. | ||
| if (showOnboarding) seen += rememberOnboardingViewModel(activityOwner, ::build) | ||
| if (showImportSheet) seen += rememberOnboardingViewModel(activityOwner, ::build) | ||
| } | ||
|
|
||
| composeRule.runOnIdle { showImportSheet = true } | ||
| // Leaving onboarding (e.g. for the sign-in detour) and coming back keeps the same instance. | ||
| composeRule.runOnIdle { showOnboarding = false } | ||
| composeRule.runOnIdle { showOnboarding = true } | ||
| composeRule.waitForIdle() | ||
|
|
||
| assertEquals(1, builds) | ||
| assertTrue(seen.size >= 3) | ||
| seen.forEach { assertSame(seen.first(), it) } | ||
| } | ||
|
|
||
| @Test | ||
| fun `finishing the activity mid-onboarding reports it abandoned once and stops its work`() { | ||
| lateinit var viewModel: OnboardingViewModel | ||
| composeRule.setContent { viewModel = rememberOnboardingViewModel(activityOwner, ::build) } | ||
| composeRule.runOnIdle { viewModel.onWelcomeScreenViewed() } | ||
|
|
||
| composeRule.runOnIdle { store.clear() } | ||
|
|
||
| val abandoned = events.filter { it.first == "onboarding_abandoned" } | ||
| assertEquals(1, abandoned.size) | ||
| assertTrue(abandoned.single().second.containsKey("last_step")) | ||
| assertFalse(viewModel.viewModelScope.isActive) | ||
| } | ||
|
|
||
| @Test | ||
| fun `a finished onboarding is not reported when the activity finishes`() { | ||
| lateinit var viewModel: OnboardingViewModel | ||
| composeRule.setContent { viewModel = rememberOnboardingViewModel(activityOwner, ::build) } | ||
| composeRule.runOnIdle { | ||
| viewModel.onWelcomeScreenViewed() | ||
| viewModel.markOnboardingCompletedSilent {} | ||
| } | ||
|
|
||
| composeRule.runOnIdle { store.clear() } | ||
|
|
||
| assertTrue(events.none { it.first == "onboarding_abandoned" }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `an onboarding never started is not reported when the activity finishes`() { | ||
| composeRule.setContent { rememberOnboardingViewModel(activityOwner, ::build) } | ||
|
|
||
| composeRule.runOnIdle { store.clear() } | ||
|
|
||
| assertTrue(events.none { it.first == "onboarding_abandoned" }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.