Skip to content

Commit

Permalink
Make tool sort changeable
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Nov 11, 2023
1 parent 35df996 commit 67e3a49
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class PinnedToolManager(private val prefs: IPreferences) {
private val pinned = mutableSetOf<Long>()
private val lock = Any()

private val key = "pinned_tools"
private val key = "pref_pinned_tools"

init {
// TODO: Listen for changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,25 +21,29 @@ 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<FragmentToolsBinding>() {

private val tools by lazy { Tools.getTools(requireContext()) }
private val prefs by lazy { UserPreferences(requireContext()) }

private val pinnedToolManager by lazy {
PinnedToolManager(
PreferencesSubsystem.getInstance(requireContext()).preferences
)
}

private val toolSorter by lazy { CategoricalToolSort(requireContext()) }
private val toolSortFactory by lazy { ToolSortFactory(requireContext()) }

private val pinnedSorter = AlphabeticalToolSort()

override fun generateBinding(
Expand Down Expand Up @@ -96,6 +98,10 @@ class ToolsFragment : BoundFragment<FragmentToolsBinding>() {
}
}

binding.sortBtn.setOnClickListener {
changeToolSort()
}

CustomUiUtils.oneTimeToast(
requireContext(),
getString(R.string.tool_long_press_hint_toast),
Expand All @@ -110,6 +116,26 @@ class ToolsFragment : BoundFragment<FragmentToolsBinding>() {
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

Expand All @@ -130,7 +156,8 @@ class ToolsFragment : BoundFragment<FragmentToolsBinding>() {
}
}

populateTools(toolSorter.sort(tools), binding.tools)
val sorter = toolSortFactory.getToolSort(prefs.toolSort)
populateTools(sorter.sort(tools), binding.tools)
}

private fun updatePinnedTools() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kylecorry.trail_sense.tools.ui.sort

enum class ToolSortType {
Name,
Category
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/sort_ascending.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!-- drawable/sort_ascending.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#ffffff"
android:pathData="M19 17H22L18 21L14 17H17V3H19M2 17H12V19H2M6 5V7H2V5M2 11H9V13H2V11Z" />
</vector>
28 changes: 23 additions & 5 deletions app/src/main/res/layout/fragment_tools.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,32 @@
app:columnCount="2"
app:useDefaultMargins="false" />

<TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:text="@string/tools"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />
android:gravity="center_vertical"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_weight="1"
android:text="@string/tools"
android:textAppearance="@style/TextAppearance.Material3.HeadlineSmall" />

<ImageButton
android:id="@+id/sort_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:backgroundTint="@android:color/transparent"
app:srcCompat="@drawable/sort_ascending"
app:tint="?android:textColorSecondary" />

</LinearLayout>

<androidx.gridlayout.widget.GridLayout
android:id="@+id/tools"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@
<string name="unpin">Unpin</string>
<string name="pin">Pin</string>
<string name="tool_long_press_hint_toast">Long press a tool to see more options</string>
<string name="pref_tool_sort" translatable="false">pref_tool_sort</string>
<plurals name="map_group_summary">
<item quantity="one">%d map</item>
<item quantity="other">%d maps</item>
Expand Down

0 comments on commit 67e3a49

Please sign in to comment.