PasswordRepository: split out PasswordSortOrder
Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
parent
12fe561be7
commit
95d53e495c
5 changed files with 58 additions and 48 deletions
|
@ -35,6 +35,7 @@ import com.zeapo.pwdstore.ui.dialogs.BasicBottomSheet
|
|||
import com.zeapo.pwdstore.ui.dialogs.ItemCreationBottomSheet
|
||||
import com.zeapo.pwdstore.utils.PasswordItem
|
||||
import com.zeapo.pwdstore.utils.PasswordRepository
|
||||
import com.zeapo.pwdstore.utils.PasswordSortOrder
|
||||
import com.zeapo.pwdstore.utils.PreferenceKeys
|
||||
import com.zeapo.pwdstore.utils.base64
|
||||
import com.zeapo.pwdstore.utils.getString
|
||||
|
@ -262,7 +263,7 @@ class PasswordFragment : Fragment(R.layout.password_recycler_view) {
|
|||
runCatching {
|
||||
listener = object : OnFragmentInteractionListener {
|
||||
override fun onFragmentInteraction(item: PasswordItem) {
|
||||
if (settings.getString(PreferenceKeys.SORT_ORDER) == PasswordRepository.PasswordSortOrder.RECENTLY_USED.name) {
|
||||
if (settings.getString(PreferenceKeys.SORT_ORDER) == PasswordSortOrder.RECENTLY_USED.name) {
|
||||
//save the time when password was used
|
||||
val preferences = context.getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
|
||||
preferences.edit {
|
||||
|
|
|
@ -28,6 +28,7 @@ import com.zeapo.pwdstore.autofill.oreo.AutofillPreferences
|
|||
import com.zeapo.pwdstore.autofill.oreo.DirectoryStructure
|
||||
import com.zeapo.pwdstore.utils.PasswordItem
|
||||
import com.zeapo.pwdstore.utils.PasswordRepository
|
||||
import com.zeapo.pwdstore.utils.PasswordSortOrder
|
||||
import com.zeapo.pwdstore.utils.PreferenceKeys
|
||||
import com.zeapo.pwdstore.utils.sharedPrefs
|
||||
import java.io.File
|
||||
|
@ -87,16 +88,16 @@ private val CaseInsensitiveComparator = Collator.getInstance().apply {
|
|||
}
|
||||
|
||||
private fun PasswordItem.Companion.makeComparator(
|
||||
typeSortOrder: PasswordRepository.PasswordSortOrder,
|
||||
typeSortOrder: PasswordSortOrder,
|
||||
directoryStructure: DirectoryStructure
|
||||
): Comparator<PasswordItem> {
|
||||
return when (typeSortOrder) {
|
||||
PasswordRepository.PasswordSortOrder.FOLDER_FIRST -> compareBy { it.type }
|
||||
PasswordSortOrder.FOLDER_FIRST -> compareBy { it.type }
|
||||
// In order to let INDEPENDENT not distinguish between items based on their type, we simply
|
||||
// declare them all equal at this stage.
|
||||
PasswordRepository.PasswordSortOrder.INDEPENDENT -> Comparator { _, _ -> 0 }
|
||||
PasswordRepository.PasswordSortOrder.FILE_FIRST -> compareByDescending { it.type }
|
||||
PasswordRepository.PasswordSortOrder.RECENTLY_USED -> PasswordRepository.PasswordSortOrder.RECENTLY_USED.comparator
|
||||
PasswordSortOrder.INDEPENDENT -> Comparator { _, _ -> 0 }
|
||||
PasswordSortOrder.FILE_FIRST -> compareByDescending { it.type }
|
||||
PasswordSortOrder.RECENTLY_USED -> PasswordSortOrder.RECENTLY_USED.comparator
|
||||
}
|
||||
.then(compareBy(nullsLast(CaseInsensitiveComparator)) {
|
||||
directoryStructure.getIdentifierFor(it.file)
|
||||
|
@ -150,7 +151,7 @@ class SearchableRepositoryViewModel(application: Application) : AndroidViewModel
|
|||
}
|
||||
|
||||
private val typeSortOrder
|
||||
get() = PasswordRepository.PasswordSortOrder.getSortOrder(settings)
|
||||
get() = PasswordSortOrder.getSortOrder(settings)
|
||||
private val directoryStructure
|
||||
get() = AutofillPreferences.directoryStructure(getApplication())
|
||||
private val itemComparator
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.zeapo.pwdstore.R
|
|||
import com.zeapo.pwdstore.UserPreference
|
||||
import com.zeapo.pwdstore.databinding.FragmentRepoLocationBinding
|
||||
import com.zeapo.pwdstore.utils.PasswordRepository
|
||||
import com.zeapo.pwdstore.utils.PasswordSortOrder
|
||||
import com.zeapo.pwdstore.utils.PreferenceKeys
|
||||
import com.zeapo.pwdstore.utils.finish
|
||||
import com.zeapo.pwdstore.utils.getString
|
||||
|
@ -36,8 +37,8 @@ class RepoLocationFragment : Fragment(R.layout.fragment_repo_location) {
|
|||
|
||||
private val settings by lazy(LazyThreadSafetyMode.NONE) { requireActivity().applicationContext.sharedPrefs }
|
||||
private val binding by viewBinding(FragmentRepoLocationBinding::bind)
|
||||
private val sortOrder: PasswordRepository.PasswordSortOrder
|
||||
get() = PasswordRepository.PasswordSortOrder.getSortOrder(settings)
|
||||
private val sortOrder: PasswordSortOrder
|
||||
get() = PasswordSortOrder.getSortOrder(settings)
|
||||
|
||||
private val repositoryInitAction = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||||
if (result.resultCode == AppCompatActivity.RESULT_OK) {
|
||||
|
|
|
@ -4,8 +4,6 @@
|
|||
*/
|
||||
package com.zeapo.pwdstore.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.core.content.edit
|
||||
|
@ -17,7 +15,6 @@ import java.io.File
|
|||
import java.io.FileFilter
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.LinkOption
|
||||
import java.util.Comparator
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.eclipse.jgit.lib.Repository
|
||||
import org.eclipse.jgit.storage.file.FileRepositoryBuilder
|
||||
|
@ -54,42 +51,6 @@ open class PasswordRepository protected constructor() {
|
|||
}
|
||||
}
|
||||
|
||||
enum class PasswordSortOrder(val comparator: Comparator<PasswordItem>) {
|
||||
|
||||
FOLDER_FIRST(Comparator { p1: PasswordItem, p2: PasswordItem ->
|
||||
(p1.type + p1.name)
|
||||
.compareTo(p2.type + p2.name, ignoreCase = true)
|
||||
}),
|
||||
|
||||
INDEPENDENT(Comparator { p1: PasswordItem, p2: PasswordItem ->
|
||||
p1.name.compareTo(p2.name, ignoreCase = true)
|
||||
}),
|
||||
|
||||
RECENTLY_USED(Comparator { p1: PasswordItem, p2: PasswordItem ->
|
||||
val recentHistory = Application.instance.getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
|
||||
val timeP1 = recentHistory.getString(p1.file.absolutePath.base64())
|
||||
val timeP2 = recentHistory.getString(p2.file.absolutePath.base64())
|
||||
when {
|
||||
timeP1 != null && timeP2 != null -> timeP2.compareTo(timeP1)
|
||||
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)
|
||||
});
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
fun getSortOrder(settings: SharedPreferences): PasswordSortOrder {
|
||||
return valueOf(settings.getString(PreferenceKeys.SORT_ORDER) ?: FOLDER_FIRST.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
private var repository: Repository? = null
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright © 2014-2020 The Android Password Store Authors. All Rights Reserved.
|
||||
* SPDX-License-Identifier: GPL-3.0-only
|
||||
*/
|
||||
|
||||
package com.zeapo.pwdstore.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.SharedPreferences
|
||||
import com.zeapo.pwdstore.Application
|
||||
|
||||
enum class PasswordSortOrder(val comparator: java.util.Comparator<PasswordItem>) {
|
||||
|
||||
FOLDER_FIRST(Comparator { p1: PasswordItem, p2: PasswordItem ->
|
||||
(p1.type + p1.name)
|
||||
.compareTo(p2.type + p2.name, ignoreCase = true)
|
||||
}),
|
||||
|
||||
INDEPENDENT(Comparator { p1: PasswordItem, p2: PasswordItem ->
|
||||
p1.name.compareTo(p2.name, ignoreCase = true)
|
||||
}),
|
||||
|
||||
RECENTLY_USED(Comparator { p1: PasswordItem, p2: PasswordItem ->
|
||||
val recentHistory = Application.instance.getSharedPreferences("recent_password_history", Context.MODE_PRIVATE)
|
||||
val timeP1 = recentHistory.getString(p1.file.absolutePath.base64())
|
||||
val timeP2 = recentHistory.getString(p2.file.absolutePath.base64())
|
||||
when {
|
||||
timeP1 != null && timeP2 != null -> timeP2.compareTo(timeP1)
|
||||
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)
|
||||
});
|
||||
|
||||
companion object {
|
||||
|
||||
@JvmStatic
|
||||
fun getSortOrder(settings: SharedPreferences): PasswordSortOrder {
|
||||
return valueOf(settings.getString(PreferenceKeys.SORT_ORDER) ?: FOLDER_FIRST.name)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue