From 95d53e495c66fd0c785397c64991a0d17509b767 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 18 Oct 2020 23:56:13 +0530 Subject: PasswordRepository: split out PasswordSortOrder Signed-off-by: Harsh Shandilya --- .../java/com/zeapo/pwdstore/PasswordFragment.kt | 3 +- .../pwdstore/SearchableRepositoryViewModel.kt | 13 +++--- .../onboarding/fragments/RepoLocationFragment.kt | 5 ++- .../com/zeapo/pwdstore/utils/PasswordRepository.kt | 39 ------------------ .../com/zeapo/pwdstore/utils/PasswordSortOrder.kt | 46 ++++++++++++++++++++++ 5 files changed, 58 insertions(+), 48 deletions(-) create mode 100644 app/src/main/java/com/zeapo/pwdstore/utils/PasswordSortOrder.kt (limited to 'app') diff --git a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt index 03d4a4f3..ffbe5bc8 100644 --- a/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/PasswordFragment.kt @@ -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 { diff --git a/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt b/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt index a73dd40d..7ac2bfc7 100644 --- a/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt +++ b/app/src/main/java/com/zeapo/pwdstore/SearchableRepositoryViewModel.kt @@ -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 { 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 diff --git a/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt b/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt index d451fabe..bea0ce3e 100644 --- a/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt +++ b/app/src/main/java/com/zeapo/pwdstore/ui/onboarding/fragments/RepoLocationFragment.kt @@ -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) { 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 6e090ba3..beb1c7b4 100644 --- a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordRepository.kt @@ -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) { - - 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 diff --git a/app/src/main/java/com/zeapo/pwdstore/utils/PasswordSortOrder.kt b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordSortOrder.kt new file mode 100644 index 00000000..0613c97d --- /dev/null +++ b/app/src/main/java/com/zeapo/pwdstore/utils/PasswordSortOrder.kt @@ -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) { + + 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) + } + } +} -- cgit v1.2.3