Skip to content

Commit

Permalink
Remove enabled flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jun 30, 2024
1 parent 5582881 commit 2d93579
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.IntentFilter
import androidx.core.os.bundleOf
import com.kylecorry.andromeda.core.system.BroadcastReceiverTopic
import com.kylecorry.luna.coroutines.CoroutineQueueRunner
import com.kylecorry.luna.coroutines.onIO
import com.kylecorry.trail_sense.shared.automations.Automation
import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools
import kotlinx.coroutines.runBlocking
Expand All @@ -13,32 +14,28 @@ object Automations {

fun setup(context: Context) {
val tools = Tools.getTools(context, false)
val actions = tools.flatMap { it.broadcasts.map { it.action } }
val broadcasts = tools.flatMap { it.broadcasts.map { it.id } }

actions.forEach { action ->
val topic = BroadcastReceiverTopic(context, IntentFilter(action))
broadcasts.forEach { broadcastId ->
val topic = BroadcastReceiverTopic(context, IntentFilter(broadcastId))
val queue = CoroutineQueueRunner(10)
// TODO: Should there be a way to unregister? Maybe only register when something is listening? When to cancel queue?
topic.subscribe { intent ->
runBlocking {
queue.enqueue {
val automationsToRun = getAutomations(context, action)
val receiversToRun =
automationsToRun.flatMap { it.receivers.map { it.receiverId } }
val automationsToRun = getAutomations(context, broadcastId)
val actionsToRun = automationsToRun.flatMap { it.actions }

val availableReceivers = tools
val availableActions = tools
.flatMap { it.actions }
.filter { receiversToRun.contains(it.id) && it.isEnabled(context) }
.filter { actionsToRun.contains(it.id) && it.isEnabled(context) }

automationsToRun.forEach {
for (receiver in it.receivers) {
if (!receiver.enabled) {
continue
}
val toolReceiver = availableReceivers
.firstOrNull { r -> r.id == receiver.receiverId } ?: continue

toolReceiver.action.onReceive(
for (action in it.actions) {
val toolAction =
availableActions.firstOrNull { r -> r.id == action } ?: continue

toolAction.action.onReceive(
context,
intent.extras ?: bundleOf()
)
Expand All @@ -51,12 +48,13 @@ object Automations {
}
}

private suspend fun getAutomations(context: Context, action: String): List<Automation> {
// TODO: Load these from the DB
val automations = listOf(
PowerSavingModeAutomation.onEnabled(context),
PowerSavingModeAutomation.onDisabled(context)
)
return automations.filter { it.broadcast == action }
}
private suspend fun getAutomations(context: Context, broadcast: String): List<Automation> =
onIO {
// TODO: Load these from the DB
val automations = listOf(
PowerSavingModeAutomation.onEnabled(context),
PowerSavingModeAutomation.onDisabled()
)
automations.filter { it.broadcast == broadcast }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,29 @@ package com.kylecorry.trail_sense.main.automations
import android.content.Context
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.automations.Automation
import com.kylecorry.trail_sense.shared.automations.AutomationReceiver
import com.kylecorry.trail_sense.tools.battery.BatteryToolRegistration
import com.kylecorry.trail_sense.tools.pedometer.PedometerToolRegistration
import com.kylecorry.trail_sense.tools.weather.WeatherToolRegistration

object PowerSavingModeAutomation {

fun onEnabled(context: Context): Automation {
val prefs = UserPreferences(context)
return Automation(
BatteryToolRegistration.BROADCAST_POWER_SAVING_MODE_ENABLED,
listOf(
AutomationReceiver(
WeatherToolRegistration.ACTION_PAUSE_WEATHER_MONITOR,
enabled = UserPreferences(context).lowPowerModeDisablesWeather
),
AutomationReceiver(PedometerToolRegistration.ACTION_PAUSE_PEDOMETER)
listOfNotNull(
if (prefs.lowPowerModeDisablesWeather) WeatherToolRegistration.ACTION_PAUSE_WEATHER_MONITOR else null,
PedometerToolRegistration.ACTION_PAUSE_PEDOMETER
)
)
}

fun onDisabled(context: Context): Automation {
fun onDisabled(): Automation {
return Automation(
BatteryToolRegistration.BROADCAST_POWER_SAVING_MODE_DISABLED,
listOf(
AutomationReceiver(WeatherToolRegistration.ACTION_RESUME_WEATHER_MONITOR),
AutomationReceiver(PedometerToolRegistration.ACTION_RESUME_PEDOMETER)
WeatherToolRegistration.ACTION_RESUME_WEATHER_MONITOR,
PedometerToolRegistration.ACTION_RESUME_PEDOMETER
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,5 @@ package com.kylecorry.trail_sense.shared.automations

data class Automation(
val broadcast: String,
val receivers: List<AutomationReceiver>
)

data class AutomationReceiver(
val receiverId: String,
val enabled: Boolean = true,
val actions: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ package com.kylecorry.trail_sense.tools.tools.infrastructure

// TODO: Indicate output parameters, so it can be matched with a receiver
data class ToolBroadcast(
val action: String,
val id: String,
val name: String
)

0 comments on commit 2d93579

Please sign in to comment.