Skip to content

Commit

Permalink
Allow tool quick actions to be customized
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Nov 12, 2023
1 parent 1532420 commit 4cd9c63
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.kylecorry.trail_sense.quickactions

import android.widget.ImageButton
import androidx.core.view.isVisible
import androidx.core.view.setMargins
import com.google.android.flexbox.FlexboxLayout
import com.kylecorry.andromeda.core.system.Resources
import com.kylecorry.andromeda.fragments.AndromedaFragment
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.databinding.FragmentToolsBinding
import com.kylecorry.trail_sense.shared.QuickActionType
import com.kylecorry.trail_sense.shared.UserPreferences

class ToolsQuickActionBinder(
private val fragment: AndromedaFragment,
private val binding: FragmentToolsBinding
) : IQuickActionBinder {

private val prefs by lazy { UserPreferences(fragment.requireContext()) }

private fun createButton(): ImageButton {
val size = Resources.dp(fragment.requireContext(), 40f).toInt()
val margins = Resources.dp(fragment.requireContext(), 8f).toInt()
Expand All @@ -31,13 +36,24 @@ class ToolsQuickActionBinder(

override fun bind() {
binding.quickActions.removeAllViews()

val selected = prefs.toolQuickActions.sortedBy { it.id }

binding.quickActions.isVisible = selected.isNotEmpty()

// TODO: Weather monitor
// TODO: Backtrack quick action should be a toggle rather than opening the path
// QuickActionBacktrack(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionFlashlight(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionWhistle(createButton(), fragment).bind(fragment.viewLifecycleOwner)
LowPowerQuickAction(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionSunsetAlert(createButton(), fragment).bind(fragment.viewLifecycleOwner)
QuickActionWhiteNoise(createButton(), fragment).bind(fragment.viewLifecycleOwner)
selected.forEach {
val action = when (it) {
QuickActionType.Flashlight -> QuickActionFlashlight(createButton(), fragment)
QuickActionType.Whistle -> QuickActionWhistle(createButton(), fragment)
QuickActionType.WhiteNoise -> QuickActionWhiteNoise(createButton(), fragment)
QuickActionType.LowPowerMode -> LowPowerQuickAction(createButton(), fragment)
QuickActionType.SunsetAlert -> QuickActionSunsetAlert(createButton(), fragment)
else -> null // No other actions are supported
}

action?.bind(fragment.viewLifecycleOwner)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import com.kylecorry.andromeda.sense.Sensors
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.backup.BackupCommand
import com.kylecorry.trail_sense.backup.RestoreCommand
import com.kylecorry.trail_sense.shared.QuickActionType
import com.kylecorry.trail_sense.shared.QuickActionUtils
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.io.ActivityUriPicker
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem
import com.kylecorry.trail_sense.shared.requireMainActivity
Expand Down Expand Up @@ -48,6 +51,7 @@ class SettingsFragment : AndromedaPreferenceFragment() {
private val uriPicker by lazy { ActivityUriPicker(requireMainActivity()) }
private val backupCommand by lazy { BackupCommand(requireContext(), uriPicker) }
private val restoreCommand by lazy { RestoreCommand(requireContext(), uriPicker) }
private val prefs by lazy { UserPreferences(requireContext()) }

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
Expand Down Expand Up @@ -98,6 +102,34 @@ class SettingsFragment : AndromedaPreferenceFragment() {
}
}
}


onClick(findPreference(getString(R.string.pref_tool_quick_action_header_key))){
val potentialActions = listOf(
QuickActionType.Flashlight,
QuickActionType.Whistle,
QuickActionType.LowPowerMode,
QuickActionType.SunsetAlert,
QuickActionType.WhiteNoise,
)

val selected = prefs.toolQuickActions

val selectedIndices = potentialActions.mapIndexedNotNull { index, quickActionType ->
if (selected.contains(quickActionType)) index else null
}

Pickers.items(
requireContext(),
getString(R.string.tool_quick_actions),
potentialActions.map { QuickActionUtils.getName(requireContext(), it) },
selectedIndices
) {
if (it != null) {
prefs.toolQuickActions = it.map { potentialActions[it] }
}
}
}
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import com.kylecorry.trail_sense.settings.infrastructure.PowerPreferences
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.putIntArray
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 @@ -303,6 +305,32 @@ class UserPreferences(private val context: Context) : IDeclinationPreferences {
ToolSortType.Category
)

var toolQuickActions: List<QuickActionType>
get() {
val ids = cache.getIntArray(context.getString(R.string.pref_tool_quick_actions))
?: return listOf(
QuickActionType.Flashlight,
QuickActionType.Whistle,
QuickActionType.LowPowerMode,
)

return ids.mapNotNull { QuickActionType.values().find { q -> q.id == it } }
}
set(value) {
cache.putIntArray(
context.getString(R.string.pref_tool_quick_actions),
value.map { it.id }
)
}

// 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)
// }

private fun getString(id: Int): String {
return context.getString(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,20 @@ fun IPreferences.putOrRemoveCoordinate(key: String, value: Coordinate?) {
} else {
putCoordinate(key, value)
}
}

fun IPreferences.putIntArray(key: String, value: List<Int>) {
putString(key, value.joinToString(","))
}

fun IPreferences.getIntArray(key: String): List<Int>? {
return getString(key)?.split(",")?.mapNotNull { it.toIntOrNull() }
}

fun IPreferences.putLongArray(key: String, value: List<Long>) {
putString(key, value.joinToString(","))
}

fun IPreferences.getLongArray(key: String): List<Long>? {
return getString(key)?.split(",")?.mapNotNull { it.toLongOrNull() }
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,9 @@
<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>
<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>
<plurals name="map_group_summary">
<item quantity="one">%d map</item>
<item quantity="other">%d maps</item>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
app:singleLineTitle="false"
app:title="@string/tools">

<Preference
app:icon="@drawable/apps"
app:key="@string/pref_tool_quick_action_header_key"
app:singleLineTitle="false"
app:title="@string/tool_quick_actions" />

<Preference
app:icon="@drawable/ic_compass_icon"
app:key="@string/pref_navigation_header_key"
Expand Down

0 comments on commit 4cd9c63

Please sign in to comment.