mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-10 19:44:10 +02:00
add login with amber button
This commit is contained in:
@@ -74,6 +74,7 @@ private object PrefKeys {
|
|||||||
const val THEME = "theme"
|
const val THEME = "theme"
|
||||||
const val PREFERRED_LANGUAGE = "preferred_Language"
|
const val PREFERRED_LANGUAGE = "preferred_Language"
|
||||||
const val AUTOMATICALLY_LOAD_URL_PREVIEW = "automatically_load_url_preview"
|
const val AUTOMATICALLY_LOAD_URL_PREVIEW = "automatically_load_url_preview"
|
||||||
|
const val LOGIN_WITH_AMBER = "login_with_amber"
|
||||||
val LAST_READ: (String) -> String = { route -> "last_read_route_$route" }
|
val LAST_READ: (String) -> String = { route -> "last_read_route_$route" }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,6 +246,7 @@ object LocalPreferences {
|
|||||||
} else {
|
} else {
|
||||||
putBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, account.showSensitiveContent!!)
|
putBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, account.showSensitiveContent!!)
|
||||||
}
|
}
|
||||||
|
putBoolean(PrefKeys.LOGIN_WITH_AMBER, account.loginWithAmber)
|
||||||
}.apply()
|
}.apply()
|
||||||
|
|
||||||
val globalPrefs = encryptedPreferences()
|
val globalPrefs = encryptedPreferences()
|
||||||
@@ -363,6 +365,7 @@ object LocalPreferences {
|
|||||||
val useProxy = getBoolean(PrefKeys.USE_PROXY, false)
|
val useProxy = getBoolean(PrefKeys.USE_PROXY, false)
|
||||||
val proxyPort = getInt(PrefKeys.PROXY_PORT, 9050)
|
val proxyPort = getInt(PrefKeys.PROXY_PORT, 9050)
|
||||||
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
||||||
|
val loginWithAmber = getBoolean(PrefKeys.LOGIN_WITH_AMBER, false)
|
||||||
|
|
||||||
val showSensitiveContent = if (contains(PrefKeys.SHOW_SENSITIVE_CONTENT)) {
|
val showSensitiveContent = if (contains(PrefKeys.SHOW_SENSITIVE_CONTENT)) {
|
||||||
getBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, false)
|
getBoolean(PrefKeys.SHOW_SENSITIVE_CONTENT, false)
|
||||||
@@ -431,7 +434,8 @@ object LocalPreferences {
|
|||||||
warnAboutPostsWithReports = warnAboutReports,
|
warnAboutPostsWithReports = warnAboutReports,
|
||||||
filterSpamFromStrangers = filterSpam,
|
filterSpamFromStrangers = filterSpam,
|
||||||
lastReadPerRoute = lastReadPerRoute,
|
lastReadPerRoute = lastReadPerRoute,
|
||||||
settings = settings
|
settings = settings,
|
||||||
|
loginWithAmber = loginWithAmber
|
||||||
)
|
)
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
@@ -84,7 +84,8 @@ class Account(
|
|||||||
var warnAboutPostsWithReports: Boolean = true,
|
var warnAboutPostsWithReports: Boolean = true,
|
||||||
var filterSpamFromStrangers: Boolean = true,
|
var filterSpamFromStrangers: Boolean = true,
|
||||||
var lastReadPerRoute: Map<String, Long> = mapOf<String, Long>(),
|
var lastReadPerRoute: Map<String, Long> = mapOf<String, Long>(),
|
||||||
var settings: Settings = Settings()
|
var settings: Settings = Settings(),
|
||||||
|
var loginWithAmber: Boolean = false
|
||||||
) {
|
) {
|
||||||
var transientHiddenUsers: ImmutableSet<String> = persistentSetOf()
|
var transientHiddenUsers: ImmutableSet<String> = persistentSetOf()
|
||||||
|
|
||||||
|
@@ -52,7 +52,8 @@ enum class SignerType {
|
|||||||
NIP04_ENCRYPT,
|
NIP04_ENCRYPT,
|
||||||
NIP04_DECRYPT,
|
NIP04_DECRYPT,
|
||||||
NIP44_ENCRYPT,
|
NIP44_ENCRYPT,
|
||||||
NIP44_DECRYPT
|
NIP44_DECRYPT,
|
||||||
|
GET_PUBLIC_KEY
|
||||||
}
|
}
|
||||||
|
|
||||||
fun openAmber(
|
fun openAmber(
|
||||||
@@ -69,6 +70,7 @@ fun openAmber(
|
|||||||
SignerType.NIP04_DECRYPT -> "nip04_decrypt"
|
SignerType.NIP04_DECRYPT -> "nip04_decrypt"
|
||||||
SignerType.NIP44_ENCRYPT -> "nip44_encrypt"
|
SignerType.NIP44_ENCRYPT -> "nip44_encrypt"
|
||||||
SignerType.NIP44_DECRYPT -> "nip44_decrypt"
|
SignerType.NIP44_DECRYPT -> "nip44_decrypt"
|
||||||
|
SignerType.GET_PUBLIC_KEY -> "get_public_key"
|
||||||
}
|
}
|
||||||
intent.putExtra("type", signerType)
|
intent.putExtra("type", signerType)
|
||||||
intent.putExtra("pubKey", pubKey)
|
intent.putExtra("pubKey", pubKey)
|
||||||
|
@@ -45,21 +45,21 @@ class AccountStateViewModel(val context: Context) : ViewModel() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun startUI(key: String, useProxy: Boolean, proxyPort: Int) {
|
fun startUI(key: String, useProxy: Boolean, proxyPort: Int, loginWithAmber: Boolean = false) {
|
||||||
val parsed = Nip19.uriToRoute(key)
|
val parsed = Nip19.uriToRoute(key)
|
||||||
val pubKeyParsed = parsed?.hex?.hexToByteArray()
|
val pubKeyParsed = parsed?.hex?.hexToByteArray()
|
||||||
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
val proxy = HttpClient.initProxy(useProxy, "127.0.0.1", proxyPort)
|
||||||
|
|
||||||
val account =
|
val account =
|
||||||
if (key.startsWith("nsec")) {
|
if (key.startsWith("nsec")) {
|
||||||
Account(KeyPair(privKey = key.bechToBytes()), proxy = proxy, proxyPort = proxyPort)
|
Account(KeyPair(privKey = key.bechToBytes()), proxy = proxy, proxyPort = proxyPort, loginWithAmber = loginWithAmber)
|
||||||
} else if (pubKeyParsed != null) {
|
} else if (pubKeyParsed != null) {
|
||||||
Account(KeyPair(pubKey = pubKeyParsed), proxy = proxy, proxyPort = proxyPort)
|
Account(KeyPair(pubKey = pubKeyParsed), proxy = proxy, proxyPort = proxyPort, loginWithAmber = loginWithAmber)
|
||||||
} else if (EMAIL_PATTERN.matcher(key).matches()) {
|
} else if (EMAIL_PATTERN.matcher(key).matches()) {
|
||||||
// Evaluate NIP-5
|
// Evaluate NIP-5
|
||||||
Account(KeyPair(), proxy = proxy, proxyPort = proxyPort)
|
Account(KeyPair(), proxy = proxy, proxyPort = proxyPort, loginWithAmber = loginWithAmber)
|
||||||
} else {
|
} else {
|
||||||
Account(KeyPair(Hex.decode(key)), proxy = proxy, proxyPort = proxyPort)
|
Account(KeyPair(Hex.decode(key)), proxy = proxy, proxyPort = proxyPort, loginWithAmber = loginWithAmber)
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalPreferences.updatePrefsForLogin(account)
|
LocalPreferences.updatePrefsForLogin(account)
|
||||||
|
@@ -37,6 +37,8 @@ import androidx.compose.ui.text.style.TextDecoration
|
|||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import com.vitorpamplona.amethyst.R
|
import com.vitorpamplona.amethyst.R
|
||||||
import com.vitorpamplona.amethyst.service.PackageUtils
|
import com.vitorpamplona.amethyst.service.PackageUtils
|
||||||
|
import com.vitorpamplona.amethyst.ui.actions.SignerDialog
|
||||||
|
import com.vitorpamplona.amethyst.ui.actions.SignerType
|
||||||
import com.vitorpamplona.amethyst.ui.qrcode.SimpleQrCodeScanner
|
import com.vitorpamplona.amethyst.ui.qrcode.SimpleQrCodeScanner
|
||||||
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
|
import com.vitorpamplona.amethyst.ui.screen.AccountStateViewModel
|
||||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ConnectOrbotDialog
|
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ConnectOrbotDialog
|
||||||
@@ -63,6 +65,7 @@ fun LoginPage(
|
|||||||
val useProxy = remember { mutableStateOf(false) }
|
val useProxy = remember { mutableStateOf(false) }
|
||||||
val proxyPort = remember { mutableStateOf("9050") }
|
val proxyPort = remember { mutableStateOf("9050") }
|
||||||
var connectOrbotDialogOpen by remember { mutableStateOf(false) }
|
var connectOrbotDialogOpen by remember { mutableStateOf(false) }
|
||||||
|
var loginWithAmber by remember { mutableStateOf(false) }
|
||||||
|
|
||||||
Column(
|
Column(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -255,6 +258,37 @@ fun LoginPage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (loginWithAmber) {
|
||||||
|
SignerDialog(
|
||||||
|
onClose = {
|
||||||
|
loginWithAmber = false
|
||||||
|
},
|
||||||
|
onPost = {
|
||||||
|
key.value = TextFieldValue(it)
|
||||||
|
if (!acceptedTerms.value) {
|
||||||
|
termsAcceptanceIsRequired =
|
||||||
|
context.getString(R.string.acceptance_of_terms_is_required)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (key.value.text.isBlank()) {
|
||||||
|
errorMessage = context.getString(R.string.key_is_required)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (acceptedTerms.value && key.value.text.isNotBlank()) {
|
||||||
|
try {
|
||||||
|
accountViewModel.startUI(key.value.text, useProxy.value, proxyPort.value.toInt(), true)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e("Login", "Could not sign in", e)
|
||||||
|
errorMessage = context.getString(R.string.invalid_key)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
loginWithAmber = false
|
||||||
|
},
|
||||||
|
data = "",
|
||||||
|
type = SignerType.GET_PUBLIC_KEY
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
Spacer(modifier = Modifier.height(20.dp))
|
Spacer(modifier = Modifier.height(20.dp))
|
||||||
|
|
||||||
Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.dp)) {
|
Box(modifier = Modifier.padding(40.dp, 0.dp, 40.dp, 0.dp)) {
|
||||||
@@ -290,6 +324,26 @@ fun LoginPage(
|
|||||||
Text(text = stringResource(R.string.login))
|
Text(text = stringResource(R.string.login))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (PackageUtils.isAmberInstalled(context)) {
|
||||||
|
Box(modifier = Modifier.padding(40.dp, 40.dp, 40.dp, 0.dp)) {
|
||||||
|
Button(
|
||||||
|
onClick = {
|
||||||
|
loginWithAmber = true
|
||||||
|
},
|
||||||
|
shape = RoundedCornerShape(Size35dp),
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxWidth()
|
||||||
|
.height(50.dp),
|
||||||
|
colors = ButtonDefaults
|
||||||
|
.buttonColors(
|
||||||
|
backgroundColor = if (acceptedTerms.value) MaterialTheme.colors.primary else Color.Gray
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
Text(text = stringResource(R.string.login_with_amber))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The last child is glued to the bottom.
|
// The last child is glued to the bottom.
|
||||||
|
@@ -551,4 +551,5 @@
|
|||||||
|
|
||||||
<string name="created_at">Created at</string>
|
<string name="created_at">Created at</string>
|
||||||
<string name="rules">Rules</string>
|
<string name="rules">Rules</string>
|
||||||
|
<string name="login_with_amber">Login with Amber</string>
|
||||||
</resources>
|
</resources>
|
||||||
|
Reference in New Issue
Block a user