Skip to content

Commit

Permalink
show error message instead of toast message when signup
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Dec 11, 2024
1 parent a076307 commit c630f9d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/greenart7c3/nostrsigner/LocalPreferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import com.greenart7c3.nostrsigner.ui.parseBiometricsTimeType
import com.vitorpamplona.ammolite.relays.COMMON_FEED_TYPES
import com.vitorpamplona.ammolite.relays.RelaySetupInfo
import com.vitorpamplona.ammolite.service.HttpClientManager
import com.vitorpamplona.quartz.crypto.CryptoUtils
import com.vitorpamplona.quartz.crypto.KeyPair
import com.vitorpamplona.quartz.encoders.HexKey
import com.vitorpamplona.quartz.encoders.hexToByteArray
import com.vitorpamplona.quartz.encoders.toHexKey
import com.vitorpamplona.quartz.encoders.toNpub
Expand Down Expand Up @@ -58,6 +60,7 @@ private object PrefKeys {
const val PROFILE_URL = "profile_url"
const val LAST_METADATA_UPDATE = "last_metadata_update"
const val LAST_CHECK = "last_check"
const val NCRYPT_SEC = "ncrypt_sec"
}

@Immutable
Expand Down Expand Up @@ -350,6 +353,18 @@ object LocalPreferences {
}
}

suspend fun saveNcryptsec(
npub: String,
privateKeyHex: HexKey,
password: String,
) {
val context = NostrSigner.getInstance()
val ncryptsec = CryptoUtils.nip49.encrypt(privateKeyHex, password)
encryptedPreferences(context, npub).edit().apply {
putString(PrefKeys.NCRYPT_SEC, ncryptsec)
}.apply()
}

fun updateProxy(
context: Context,
useProxy: Boolean,
Expand Down
43 changes: 36 additions & 7 deletions app/src/main/java/com/greenart7c3/nostrsigner/ui/LoginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ import com.anggrayudi.storage.SimpleStorageHelper
import com.anggrayudi.storage.file.CreateMode
import com.anggrayudi.storage.file.makeFile
import com.anggrayudi.storage.file.openOutputStream
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.NostrSigner
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.ui.components.AmberButton
import com.greenart7c3.nostrsigner.ui.components.AmberElevatedButton
Expand Down Expand Up @@ -454,12 +456,13 @@ fun SignUpPage(
capitalization = KeyboardCapitalization.None,
keyboardType = KeyboardType.Password,
autoCorrectEnabled = false,
imeAction = ImeAction.Next,
imeAction = ImeAction.Done,
),
modifier = Modifier.fillMaxWidth(),
)

AmberButton(
enabled = nickname.text.isNotBlank() && password.text.isNotBlank(),
modifier = Modifier.padding(vertical = 40.dp),
onClick = {
if (nickname.text.isBlank()) {
Expand Down Expand Up @@ -489,6 +492,7 @@ fun SignUpPage(

1 -> {
var showPassword by remember { mutableStateOf(false) }
var errorMessage by remember { mutableStateOf("") }
Column(
Modifier
.fillMaxSize()
Expand All @@ -505,6 +509,9 @@ fun SignUpPage(
value = password2,
onValueChange = { value ->
password2 = value
if (errorMessage.isNotBlank()) {
errorMessage = ""
}
},
trailingIcon = {
Row {
Expand Down Expand Up @@ -539,11 +546,7 @@ fun SignUpPage(
AmberButton(
onClick = {
if (password2.text != password.text) {
Toast.makeText(
context,
"Passwords do not match",
Toast.LENGTH_SHORT,
).show()
errorMessage = "Passwords do not match"
return@AmberButton
}
storageHelper.openFolderPicker()
Expand Down Expand Up @@ -615,9 +618,27 @@ fun SignUpPage(
}
}
}

if (errorMessage.isNotBlank()) {
Text(
text = errorMessage,
color = Color.Red,
)
}

AmberButton(
enabled = enabled,
enabled = password2.text.isNotBlank(),
onClick = {
if (password2.text.isBlank()) {
errorMessage = "Password is required"
return@AmberButton
}

if (password2.text != password.text) {
errorMessage = "Passwords do not match"
return@AmberButton
}

scope.launch {
state.animateScrollToPage(2)
}
Expand Down Expand Up @@ -800,6 +821,14 @@ fun SignUpPage(
name = nickname.text,
)

NostrSigner.getInstance().applicationIOScope.launch(Dispatchers.IO) {
LocalPreferences.saveNcryptsec(
keyPair.pubKey.toNpub(),
keyPair.privKey!!.toHexKey(),
password.text,
)
}

onFinish()
},
text = stringResource(R.string.finish),
Expand Down

0 comments on commit c630f9d

Please sign in to comment.