diff options
| author | Albin <albin@mullvad.net> | 2022-04-27 11:55:48 +0200 |
|---|---|---|
| committer | Albin <albin@mullvad.net> | 2022-05-17 15:06:04 +0200 |
| commit | dca2d61be24a65e662036dc0a4bb38b4c76688d6 (patch) | |
| tree | baf9ef4adec19334c3d7824a39f5c2d2ec7493c8 /android/app/src | |
| parent | 2a18fa4b7255a93e94691a47a34ed87073851a35 (diff) | |
| download | mullvadvpn-dca2d61be24a65e662036dc0a4bb38b4c76688d6.tar.xz mullvadvpn-dca2d61be24a65e662036dc0a4bb38b4c76688d6.zip | |
Use separate event for Android account creation
Diffstat (limited to 'android/app/src')
5 files changed, 53 insertions, 2 deletions
diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ipc/Event.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ipc/Event.kt index 7fe971e678..4f2d5857fd 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ipc/Event.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ipc/Event.kt @@ -3,6 +3,7 @@ package net.mullvad.mullvadvpn.ipc import android.os.Message as RawMessage import android.os.Messenger import kotlinx.parcelize.Parcelize +import net.mullvad.mullvadvpn.model.AccountCreationResult import net.mullvad.mullvadvpn.model.AppVersionInfo as AppVersionInfoData import net.mullvad.mullvadvpn.model.DeviceState import net.mullvad.mullvadvpn.model.GeoIpLocation @@ -18,6 +19,9 @@ sealed class Event : Message.EventMessage() { protected override val messageKey = MESSAGE_KEY @Parcelize + data class AccountCreationEvent(val result: AccountCreationResult) : Event() + + @Parcelize data class AccountHistory(val history: String?) : Event() @Parcelize diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/model/AccountCreationResult.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/model/AccountCreationResult.kt new file mode 100644 index 0000000000..23115b606d --- /dev/null +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/model/AccountCreationResult.kt @@ -0,0 +1,12 @@ +package net.mullvad.mullvadvpn.model + +import android.os.Parcelable +import kotlinx.parcelize.Parcelize + +sealed class AccountCreationResult : Parcelable { + @Parcelize + data class Success(val accountToken: String) : AccountCreationResult() + + @Parcelize + object Failure : AccountCreationResult() +} diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt index ea72a8379a..78ee4cb850 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/service/endpoint/AccountCache.kt @@ -9,6 +9,7 @@ import kotlinx.coroutines.channels.sendBlocking import kotlinx.coroutines.delay import net.mullvad.mullvadvpn.ipc.Event import net.mullvad.mullvadvpn.ipc.Request +import net.mullvad.mullvadvpn.model.AccountCreationResult import net.mullvad.mullvadvpn.model.GetAccountDataResult import net.mullvad.mullvadvpn.model.LoginResult import net.mullvad.mullvadvpn.model.LoginStatus @@ -179,6 +180,16 @@ class AccountCache(private val endpoint: ServiceEndpoint) { createdAccountExpiry = null daemon.await().createNewAccount() + .let { newAccountNumber -> + if (newAccountNumber != null) { + AccountCreationResult.Success(newAccountNumber) + } else { + AccountCreationResult.Failure + } + } + .also { result -> + endpoint.sendEvent(Event.AccountCreationEvent(result)) + } } private suspend fun doLogin(account: String) { diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt index 6bec5c88b5..d74efa66ad 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/serviceconnection/AccountCache.kt @@ -1,9 +1,13 @@ package net.mullvad.mullvadvpn.ui.serviceconnection import android.os.Messenger +import kotlinx.coroutines.channels.BufferOverflow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow import net.mullvad.mullvadvpn.ipc.Event import net.mullvad.mullvadvpn.ipc.EventDispatcher import net.mullvad.mullvadvpn.ipc.Request +import net.mullvad.mullvadvpn.model.AccountCreationResult import net.mullvad.mullvadvpn.model.LoginStatus import net.mullvad.talpid.util.EventNotifier import org.joda.time.DateTime @@ -16,6 +20,12 @@ class AccountCache(private val connection: Messenger, eventDispatcher: EventDisp private var accountHistory by onAccountHistoryChange.notifiable() private var loginStatus by onLoginStatusChange.notifiable() + private val _accountCreationEvents = MutableSharedFlow<AccountCreationResult>( + extraBufferCapacity = 1, + onBufferOverflow = BufferOverflow.DROP_OLDEST + ) + val accountCreationEvents = _accountCreationEvents.asSharedFlow() + init { eventDispatcher.apply { registerHandler(Event.AccountHistory::class) { event -> @@ -26,6 +36,10 @@ class AccountCache(private val connection: Messenger, eventDispatcher: EventDisp loginStatus = event.status onAccountExpiryChange.notifyIfChanged(loginStatus?.expiry) } + + registerHandler(Event.AccountCreationEvent::class) { event -> + _accountCreationEvents.tryEmit(event.result) + } } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt index 55079458fa..67662b884c 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LoginViewModel.kt @@ -5,8 +5,11 @@ import androidx.annotation.RestrictTo import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.viewModelScope import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.launch import net.mullvad.mullvadvpn.model.LoginResult import net.mullvad.mullvadvpn.ui.serviceconnection.AccountCache @@ -47,8 +50,15 @@ class LoginViewModel( } fun createAccount() { - _uiState.value = LoginUiState.CreatingAccount - accountCache?.createNewAccount() + accountCache?.apply { + _uiState.value = LoginUiState.CreatingAccount + + viewModelScope.launch { + _uiState.value = accountCreationEvents.first().mapToUiState() + } + + createNewAccount() + } } fun login(accountToken: String) { |
