From 03f7267fe86a3791c5d5e0a8ac2768d0836c1223 Mon Sep 17 00:00:00 2001 From: Kyle Corry Date: Thu, 4 Jul 2024 08:01:42 -0400 Subject: [PATCH] Keep sunset alert switch in sync --- .../astronomy/AstronomyToolRegistration.kt | 17 ++++++- .../receivers/SunsetAlarmReceiver.kt | 3 ++ .../quickactions/QuickActionSunsetAlert.kt | 3 ++ .../astronomy/ui/AstronomySettingsFragment.kt | 48 +++++++++++++++++-- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/AstronomyToolRegistration.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/AstronomyToolRegistration.kt index e81bd5258..21c447169 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/AstronomyToolRegistration.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/AstronomyToolRegistration.kt @@ -11,13 +11,14 @@ import com.kylecorry.trail_sense.tools.astronomy.infrastructure.receivers.Sunset import com.kylecorry.trail_sense.tools.astronomy.quickactions.QuickActionNightMode import com.kylecorry.trail_sense.tools.astronomy.quickactions.QuickActionSunsetAlert import com.kylecorry.trail_sense.tools.tools.infrastructure.Tool +import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolBroadcast import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolCategory -import com.kylecorry.trail_sense.tools.tools.infrastructure.diagnostics.ToolDiagnosticFactory import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolNotificationChannel import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolQuickAction import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolRegistration import com.kylecorry.trail_sense.tools.tools.infrastructure.ToolService import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools +import com.kylecorry.trail_sense.tools.tools.infrastructure.diagnostics.ToolDiagnosticFactory import java.time.Duration object AstronomyToolRegistration : ToolRegistration { @@ -67,6 +68,7 @@ object AstronomyToolRegistration : ToolRegistration { }, disable = { UserPreferences(it).astronomy.sendSunsetAlerts = false + Tools.broadcast(BROADCAST_SUNSET_ALERTS_DISABLED) }, stop = { SunsetAlarmReceiver.scheduler(it).cancel() @@ -109,7 +111,20 @@ object AstronomyToolRegistration : ToolRegistration { AstronomyAlertCommand.NOTIFICATION_CHANNEL, context.getString(R.string.astronomy_alerts) ) + ), + broadcasts = listOf( + ToolBroadcast( + BROADCAST_SUNSET_ALERTS_ENABLED, + "Sunset alerts enabled" + ), + ToolBroadcast( + BROADCAST_SUNSET_ALERTS_DISABLED, + "Sunset alerts disabled" + ) ) ) } + + const val BROADCAST_SUNSET_ALERTS_ENABLED = "astronomy-broadcast-sunset-alerts-enabled" + const val BROADCAST_SUNSET_ALERTS_DISABLED = "astronomy-broadcast-sunset-alerts-disabled" } \ No newline at end of file diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/infrastructure/receivers/SunsetAlarmReceiver.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/infrastructure/receivers/SunsetAlarmReceiver.kt index 2ba74cfc1..9ef20b767 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/infrastructure/receivers/SunsetAlarmReceiver.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/infrastructure/receivers/SunsetAlarmReceiver.kt @@ -10,7 +10,9 @@ import com.kylecorry.andromeda.fragments.IPermissionRequester import com.kylecorry.trail_sense.shared.UserPreferences import com.kylecorry.trail_sense.shared.permissions.RequestBackgroundLocationCommand import com.kylecorry.trail_sense.shared.permissions.requestScheduleExactAlarms +import com.kylecorry.trail_sense.tools.astronomy.AstronomyToolRegistration import com.kylecorry.trail_sense.tools.astronomy.infrastructure.commands.SunsetAlarmCommand +import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch @@ -61,6 +63,7 @@ class SunsetAlarmReceiver : BroadcastReceiver() { shouldRequestPermissions: Boolean ) where T : Fragment, T : IPermissionRequester { UserPreferences(fragment.requireContext()).astronomy.sendSunsetAlerts = true + Tools.broadcast(AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_ENABLED) if (shouldRequestPermissions) { fragment.requestScheduleExactAlarms { start(fragment.requireContext()) diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/quickactions/QuickActionSunsetAlert.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/quickactions/QuickActionSunsetAlert.kt index 67583119b..bd4d33f28 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/quickactions/QuickActionSunsetAlert.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/quickactions/QuickActionSunsetAlert.kt @@ -8,7 +8,9 @@ import com.kylecorry.trail_sense.R import com.kylecorry.trail_sense.shared.FormatService import com.kylecorry.trail_sense.shared.QuickActionButton import com.kylecorry.trail_sense.shared.UserPreferences +import com.kylecorry.trail_sense.tools.astronomy.AstronomyToolRegistration import com.kylecorry.trail_sense.tools.astronomy.infrastructure.receivers.SunsetAlarmReceiver +import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools import java.time.Duration class QuickActionSunsetAlert(btn: ImageButton, fragment: Fragment) : @@ -30,6 +32,7 @@ class QuickActionSunsetAlert(btn: ImageButton, fragment: Fragment) : super.onClick() if (isOn()) { prefs.astronomy.sendSunsetAlerts = false + Tools.broadcast(AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_DISABLED) updateState() } else if (fragment is IPermissionRequester) { SunsetAlarmReceiver.enable(fragment, true) diff --git a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/ui/AstronomySettingsFragment.kt b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/ui/AstronomySettingsFragment.kt index 5534e1958..d347ee24f 100644 --- a/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/ui/AstronomySettingsFragment.kt +++ b/app/src/main/java/com/kylecorry/trail_sense/tools/astronomy/ui/AstronomySettingsFragment.kt @@ -3,11 +3,13 @@ package com.kylecorry.trail_sense.tools.astronomy.ui import android.os.Bundle import android.view.View import androidx.preference.ListPreference +import androidx.preference.SwitchPreferenceCompat import com.kylecorry.andromeda.core.topics.generic.asLiveData import com.kylecorry.andromeda.fragments.AndromedaPreferenceFragment import com.kylecorry.trail_sense.R import com.kylecorry.trail_sense.shared.UserPreferences import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem +import com.kylecorry.trail_sense.tools.astronomy.AstronomyToolRegistration import com.kylecorry.trail_sense.tools.astronomy.infrastructure.receivers.SunsetAlarmReceiver import com.kylecorry.trail_sense.tools.tools.infrastructure.Tools @@ -16,7 +18,31 @@ class AstronomySettingsFragment : AndromedaPreferenceFragment() { private lateinit var prefs: UserPreferences private var prefleftButton: ListPreference? = null private var prefrightButton: ListPreference? = null + private var prefSunsetAlertsSwitch: SwitchPreferenceCompat? = null + override fun onResume() { + super.onResume() + Tools.subscribe( + AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_ENABLED, + ::onSunsetAlertsEnabled + ) + Tools.subscribe( + AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_DISABLED, + ::onSunsetAlertsDisabled + ) + } + + override fun onPause() { + super.onPause() + Tools.unsubscribe( + AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_ENABLED, + ::onSunsetAlertsEnabled + ) + Tools.unsubscribe( + AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_DISABLED, + ::onSunsetAlertsDisabled + ) + } override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { setPreferencesFromResource(R.xml.astronomy_preferences, rootKey) @@ -26,6 +52,8 @@ class AstronomySettingsFragment : AndromedaPreferenceFragment() { prefleftButton = list(R.string.pref_astronomy_quick_action_left) prefrightButton = list(R.string.pref_astronomy_quick_action_right) + prefSunsetAlertsSwitch = switch(R.string.pref_sunset_alerts) + val actions = Tools.getQuickActions(requireContext()) val actionNames = actions.map { it.name } val actionValues = actions.map { it.id.toString() } @@ -38,19 +66,24 @@ class AstronomySettingsFragment : AndromedaPreferenceFragment() { switch(R.string.pref_start_camera_in_3d_view)?.isVisible = Tools.isToolAvailable(requireContext(), Tools.AUGMENTED_REALITY) + + onClick(prefSunsetAlertsSwitch) { + if (prefs.astronomy.sendSunsetAlerts) { + SunsetAlarmReceiver.enable(this, true) + } else { + Tools.broadcast(AstronomyToolRegistration.BROADCAST_SUNSET_ALERTS_DISABLED) + } + } } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val alertTimePrefKey = getString(R.string.pref_sunset_alert_time) - val alertPrefKey = getString(R.string.pref_sunset_alerts) PreferencesSubsystem.getInstance(requireContext()).preferences.onChange.asLiveData() .observe(viewLifecycleOwner) { if (it == alertTimePrefKey) { restartSunsetAlerts(false) - } else if (it == alertPrefKey) { - restartSunsetAlerts(true) } } } @@ -63,4 +96,13 @@ class AstronomySettingsFragment : AndromedaPreferenceFragment() { SunsetAlarmReceiver.enable(this, shouldRequestPermissions) } + private fun onSunsetAlertsEnabled(data: Bundle): Boolean { + prefSunsetAlertsSwitch?.isChecked = true + return true + } + + private fun onSunsetAlertsDisabled(data: Bundle): Boolean { + prefSunsetAlertsSwitch?.isChecked = false + return true + } } \ No newline at end of file