diff options
author | Gigahawk <jasperchan515@gmail.com> | 2020-02-19 22:19:09 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-20 11:49:09 +0530 |
commit | 9255583f2d0e5cfb7a88ee54ceb4f5284d41cbfa (patch) | |
tree | 2ff407f6dfab39254d2a79561f0520a21351893a /app/src/main/java | |
parent | bcdc43b1f1d4dbfbf3014a084c0b0c0ee5aa8207 (diff) |
Better UI flow (#630)
* Filter against entire pathname when searching
* Add option to directly open search dialog on start
* Replace basic search filter with fuzzy search
* Update CHANGELOG
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
* Open search in onResume, force search icon to always show
* Add option to always search from root directory
Co-authored-by: Harsh Shandilya <me@msfjarvis.dev>
Diffstat (limited to 'app/src/main/java')
3 files changed, 37 insertions, 4 deletions
diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt index dc3331d2..ad01000c 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt @@ -24,7 +24,6 @@ import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getPasswords import com.zeapo.pwdstore.utils.PasswordRepository.Companion.getRepositoryDirectory import com.zeapo.pwdstore.utils.PasswordRepository.PasswordSortOrder.Companion.getSortOrder import java.io.File -import java.util.Locale import java.util.Stack import me.zhanghai.android.fastscroll.FastScrollerBuilder @@ -157,11 +156,37 @@ class PasswordFragment : Fragment() { if (filter.isEmpty()) { refreshAdapter() } else { - recursiveFilter(filter, if (pathStack.isEmpty()) null else pathStack.peek()) + recursiveFilter( + filter, + if (pathStack.isEmpty() || + settings.getBoolean("search_from_root", false)) + null + else pathStack.peek()) } } /** + * fuzzy matches the filter against the given string + * + * based on https://www.forrestthewoods.com/blog/reverse_engineering_sublime_texts_fuzzy_match/ + * + * @param filter the filter to apply + * @param str the string to filter against + * + * @return true if the filter fuzzymatches the string + */ + private fun fuzzyMatch(filter: String, str: String): Boolean { + var i = 0 + var j = 0 + while (i < filter.length && j < str.length) { + if (filter[i].isWhitespace() || filter[i].toLowerCase() == str[j].toLowerCase()) + i++ + j++ + } + return i == filter.length + } + + /** * recursively filters a directory and extract all the matching items * * @param filter the filter to apply @@ -177,7 +202,7 @@ class PasswordFragment : Fragment() { if (item.type == PasswordItem.TYPE_CATEGORY && rec) { recursiveFilter(filter, item.file) } - val matches = item.toString().toLowerCase(Locale.ROOT).contains(filter.toLowerCase(Locale.ROOT)) + val matches = fuzzyMatch(filter, item.longName) val inAdapter = recyclerAdapter.values.contains(item) if (matches && !inAdapter) { recyclerAdapter.add(item) diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt index 137f4bea..14d039ce 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordStore.kt @@ -142,6 +142,11 @@ class PasswordStore : AppCompatActivity() { } else { checkLocalRepository() } + if (settings.getBoolean("search_on_start", false) && ::searchItem.isInitialized) { + if (!searchItem.isActionViewExpanded) { + searchItem.expandActionView() + } + } } override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { @@ -183,6 +188,9 @@ class PasswordStore : AppCompatActivity() { return true } }) + if (settings.getBoolean("search_on_start", false)) { + searchItem.expandActionView() + } return super.onCreateOptionsMenu(menu) } diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt index 06608671..8030d976 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt @@ -174,7 +174,7 @@ open class PasswordRepository protected constructor() { /** * Gets the password items in the root directory * - * @return a list of passwords in the root direcotyr + * @return a list of passwords in the root directory */ @JvmStatic fun getPasswords(rootDir: File, sortOrder: PasswordSortOrder): ArrayList<PasswordItem> { |