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

Commit

Permalink
Consolidate password list refresh
Browse files Browse the repository at this point in the history
Replace refreshPasswordList and resetPasswordList with a single
function that resets navigation only if necessary, namely if the
current directory no longer exists.
This function is now also called mostly from PasswordStore, whereas
previously refresh and reset calls were made from many different
places in the code base, sometimes twice, sometimes ineffectively.

There is a remaining issue to fix: The sync operation does not return
with the correct request code (105), but with 65535. As a result, as
before, no refresh/reset happens after a sync operation while
everything works as expected after a pull operation.
  • Loading branch information
fmeum committed Jun 26, 2020
1 parent b60c5fb commit 3360650
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 34 deletions.
37 changes: 16 additions & 21 deletions app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt
Original file line number Diff line number Diff line change
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 Down
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 3360650

Please sign in to comment.