Skip to content

Commit

Permalink
Extract foreground service start method for tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jan 12, 2024
1 parent f235d99 commit cfc59d4
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 22 deletions.
14 changes: 2 additions & 12 deletions app/src/main/java/com/kylecorry/trail_sense/tiles/BacktrackTile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeatureState>
get() = backtrack.state.replay()
Expand All @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ class PedometerTile : TopicTile() {
}

override fun start() {
if (isForegroundWorkaroundNeeded()) {
startWorkaround {
pedometer.enable()
}
} else {
startForegroundService {
pedometer.enable()
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/kylecorry/trail_sense/tiles/TileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ class WeatherMonitorTile : TopicTile() {
}

override fun start() {
if (isForegroundWorkaroundNeeded()) {
startWorkaround {
weather.enableMonitor()
}
} else {
startForegroundService {
weather.enableMonitor()
}
}
Expand Down

0 comments on commit cfc59d4

Please sign in to comment.