PasswordStore: Remove unnecessary class-level variables

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2020-10-18 23:55:04 +05:30
parent 543cf5675a
commit 12fe561be7
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
3 changed files with 36 additions and 38 deletions

View file

@ -312,6 +312,12 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
const val ACTION_KEY = "action" const val ACTION_KEY = "action"
const val ACTION_FOLDER = "folder" const val ACTION_FOLDER = "folder"
const val ACTION_PASSWORD = "password" const val ACTION_PASSWORD = "password"
fun newInstance(args: Bundle): PasswordFragment {
val fragment = PasswordFragment()
fragment.arguments = args
return fragment
}
} }

View file

@ -37,7 +37,6 @@ import com.github.michaelbull.result.getOr
import com.github.michaelbull.result.onFailure import com.github.michaelbull.result.onFailure
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputEditText
import com.zeapo.pwdstore.autofill.oreo.AutofillMatcher import com.zeapo.pwdstore.autofill.oreo.AutofillMatcher
import com.zeapo.pwdstore.crypto.BasePgpActivity.Companion.getLongName import com.zeapo.pwdstore.crypto.BasePgpActivity.Companion.getLongName
@ -54,7 +53,6 @@ import com.zeapo.pwdstore.utils.PasswordRepository
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepository import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepository
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized
import com.zeapo.pwdstore.utils.PreferenceKeys import com.zeapo.pwdstore.utils.PreferenceKeys
import com.zeapo.pwdstore.utils.base64 import com.zeapo.pwdstore.utils.base64
import com.zeapo.pwdstore.utils.commitChange import com.zeapo.pwdstore.utils.commitChange
@ -72,14 +70,11 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
const val PASSWORD_FRAGMENT_TAG = "PasswordsList"
class PasswordStore : BaseGitActivity() { class PasswordStore : BaseGitActivity() {
private lateinit var activity: PasswordStore
private lateinit var searchItem: MenuItem private lateinit var searchItem: MenuItem
private lateinit var searchView: SearchView
private var plist: PasswordFragment? = null
private var shortcutManager: ShortcutManager? = null
private val settings by lazy { sharedPrefs } private val settings by lazy { sharedPrefs }
private val model: SearchableRepositoryViewModel by viewModels { private val model: SearchableRepositoryViewModel by viewModels {
@ -174,7 +169,7 @@ class PasswordStore : BaseGitActivity() {
} }
} }
refreshPasswordList() refreshPasswordList()
plist?.dismissActionMode() getPasswordFragment()?.dismissActionMode()
} }
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
@ -190,7 +185,7 @@ class PasswordStore : BaseGitActivity() {
val printable = isPrintable(c) val printable = isPrintable(c)
if (printable && !searchItem.isActionViewExpanded) { if (printable && !searchItem.isActionViewExpanded) {
searchItem.expandActionView() searchItem.expandActionView()
searchView.setQuery(c.toString(), true) (searchItem.actionView as SearchView).setQuery(c.toString(), true)
return true return true
} }
return super.onKeyDown(keyCode, event) return super.onKeyDown(keyCode, event)
@ -198,7 +193,6 @@ class PasswordStore : BaseGitActivity() {
@SuppressLint("NewApi") @SuppressLint("NewApi")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
activity = this
// If user opens app with permission granted then revokes and returns, // If user opens app with permission granted then revokes and returns,
// prevent attempt to create password list fragment // prevent attempt to create password list fragment
var savedInstance = savedInstanceState var savedInstance = savedInstanceState
@ -208,9 +202,6 @@ class PasswordStore : BaseGitActivity() {
} }
super.onCreate(savedInstance) super.onCreate(savedInstance)
setContentView(R.layout.activity_pwdstore) setContentView(R.layout.activity_pwdstore)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
shortcutManager = getSystemService()
}
model.currentDir.observe(this) { dir -> model.currentDir.observe(this) { dir ->
val basePath = getRepositoryDirectory().absoluteFile val basePath = getRepositoryDirectory().absoluteFile
@ -257,7 +248,7 @@ class PasswordStore : BaseGitActivity() {
// we can get by without any noticeable difference in performance. // we can get by without any noticeable difference in performance.
invalidateOptionsMenu() invalidateOptionsMenu()
searchItem = menu.findItem(R.id.action_search) searchItem = menu.findItem(R.id.action_search)
searchView = searchItem.actionView as SearchView val searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener( searchView.setOnQueryTextListener(
object : OnQueryTextListener { object : OnQueryTextListener {
override fun onQueryTextSubmit(s: String): Boolean { override fun onQueryTextSubmit(s: String): Boolean {
@ -317,7 +308,7 @@ class PasswordStore : BaseGitActivity() {
return true return true
} }
R.id.git_push -> { R.id.git_push -> {
if (!isInitialized) { if (!PasswordRepository.isInitialized) {
initBefore.show() initBefore.show()
return false return false
} }
@ -325,7 +316,7 @@ class PasswordStore : BaseGitActivity() {
return true return true
} }
R.id.git_pull -> { R.id.git_pull -> {
if (!isInitialized) { if (!PasswordRepository.isInitialized) {
initBefore.show() initBefore.show()
return false return false
} }
@ -333,7 +324,7 @@ class PasswordStore : BaseGitActivity() {
return true return true
} }
R.id.git_sync -> { R.id.git_sync -> {
if (!isInitialized) { if (!PasswordRepository.isInitialized) {
initBefore.show() initBefore.show()
return false return false
} }
@ -351,9 +342,13 @@ class PasswordStore : BaseGitActivity() {
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
override fun onDestroy() { override fun onBackPressed() {
plist = null if (getPasswordFragment()?.onBackPressedInActivity() != true)
super.onDestroy() super.onBackPressed()
}
private fun getPasswordFragment(): PasswordFragment? {
return supportFragmentManager.findFragmentByTag(PASSWORD_FRAGMENT_TAG) as? PasswordFragment
} }
fun clearSearch() { fun clearSearch() {
@ -401,10 +396,9 @@ class PasswordStore : BaseGitActivity() {
if (localDir != null && settings.getBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false)) { if (localDir != null && settings.getBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false)) {
d { "Check, dir: ${localDir.absolutePath}" } d { "Check, dir: ${localDir.absolutePath}" }
// do not push the fragment if we already have it // do not push the fragment if we already have it
if (supportFragmentManager.findFragmentByTag("PasswordsList") == null || if (getPasswordFragment() == null ||
settings.getBoolean(PreferenceKeys.REPO_CHANGED, false)) { settings.getBoolean(PreferenceKeys.REPO_CHANGED, false)) {
settings.edit { putBoolean(PreferenceKeys.REPO_CHANGED, false) } settings.edit { putBoolean(PreferenceKeys.REPO_CHANGED, false) }
plist = PasswordFragment()
val args = Bundle() val args = Bundle()
args.putString(REQUEST_ARG_PATH, getRepositoryDirectory().absolutePath) args.putString(REQUEST_ARG_PATH, getRepositoryDirectory().absolutePath)
@ -413,12 +407,13 @@ class PasswordStore : BaseGitActivity() {
if (intent.getBooleanExtra("matchWith", false)) { if (intent.getBooleanExtra("matchWith", false)) {
args.putBoolean("matchWith", true) args.putBoolean("matchWith", true)
} }
plist!!.arguments = args supportActionBar?.apply {
supportActionBar!!.show() show()
supportActionBar!!.setDisplayHomeAsUpEnabled(false) setDisplayHomeAsUpEnabled(false)
}
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
supportFragmentManager.commit { supportFragmentManager.commit {
replace(R.id.main_layout, plist!!, "PasswordsList") replace(R.id.main_layout, PasswordFragment.newInstance(args), PASSWORD_FRAGMENT_TAG)
} }
} }
} else { } else {
@ -426,11 +421,6 @@ class PasswordStore : BaseGitActivity() {
} }
} }
override fun onBackPressed() {
if (plist?.onBackPressedInActivity() != true)
super.onBackPressed()
}
private fun getRelativePath(fullPath: String, repositoryPath: String): String { private fun getRelativePath(fullPath: String, repositoryPath: String): String {
return fullPath.replace(repositoryPath, "").replace("/+".toRegex(), "/") return fullPath.replace(repositoryPath, "").replace("/+".toRegex(), "/")
} }
@ -468,26 +458,27 @@ class PasswordStore : BaseGitActivity() {
// Adds shortcut // Adds shortcut
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
val shortcutManager: ShortcutManager = getSystemService() ?: return
val shortcut = Builder(this, item.fullPathToParent) val shortcut = Builder(this, item.fullPathToParent)
.setShortLabel(item.toString()) .setShortLabel(item.toString())
.setLongLabel(item.fullPathToParent + item.toString()) .setLongLabel(item.fullPathToParent + item.toString())
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher)) .setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
.setIntent(authDecryptIntent) .setIntent(authDecryptIntent)
.build() .build()
val shortcuts = shortcutManager!!.dynamicShortcuts val shortcuts = shortcutManager.dynamicShortcuts
if (shortcuts.size >= shortcutManager!!.maxShortcutCountPerActivity && shortcuts.size > 0) { if (shortcuts.size >= shortcutManager.maxShortcutCountPerActivity && shortcuts.size > 0) {
shortcuts.removeAt(shortcuts.size - 1) shortcuts.removeAt(shortcuts.size - 1)
shortcuts.add(0, shortcut) shortcuts.add(0, shortcut)
shortcutManager!!.dynamicShortcuts = shortcuts shortcutManager.dynamicShortcuts = shortcuts
} else { } else {
shortcutManager!!.addDynamicShortcuts(listOf(shortcut)) shortcutManager.addDynamicShortcuts(listOf(shortcut))
} }
} }
startActivity(decryptIntent) startActivity(decryptIntent)
} }
private fun validateState(): Boolean { private fun validateState(): Boolean {
if (!isInitialized) { if (!PasswordRepository.isInitialized) {
MaterialAlertDialogBuilder(this) MaterialAlertDialogBuilder(this)
.setMessage(resources.getString(R.string.creation_dialog_text)) .setMessage(resources.getString(R.string.creation_dialog_text))
.setPositiveButton(resources.getString(R.string.dialog_ok), null) .setPositiveButton(resources.getString(R.string.dialog_ok), null)
@ -638,6 +629,7 @@ class PasswordStore : BaseGitActivity() {
* the current directory). * the current directory).
*/ */
fun refreshPasswordList(target: File? = null) { fun refreshPasswordList(target: File? = null) {
val plist = getPasswordFragment()
if (target?.isDirectory == true && model.currentDir.value?.contains(target) == true) { if (target?.isDirectory == true && model.currentDir.value?.contains(target) == true) {
plist?.navigateTo(target) plist?.navigateTo(target)
} else if (target?.isFile == true && model.currentDir.value?.contains(target) == true) { } else if (target?.isFile == true && model.currentDir.value?.contains(target) == true) {
@ -652,7 +644,7 @@ class PasswordStore : BaseGitActivity() {
} }
private val currentDir: File private val currentDir: File
get() = plist?.currentDir ?: getRepositoryDirectory() get() = getPasswordFragment()?.currentDir ?: getRepositoryDirectory()
private suspend fun moveFile(source: File, destinationFile: File) { private suspend fun moveFile(source: File, destinationFile: File) {
val sourceDestinationMap = if (source.isDirectory) { val sourceDestinationMap = if (source.isDirectory) {

View file

@ -31,7 +31,7 @@ class SelectFolderActivity : AppCompatActivity(R.layout.select_folder_layout) {
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
supportFragmentManager.commit { supportFragmentManager.commit {
replace(R.id.pgp_handler_linearlayout, passwordList, "PasswordsList") replace(R.id.pgp_handler_linearlayout, passwordList, PASSWORD_FRAGMENT_TAG)
} }
} }