Skip to content

Commit

Permalink
Keep sunset alert switch in sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kylecorry31 committed Jul 4, 2024
1 parent 8509710 commit 03f7267
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -67,6 +68,7 @@ object AstronomyToolRegistration : ToolRegistration {
},
disable = {
UserPreferences(it).astronomy.sendSunsetAlerts = false
Tools.broadcast(BROADCAST_SUNSET_ALERTS_DISABLED)
},
stop = {
SunsetAlarmReceiver.scheduler(it).cancel()
Expand Down Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) :
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand All @@ -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() }
Expand All @@ -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)
}
}
}
Expand All @@ -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
}
}

0 comments on commit 03f7267

Please sign in to comment.