Skip to content

Commit

Permalink
Move pinned tools to prefs
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Nov 12, 2023
1 parent 4cd9c63 commit 0e1cc36
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ import com.kylecorry.trail_sense.settings.infrastructure.PrivacyPreferences
import com.kylecorry.trail_sense.settings.infrastructure.ThermometerPreferences
import com.kylecorry.trail_sense.settings.infrastructure.TidePreferences
import com.kylecorry.trail_sense.shared.extensions.getIntArray
import com.kylecorry.trail_sense.shared.extensions.getLongArray
import com.kylecorry.trail_sense.shared.extensions.putIntArray
import com.kylecorry.trail_sense.shared.extensions.putLongArray
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
Expand Down Expand Up @@ -323,13 +325,17 @@ class UserPreferences(private val context: Context) : IDeclinationPreferences {
)
}

// var toolPinnedIds: List<Long>
// get() {
// return cache.getLongArray(context.getString(R.string.pref_tool_pinned_ids)) ?: listOf()
// }
// set(value) {
// cache.putLongArray(context.getString(R.string.pref_tool_pinned_ids), value)
// }
var toolPinnedIds: List<Long>
get() {
return cache.getLongArray(context.getString(R.string.pref_pinned_tools)) ?: listOf(
6L, // Navigation
20L, // Weather
14L // Astronomy
)
}
set(value) {
cache.putLongArray(context.getString(R.string.pref_pinned_tools), value)
}

private fun getString(id: Int): String {
return context.getString(id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.kylecorry.trail_sense.tools.ui

import com.kylecorry.andromeda.preferences.IPreferences
import com.kylecorry.trail_sense.shared.UserPreferences

class PinnedToolManager(private val prefs: IPreferences) {
class PinnedToolManager(private val prefs: UserPreferences) {

private val pinned = mutableSetOf<Long>()
private val lock = Any()

private val key = "pref_pinned_tools"

init {
// TODO: Listen for changes
val all = readPrefs()
val all = prefs.toolPinnedIds
synchronized(lock) {
pinned.clear()
pinned.addAll(all)
Expand All @@ -29,40 +27,26 @@ class PinnedToolManager(private val prefs: IPreferences) {
pinned.clear()
pinned.addAll(toolIds)
}
writePrefs(getPinnedToolIds())
prefs.toolPinnedIds = getPinnedToolIds()
}

fun pin(toolId: Long) {
synchronized(lock) {
pinned.add(toolId)
}
writePrefs(getPinnedToolIds())
prefs.toolPinnedIds = getPinnedToolIds()
}

fun unpin(toolId: Long) {
synchronized(lock) {
pinned.remove(toolId)
}
writePrefs(getPinnedToolIds())
prefs.toolPinnedIds = getPinnedToolIds()
}

fun isPinned(toolId: Long): Boolean {
return synchronized(lock) {
pinned.contains(toolId)
}
}

private fun readPrefs(): List<Long> {
val str = prefs.getString(key) ?: return listOf(
6L, // Navigation
20L, // Weather
14L // Astronomy
)
return str.split(",").mapNotNull { it.toLongOrNull() }
}

private fun writePrefs(toolIds: List<Long>) {
prefs.putString(key, toolIds.joinToString(","))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,7 @@ 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 pinnedToolManager by lazy { PinnedToolManager(prefs) }

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

Expand Down Expand Up @@ -129,7 +125,7 @@ class ToolsFragment : BoundFragment<FragmentToolsBinding>() {
sortTypes.map { sortTypeNames[it] ?: "" },
sortTypes.indexOf(prefs.toolSort)
) { selectedIdx ->
if (selectedIdx != null){
if (selectedIdx != null) {
prefs.toolSort = sortTypes[selectedIdx]
updateTools()
}
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 @@ -1364,6 +1364,7 @@
<string name="tool_quick_actions">Tool quick actions</string>
<string name="pref_tool_quick_action_header_key" translatable="false">pref_tool_quick_action_header_key</string>
<string name="pref_tool_quick_actions" translatable="false">pref_tool_quick_actions</string>
<string name="pref_pinned_tools" translatable="false">pref_pinned_tools</string>
<plurals name="map_group_summary">
<item quantity="one">%d map</item>
<item quantity="other">%d maps</item>
Expand Down

0 comments on commit 0e1cc36

Please sign in to comment.