Skip to content

Commit

Permalink
Minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Jun 11, 2024
1 parent 37a919f commit b18ad00
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fun SaveApiAccessMethod(
val state by viewModel.uiState.collectAsStateWithLifecycle()
SaveApiAccessMethodDialog(
state = state,
onCancel = { backNavigator.navigateBack() },
onCancel = backNavigator::navigateBack,
onSave = viewModel::save
)
}
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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) ->
Expand Down Expand Up @@ -158,30 +156,22 @@ class EditApiAccessMethodViewModel(
.getOrElse { error("Access method with id $apiAccessMethodId not found") }
}

private fun EditApiAccessFormData.parseFormData(
skipNameValidation: Boolean
): Either<
NonEmptyList<InvalidDataError>,
Pair<ApiAccessMethodName, ApiAccessMethodType.CustomProxy>
> =
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<InvalidDataError, Pair<ApiAccessMethodName, ApiAccessMethodType.CustomProxy>> =
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<InvalidDataError, ApiAccessMethodType.CustomProxy.Shadowsocks> =
Expand Down Expand Up @@ -216,7 +206,7 @@ class EditApiAccessMethodViewModel(

private fun parseSocks5RemoteFormData(
formData: EditApiAccessFormData
): Either<NonEmptyList<InvalidDataError>, ApiAccessMethodType.CustomProxy.Socks5Remote> =
): EitherNel<InvalidDataError, ApiAccessMethodType.CustomProxy.Socks5Remote> =
zipOrAccumulate(
parseIpAndPort(formData.serverIp, formData.port),
parseAuth(
Expand All @@ -240,7 +230,7 @@ class EditApiAccessMethodViewModel(
authEnabled: Boolean,
inputUsername: String,
inputPassword: String
): Either<NonEmptyList<InvalidDataError>, SocksAuth?> =
): EitherNel<InvalidDataError, SocksAuth?> =
if (!authEnabled) {
Either.Right(null)
} else {
Expand Down

0 comments on commit b18ad00

Please sign in to comment.