Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 12fe561

Browse files
committed
PasswordStore: Remove unnecessary class-level variables
Signed-off-by: Harsh Shandilya <[email protected]>
1 parent 543cf56 commit 12fe561

File tree

3 files changed

+36
-38
lines changed

3 files changed

+36
-38
lines changed

app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
312312
const val ACTION_KEY = "action"
313313
const val ACTION_FOLDER = "folder"
314314
const val ACTION_PASSWORD = "password"
315+
316+
fun newInstance(args: Bundle): PasswordFragment {
317+
val fragment = PasswordFragment()
318+
fragment.arguments = args
319+
return fragment
320+
}
315321
}
316322

317323

app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import com.github.michaelbull.result.getOr
3737
import com.github.michaelbull.result.onFailure
3838
import com.github.michaelbull.result.runCatching
3939
import com.google.android.material.dialog.MaterialAlertDialogBuilder
40-
import com.google.android.material.snackbar.Snackbar
4140
import com.google.android.material.textfield.TextInputEditText
4241
import com.zeapo.pwdstore.autofill.oreo.AutofillMatcher
4342
import com.zeapo.pwdstore.crypto.BasePgpActivity.Companion.getLongName
@@ -54,7 +53,6 @@ import com.zeapo.pwdstore.utils.PasswordRepository
5453
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepository
5554
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
5655
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize
57-
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.isInitialized
5856
import com.zeapo.pwdstore.utils.PreferenceKeys
5957
import com.zeapo.pwdstore.utils.base64
6058
import com.zeapo.pwdstore.utils.commitChange
@@ -72,14 +70,11 @@ import kotlinx.coroutines.launch
7270
import kotlinx.coroutines.withContext
7371
import org.eclipse.jgit.api.Git
7472

73+
const val PASSWORD_FRAGMENT_TAG = "PasswordsList"
74+
7575
class PasswordStore : BaseGitActivity() {
7676

77-
private lateinit var activity: PasswordStore
7877
private lateinit var searchItem: MenuItem
79-
private lateinit var searchView: SearchView
80-
private var plist: PasswordFragment? = null
81-
private var shortcutManager: ShortcutManager? = null
82-
8378
private val settings by lazy { sharedPrefs }
8479

8580
private val model: SearchableRepositoryViewModel by viewModels {
@@ -174,7 +169,7 @@ class PasswordStore : BaseGitActivity() {
174169
}
175170
}
176171
refreshPasswordList()
177-
plist?.dismissActionMode()
172+
getPasswordFragment()?.dismissActionMode()
178173
}
179174

180175
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
@@ -190,15 +185,14 @@ class PasswordStore : BaseGitActivity() {
190185
val printable = isPrintable(c)
191186
if (printable && !searchItem.isActionViewExpanded) {
192187
searchItem.expandActionView()
193-
searchView.setQuery(c.toString(), true)
188+
(searchItem.actionView as SearchView).setQuery(c.toString(), true)
194189
return true
195190
}
196191
return super.onKeyDown(keyCode, event)
197192
}
198193

199194
@SuppressLint("NewApi")
200195
override fun onCreate(savedInstanceState: Bundle?) {
201-
activity = this
202196
// If user opens app with permission granted then revokes and returns,
203197
// prevent attempt to create password list fragment
204198
var savedInstance = savedInstanceState
@@ -208,9 +202,6 @@ class PasswordStore : BaseGitActivity() {
208202
}
209203
super.onCreate(savedInstance)
210204
setContentView(R.layout.activity_pwdstore)
211-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
212-
shortcutManager = getSystemService()
213-
}
214205

215206
model.currentDir.observe(this) { dir ->
216207
val basePath = getRepositoryDirectory().absoluteFile
@@ -257,7 +248,7 @@ class PasswordStore : BaseGitActivity() {
257248
// we can get by without any noticeable difference in performance.
258249
invalidateOptionsMenu()
259250
searchItem = menu.findItem(R.id.action_search)
260-
searchView = searchItem.actionView as SearchView
251+
val searchView = searchItem.actionView as SearchView
261252
searchView.setOnQueryTextListener(
262253
object : OnQueryTextListener {
263254
override fun onQueryTextSubmit(s: String): Boolean {
@@ -317,23 +308,23 @@ class PasswordStore : BaseGitActivity() {
317308
return true
318309
}
319310
R.id.git_push -> {
320-
if (!isInitialized) {
311+
if (!PasswordRepository.isInitialized) {
321312
initBefore.show()
322313
return false
323314
}
324315
runGitOperation(GitOp.PUSH)
325316
return true
326317
}
327318
R.id.git_pull -> {
328-
if (!isInitialized) {
319+
if (!PasswordRepository.isInitialized) {
329320
initBefore.show()
330321
return false
331322
}
332323
runGitOperation(GitOp.PULL)
333324
return true
334325
}
335326
R.id.git_sync -> {
336-
if (!isInitialized) {
327+
if (!PasswordRepository.isInitialized) {
337328
initBefore.show()
338329
return false
339330
}
@@ -351,9 +342,13 @@ class PasswordStore : BaseGitActivity() {
351342
return super.onOptionsItemSelected(item)
352343
}
353344

354-
override fun onDestroy() {
355-
plist = null
356-
super.onDestroy()
345+
override fun onBackPressed() {
346+
if (getPasswordFragment()?.onBackPressedInActivity() != true)
347+
super.onBackPressed()
348+
}
349+
350+
private fun getPasswordFragment(): PasswordFragment? {
351+
return supportFragmentManager.findFragmentByTag(PASSWORD_FRAGMENT_TAG) as? PasswordFragment
357352
}
358353

359354
fun clearSearch() {
@@ -401,10 +396,9 @@ class PasswordStore : BaseGitActivity() {
401396
if (localDir != null && settings.getBoolean(PreferenceKeys.REPOSITORY_INITIALIZED, false)) {
402397
d { "Check, dir: ${localDir.absolutePath}" }
403398
// do not push the fragment if we already have it
404-
if (supportFragmentManager.findFragmentByTag("PasswordsList") == null ||
399+
if (getPasswordFragment() == null ||
405400
settings.getBoolean(PreferenceKeys.REPO_CHANGED, false)) {
406401
settings.edit { putBoolean(PreferenceKeys.REPO_CHANGED, false) }
407-
plist = PasswordFragment()
408402
val args = Bundle()
409403
args.putString(REQUEST_ARG_PATH, getRepositoryDirectory().absolutePath)
410404

@@ -413,24 +407,20 @@ class PasswordStore : BaseGitActivity() {
413407
if (intent.getBooleanExtra("matchWith", false)) {
414408
args.putBoolean("matchWith", true)
415409
}
416-
plist!!.arguments = args
417-
supportActionBar!!.show()
418-
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
410+
supportActionBar?.apply {
411+
show()
412+
setDisplayHomeAsUpEnabled(false)
413+
}
419414
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
420415
supportFragmentManager.commit {
421-
replace(R.id.main_layout, plist!!, "PasswordsList")
416+
replace(R.id.main_layout, PasswordFragment.newInstance(args), PASSWORD_FRAGMENT_TAG)
422417
}
423418
}
424419
} else {
425420
startActivity(Intent(this, OnboardingActivity::class.java))
426421
}
427422
}
428423

429-
override fun onBackPressed() {
430-
if (plist?.onBackPressedInActivity() != true)
431-
super.onBackPressed()
432-
}
433-
434424
private fun getRelativePath(fullPath: String, repositoryPath: String): String {
435425
return fullPath.replace(repositoryPath, "").replace("/+".toRegex(), "/")
436426
}
@@ -468,26 +458,27 @@ class PasswordStore : BaseGitActivity() {
468458

469459
// Adds shortcut
470460
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
461+
val shortcutManager: ShortcutManager = getSystemService() ?: return
471462
val shortcut = Builder(this, item.fullPathToParent)
472463
.setShortLabel(item.toString())
473464
.setLongLabel(item.fullPathToParent + item.toString())
474465
.setIcon(Icon.createWithResource(this, R.mipmap.ic_launcher))
475466
.setIntent(authDecryptIntent)
476467
.build()
477-
val shortcuts = shortcutManager!!.dynamicShortcuts
478-
if (shortcuts.size >= shortcutManager!!.maxShortcutCountPerActivity && shortcuts.size > 0) {
468+
val shortcuts = shortcutManager.dynamicShortcuts
469+
if (shortcuts.size >= shortcutManager.maxShortcutCountPerActivity && shortcuts.size > 0) {
479470
shortcuts.removeAt(shortcuts.size - 1)
480471
shortcuts.add(0, shortcut)
481-
shortcutManager!!.dynamicShortcuts = shortcuts
472+
shortcutManager.dynamicShortcuts = shortcuts
482473
} else {
483-
shortcutManager!!.addDynamicShortcuts(listOf(shortcut))
474+
shortcutManager.addDynamicShortcuts(listOf(shortcut))
484475
}
485476
}
486477
startActivity(decryptIntent)
487478
}
488479

489480
private fun validateState(): Boolean {
490-
if (!isInitialized) {
481+
if (!PasswordRepository.isInitialized) {
491482
MaterialAlertDialogBuilder(this)
492483
.setMessage(resources.getString(R.string.creation_dialog_text))
493484
.setPositiveButton(resources.getString(R.string.dialog_ok), null)
@@ -638,6 +629,7 @@ class PasswordStore : BaseGitActivity() {
638629
* the current directory).
639630
*/
640631
fun refreshPasswordList(target: File? = null) {
632+
val plist = getPasswordFragment()
641633
if (target?.isDirectory == true && model.currentDir.value?.contains(target) == true) {
642634
plist?.navigateTo(target)
643635
} else if (target?.isFile == true && model.currentDir.value?.contains(target) == true) {
@@ -652,7 +644,7 @@ class PasswordStore : BaseGitActivity() {
652644
}
653645

654646
private val currentDir: File
655-
get() = plist?.currentDir ?: getRepositoryDirectory()
647+
get() = getPasswordFragment()?.currentDir ?: getRepositoryDirectory()
656648

657649
private suspend fun moveFile(source: File, destinationFile: File) {
658650
val sourceDestinationMap = if (source.isDirectory) {

app/src/main/java/com/zeapo/pwdstore/SelectFolderActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SelectFolderActivity : AppCompatActivity(R.layout.select_folder_layout) {
3131
supportFragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE)
3232

3333
supportFragmentManager.commit {
34-
replace(R.id.pgp_handler_linearlayout, passwordList, "PasswordsList")
34+
replace(R.id.pgp_handler_linearlayout, passwordList, PASSWORD_FRAGMENT_TAG)
3535
}
3636
}
3737

0 commit comments

Comments
 (0)