This repository has been archived by the owner on Oct 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for changing email address
- Loading branch information
Showing
9 changed files
with
150 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
app/src/main/java/dev/medzik/librepass/android/ui/screens/settings/account/ChangeEmail.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package dev.medzik.librepass.android.ui.screens.settings.account | ||
|
||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.input.KeyboardType | ||
import androidx.compose.ui.unit.dp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import androidx.navigation.NavController | ||
import dev.medzik.android.components.LoadingButton | ||
import dev.medzik.android.components.navigate | ||
import dev.medzik.android.components.rememberMutableBoolean | ||
import dev.medzik.android.components.rememberMutableString | ||
import dev.medzik.android.utils.runOnUiThread | ||
import dev.medzik.librepass.android.R | ||
import dev.medzik.librepass.android.ui.LibrePassViewModel | ||
import dev.medzik.librepass.android.ui.Screen | ||
import dev.medzik.librepass.android.ui.components.TextInputField | ||
import dev.medzik.librepass.android.utils.showErrorToast | ||
import dev.medzik.librepass.client.Server | ||
import dev.medzik.librepass.client.api.UserClient | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.runBlocking | ||
|
||
@Composable | ||
fun SettingsAccountChangeEmailScreen( | ||
navController: NavController, | ||
viewModel: LibrePassViewModel = hiltViewModel() | ||
) { | ||
val context = LocalContext.current | ||
val credentials = viewModel.credentialRepository.get() ?: return | ||
|
||
var newEmail by rememberMutableString() | ||
var password by rememberMutableString() | ||
var loading by rememberMutableBoolean() | ||
|
||
val scope = rememberCoroutineScope() | ||
|
||
val userClient = | ||
UserClient( | ||
email = credentials.email, | ||
apiKey = credentials.apiKey, | ||
apiUrl = credentials.apiUrl ?: Server.PRODUCTION | ||
) | ||
|
||
fun changeEmail( | ||
newEmail: String, | ||
password: String | ||
) { | ||
loading = true | ||
|
||
// TODO: show error "invalid password" | ||
|
||
scope.launch(Dispatchers.IO) { | ||
try { | ||
userClient.changeEmail(newEmail, password) | ||
|
||
runBlocking { | ||
viewModel.credentialRepository.drop() | ||
viewModel.cipherRepository.drop(credentials.userId) | ||
context | ||
} | ||
|
||
runOnUiThread { | ||
navController.navigate( | ||
screen = Screen.Welcome, | ||
disableBack = true | ||
) | ||
} | ||
} catch (e: Exception) { | ||
e.showErrorToast(context) | ||
|
||
loading = false | ||
} | ||
} | ||
} | ||
|
||
TextInputField( | ||
label = stringResource(R.string.NewEmail), | ||
value = newEmail, | ||
onValueChange = { newEmail = it }, | ||
isError = newEmail.isNotEmpty() && !newEmail.contains('@'), | ||
errorMessage = stringResource(R.string.Error_InvalidEmail), | ||
keyboardType = KeyboardType.Email | ||
) | ||
|
||
TextInputField( | ||
label = stringResource(R.string.Password), | ||
value = password, | ||
onValueChange = { password = it }, | ||
emptySupportingText = true, | ||
hidden = true, | ||
keyboardType = KeyboardType.Password, | ||
) | ||
|
||
LoadingButton( | ||
loading = loading, | ||
onClick = { changeEmail(newEmail, password) }, | ||
enabled = newEmail.isNotEmpty() && newEmail.contains('@') && password.isNotEmpty(), | ||
modifier = | ||
Modifier | ||
.fillMaxWidth() | ||
.padding(horizontal = 40.dp, vertical = 8.dp) | ||
) { | ||
Text(stringResource(R.string.ChangeEmail)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters