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

Commit c70c179

Browse files
committed
all: refactor ActivityResultContracts usages to adhere to API requirements
The newest versions of AndroidX Activity and Fragments correctly enforce the requirement for all contracts to be registered at class init or before the lifecycle has reached `Lifecycle.State.STARTED`. To comply with these requirements, move all instances of `registerForActivityResult` being called at arbitrary points in the code to be done at class init. Signed-off-by: Harsh Shandilya <[email protected]> (cherry picked from commit cf03c55)
1 parent 8355db8 commit c70c179

File tree

6 files changed

+300
-267
lines changed

6 files changed

+300
-267
lines changed

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

Lines changed: 95 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,97 @@ class PasswordStore : BaseGitActivity() {
8585
ViewModelProvider.AndroidViewModelFactory(application)
8686
}
8787

88+
private val storagePermissionRequest = registerForActivityResult(RequestPermission()) { granted ->
89+
if (granted) checkLocalRepository()
90+
}
91+
92+
private val directorySelectAction = registerForActivityResult(StartActivityForResult()) { result ->
93+
if (result.resultCode == RESULT_OK) {
94+
checkLocalRepository()
95+
}
96+
}
97+
98+
private val listRefreshAction = registerForActivityResult(StartActivityForResult()) { result ->
99+
if (result.resultCode == RESULT_OK) {
100+
refreshPasswordList()
101+
}
102+
}
103+
104+
private val passwordMoveAction = registerForActivityResult(StartActivityForResult()) { result ->
105+
val intentData = result.data ?: return@registerForActivityResult
106+
val filesToMove = requireNotNull(intentData.getStringArrayExtra("Files"))
107+
val target = File(requireNotNull(intentData.getStringExtra("SELECTED_FOLDER_PATH")))
108+
val repositoryPath = getRepositoryDirectory().absolutePath
109+
if (!target.isDirectory) {
110+
e { "Tried moving passwords to a non-existing folder." }
111+
return@registerForActivityResult
112+
}
113+
114+
d { "Moving passwords to ${intentData.getStringExtra("SELECTED_FOLDER_PATH")}" }
115+
d { filesToMove.joinToString(", ") }
116+
117+
lifecycleScope.launch(Dispatchers.IO) {
118+
for (file in filesToMove) {
119+
val source = File(file)
120+
if (!source.exists()) {
121+
e { "Tried moving something that appears non-existent." }
122+
continue
123+
}
124+
val destinationFile = File(target.absolutePath + "/" + source.name)
125+
val basename = source.nameWithoutExtension
126+
val sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename)
127+
val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename)
128+
if (destinationFile.exists()) {
129+
e { "Trying to move a file that already exists." }
130+
withContext(Dispatchers.Main) {
131+
MaterialAlertDialogBuilder(this@PasswordStore)
132+
.setTitle(resources.getString(R.string.password_exists_title))
133+
.setMessage(resources.getString(
134+
R.string.password_exists_message,
135+
destinationLongName,
136+
sourceLongName)
137+
)
138+
.setPositiveButton(R.string.dialog_ok) { _, _ ->
139+
launch(Dispatchers.IO) {
140+
moveFile(source, destinationFile)
141+
}
142+
}
143+
.setNegativeButton(R.string.dialog_cancel, null)
144+
.show()
145+
}
146+
} else {
147+
launch(Dispatchers.IO) {
148+
moveFile(source, destinationFile)
149+
}
150+
}
151+
}
152+
when (filesToMove.size) {
153+
1 -> {
154+
val source = File(filesToMove[0])
155+
val basename = source.nameWithoutExtension
156+
val sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename)
157+
val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename)
158+
withContext(Dispatchers.Main) {
159+
commitChange(
160+
resources.getString(R.string.git_commit_move_text, sourceLongName, destinationLongName),
161+
)
162+
}
163+
}
164+
else -> {
165+
val repoDir = getRepositoryDirectory().absolutePath
166+
val relativePath = getRelativePath("${target.absolutePath}/", repoDir)
167+
withContext(Dispatchers.Main) {
168+
commitChange(
169+
resources.getString(R.string.git_commit_move_multiple_text, relativePath),
170+
)
171+
}
172+
}
173+
}
174+
}
175+
refreshPasswordList()
176+
plist?.dismissActionMode()
177+
}
178+
88179
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
89180
// open search view on search key, or Ctr+F
90181
if ((keyCode == KeyEvent.KEYCODE_SEARCH || keyCode == KeyEvent.KEYCODE_F && event.isCtrlPressed) &&
@@ -288,9 +379,7 @@ class PasswordStore : BaseGitActivity() {
288379
Snackbar.LENGTH_INDEFINITE
289380
).run {
290381
setAction(getString(R.string.snackbar_action_grant)) {
291-
registerForActivityResult(RequestPermission()) { granted ->
292-
if (granted) checkLocalRepository()
293-
}.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
382+
storagePermissionRequest.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
294383
dismiss()
295384
}
296385
show()
@@ -305,11 +394,7 @@ class PasswordStore : BaseGitActivity() {
305394
private fun checkLocalRepository() {
306395
val repo = initialize()
307396
if (repo == null) {
308-
registerForActivityResult(StartActivityForResult()) { result ->
309-
if (result.resultCode == RESULT_OK) {
310-
checkLocalRepository()
311-
}
312-
}.launch(UserPreference.createDirectorySelectionIntent(this))
397+
directorySelectAction.launch(UserPreference.createDirectorySelectionIntent(this))
313398
} else {
314399
checkLocalRepository(getRepositoryDirectory())
315400
}
@@ -422,11 +507,7 @@ class PasswordStore : BaseGitActivity() {
422507
val intent = Intent(this, PasswordCreationActivity::class.java)
423508
intent.putExtra("FILE_PATH", currentDir.absolutePath)
424509
intent.putExtra("REPO_PATH", getRepositoryDirectory().absolutePath)
425-
registerForActivityResult(StartActivityForResult()) { result ->
426-
if (result.resultCode == RESULT_OK) {
427-
refreshPasswordList()
428-
}
429-
}.launch(intent)
510+
listRefreshAction.launch(intent)
430511
}
431512

432513
fun createFolder() {
@@ -477,80 +558,7 @@ class PasswordStore : BaseGitActivity() {
477558
val intent = Intent(this, SelectFolderActivity::class.java)
478559
val fileLocations = values.map { it.file.absolutePath }.toTypedArray()
479560
intent.putExtra("Files", fileLocations)
480-
registerForActivityResult(StartActivityForResult()) { result ->
481-
val intentData = result.data ?: return@registerForActivityResult
482-
val filesToMove = requireNotNull(intentData.getStringArrayExtra("Files"))
483-
val target = File(requireNotNull(intentData.getStringExtra("SELECTED_FOLDER_PATH")))
484-
val repositoryPath = getRepositoryDirectory().absolutePath
485-
if (!target.isDirectory) {
486-
e { "Tried moving passwords to a non-existing folder." }
487-
return@registerForActivityResult
488-
}
489-
490-
d { "Moving passwords to ${intentData.getStringExtra("SELECTED_FOLDER_PATH")}" }
491-
d { filesToMove.joinToString(", ") }
492-
493-
lifecycleScope.launch(Dispatchers.IO) {
494-
for (file in filesToMove) {
495-
val source = File(file)
496-
if (!source.exists()) {
497-
e { "Tried moving something that appears non-existent." }
498-
continue
499-
}
500-
val destinationFile = File(target.absolutePath + "/" + source.name)
501-
val basename = source.nameWithoutExtension
502-
val sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename)
503-
val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename)
504-
if (destinationFile.exists()) {
505-
e { "Trying to move a file that already exists." }
506-
withContext(Dispatchers.Main) {
507-
MaterialAlertDialogBuilder(this@PasswordStore)
508-
.setTitle(resources.getString(R.string.password_exists_title))
509-
.setMessage(resources.getString(
510-
R.string.password_exists_message,
511-
destinationLongName,
512-
sourceLongName)
513-
)
514-
.setPositiveButton(R.string.dialog_ok) { _, _ ->
515-
launch(Dispatchers.IO) {
516-
moveFile(source, destinationFile)
517-
}
518-
}
519-
.setNegativeButton(R.string.dialog_cancel, null)
520-
.show()
521-
}
522-
} else {
523-
launch(Dispatchers.IO) {
524-
moveFile(source, destinationFile)
525-
}
526-
}
527-
}
528-
when (filesToMove.size) {
529-
1 -> {
530-
val source = File(filesToMove[0])
531-
val basename = source.nameWithoutExtension
532-
val sourceLongName = getLongName(requireNotNull(source.parent), repositoryPath, basename)
533-
val destinationLongName = getLongName(target.absolutePath, repositoryPath, basename)
534-
withContext(Dispatchers.Main) {
535-
commitChange(
536-
resources.getString(R.string.git_commit_move_text, sourceLongName, destinationLongName),
537-
)
538-
}
539-
}
540-
else -> {
541-
val repoDir = getRepositoryDirectory().absolutePath
542-
val relativePath = getRelativePath("${target.absolutePath}/", repoDir)
543-
withContext(Dispatchers.Main) {
544-
commitChange(
545-
resources.getString(R.string.git_commit_move_multiple_text, relativePath),
546-
)
547-
}
548-
}
549-
}
550-
}
551-
refreshPasswordList()
552-
plist?.dismissActionMode()
553-
}.launch(intent)
561+
passwordMoveAction.launch(intent)
554562
}
555563

556564
enum class CategoryRenameError(val resource: Int) {

0 commit comments

Comments
 (0)