Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Use runOnUiThread instead of scope.launch(Dispatchers.Main)
Browse files Browse the repository at this point in the history
  • Loading branch information
M3DZIK committed Oct 29, 2023
1 parent 29e4202 commit f39baa7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ fun LoginScreen(navController: NavController) {
}

// navigate to dashboard
scope.launch(Dispatchers.Main) {
runOnUiThread {
navController.navigate(
screen = Screen.Vault,
disableBack = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import dev.medzik.android.components.navigate
import dev.medzik.android.components.rememberDialogState
import dev.medzik.android.components.rememberMutableBoolean
import dev.medzik.android.components.rememberMutableString
import dev.medzik.android.utils.runOnUiThread
import dev.medzik.android.utils.showToast
import dev.medzik.librepass.android.BuildConfig
import dev.medzik.librepass.android.R
Expand Down Expand Up @@ -49,7 +50,10 @@ fun RegisterScreen(navController: NavController) {
var server by rememberMutableString(Server.PRODUCTION)

// Register user with given credentials and navigate to log in screen.
fun submit(email: String, password: String) {
fun submit(
email: String,
password: String
) {
val authClient = AuthClient(apiUrl = server)

// disable button
Expand All @@ -60,7 +64,7 @@ fun RegisterScreen(navController: NavController) {
authClient.register(email, password, passwordHint)

// navigate to login
scope.launch(Dispatchers.Main) {
runOnUiThread {
context.showToast(R.string.Success_Registration_Please_Verify)

navController.navigate(
Expand Down Expand Up @@ -129,9 +133,10 @@ fun RegisterScreen(navController: NavController) {
}

Row(
modifier = Modifier
.padding(vertical = 8.dp)
.clickable { serverChoiceDialog.show() }
modifier =
Modifier
.padding(vertical = 8.dp)
.clickable { serverChoiceDialog.show() }
) {
Text(
text = stringResource(R.string.Server_AuthScreen_Server_Address) + ": ",
Expand All @@ -150,16 +155,18 @@ fun RegisterScreen(navController: NavController) {
loading = loading,
onClick = { submit(email, password) },
enabled = email.contains("@") && password.length >= 8,
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 40.dp)
modifier =
Modifier
.fillMaxWidth()
.padding(horizontal = 40.dp)
) {
Text(stringResource(R.string.Button_Register))
}

var servers = listOf(Server.PRODUCTION)
.plus(context.readKey(StoreKey.CustomServers))
.plus("custom_server")
var servers =
listOf(Server.PRODUCTION)
.plus(context.readKey(StoreKey.CustomServers))
.plus("custom_server")

if (BuildConfig.DEBUG) servers = servers.plus(Server.TEST)

Expand All @@ -170,22 +177,26 @@ fun RegisterScreen(navController: NavController) {
onSelected = {
if (it == "custom_server") {
navController.navigate(Screen.AddCustomServer)
} else server = it
} else {
server = it
}
}
) {
val text = when (it) {
"custom_server" -> {
stringResource(R.string.Server_Choice_Dialog_Add_Custom)
}
val text =
when (it) {
"custom_server" -> {
stringResource(R.string.Server_Choice_Dialog_Add_Custom)
}

else -> getServerName(it)
}
else -> getServerName(it)
}

Text(
text = text,
modifier = Modifier
.padding(vertical = 12.dp)
.fillMaxWidth()
modifier =
Modifier
.padding(vertical = 12.dp)
.fillMaxWidth()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import dev.medzik.android.components.navigate
import dev.medzik.android.components.rememberMutableBoolean
import dev.medzik.android.components.rememberMutableString
import dev.medzik.android.crypto.KeyStore
import dev.medzik.android.utils.runOnUiThread
import dev.medzik.android.utils.showToast
import dev.medzik.libcrypto.Argon2
import dev.medzik.libcrypto.Hex
Expand Down Expand Up @@ -91,7 +92,7 @@ fun UnlockScreen(navController: NavController) {

// run only if loading is true (if no error occurred)
if (loading) {
scope.launch(Dispatchers.Main) {
runOnUiThread {
navController.navigate(
screen = Screen.Vault,
disableBack = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import dev.medzik.android.components.SecondaryText
import dev.medzik.android.components.navigate
import dev.medzik.android.components.rememberMutable
import dev.medzik.android.components.rememberMutableBoolean
import dev.medzik.android.utils.runOnUiThread
import dev.medzik.librepass.android.R
import dev.medzik.librepass.android.data.CipherTable
import dev.medzik.librepass.android.data.getRepository
Expand Down Expand Up @@ -58,8 +59,9 @@ fun CipherAddEditView(
) {
val context = LocalContext.current

val userSecrets = context.getUserSecrets()
?: return
val userSecrets =
context.getUserSecrets()
?: return

val scope = rememberCoroutineScope()
var loading by rememberMutableBoolean()
Expand All @@ -69,10 +71,11 @@ fun CipherAddEditView(
val credentials = repository.credentials.get()!!
val cipherRepository = repository.cipher

val cipherClient = CipherClient(
apiKey = credentials.apiKey,
apiUrl = credentials.apiUrl ?: Server.PRODUCTION
)
val cipherClient =
CipherClient(
apiKey = credentials.apiKey,
apiUrl = credentials.apiUrl ?: Server.PRODUCTION
)

// observe username and password from navController
// used to get password from password generator
Expand All @@ -99,13 +102,14 @@ fun CipherAddEditView(
loading = true

// update existing cipher or create new one
var cipher = baseCipher?.copy(loginData = cipherData)
?: Cipher(
id = UUID.randomUUID(),
owner = credentials.userId,
type = CipherType.Login,
loginData = cipherData
)
var cipher =
baseCipher?.copy(loginData = cipherData)
?: Cipher(
id = UUID.randomUUID(),
owner = credentials.userId,
type = CipherType.Login,
loginData = cipherData
)

scope.launch(Dispatchers.IO) {
if (baseCipher != null) {
Expand All @@ -130,15 +134,17 @@ fun CipherAddEditView(
// insert or update cipher on server
if (baseCipher == null)
cipherClient.insert(encryptedCipher)
else cipherClient.update(encryptedCipher)
else
cipherClient.update(encryptedCipher)

// insert or update cipher in a local database
val cipherTable = CipherTable(encryptedCipher)
if (baseCipher == null)
cipherRepository.insert(cipherTable)
else cipherRepository.update(cipherTable)
else
cipherRepository.update(cipherTable)

scope.launch(Dispatchers.Main) { navController.popBackStack() }
runOnUiThread { navController.popBackStack() }
} catch (e: Exception) {
loading = false
e.showErrorToast(context)
Expand All @@ -162,16 +168,18 @@ fun CipherAddEditView(
}
) { innerPadding ->
Column(
modifier = Modifier
.padding(innerPadding)
.padding(horizontal = 16.dp)
.verticalScroll(rememberScrollState())
modifier =
Modifier
.padding(innerPadding)
.padding(horizontal = 16.dp)
.verticalScroll(rememberScrollState())
) {
TextInputFieldBase(
label = stringResource(R.string.CipherField_Name),
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
modifier =
Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
value = cipherData.name,
onValueChange = { cipherData = cipherData.copy(name = it) }
)
Expand All @@ -183,18 +191,20 @@ fun CipherAddEditView(

TextInputFieldBase(
label = stringResource(R.string.CipherField_Username),
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
modifier =
Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
value = cipherData.username,
onValueChange = { cipherData = cipherData.copy(username = it) }
)

TextInputFieldBase(
label = stringResource(R.string.CipherField_Password),
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
modifier =
Modifier
.fillMaxWidth()
.padding(vertical = 4.dp),
value = cipherData.password,
onValueChange = { cipherData = cipherData.copy(password = it) },
hidden = true,
Expand Down Expand Up @@ -228,19 +238,23 @@ fun CipherAddEditView(
modifier = Modifier.fillMaxWidth(),
value = uri,
onValueChange = {
cipherData = cipherData.copy(
uris = cipherData.uris.orEmpty().toMutableList().apply {
this[index] = it
}
)
cipherData =
cipherData.copy(
uris =
cipherData.uris.orEmpty().toMutableList().apply {
this[index] = it
}
)
},
trailingIcon = {
IconButton(onClick = {
cipherData = cipherData.copy(
uris = cipherData.uris.orEmpty().toMutableList().apply {
this.removeAt(index)
}
)
cipherData =
cipherData.copy(
uris =
cipherData.uris.orEmpty().toMutableList().apply {
this.removeAt(index)
}
)
}) {
Icon(
imageVector = Icons.Default.Delete,
Expand All @@ -254,14 +268,16 @@ fun CipherAddEditView(
// button for adding more fields
Button(
onClick = {
cipherData = cipherData.copy(
uris = cipherData.uris.orEmpty() + ""
)
cipherData =
cipherData.copy(
uris = cipherData.uris.orEmpty() + ""
)
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 60.dp)
.padding(top = 8.dp)
modifier =
Modifier
.fillMaxWidth()
.padding(horizontal = 60.dp)
.padding(top = 8.dp)
) {
Text(stringResource(R.string.Button_AddField))
}
Expand All @@ -283,10 +299,11 @@ fun CipherAddEditView(
loading = loading,
onClick = { submit() },
enabled = cipherData.name.isNotEmpty(),
modifier = Modifier
.fillMaxWidth()
.padding(top = 16.dp)
.padding(horizontal = 40.dp)
modifier =
Modifier
.fillMaxWidth()
.padding(top = 16.dp)
.padding(horizontal = 40.dp)
) {
Text(
stringResource(
Expand Down

0 comments on commit f39baa7

Please sign in to comment.