Skip to content

Commit

Permalink
Create backtrack quick action
Browse files Browse the repository at this point in the history
Closes #2048
  • Loading branch information
kylecorry31 committed Nov 13, 2023
1 parent 39d4829 commit 49667c9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class NavigationPreferences(private val context: Context) : ICompassStylePrefere
get() {
val id = cache.getString(context.getString(R.string.pref_navigation_quick_action_left))
?.toIntCompat()
return QuickActionType.values().firstOrNull { it.id == id } ?: QuickActionType.Backtrack
return QuickActionType.values().firstOrNull { it.id == id } ?: QuickActionType.Paths
}

val rightButton: QuickActionType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ class NavigationQuickActionBinder(
): QuickActionButton {
return when (type) {
QuickActionType.None -> QuickActionNone(button, fragment)
QuickActionType.Backtrack -> QuickActionBacktrack(button, fragment)
QuickActionType.Paths -> QuickActionPaths(button, fragment)
QuickActionType.Flashlight -> QuickActionFlashlight(button, fragment)
QuickActionType.Ruler -> QuickActionRuler(button, fragment, binding.ruler)
QuickActionType.Maps -> QuickActionOfflineMaps(button, fragment)
QuickActionType.Whistle -> QuickActionWhistle(button, fragment)
QuickActionType.LowPowerMode -> LowPowerQuickAction(button, fragment)
QuickActionType.NightMode -> QuickActionNightMode(button, fragment)
QuickActionType.Backtrack -> QuickActionBacktrack(button, fragment)
else -> QuickActionNone(button, fragment)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,46 @@ package com.kylecorry.trail_sense.quickactions
import android.widget.ImageButton
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.kylecorry.andromeda.alerts.toast
import com.kylecorry.andromeda.core.topics.generic.ITopic
import com.kylecorry.andromeda.core.topics.generic.replay
import com.kylecorry.andromeda.fragments.AndromedaFragment
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.navigation.paths.infrastructure.subsystem.BacktrackSubsystem
import com.kylecorry.trail_sense.shared.FeatureState
import com.kylecorry.trail_sense.shared.permissions.RequestRemoveBatteryRestrictionCommand
import com.kylecorry.trail_sense.shared.permissions.requestLocationForegroundServicePermission

class QuickActionBacktrack(btn: ImageButton, fragment: Fragment) :
TopicQuickAction(btn, fragment, hideWhenUnavailable = false) {
class QuickActionBacktrack(btn: ImageButton, private val andromedaFragment: AndromedaFragment) :
TopicQuickAction(btn, andromedaFragment, hideWhenUnavailable = false) {

private val backtrack = BacktrackSubsystem.getInstance(context)

override fun onCreate() {
super.onCreate()
button.setImageResource(R.drawable.ic_tool_backtrack)
button.setOnClickListener {
fragment.findNavController()
.navigate(R.id.action_navigatorFragment_to_fragmentBacktrack)
when (backtrack.getState()) {
FeatureState.On -> backtrack.disable()
FeatureState.Off -> {
andromedaFragment.requestLocationForegroundServicePermission { success ->
if (success) {
backtrack.enable(true)
RequestRemoveBatteryRestrictionCommand(andromedaFragment).execute()
}
}
}

FeatureState.Unavailable -> fragment.toast(context.getString(R.string.backtrack_disabled_low_power_toast))
}
}

button.setOnLongClickListener {
fragment.findNavController().navigate(R.id.fragmentBacktrack)
true
}
}

override val state: ITopic<FeatureState> =
BacktrackSubsystem.getInstance(context).state.replay()
override val state: ITopic<FeatureState> = backtrack.state.replay()

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.kylecorry.trail_sense.quickactions

import android.widget.ImageButton
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import com.kylecorry.andromeda.core.topics.generic.ITopic
import com.kylecorry.andromeda.core.topics.generic.replay
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.navigation.paths.infrastructure.subsystem.BacktrackSubsystem
import com.kylecorry.trail_sense.shared.FeatureState

class QuickActionPaths(btn: ImageButton, fragment: Fragment) :
TopicQuickAction(btn, fragment, hideWhenUnavailable = false) {

override fun onCreate() {
super.onCreate()
button.setImageResource(R.drawable.ic_tool_backtrack)
button.setOnClickListener {
fragment.findNavController()
.navigate(R.id.action_navigatorFragment_to_fragmentBacktrack)
}
}

override val state: ITopic<FeatureState> =
BacktrackSubsystem.getInstance(context).state.replay()

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class ToolsQuickActionBinder(
binding.quickActions.isVisible = selected.isNotEmpty()

// TODO: Weather monitor
// TODO: Backtrack quick action should be a toggle rather than opening the path
selected.forEach {
val action = when (it) {
QuickActionType.Flashlight -> QuickActionFlashlight(createButton(), fragment)
Expand All @@ -54,6 +53,7 @@ class ToolsQuickActionBinder(
QuickActionType.LowPowerMode -> LowPowerQuickAction(createButton(), fragment)
QuickActionType.SunsetAlert -> QuickActionSunsetAlert(createButton(), fragment)
QuickActionType.NightMode -> QuickActionNightMode(createButton(), fragment)
QuickActionType.Backtrack -> QuickActionBacktrack(createButton(), fragment)
else -> null // No other actions are supported
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.kylecorry.trail_sense.tools.flashlight.infrastructure.FlashlightSubsy

enum class QuickActionType(val id: Int) {
None(-1),
Backtrack(0),
Paths(0),
Flashlight(1),
Clouds(2),
Temperature(3),
Expand All @@ -18,15 +18,16 @@ enum class QuickActionType(val id: Int) {
Thunder(11),
Climate(12),
SunsetAlert(13),
NightMode(14)
NightMode(14),
Backtrack(15)
}

object QuickActionUtils {

fun getName(context: Context, quickActionType: QuickActionType): String {
return when (quickActionType) {
QuickActionType.None -> context.getString(R.string.none)
QuickActionType.Backtrack -> context.getString(R.string.backtrack)
QuickActionType.Paths -> context.getString(R.string.paths)
QuickActionType.Flashlight -> context.getString(R.string.flashlight_title)
QuickActionType.Clouds -> context.getString(R.string.clouds)
QuickActionType.Temperature -> context.getString(R.string.tool_temperature_estimation_title)
Expand All @@ -39,6 +40,7 @@ object QuickActionUtils {
QuickActionType.Climate -> context.getString(R.string.tool_climate)
QuickActionType.SunsetAlert -> context.getString(R.string.sunset_alerts)
QuickActionType.NightMode -> context.getString(R.string.night)
QuickActionType.Backtrack -> context.getString(R.string.backtrack)
}
}

Expand All @@ -49,20 +51,22 @@ object QuickActionUtils {
QuickActionType.LowPowerMode,
QuickActionType.SunsetAlert,
QuickActionType.WhiteNoise,
QuickActionType.NightMode
QuickActionType.NightMode,
QuickActionType.Backtrack
)
}

fun navigation(context: Context): List<QuickActionType> {
val list = mutableListOf(
QuickActionType.None,
QuickActionType.Backtrack,
QuickActionType.Paths,
if (FlashlightSubsystem.getInstance(context).isAvailable()) QuickActionType.Flashlight else null,
QuickActionType.Whistle,
QuickActionType.Ruler,
QuickActionType.LowPowerMode,
QuickActionType.Maps,
QuickActionType.NightMode)
QuickActionType.NightMode,
QuickActionType.Backtrack)

return list.filterNotNull()
}
Expand Down

0 comments on commit 49667c9

Please sign in to comment.