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

Commit 30c8c27

Browse files
committed
Convert PasswordRepository to an object
Signed-off-by: Harsh Shandilya <[email protected]>
1 parent 95d53e4 commit 30c8c27

File tree

4 files changed

+172
-180
lines changed

4 files changed

+172
-180
lines changed

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

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ import com.zeapo.pwdstore.ui.dialogs.FolderCreationDialogFragment
5050
import com.zeapo.pwdstore.ui.onboarding.activity.OnboardingActivity
5151
import com.zeapo.pwdstore.utils.PasswordItem
5252
import com.zeapo.pwdstore.utils.PasswordRepository
53-
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepository
54-
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
55-
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.initialize
5653
import com.zeapo.pwdstore.utils.PreferenceKeys
5754
import com.zeapo.pwdstore.utils.base64
5855
import com.zeapo.pwdstore.utils.commitChange
@@ -101,7 +98,7 @@ class PasswordStore : BaseGitActivity() {
10198
val intentData = result.data ?: return@registerForActivityResult
10299
val filesToMove = requireNotNull(intentData.getStringArrayExtra("Files"))
103100
val target = File(requireNotNull(intentData.getStringExtra("SELECTED_FOLDER_PATH")))
104-
val repositoryPath = getRepositoryDirectory().absolutePath
101+
val repositoryPath = PasswordRepository.getRepositoryDirectory().absolutePath
105102
if (!target.isDirectory) {
106103
e { "Tried moving passwords to a non-existing folder." }
107104
return@registerForActivityResult
@@ -158,7 +155,7 @@ class PasswordStore : BaseGitActivity() {
158155
}
159156
}
160157
else -> {
161-
val repoDir = getRepositoryDirectory().absolutePath
158+
val repoDir = PasswordRepository.getRepositoryDirectory().absolutePath
162159
val relativePath = getRelativePath("${target.absolutePath}/", repoDir)
163160
withContext(Dispatchers.Main) {
164161
commitChange(
@@ -204,7 +201,7 @@ class PasswordStore : BaseGitActivity() {
204201
setContentView(R.layout.activity_pwdstore)
205202

206203
model.currentDir.observe(this) { dir ->
207-
val basePath = getRepositoryDirectory().absoluteFile
204+
val basePath = PasswordRepository.getRepositoryDirectory().absoluteFile
208205
supportActionBar!!.apply {
209206
if (dir != basePath)
210207
title = dir.name
@@ -384,11 +381,11 @@ class PasswordStore : BaseGitActivity() {
384381
}
385382

386383
private fun checkLocalRepository() {
387-
val repo = initialize()
384+
val repo = PasswordRepository.initialize()
388385
if (repo == null) {
389386
directorySelectAction.launch(UserPreference.createDirectorySelectionIntent(this))
390387
} else {
391-
checkLocalRepository(getRepositoryDirectory())
388+
checkLocalRepository(PasswordRepository.getRepositoryDirectory())
392389
}
393390
}
394391

@@ -400,7 +397,7 @@ class PasswordStore : BaseGitActivity() {
400397
settings.getBoolean(PreferenceKeys.REPO_CHANGED, false)) {
401398
settings.edit { putBoolean(PreferenceKeys.REPO_CHANGED, false) }
402399
val args = Bundle()
403-
args.putString(REQUEST_ARG_PATH, getRepositoryDirectory().absolutePath)
400+
args.putString(REQUEST_ARG_PATH, PasswordRepository.getRepositoryDirectory().absolutePath)
404401

405402
// if the activity was started from the autofill settings, the
406403
// intent is to match a clicked pwd with app. pass this to fragment
@@ -426,8 +423,8 @@ class PasswordStore : BaseGitActivity() {
426423
}
427424

428425
private fun getLastChangedTimestamp(fullPath: String): Long {
429-
val repoPath = getRepositoryDirectory()
430-
val repository = getRepository(repoPath)
426+
val repoPath = PasswordRepository.getRepositoryDirectory()
427+
val repository = PasswordRepository.getRepository(repoPath)
431428
if (repository == null) {
432429
d { "getLastChangedTimestamp: No git repository" }
433430
return File(fullPath).lastModified()
@@ -450,7 +447,7 @@ class PasswordStore : BaseGitActivity() {
450447
for (intent in arrayOf(decryptIntent, authDecryptIntent)) {
451448
intent.putExtra("NAME", item.toString())
452449
intent.putExtra("FILE_PATH", item.file.absolutePath)
453-
intent.putExtra("REPO_PATH", getRepositoryDirectory().absolutePath)
450+
intent.putExtra("REPO_PATH", PasswordRepository.getRepositoryDirectory().absolutePath)
454451
intent.putExtra("LAST_CHANGED_TIMESTAMP", getLastChangedTimestamp(item.file.absolutePath))
455452
}
456453
// Needs an action to be a shortcut intent
@@ -494,7 +491,7 @@ class PasswordStore : BaseGitActivity() {
494491
i { "Adding file to : ${currentDir.absolutePath}" }
495492
val intent = Intent(this, PasswordCreationActivity::class.java)
496493
intent.putExtra("FILE_PATH", currentDir.absolutePath)
497-
intent.putExtra("REPO_PATH", getRepositoryDirectory().absolutePath)
494+
intent.putExtra("REPO_PATH", PasswordRepository.getRepositoryDirectory().absolutePath)
498495
listRefreshAction.launch(intent)
499496
}
500497

@@ -530,7 +527,7 @@ class PasswordStore : BaseGitActivity() {
530527
refreshPasswordList()
531528
AutofillMatcher.updateMatches(applicationContext, delete = filesToDelete)
532529
val fmt = selectedItems.joinToString(separator = ", ") { item ->
533-
item.file.toRelativeString(getRepositoryDirectory())
530+
item.file.toRelativeString(PasswordRepository.getRepositoryDirectory())
534531
}
535532
lifecycleScope.launch {
536533
commitChange(
@@ -644,7 +641,7 @@ class PasswordStore : BaseGitActivity() {
644641
}
645642

646643
private val currentDir: File
647-
get() = getPasswordFragment()?.currentDir ?: getRepositoryDirectory()
644+
get() = getPasswordFragment()?.currentDir ?: PasswordRepository.getRepositoryDirectory()
648645

649646
private suspend fun moveFile(source: File, destinationFile: File) {
650647
val sourceDestinationMap = if (source.isDirectory) {
@@ -674,7 +671,7 @@ class PasswordStore : BaseGitActivity() {
674671
fun matchPasswordWithApp(item: PasswordItem) {
675672
val path = item.file
676673
.absolutePath
677-
.replace(getRepositoryDirectory().toString() + "/", "")
674+
.replace(PasswordRepository.getRepositoryDirectory().toString() + "/", "")
678675
.replace(".gpg", "")
679676
val data = Intent()
680677
data.putExtra("path", path)

app/src/main/java/com/zeapo/pwdstore/ui/dialogs/FolderCreationDialogFragment.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import com.zeapo.pwdstore.R
2222
import com.zeapo.pwdstore.crypto.BasePgpActivity
2323
import com.zeapo.pwdstore.crypto.GetKeyIdsActivity
2424
import com.zeapo.pwdstore.utils.PasswordRepository
25-
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
2625
import com.zeapo.pwdstore.utils.commitChange
2726
import com.zeapo.pwdstore.utils.requestInputFocusOnView
2827
import java.io.File
@@ -41,7 +40,7 @@ class FolderCreationDialogFragment : DialogFragment() {
4140
val repo = PasswordRepository.getRepository(null)
4241
if (repo != null) {
4342
lifecycleScope.launch {
44-
val repoPath = getRepositoryDirectory().absolutePath
43+
val repoPath = PasswordRepository.getRepositoryDirectory().absolutePath
4544
requireActivity().commitChange(
4645
getString(
4746
R.string.git_commit_gpg_id,

app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package com.zeapo.pwdstore.utils
66

77
import com.github.michaelbull.result.getOrElse
88
import com.github.michaelbull.result.runCatching
9-
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
109
import java.io.File
1110
import java.util.Date
1211
import org.eclipse.jgit.lib.ObjectId
@@ -50,10 +49,10 @@ fun File.contains(other: File): Boolean {
5049

5150
/**
5251
* Checks if this [File] is in the password repository directory as given
53-
* by [getRepositoryDirectory]
52+
* by [PasswordRepository.getRepositoryDirectory]
5453
*/
5554
fun File.isInsideRepository(): Boolean {
56-
return canonicalPath.contains(getRepositoryDirectory().canonicalPath)
55+
return canonicalPath.contains(PasswordRepository.getRepositoryDirectory().canonicalPath)
5756
}
5857

5958
/**

0 commit comments

Comments
 (0)