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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort by recently used #1031

Merged
Merged
1 change: 0 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,9 +7,11 @@ All notable changes to this project will be documented in this file.
## [1.11.0] - 2020-08-18

### Added

Dioxo marked this conversation as resolved.
Show resolved Hide resolved
- Allow changing the branch used for Git operations
- Allow setting a subdirectory key when creating folders
- Allow adding digits/symbols in XkPasswd generated passwords using a mask-like value (`dds` gives you two digits and a symbol, and so on)
Dioxo marked this conversation as resolved.
Show resolved Hide resolved
- Allow sort by recently used password

### Changed

Expand Down
10 changes: 10 additions & 0 deletions app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt
Expand Up @@ -32,6 +32,8 @@ import com.zeapo.pwdstore.ui.adapters.PasswordItemRecyclerAdapter
import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet
import com.zeapo.pwdstore.utils.PasswordItem
import com.zeapo.pwdstore.utils.PasswordRepository
import com.zeapo.pwdstore.utils.PreferenceKeys
import com.zeapo.pwdstore.utils.getString
import com.zeapo.pwdstore.utils.sharedPrefs
import com.zeapo.pwdstore.utils.viewBinding
import java.io.File
Expand Down Expand Up @@ -243,6 +245,14 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
try {
listener = object : OnFragmentInteractionListener {
override fun onFragmentInteraction(item: PasswordItem) {
if (settings.getString(PreferenceKeys.SORT_ORDER) == PasswordRepository.PasswordSortOrder.RECENTLY_USED.name) {
//save the time when password was used
fmeum marked this conversation as resolved.
Show resolved Hide resolved
val preferences = context.getSharedPreferences("recent_password_history", Context.MODE_PRIVATE )
Dioxo marked this conversation as resolved.
Show resolved Hide resolved
val editor = preferences.edit()
editor.putString(item.file.absolutePath, System.currentTimeMillis().toString())
fmeum marked this conversation as resolved.
Show resolved Hide resolved
editor.apply()
Dioxo marked this conversation as resolved.
Show resolved Hide resolved
}

if (item.type == PasswordItem.TYPE_CATEGORY) {
navigateTo(item.file)
} else {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt
Expand Up @@ -6,6 +6,7 @@ package com.zeapo.pwdstore

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ShortcutInfo.Builder
Expand Down Expand Up @@ -753,6 +754,17 @@ class PasswordStore : AppCompatActivity(R.layout.activity_pwdstore) {
!newCategory.isInsideRepository() -> renameCategory(oldCategory, CategoryRenameError.DestinationOutsideRepo)
else -> lifecycleScope.launch(Dispatchers.IO) {
moveFile(oldCategory.file, newCategory)

//associate the new category with the last category's timestamp in history
val preference = getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
val timestamp = preference.getString(oldCategory.file.absolutePath)
msfjarvis marked this conversation as resolved.
Show resolved Hide resolved
msfjarvis marked this conversation as resolved.
Show resolved Hide resolved
if (timestamp != null) {
val editor = preference.edit()
Dioxo marked this conversation as resolved.
Show resolved Hide resolved
editor.remove(oldCategory.file.absolutePath)
editor.putString(newCategory.absolutePath, timestamp)
editor.apply()
}

withContext(Dispatchers.Main) {
commitChange(
resources.getString(R.string.git_commit_move_text, oldCategory.name, newCategory.name),
Expand Down
Expand Up @@ -96,6 +96,7 @@ private fun PasswordItem.Companion.makeComparator(
// declare them all equal at this stage.
PasswordRepository.PasswordSortOrder.INDEPENDENT -> Comparator<PasswordItem> { _, _ -> 0 }
PasswordRepository.PasswordSortOrder.FILE_FIRST -> compareByDescending { it.type }
PasswordRepository.PasswordSortOrder.RECENTLY_USED -> PasswordRepository.PasswordSortOrder.RECENTLY_USED.comparator
}
.then(compareBy(nullsLast(CaseInsensitiveComparator)) {
directoryStructure.getIdentifierFor(it.file)
Expand Down
Expand Up @@ -5,6 +5,7 @@

package com.zeapo.pwdstore.crypto

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.InputType
Expand Down Expand Up @@ -411,6 +412,17 @@ class PasswordCreationActivity : BasePgpActivity(), OpenPgpServiceConnection.OnB
return@executeApiAsync
}

//associate the new password name with the last name's timestamp in history
val preference = getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
val oldFilePath = "$repoPath/${oldCategory?.trim('/')}/$oldFileName.gpg"
val timestamp = preference.getString(oldFilePath)
if (timestamp != null) {
val editor = preference.edit()
Dioxo marked this conversation as resolved.
Show resolved Hide resolved
editor.remove(oldFilePath)
editor.putString(file.absolutePath, timestamp)
editor.apply()
}

val returnIntent = Intent()
returnIntent.putExtra(RETURN_EXTRA_CREATED_FILE, path)
returnIntent.putExtra(RETURN_EXTRA_NAME, editName)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt
Expand Up @@ -31,6 +31,18 @@ open class PasswordRepository protected constructor() {
p1.name.compareTo(p2.name, ignoreCase = true)
}),

RECENTLY_USED(Comparator { p1: PasswordItem, p2: PasswordItem ->
val recentHistory = Application.instance.getSharedPreferences("recent_password_history", android.content.Context.MODE_PRIVATE)
val timeP1 = recentHistory.getString(p1.file.absolutePath)
val timeP2 = recentHistory.getString(p2.file.absolutePath)
when {
timeP1 != null && timeP2 != null -> timeP2.compareTo(timeP1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be simplified using compareBy and nullsLast, but I could also add that later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll leave it to you, I have no idea how to use those...

timeP1 != null && timeP2 == null -> return@Comparator -1
timeP1 == null && timeP2 != null -> return@Comparator 1
else -> p1.name.compareTo(p2.name, ignoreCase = true)
}
}),

FILE_FIRST(Comparator { p1: PasswordItem, p2: PasswordItem ->
(p2.type + p1.name).compareTo(p1.type + p2.name, ignoreCase = true)
});
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/arrays.xml
Expand Up @@ -8,11 +8,13 @@
<item>@string/pref_folder_first_sort_order</item>
<item>@string/pref_file_first_sort_order</item>
<item>@string/pref_type_independent_sort_order</item>
<item>@string/pref_recently_used_sort_order</item>
</string-array>
<string-array name="sort_order_values">
<item>FOLDER_FIRST</item>
<item>FILE_FIRST</item>
<item>INDEPENDENT</item>
<item>RECENTLY_USED</item>
</string-array>
<string-array name="capitalization_type_values">
<item>lowercase</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Expand Up @@ -140,6 +140,7 @@
<string name="pref_folder_first_sort_order">Folders first</string>
<string name="pref_file_first_sort_order">Files first</string>
<string name="pref_type_independent_sort_order">Type independent</string>
<string name="pref_recently_used_sort_order">Recently used</string>
<string name="pref_autofill_title">Autofill</string>
<string name="pref_autofill_enable_title">Enable Autofill</string>
<string name="pref_autofill_enable_msg">Tap OK to go to Accessibility settings. There, tap Password Store under Services then tap the switch in the top right to turn it on or off.</string>
Expand Down