feat: make key import update key list

This commit is contained in:
Harsh Shandilya 2022-11-18 14:23:10 +05:30
parent 84b9839635
commit 11f192532f
No known key found for this signature in database
3 changed files with 19 additions and 6 deletions

View file

@ -73,6 +73,7 @@ class PGPKeyImportActivity : AppCompatActivity() {
is Ok<PGPKey?> -> { is Ok<PGPKey?> -> {
val key = result.value val key = result.value
if (key == null) { if (key == null) {
setResult(RESULT_CANCELED)
finish() finish()
// This return convinces Kotlin that the control flow for `key == null` definitely // This return convinces Kotlin that the control flow for `key == null` definitely
// terminates here and allows for a smart cast below. // terminates here and allows for a smart cast below.
@ -81,7 +82,10 @@ class PGPKeyImportActivity : AppCompatActivity() {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setTitle(getString(R.string.pgp_key_import_succeeded)) .setTitle(getString(R.string.pgp_key_import_succeeded))
.setMessage(getString(R.string.pgp_key_import_succeeded_message, tryGetId(key))) .setMessage(getString(R.string.pgp_key_import_succeeded_message, tryGetId(key)))
.setPositiveButton(android.R.string.ok) { _, _ -> finish() } .setPositiveButton(android.R.string.ok) { _, _ ->
setResult(RESULT_OK)
finish()
}
.setCancelable(false) .setCancelable(false)
.show() .show()
} }

View file

@ -1,8 +1,10 @@
package app.passwordstore.ui.pgp package app.passwordstore.ui.pgp
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
@ -19,7 +21,6 @@ import app.passwordstore.R
import app.passwordstore.ui.APSAppBar import app.passwordstore.ui.APSAppBar
import app.passwordstore.ui.compose.theme.APSTheme import app.passwordstore.ui.compose.theme.APSTheme
import app.passwordstore.ui.compose.theme.decideColorScheme import app.passwordstore.ui.compose.theme.decideColorScheme
import app.passwordstore.util.extensions.launchActivity
import app.passwordstore.util.viewmodel.PGPKeyListViewModel import app.passwordstore.util.viewmodel.PGPKeyListViewModel
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
@ -28,6 +29,12 @@ import dagger.hilt.android.AndroidEntryPoint
class PGPKeyListActivity : ComponentActivity() { class PGPKeyListActivity : ComponentActivity() {
private val viewModel: PGPKeyListViewModel by viewModels() private val viewModel: PGPKeyListViewModel by viewModels()
private val keyImportAction =
registerForActivityResult(StartActivityForResult()) {
if (it.resultCode == RESULT_OK) {
viewModel.updateKeySet()
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -44,7 +51,9 @@ class PGPKeyListActivity : ComponentActivity() {
) )
}, },
floatingActionButton = { floatingActionButton = {
FloatingActionButton(onClick = { launchActivity(PGPKeyImportActivity::class.java) }) { FloatingActionButton(
onClick = { keyImportAction.launch(Intent(this, PGPKeyImportActivity::class.java)) }
) {
Icon( Icon(
painter = painterResource(R.drawable.ic_add_48dp), painter = painterResource(R.drawable.ic_add_48dp),
stringResource(R.string.pref_import_pgp_key_title) stringResource(R.string.pref_import_pgp_key_title)

View file

@ -20,10 +20,10 @@ class PGPKeyListViewModel @Inject constructor(private val keyManager: PGPKeyMana
var keys: List<GpgIdentifier> by mutableStateOf(emptyList()) var keys: List<GpgIdentifier> by mutableStateOf(emptyList())
init { init {
getKeys() updateKeySet()
} }
private fun getKeys() { fun updateKeySet() {
viewModelScope.launch { viewModelScope.launch {
when ( when (
val result = val result =
@ -40,7 +40,7 @@ class PGPKeyListViewModel @Inject constructor(private val keyManager: PGPKeyMana
fun deleteKey(identifier: GpgIdentifier) { fun deleteKey(identifier: GpgIdentifier) {
viewModelScope.launch { viewModelScope.launch {
keyManager.removeKey(identifier) keyManager.removeKey(identifier)
getKeys() updateKeySet()
} }
} }
} }