From cfc59d47cebceced432a127878dfd2f89d6650d3 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Fri, 12 Jan 2024 16:51:41 -0500 Subject: [PATCH] Extract foreground service start method for tiles --- .../kylecorry/trail_sense/tiles/BacktrackTile.kt | 14 ++------------ .../kylecorry/trail_sense/tiles/PedometerTile.kt | 6 +----- .../com/kylecorry/trail_sense/tiles/TileUtils.kt | 11 +++++++++++ .../trail_sense/tiles/WeatherMonitorTile.kt | 6 +----- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/com/kylecorry/trail_sense/tiles/BacktrackTile.kt b/app/src/main/java/com/kylecorry/trail_sense/tiles/BacktrackTile.kt index 55b7ce9f1..61c281457 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tiles/BacktrackTile.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tiles/BacktrackTile.kt @@ -8,15 +8,11 @@ import com.kylecorry.andromeda.core.topics.generic.replay import com.kylecorry.trail_sense.navigation.paths.infrastructure.subsystem.BacktrackSubsystem import com.kylecorry.trail_sense.shared.FeatureState import com.kylecorry.trail_sense.shared.FormatService -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch @RequiresApi(Build.VERSION_CODES.N) class BacktrackTile : TopicTile() { private val backtrack by lazy { BacktrackSubsystem.getInstance(this) } private val formatter by lazy { FormatService.getInstance(this) } - private val scope = CoroutineScope(Dispatchers.Default) override val stateTopic: ITopic get() = backtrack.state.replay() @@ -30,14 +26,8 @@ class BacktrackTile : TopicTile() { } override fun start() { - if (isForegroundWorkaroundNeeded()) { - startWorkaround { - backtrack.enable(true) - } - } else { - scope.launch { - backtrack.enable(true) - } + startForegroundService { + backtrack.enable(true) } } } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tiles/PedometerTile.kt b/app/src/main/java/com/kylecorry/trail_sense/tiles/PedometerTile.kt index 76a964a31..3db2685e4 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tiles/PedometerTile.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tiles/PedometerTile.kt @@ -33,11 +33,7 @@ class PedometerTile : TopicTile() { } override fun start() { - if (isForegroundWorkaroundNeeded()) { - startWorkaround { - pedometer.enable() - } - } else { + startForegroundService { pedometer.enable() } } diff --git a/app/src/main/java/com/kylecorry/trail_sense/tiles/TileUtils.kt b/app/src/main/java/com/kylecorry/trail_sense/tiles/TileUtils.kt index 90e36174f..81ed97ff1 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tiles/TileUtils.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tiles/TileUtils.kt @@ -38,4 +38,15 @@ inline fun AndromedaTileService.startWorkaround(crossinline action: suspend () - } showDialog(dialog) } +} + +inline fun AndromedaTileService.startForegroundService(crossinline action: suspend () -> Unit) { + if (isForegroundWorkaroundNeeded()) { + startWorkaround { + action() + } + } else { + val scope = CoroutineScope(Dispatchers.Default) + scope.launch { action() } + } } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tiles/WeatherMonitorTile.kt b/app/src/main/java/com/kylecorry/trail_sense/tiles/WeatherMonitorTile.kt index 5671656f1..795deae86 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tiles/WeatherMonitorTile.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tiles/WeatherMonitorTile.kt @@ -30,11 +30,7 @@ class WeatherMonitorTile : TopicTile() { } override fun start() { - if (isForegroundWorkaroundNeeded()) { - startWorkaround { - weather.enableMonitor() - } - } else { + startForegroundService { weather.enableMonitor() } }