From 67e3a49ab57a06dcbd1850a036990dbac52802d6 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Sat, 11 Nov 2023 16:43:25 -0500 Subject: [PATCH] Make tool sort changeable --- .../trail_sense/shared/UserPreferences.kt | 12 ++++++ .../trail_sense/tools/ui/PinnedToolManager.kt | 2 +- .../trail_sense/tools/ui/ToolsFragment.kt | 37 ++++++++++++++++--- .../tools/ui/sort/ToolSortFactory.kt | 14 +++++++ .../trail_sense/tools/ui/sort/ToolSortType.kt | 6 +++ app/src/main/res/drawable/sort_ascending.xml | 10 +++++ app/src/main/res/layout/fragment_tools.xml | 28 +++++++++++--- app/src/main/res/values/strings.xml | 1 + 8 files changed, 99 insertions(+), 11 deletions(-) create mode 100644 app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortFactory.kt create mode 100644 app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortType.kt create mode 100644 app/src/main/res/drawable/sort_ascending.xml diff --git a/app/src/main/java/com/kylecorry/trail_sense/shared/UserPreferences.kt b/app/src/main/java/com/kylecorry/trail_sense/shared/UserPreferences.kt index 052e4e08e..ae215c061 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/shared/UserPreferences.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/shared/UserPreferences.kt @@ -5,6 +5,7 @@ import android.hardware.SensorManager import com.kylecorry.andromeda.core.system.Resources import com.kylecorry.andromeda.core.toFloatCompat import com.kylecorry.andromeda.preferences.BooleanPreference +import com.kylecorry.andromeda.preferences.IntEnumPreference import com.kylecorry.andromeda.preferences.IntPreference import com.kylecorry.andromeda.preferences.StringEnumPreference import com.kylecorry.sol.units.Coordinate @@ -31,6 +32,7 @@ import com.kylecorry.trail_sense.settings.infrastructure.ThermometerPreferences import com.kylecorry.trail_sense.settings.infrastructure.TidePreferences import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem import com.kylecorry.trail_sense.shared.sharing.MapSite +import com.kylecorry.trail_sense.tools.ui.sort.ToolSortType import com.kylecorry.trail_sense.weather.infrastructure.WeatherPreferences import java.time.Duration @@ -291,6 +293,16 @@ class UserPreferences(private val context: Context) : IDeclinationPreferences { ), MapSite.OSM ) + var toolSort: ToolSortType by IntEnumPreference( + cache, + context.getString(R.string.pref_tool_sort), + mapOf( + 1 to ToolSortType.Name, + 2 to ToolSortType.Category + ), + ToolSortType.Category + ) + private fun getString(id: Int): String { return context.getString(id) } diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/PinnedToolManager.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/PinnedToolManager.kt index a8c17a15d..8d3edcd73 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/PinnedToolManager.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/PinnedToolManager.kt @@ -7,7 +7,7 @@ class PinnedToolManager(private val prefs: IPreferences) { private val pinned = mutableSetOf() private val lock = Any() - private val key = "pinned_tools" + private val key = "pref_pinned_tools" init { // TODO: Listen for changes diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt index eddfda3d1..cb8f9af13 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/ToolsFragment.kt @@ -6,9 +6,7 @@ import android.view.Gravity import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import android.widget.ImageButton import android.widget.TextView -import androidx.core.view.children import androidx.core.view.isVisible import androidx.core.view.setMargins import androidx.gridlayout.widget.GridLayout @@ -23,17 +21,20 @@ import com.kylecorry.trail_sense.R import com.kylecorry.trail_sense.databinding.FragmentToolsBinding import com.kylecorry.trail_sense.quickactions.ToolsQuickActionBinder import com.kylecorry.trail_sense.shared.CustomUiUtils +import com.kylecorry.trail_sense.shared.UserPreferences import com.kylecorry.trail_sense.shared.colors.AppColor import com.kylecorry.trail_sense.shared.extensions.setOnQueryTextListener import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem import com.kylecorry.trail_sense.tools.guide.infrastructure.UserGuideUtils import com.kylecorry.trail_sense.tools.ui.sort.AlphabeticalToolSort -import com.kylecorry.trail_sense.tools.ui.sort.CategoricalToolSort import com.kylecorry.trail_sense.tools.ui.sort.CategorizedTools +import com.kylecorry.trail_sense.tools.ui.sort.ToolSortFactory +import com.kylecorry.trail_sense.tools.ui.sort.ToolSortType class ToolsFragment : BoundFragment() { private val tools by lazy { Tools.getTools(requireContext()) } + private val prefs by lazy { UserPreferences(requireContext()) } private val pinnedToolManager by lazy { PinnedToolManager( @@ -41,7 +42,8 @@ class ToolsFragment : BoundFragment() { ) } - private val toolSorter by lazy { CategoricalToolSort(requireContext()) } + private val toolSortFactory by lazy { ToolSortFactory(requireContext()) } + private val pinnedSorter = AlphabeticalToolSort() override fun generateBinding( @@ -96,6 +98,10 @@ class ToolsFragment : BoundFragment() { } } + binding.sortBtn.setOnClickListener { + changeToolSort() + } + CustomUiUtils.oneTimeToast( requireContext(), getString(R.string.tool_long_press_hint_toast), @@ -110,6 +116,26 @@ class ToolsFragment : BoundFragment() { ToolsQuickActionBinder(this, binding).bind() } + private fun changeToolSort() { + val sortTypes = ToolSortType.values() + val sortTypeNames = mapOf( + ToolSortType.Name to getString(R.string.name), + ToolSortType.Category to getString(R.string.category) + ) + + Pickers.item( + requireContext(), + getString(R.string.sort), + sortTypes.map { sortTypeNames[it] ?: "" }, + sortTypes.indexOf(prefs.toolSort) + ) { selectedIdx -> + if (selectedIdx != null){ + prefs.toolSort = sortTypes[selectedIdx] + updateTools() + } + } + } + private fun updateTools() { val filter = binding.searchbox.query @@ -130,7 +156,8 @@ class ToolsFragment : BoundFragment() { } } - populateTools(toolSorter.sort(tools), binding.tools) + val sorter = toolSortFactory.getToolSort(prefs.toolSort) + populateTools(sorter.sort(tools), binding.tools) } private fun updatePinnedTools() { diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortFactory.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortFactory.kt new file mode 100644 index 000000000..b4e65845c --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortFactory.kt @@ -0,0 +1,14 @@ +package com.kylecorry.trail_sense.tools.ui.sort + +import android.content.Context + +class ToolSortFactory(private val context: Context) { + + fun getToolSort(sort: ToolSortType): ToolSort { + return when (sort) { + ToolSortType.Name -> AlphabeticalToolSort() + ToolSortType.Category -> CategoricalToolSort(context) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortType.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortType.kt new file mode 100644 index 000000000..ac2c3b6d3 --- /dev/null +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/ui/sort/ToolSortType.kt @@ -0,0 +1,6 @@ +package com.kylecorry.trail_sense.tools.ui.sort + +enum class ToolSortType { + Name, + Category +} \ No newline at end of file diff --git a/app/src/main/res/drawable/sort_ascending.xml b/app/src/main/res/drawable/sort_ascending.xml new file mode 100644 index 000000000..4ca2efd81 --- /dev/null +++ b/app/src/main/res/drawable/sort_ascending.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_tools.xml b/app/src/main/res/layout/fragment_tools.xml index d5b9db025..6d024bd35 100644 --- a/app/src/main/res/layout/fragment_tools.xml +++ b/app/src/main/res/layout/fragment_tools.xml @@ -88,14 +88,32 @@ app:columnCount="2" app:useDefaultMargins="false" /> - + android:gravity="center_vertical" + android:orientation="horizontal"> + + + + + + Unpin Pin Long press a tool to see more options + pref_tool_sort %d map %d maps