Skip to content

Commit

Permalink
add support for rejecting the event forever
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 17, 2024
1 parent 1cf5210 commit 43497f8
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 29 deletions.
45 changes: 39 additions & 6 deletions app/src/main/java/com/greenart7c3/nostrsigner/SignerProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,19 @@ class SignerProvider : ContentProvider() {
LocalPreferences.loadFromEncryptedStorage(projection[2]) ?: return null
val event = Event.fromJson(json)
val key = "$packageName-SIGN_EVENT-${event.kind}"
val isRemembered = account.savedApps[key] ?: false
if (!isRemembered) return null
val currentSelection = selection ?: "0"
if (currentSelection == "1") {
val isRemembered = account.savedApps[key] ?: return null
if (!isRemembered) {
val cursor = MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
}
return cursor
}
} else {
val isRemembered = account.savedApps[key] ?: false
if (!isRemembered) return null
}

var cursor: MatrixCursor? = null

Expand Down Expand Up @@ -76,8 +87,19 @@ class SignerProvider : ContentProvider() {
val key = "$packageName-${uri.toString().replace("content://$appId.", "")}"
val pubkey = projection[1]
val account = LocalPreferences.loadFromEncryptedStorage(projection[2]) ?: return null
val isRemembered = account.savedApps[key] ?: false
if (!isRemembered) return null
val currentSelection = selection ?: "0"
if (currentSelection == "1") {
val isRemembered = account.savedApps[key] ?: return null
if (!isRemembered) {
val cursor = MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
}
return cursor
}
} else {
val isRemembered = account.savedApps[key] ?: false
if (!isRemembered) return null
}

val type = when (stringType) {
"NIP04_DECRYPT" -> SignerType.NIP04_DECRYPT
Expand Down Expand Up @@ -114,8 +136,19 @@ class SignerProvider : ContentProvider() {
val packageName = callingPackage ?: return null
val key = "$packageName-GET_PUBLIC_KEY"
val account = LocalPreferences.loadFromEncryptedStorage() ?: return null
val isRemembered = account.savedApps[key] ?: false
if (!isRemembered) return null
val currentSelection = selection ?: "0"
if (currentSelection == "1") {
val isRemembered = account.savedApps[key] ?: return null
if (!isRemembered) {
val cursor = MatrixCursor(arrayOf("rejected")).also {
it.addRow(arrayOf("true"))
}
return cursor
}
} else {
val isRemembered = account.savedApps[key] ?: false
if (!isRemembered) return null
}

LocalPreferences.saveToEncryptedStorage(account)

Expand Down
41 changes: 28 additions & 13 deletions app/src/main/java/com/greenart7c3/nostrsigner/ui/EditPermission.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Check
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -45,13 +45,17 @@ fun EditPermission(
navController: NavController
) {
val localAccount = LocalPreferences.loadFromEncryptedStorage(account.keyPair.pubKey.toNpub())!!
var permissions by remember {
mutableStateOf(
localAccount.savedApps.keys.filter { item -> item.startsWith(selectedPackage) }.map { item ->
item.replace("$selectedPackage-", "")
}.sorted()
val permissions = remember {
val pairsList = buildList {
for (key in localAccount.savedApps.keys.toList().sorted()) {
add(Pair(key, localAccount.savedApps[key]))
}
}
mutableStateMapOf(
*pairsList.toTypedArray()
)
}

val scope = rememberCoroutineScope()

Column(
Expand All @@ -69,8 +73,8 @@ fun EditPermission(
LazyColumn(
Modifier.weight(1f)
) {
itemsIndexed(permissions, { index, _ -> index }) { index, permission ->
val message = when (permission) {
itemsIndexed(permissions.keys.toList().sorted(), { index, _ -> index }) { _, permission ->
val message = when (permission.replace("$selectedPackage-", "")) {
"NIP04_DECRYPT" -> stringResource(R.string.decrypt_nip_04_data)
"NIP44_DECRYPT" -> stringResource(R.string.decrypt_nip_44_data)
"NIP44_ENCRYPT" -> stringResource(R.string.encrypt_nip_44_data)
Expand All @@ -84,6 +88,18 @@ fun EditPermission(
.padding(vertical = 15.dp, horizontal = 25.dp)
.fillMaxWidth()
) {
val localPermission = permissions[permission]!!
Icon(
if (localPermission) Icons.Default.Check else Icons.Default.Close,
null,
modifier = Modifier
.size(22.dp)
.padding(end = 4.dp)
.clickable {
permissions[permission] = !permissions[permission]!!
},
tint = if (localPermission) Color.Green else Color.Red
)
Row(
modifier = Modifier
.weight(1f),
Expand All @@ -100,8 +116,7 @@ fun EditPermission(
modifier = Modifier
.size(22.dp)
.clickable {
permissions =
permissions.filterIndexed { i, _ -> i != index }
permissions.remove(permission)
},
tint = Color.Red
)
Expand All @@ -126,7 +141,7 @@ fun EditPermission(
scope.launch {
val localSaved = localAccount.savedApps.filter { !it.key.contains(selectedPackage) }.toMutableMap()
permissions.forEach {
localSaved["$selectedPackage-$it"] = true
localSaved[it.key] = it.value!!
}
localAccount.savedApps = localSaved
LocalPreferences.saveToEncryptedStorage(localAccount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ fun MultiEventHomeScreen(
}
val key = "$packageName-${intentData.type}-${localEvent.kind}"
if (intentData.rememberMyChoice.value) {
localAccount.savedApps[key] =
intentData.rememberMyChoice.value
localAccount.savedApps[key] = intentData.rememberMyChoice.value
LocalPreferences.saveToEncryptedStorage(
localAccount
)
Expand All @@ -281,11 +280,12 @@ fun MultiEventHomeScreen(
}
} else {
val key = "$packageName-${intentData.type}"
localAccount.savedApps[key] =
intentData.rememberMyChoice.value
LocalPreferences.saveToEncryptedStorage(
localAccount
)
if (intentData.rememberMyChoice.value) {
localAccount.savedApps[key] = intentData.rememberMyChoice.value
LocalPreferences.saveToEncryptedStorage(
localAccount
)
}
val signature = AmberUtils.encryptOrDecryptData(
intentData.data,
intentData.type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalClipboardManager
import androidx.compose.ui.platform.LocalContext
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.models.IntentData
Expand Down Expand Up @@ -38,7 +39,7 @@ fun SingleEventHomeScreen(
when (intentData.type) {
SignerType.GET_PUBLIC_KEY -> {
val remember = remember {
mutableStateOf(account.savedApps[key] ?: false)
mutableStateOf(account.savedApps[key] != null)
}
LoginWithPubKey(
appName,
Expand All @@ -63,14 +64,19 @@ fun SingleEventHomeScreen(
return@LoginWithPubKey
},
{
if (remember.value) {
account.savedApps[key] = false
}

LocalPreferences.saveToEncryptedStorage(account)
context.getAppCompatActivity()?.finish()
}
)
}

SignerType.NIP04_DECRYPT, SignerType.NIP04_ENCRYPT, SignerType.NIP44_ENCRYPT, SignerType.NIP44_DECRYPT, SignerType.DECRYPT_ZAP_EVENT -> {
val remember = remember {
mutableStateOf(account.savedApps[key] ?: false)
mutableStateOf(account.savedApps[key] != null)
}
val shouldRunOnAccept = account.savedApps[key] ?: false
EncryptDecryptData(
Expand Down Expand Up @@ -164,6 +170,11 @@ fun SingleEventHomeScreen(
}
},
{
if (remember.value) {
account.savedApps[key] = false
}

LocalPreferences.saveToEncryptedStorage(account)
context.getAppCompatActivity()?.finish()
},
{
Expand All @@ -186,7 +197,7 @@ fun SingleEventHomeScreen(
val event = IntentUtils.getIntent(intentData.data, account.keyPair)
key = "$packageName-${intentData.type}-${event.kind}"
val remember = remember {
mutableStateOf(account.savedApps[key] ?: false)
mutableStateOf(account.savedApps[key] != null)
}
val shouldRunOnAccept = account.savedApps[key] ?: false
EventData(
Expand Down Expand Up @@ -238,6 +249,11 @@ fun SingleEventHomeScreen(
}
},
{
if (remember.value) {
account.savedApps[key] = false
}

LocalPreferences.saveToEncryptedStorage(account)
context.getAppCompatActivity()?.finish()
}
)
Expand Down

0 comments on commit 43497f8

Please sign in to comment.