diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/SaveApiAccessMethodDialog.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/SaveApiAccessMethodDialog.kt index c0786c3b1ddc..7156dde91119 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/SaveApiAccessMethodDialog.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/SaveApiAccessMethodDialog.kt @@ -70,7 +70,7 @@ fun SaveApiAccessMethod( val state by viewModel.uiState.collectAsStateWithLifecycle() SaveApiAccessMethodDialog( state = state, - onCancel = { backNavigator.navigateBack() }, + onCancel = backNavigator::navigateBack, onSave = viewModel::save ) } @@ -104,7 +104,7 @@ fun SaveApiAccessMethodDialog( } }, title = { Text(text = state.text(), style = MaterialTheme.typography.headlineSmall) }, - onDismissRequest = { /*Should not be able to dismiss*/}, + onDismissRequest = { /*Should not be able to dismiss*/ }, confirmButton = { PrimaryButton( onClick = onCancel, diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/EditApiAccessMethodViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/EditApiAccessMethodViewModel.kt index 4c84ae1f9f40..94242f109689 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/EditApiAccessMethodViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/EditApiAccessMethodViewModel.kt @@ -5,12 +5,10 @@ import androidx.lifecycle.viewModelScope import arrow.core.Either import arrow.core.Either.Companion.zipOrAccumulate import arrow.core.EitherNel -import arrow.core.NonEmptyList import arrow.core.getOrElse import arrow.core.nel import arrow.core.raise.either import arrow.core.raise.ensure -import arrow.core.right import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.SharingStarted @@ -96,10 +94,10 @@ class EditApiAccessMethodViewModel( fun testMethod() { viewModelScope.launch { formData.value - .parseFormData(skipNameValidation = true) + .parseConnectionFormData() .fold( { errors -> formData.update { it.updateWithErrors(errors) } }, - { (_, customProxy) -> + { customProxy -> isTestingApiAccessMethod.value = true apiAccessRepository .testCustomApiAccessMethod(customProxy) @@ -124,7 +122,7 @@ class EditApiAccessMethodViewModel( fun trySave() { viewModelScope.launch { formData.value - .parseFormData(skipNameValidation = false) + .parseFormData() .fold( { errors -> formData.update { it.updateWithErrors(errors) } }, { (name, customProxy) -> @@ -158,30 +156,22 @@ class EditApiAccessMethodViewModel( .getOrElse { error("Access method with id $apiAccessMethodId not found") } } - private fun EditApiAccessFormData.parseFormData( - skipNameValidation: Boolean - ): Either< - NonEmptyList, - Pair - > = - zipOrAccumulate( - if (skipNameValidation) { - ApiAccessMethodName.fromString(name).right() - } else { - parseName(name) - }, - when (apiAccessMethodTypes) { - ApiAccessMethodTypes.SHADOWSOCKS -> { - parseShadowSocksFormData(this) - } - ApiAccessMethodTypes.SOCKS5_REMOTE -> { - parseSocks5RemoteFormData(this) - } - } - ) { name, customProxy -> + private fun EditApiAccessFormData.parseFormData(): + EitherNel> = + zipOrAccumulate(parseName(name), parseConnectionFormData()) { name, customProxy -> name to customProxy } + private fun EditApiAccessFormData.parseConnectionFormData() = + when (apiAccessMethodTypes) { + ApiAccessMethodTypes.SHADOWSOCKS -> { + parseShadowSocksFormData(this) + } + ApiAccessMethodTypes.SOCKS5_REMOTE -> { + parseSocks5RemoteFormData(this) + } + } + private fun parseShadowSocksFormData( formData: EditApiAccessFormData ): EitherNel = @@ -216,7 +206,7 @@ class EditApiAccessMethodViewModel( private fun parseSocks5RemoteFormData( formData: EditApiAccessFormData - ): Either, ApiAccessMethodType.CustomProxy.Socks5Remote> = + ): EitherNel = zipOrAccumulate( parseIpAndPort(formData.serverIp, formData.port), parseAuth( @@ -240,7 +230,7 @@ class EditApiAccessMethodViewModel( authEnabled: Boolean, inputUsername: String, inputPassword: String - ): Either, SocksAuth?> = + ): EitherNel = if (!authEnabled) { Either.Right(null) } else {