From f769968bdc147af2e2e9ffbbbe1a7c3621124327 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 31 May 2021 11:47:35 +0530 Subject: Fix 'View generated SSH key' option showing when not applicable (#1426) * RepositorySettings: only show preference when available Signed-off-by: Harsh Shandilya * RepositorySettings: update 'View generated SSH key' preference after generating Signed-off-by: Harsh Shandilya --- .../aps/ui/settings/RepositorySettings.kt | 26 +++++++++++++++------- .../aps/ui/sshkeygen/SshKeyGenActivity.kt | 5 ++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt b/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt index 289391b4..8671b5cb 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/settings/RepositorySettings.kt @@ -9,6 +9,7 @@ import android.content.Intent import android.content.SharedPreferences import android.content.pm.ShortcutManager import android.os.Build +import androidx.activity.result.contract.ActivityResultContracts import androidx.core.content.edit import androidx.core.content.getSystemService import androidx.fragment.app.FragmentActivity @@ -38,11 +39,17 @@ import dev.msfjarvis.aps.util.extensions.getString import dev.msfjarvis.aps.util.extensions.sharedPrefs import dev.msfjarvis.aps.util.extensions.snackbar import dev.msfjarvis.aps.util.extensions.unsafeLazy +import dev.msfjarvis.aps.util.git.sshj.SshKey import dev.msfjarvis.aps.util.settings.GitSettings import dev.msfjarvis.aps.util.settings.PreferenceKeys class RepositorySettings(private val activity: FragmentActivity) : SettingsProvider { + private val generateSshKey = + activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { + showSshKeyPref?.visible = SshKey.canShowSshPublicKey + } + private val hiltEntryPoint by unsafeLazy { EntryPointAccessors.fromApplication( activity.applicationContext, @@ -50,6 +57,8 @@ class RepositorySettings(private val activity: FragmentActivity) : SettingsProvi ) } + private var showSshKeyPref: Preference? = null + private fun launchActivity(clazz: Class) { activity.startActivity(Intent(activity, clazz)) } @@ -111,18 +120,19 @@ class RepositorySettings(private val activity: FragmentActivity) : SettingsProvi pref(PreferenceKeys.SSH_KEYGEN) { titleRes = R.string.pref_ssh_keygen_title onClick { - launchActivity(SshKeyGenActivity::class.java) + generateSshKey.launch(Intent(activity, SshKeyGenActivity::class.java)) true } } - pref(PreferenceKeys.SSH_SEE_KEY) { - titleRes = R.string.pref_ssh_see_key_title - visible = PasswordRepository.isGitRepo() - onClick { - ShowSshKeyFragment().show(activity.supportFragmentManager, "public_key") - true + showSshKeyPref = + pref(PreferenceKeys.SSH_SEE_KEY) { + titleRes = R.string.pref_ssh_see_key_title + visible = PasswordRepository.isGitRepo() && SshKey.canShowSshPublicKey + onClick { + ShowSshKeyFragment().show(activity.supportFragmentManager, "public_key") + true + } } - } pref(PreferenceKeys.CLEAR_SAVED_PASS) { fun Preference.updatePref() { val sshPass = encryptedPreferences.getString(PreferenceKeys.SSH_KEY_LOCAL_PASSPHRASE) diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/sshkeygen/SshKeyGenActivity.kt b/app/src/main/java/dev/msfjarvis/aps/ui/sshkeygen/SshKeyGenActivity.kt index 83f44aa6..0d8ffcbc 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/sshkeygen/SshKeyGenActivity.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/sshkeygen/SshKeyGenActivity.kt @@ -64,7 +64,10 @@ class SshKeyGenActivity : AppCompatActivity() { setPositiveButton(R.string.ssh_keygen_existing_replace) { _, _ -> lifecycleScope.launch { generate() } } - setNegativeButton(R.string.ssh_keygen_existing_keep) { _, _ -> finish() } + setNegativeButton(R.string.ssh_keygen_existing_keep) { _, _ -> + setResult(RESULT_CANCELED) + finish() + } show() } } else { -- cgit v1.2.3