From 0e1cc36a9e88e60825b350704f26201b14d1227d Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Sun, 12 Nov 2023 07:35:17 -0500 Subject: [PATCH] Move pinned tools to prefs --- .../trail_sense/shared/UserPreferences.kt | 20 ++++++++----- .../trail_sense/tools/ui/PinnedToolManager.kt | 28 ++++--------------- .../trail_sense/tools/ui/ToolsFragment.kt | 8 ++---- app/src/main/res/values/strings.xml | 1 + 4 files changed, 22 insertions(+), 35 deletions(-) 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 18b9ba6a6..f21a35e75 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 @@ -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 @@ -323,13 +325,17 @@ class UserPreferences(private val context: Context) : IDeclinationPreferences { ) } -// var toolPinnedIds: List -// 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 + 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) 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 8d3edcd73..c10520cb1 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 @@ -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() 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) @@ -29,21 +27,21 @@ 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 { @@ -51,18 +49,4 @@ class PinnedToolManager(private val prefs: IPreferences) { pinned.contains(toolId) } } - - private fun readPrefs(): List { - val str = prefs.getString(key) ?: return listOf( - 6L, // Navigation - 20L, // Weather - 14L // Astronomy - ) - return str.split(",").mapNotNull { it.toLongOrNull() } - } - - private fun writePrefs(toolIds: List) { - prefs.putString(key, toolIds.joinToString(",")) - } - } \ No newline at end of file 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 481f81a75..8c289fb99 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 @@ -36,11 +36,7 @@ class ToolsFragment : BoundFragment() { 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()) } @@ -129,7 +125,7 @@ class ToolsFragment : BoundFragment() { sortTypes.map { sortTypeNames[it] ?: "" }, sortTypes.indexOf(prefs.toolSort) ) { selectedIdx -> - if (selectedIdx != null){ + if (selectedIdx != null) { prefs.toolSort = sortTypes[selectedIdx] updateTools() } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a2a1a8a30..96fc5265b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1364,6 +1364,7 @@ Tool quick actions pref_tool_quick_action_header_key pref_tool_quick_actions + pref_pinned_tools %d map %d maps