Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate password list refresh #887

Merged
merged 4 commits into from Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 24 additions & 21 deletions app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt
Expand Up @@ -179,6 +179,11 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
}
}

public override fun onStart() {
super.onStart()
refreshPasswordList()
}

public override fun onResume() {
super.onResume()
// do not attempt to checkLocalRepository() if no storage permission: immediate crash
Expand Down Expand Up @@ -584,7 +589,6 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
item.file.toRelativeString(getRepositoryDirectory(this))
}
))
refreshPasswordList()
}
.setNegativeButton(resources.getString(R.string.dialog_no), null)
.show()
Expand Down Expand Up @@ -662,7 +666,7 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
}
}
}
resetPasswordList()
refreshPasswordList()
plist?.dismissActionMode()
}.launch(intent)
}
Expand Down Expand Up @@ -727,24 +731,17 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
}

/**
* Resets navigation to the repository root and refreshes the password list accordingly.
*
* Use this rather than [refreshPasswordList] after major file system operations that may remove
* the current directory and thus require a full reset of the navigation stack.
*/
fun resetPasswordList() {
model.reset()
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
}

/**
* Refreshes the password list by re-executing the last navigation or search action.
*
* Use this rather than [resetPasswordList] after file system operations limited to the current
* folder since it preserves the scroll position and navigation stack.
* Refreshes the password list by re-executing the last navigation or search action, preserving
* the navigation stack and scroll position. If the current directory no longer exists,
* navigation is reset to the repository root.
*/
fun refreshPasswordList() {
model.forceRefresh()
if (model.currentDir.value?.isDirectory == true) {
model.forceRefresh()
} else {
model.reset()
supportActionBar!!.setDisplayHomeAsUpEnabled(false)
}
}

private val currentDir: File
Expand All @@ -763,15 +760,13 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
data.extras!!.getString("LONG_NAME")))
}
}
refreshPasswordList()
}
REQUEST_CODE_ENCRYPT -> {
commitChange(resources.getString(R.string.git_commit_add_text,
data!!.extras!!.getString("LONG_NAME")))
refreshPasswordList()
}
BaseGitActivity.REQUEST_INIT, NEW_REPO_BUTTON -> initializeRepositoryInfo()
BaseGitActivity.REQUEST_SYNC, BaseGitActivity.REQUEST_PULL -> resetPasswordList()
BaseGitActivity.REQUEST_SYNC, BaseGitActivity.REQUEST_PULL -> refreshPasswordList()
HOME -> checkLocalRepository()
// duplicate code
CLONE_REPO_BUTTON -> {
Expand All @@ -793,6 +788,14 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
intent.putExtra(BaseGitActivity.REQUEST_ARG_OP, BaseGitActivity.REQUEST_CLONE)
startActivityForResult(intent, BaseGitActivity.REQUEST_CLONE)
}
else -> {
d { "Unexpected request code: $requestCode" }
// FIXME: The sync operation returns with a requestCode of 65535 instead of the
// expected 105. It is completely unclear why, but the issue might be resolved
// by switching to ActivityResultContracts. For now, we run the post-sync code
// also when encountering an unexpected request code.
refreshPasswordList()
}
}
}
super.onActivityResult(requestCode, resultCode, data)
Expand Down
Expand Up @@ -58,7 +58,7 @@ class BreakOutOfDetached(fileDir: File, callingActivity: Activity) : GitOperatio
}
}
}
GitAsyncTask(callingActivity, true, this, null)
GitAsyncTask(callingActivity, this, null)
.execute(*this.commands.toTypedArray())
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/zeapo/pwdstore/git/CloneOperation.kt
Expand Up @@ -36,7 +36,7 @@ class CloneOperation(fileDir: File, callingActivity: Activity) : GitOperation(fi

override fun execute() {
(this.command as? CloneCommand)?.setCredentialsProvider(this.provider)
GitAsyncTask(callingActivity, false, this, Intent()).execute(this.command)
GitAsyncTask(callingActivity, this, Intent()).execute(this.command)
}

override fun onError(err: Exception) {
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/com/zeapo/pwdstore/git/GitAsyncTask.kt
Expand Up @@ -10,7 +10,6 @@ import android.content.Context
import android.content.Intent
import android.os.AsyncTask
import com.github.ajalt.timberkt.e
import com.zeapo.pwdstore.PasswordStore
import com.zeapo.pwdstore.R
import com.zeapo.pwdstore.git.config.SshjSessionFactory
import net.schmizz.sshj.common.DisconnectReason
Expand All @@ -30,7 +29,6 @@ import java.lang.ref.WeakReference

class GitAsyncTask(
activity: Activity,
private val refreshListOnEnd: Boolean,
private val operation: GitOperation,
private val finishWithResultOnEnd: Intent?,
private val silentlyExecute: Boolean = false
Expand Down Expand Up @@ -170,9 +168,6 @@ class GitAsyncTask(
}
}
}
if (refreshListOnEnd) {
(activity as? PasswordStore)?.resetPasswordList()
}
(SshSessionFactory.getInstance() as? SshjSessionFactory)?.clearCredentials()
SshSessionFactory.setInstance(null)
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/zeapo/pwdstore/git/PullOperation.kt
Expand Up @@ -35,7 +35,7 @@ class PullOperation(fileDir: File, callingActivity: Activity) : GitOperation(fil

override fun execute() {
(this.command as? PullCommand)?.setCredentialsProvider(this.provider)
GitAsyncTask(callingActivity, false, this, Intent()).execute(this.command)
GitAsyncTask(callingActivity, this, Intent()).execute(this.command)
}

override fun onError(err: Exception) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/zeapo/pwdstore/git/PushOperation.kt
Expand Up @@ -35,7 +35,7 @@ class PushOperation(fileDir: File, callingActivity: Activity) : GitOperation(fil

override fun execute() {
(this.command as? PushCommand)?.setCredentialsProvider(this.provider)
GitAsyncTask(callingActivity, false, this, Intent()).execute(this.command)
GitAsyncTask(callingActivity, this, Intent()).execute(this.command)
}

override fun onError(err: Exception) {
Expand Down
Expand Up @@ -41,7 +41,7 @@ class ResetToRemoteOperation(fileDir: File, callingActivity: Activity) : GitOper

override fun execute() {
this.fetchCommand?.setCredentialsProvider(this.provider)
GitAsyncTask(callingActivity, false, this, Intent())
GitAsyncTask(callingActivity, this, Intent())
.execute(this.addCommand, this.fetchCommand, this.resetCommand)
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/zeapo/pwdstore/git/SyncOperation.kt
Expand Up @@ -50,7 +50,7 @@ class SyncOperation(fileDir: File, callingActivity: Activity) : GitOperation(fil
this.pullCommand?.setCredentialsProvider(this.provider)
this.pushCommand?.setCredentialsProvider(this.provider)
}
GitAsyncTask(callingActivity, false, this, Intent()).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand)
GitAsyncTask(callingActivity, this, Intent()).execute(this.addCommand, this.statusCommand, this.commitCommand, this.pullCommand, this.pushCommand)
}

override fun onError(err: Exception) {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/java/com/zeapo/pwdstore/utils/Extensions.kt
Expand Up @@ -23,7 +23,6 @@ import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKey
import com.github.ajalt.timberkt.d
import com.google.android.material.snackbar.Snackbar
import com.zeapo.pwdstore.PasswordStore
import com.zeapo.pwdstore.git.GitAsyncTask
import com.zeapo.pwdstore.git.GitOperation
import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory
Expand Down Expand Up @@ -92,7 +91,7 @@ fun Activity.commitChange(message: String, finishWithResultOnEnd: Intent? = null
override fun execute() {
d { "Comitting with message: '$message'" }
val git = Git(repository)
val task = GitAsyncTask(this@commitChange, true, this, finishWithResultOnEnd, silentlyExecute = true)
val task = GitAsyncTask(this@commitChange, this, finishWithResultOnEnd, silentlyExecute = true)
task.execute(
git.add().addFilepattern("."),
git.commit().setAll(true).setMessage(message)
Expand Down