mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 12:07:28 +00:00
Save the selected account provider to history even if the flow default is reset
This commit is contained in:
+7
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
+5
-1
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
+16
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user