Save the selected account provider to history even if the flow default is reset

This commit is contained in:
Jenna Vassar
2026-08-10 09:28:32 -07:00
parent f8816b2449
commit bbb3a68087
3 changed files with 28 additions and 1 deletions
@@ -43,6 +43,12 @@ class AccountProviderDataSource(
val flow: StateFlow<AccountProvider> = accountProvider.asStateFlow()
// The account provider the user last explicitly selected (via [setAccountProvider] / [setUrl]).
// Unlike [flow], this is not recomputed by [reset], so it survives to be persisted to history on a
// successful sign-in even when the login flow is torn down in between (e.g. across an OAuth round-trip).
var lastSelectedAccountProviderUrl: String? = null
private set
init {
// Seed the default from the last used provider, unless the user has already selected one.
coroutineScope.launch {
@@ -72,6 +78,7 @@ class AccountProviderDataSource(
}
suspend fun setAccountProvider(data: AccountProvider) {
lastSelectedAccountProviderUrl = data.url
accountProvider.emit(data)
}
@@ -23,6 +23,10 @@ class SaveAccountProviderToHistory(
private val appPreferencesStore: AppPreferencesStore,
) {
suspend operator fun invoke() {
appPreferencesStore.addHomeserverToHistory(accountProviderDataSource.flow.value.url)
// Persist the provider the user actually selected, not the currently-exposed default: the latter can be
// reset back to the history default while the login flow is torn down across an OAuth round-trip.
val url = accountProviderDataSource.lastSelectedAccountProviderUrl
?: accountProviderDataSource.flow.value.url
appPreferencesStore.addHomeserverToHistory(url)
}
}
@@ -31,4 +31,20 @@ class SaveAccountProviderToHistoryTest {
assertThat(appPreferencesStore.getHomeserverHistoryFlow().first())
.containsExactly("https://example.com")
}
@Test
fun `invoke saves the selected provider even after the exposed default has been reset`() = runTest {
val appPreferencesStore = InMemoryAppPreferencesStore()
val accountProviderDataSource = anAccountProviderDataSource(appPreferencesStore = appPreferencesStore)
accountProviderDataSource.setUrl("https://example.com")
// Simulate the login flow being torn down (e.g. across an OAuth round-trip), which reverts the
// exposed default; the user's selection must still be the one saved to history.
accountProviderDataSource.reset()
val sut = SaveAccountProviderToHistory(accountProviderDataSource, appPreferencesStore)
sut()
assertThat(appPreferencesStore.getHomeserverHistoryFlow().first())
.containsExactly("https://example.com")
}
}