Fix 'View generated SSH key' option showing when not applicable (#1426)

* RepositorySettings: only show preference when available

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>

* RepositorySettings: update 'View generated SSH key' preference after generating

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2021-05-31 11:47:35 +05:30 committed by GitHub
parent 1446947811
commit f769968bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 9 deletions

View File

@ -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 <T : FragmentActivity> launchActivity(clazz: Class<T>) {
activity.startActivity(Intent(activity, clazz))
}
@ -111,13 +120,14 @@ 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
}
}
showSshKeyPref =
pref(PreferenceKeys.SSH_SEE_KEY) {
titleRes = R.string.pref_ssh_see_key_title
visible = PasswordRepository.isGitRepo()
visible = PasswordRepository.isGitRepo() && SshKey.canShowSshPublicKey
onClick {
ShowSshKeyFragment().show(activity.supportFragmentManager, "public_key")
true

View File

@ -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 {