From 9836b3fc8a917a63ec93a228a5aa3f894dc3b3e2 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 7 Jun 2023 10:26:13 +0200 Subject: [PATCH] More fixes --- .../features/login/impl/LoginFlowNode.kt | 1 + .../AccountProviderPresenter.kt | 15 ++++----------- .../accountprovider/AccountProviderView.kt | 9 +-------- .../form/ChangeAccountProviderFormPresenter.kt | 18 +++++++++++++----- .../datasource/AccountProviderDataSource.kt | 4 ++++ 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt index 67a28d1569..3220d8b897 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/LoginFlowNode.kt @@ -192,6 +192,7 @@ class LoginFlowNode @AssistedInject constructor( DisposableEffect(Unit) { onDispose { activity = null + accountProviderDataSource.reset() } } Children( diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderPresenter.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderPresenter.kt index 9302ed7a48..5246f9c528 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderPresenter.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderPresenter.kt @@ -23,13 +23,11 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope -import androidx.compose.runtime.saveable.rememberSaveable import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject -import io.element.android.features.login.impl.error.ChangeServerError import io.element.android.features.login.impl.datasource.AccountProviderDataSource -import io.element.android.features.login.impl.util.LoginConstants +import io.element.android.features.login.impl.error.ChangeServerError import io.element.android.libraries.architecture.Async import io.element.android.libraries.architecture.Presenter import io.element.android.libraries.architecture.execute @@ -57,12 +55,8 @@ class AccountProviderPresenter @AssistedInject constructor( @Composable override fun present(): AccountProviderState { val accountProvider by accountProviderDataSource.flow().collectAsState() - val currentHomeServerDetails = authenticationService.getHomeserverDetails().collectAsState().value val localCoroutineScope = rememberCoroutineScope() - val homeserver = rememberSaveable { - mutableStateOf(currentHomeServerDetails?.url ?: LoginConstants.DEFAULT_HOMESERVER_URL) - } val loginFlowAction: MutableState> = remember { mutableStateOf(Async.Uninitialized) } @@ -70,7 +64,7 @@ class AccountProviderPresenter @AssistedInject constructor( fun handleEvents(event: AccountProviderEvents) { when (event) { AccountProviderEvents.Continue -> { - localCoroutineScope.submit(homeserver, loginFlowAction) + localCoroutineScope.submit(accountProvider.title, loginFlowAction) } AccountProviderEvents.ClearError -> loginFlowAction.value = Async.Uninitialized } @@ -85,12 +79,11 @@ class AccountProviderPresenter @AssistedInject constructor( } private fun CoroutineScope.submit( - homeserverUrl: MutableState, + homeserverUrl: String, loginFlowAction: MutableState>, ) = launch { suspend { - val domain = tryOrNull { URL(homeserverUrl.value) }?.host ?: homeserverUrl.value - homeserverUrl.value = domain + val domain = tryOrNull { URL(homeserverUrl) }?.host ?: homeserverUrl authenticationService.setHomeserver(domain).map { authenticationService.getHomeserverDetails().value!! }.map { diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderView.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderView.kt index 6c8b1df60e..36352416f9 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderView.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/accountprovider/AccountProviderView.kt @@ -114,14 +114,7 @@ fun AccountProviderView( } ) { when (state.loginFlow) { - is Async.Failure -> { - AsyncFailure( - throwable = state.loginFlow.error, - onRetry = { - state.eventSink.invoke(AccountProviderEvents.Continue) - } - ) - } + is Async.Failure -> Unit // Error dialog will be displayed is Async.Loading -> Unit // The Continue button shows the loading state is Async.Success -> { when (val loginFlowState = state.loginFlow.state) { diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/changeaccountprovider/form/ChangeAccountProviderFormPresenter.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/changeaccountprovider/form/ChangeAccountProviderFormPresenter.kt index 24da7892ee..157dc8a120 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/changeaccountprovider/form/ChangeAccountProviderFormPresenter.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/changeaccountprovider/form/ChangeAccountProviderFormPresenter.kt @@ -24,9 +24,9 @@ import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.runtime.saveable.rememberSaveable import io.element.android.libraries.architecture.Async import io.element.android.libraries.architecture.Presenter -import io.element.android.libraries.architecture.execute import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import javax.inject.Inject @@ -63,9 +63,17 @@ class ChangeAccountProviderFormPresenter @Inject constructor( ) } - private fun CoroutineScope.userInput(userInput: String, userInputResult: MutableState>>) = launch { - suspend { - homeserverResolver.resolve(userInput) - }.execute(userInputResult) + // Could be reworked using LaunchedEffect + private fun CoroutineScope.userInput(userInput: String, state: MutableState>>) = launch { + state.value = Async.Uninitialized + // Debounce + delay(300) + state.value = Async.Loading() + try { + val result = homeserverResolver.resolve(userInput) + state.value = Async.Success(result) + } catch (error: Throwable) { + state.value = Async.Failure(error) + } } } diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/datasource/AccountProviderDataSource.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/datasource/AccountProviderDataSource.kt index 1240124c5a..ebd1c15ae1 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/datasource/AccountProviderDataSource.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/datasource/AccountProviderDataSource.kt @@ -36,6 +36,10 @@ class AccountProviderDataSource @Inject constructor( return accountProvider.asStateFlow() } + fun reset() { + accountProvider.tryEmit(defaultAccountProvider) + } + fun userSelection(data: AccountProvider) { accountProvider.tryEmit(data) }