Skip to content

Commit

Permalink
Add night mode quick action
Browse files Browse the repository at this point in the history
Closes #1744
  • Loading branch information
kylecorry31 committed Nov 12, 2023
1 parent e887bda commit 209109a
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 39 deletions.
12 changes: 11 additions & 1 deletion app/src/main/java/com/kylecorry/trail_sense/main/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MainActivity : AndromedaActivity() {
UserPreferences.Theme.System -> ColorTheme.System
UserPreferences.Theme.SunriseSunset -> sunriseSunsetTheme()
}
setTheme(R.style.AppTheme)
// setTheme(R.style.AppTheme)
setColorTheme(mode, userPrefs.useDynamicColors)
super.onCreate(savedInstanceState)

Expand Down Expand Up @@ -108,9 +108,19 @@ class MainActivity : AndromedaActivity() {
}
}

fun reloadTheme(){
cache.putBoolean("pref_theme_just_changed", true)
recreate()
}

override fun onResume() {
super.onResume()
FlashlightSubsystem.getInstance(this).startSystemMonitor()

if (cache.getBoolean("pref_theme_just_changed") == true) {
cache.putBoolean("pref_theme_just_changed", false)
recreate()
}
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class AstronomyQuickActionBinder(
QuickActionType.WhiteNoise -> QuickActionWhiteNoise(button, fragment)
QuickActionType.LowPowerMode -> LowPowerQuickAction(button, fragment)
QuickActionType.SunsetAlert -> QuickActionSunsetAlert(button, fragment)
QuickActionType.NightMode -> QuickActionNightMode(button, fragment)
else -> QuickActionNone(button, fragment)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class NavigationQuickActionBinder(
QuickActionType.Maps -> QuickActionOfflineMaps(button, fragment)
QuickActionType.Whistle -> QuickActionWhistle(button, fragment)
QuickActionType.LowPowerMode -> LowPowerQuickAction(button, fragment)
QuickActionType.NightMode -> QuickActionNightMode(button, fragment)
else -> QuickActionNone(button, fragment)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.kylecorry.trail_sense.quickactions

import android.widget.ImageButton
import androidx.fragment.app.Fragment
import com.kylecorry.andromeda.alerts.toast
import com.kylecorry.trail_sense.R
import com.kylecorry.trail_sense.shared.CustomUiUtils
import com.kylecorry.trail_sense.shared.QuickActionButton
import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.requireMainActivity

class QuickActionNightMode(btn: ImageButton, fragment: Fragment) :
QuickActionButton(btn, fragment) {

override fun onCreate() {
super.onCreate()
val prefs = UserPreferences(fragment.requireContext())
button.setImageResource(R.drawable.ic_astronomy)
CustomUiUtils.setButtonState(button, prefs.theme == UserPreferences.Theme.Night)
var isSwitching = false
button.setOnClickListener {
if (isSwitching){
return@setOnClickListener
}
isSwitching = true
fragment.toast(fragment.getString(R.string.loading))

prefs.theme = if (prefs.theme == UserPreferences.Theme.Night) {
prefs.lastTheme
} else {
prefs.lastTheme = prefs.theme
UserPreferences.Theme.Night
}

fragment.requireMainActivity().reloadTheme()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ToolsQuickActionBinder(
QuickActionType.WhiteNoise -> QuickActionWhiteNoise(createButton(), fragment)
QuickActionType.LowPowerMode -> LowPowerQuickAction(createButton(), fragment)
QuickActionType.SunsetAlert -> QuickActionSunsetAlert(createButton(), fragment)
QuickActionType.NightMode -> QuickActionNightMode(createButton(), fragment)
else -> null // No other actions are supported
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class WeatherQuickActionBinder(
QuickActionType.LowPowerMode -> LowPowerQuickAction(button, fragment)
QuickActionType.Thunder -> QuickActionThunder(button, fragment)
QuickActionType.Climate -> QuickActionClimate(button, fragment)
QuickActionType.NightMode -> QuickActionNightMode(button, fragment)
else -> QuickActionNone(button, fragment)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.kylecorry.trail_sense.shared.UserPreferences
import com.kylecorry.trail_sense.shared.io.ActivityUriPicker
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem
import com.kylecorry.trail_sense.shared.requireMainActivity
import com.kylecorry.trail_sense.shared.sensors.SensorService
import com.kylecorry.trail_sense.tools.flashlight.infrastructure.FlashlightSubsystem
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -66,7 +65,7 @@ class SettingsFragment : AndromedaPreferenceFragment() {
preference(R.string.pref_flashlight_settings)?.isVisible =
FlashlightSubsystem.getInstance(requireContext()).isAvailable()

refreshOnChange(list(R.string.pref_theme))
reloadThemeOnChange(list(R.string.pref_theme))

onClick(preference(R.string.pref_github)) {
val i = Intents.url(it.summary.toString())
Expand Down Expand Up @@ -105,13 +104,7 @@ class SettingsFragment : AndromedaPreferenceFragment() {


onClick(findPreference(getString(R.string.pref_tool_quick_action_header_key))){
val potentialActions = listOf(
QuickActionType.Flashlight,
QuickActionType.Whistle,
QuickActionType.LowPowerMode,
QuickActionType.SunsetAlert,
QuickActionType.WhiteNoise,
)
val potentialActions = QuickActionUtils.tools(requireContext())

val selected = prefs.toolQuickActions

Expand All @@ -132,13 +125,6 @@ class SettingsFragment : AndromedaPreferenceFragment() {
}
}

override fun onResume() {
super.onResume()
if (cache.getBoolean("pref_theme_just_changed") == true) {
refresh(false)
}
}

private fun backup() {
lifecycleScope.launch {
backupCommand.execute()
Expand All @@ -151,14 +137,9 @@ class SettingsFragment : AndromedaPreferenceFragment() {
}
}

private fun refresh(recordChange: Boolean) {
cache.putBoolean("pref_theme_just_changed", recordChange)
activity?.recreate()
}

private fun refreshOnChange(pref: Preference?) {
private fun reloadThemeOnChange(pref: Preference?) {
pref?.setOnPreferenceChangeListener { _, _ ->
refresh(true)
requireMainActivity().reloadTheme()
true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ enum class QuickActionType(val id: Int) {
LowPowerMode(10),
Thunder(11),
Climate(12),
SunsetAlert(13)
SunsetAlert(13),
NightMode(14)
}

object QuickActionUtils {
Expand All @@ -37,9 +38,21 @@ object QuickActionUtils {
QuickActionType.Thunder -> context.getString(R.string.tool_lightning_title)
QuickActionType.Climate -> context.getString(R.string.tool_climate)
QuickActionType.SunsetAlert -> context.getString(R.string.sunset_alerts)
QuickActionType.NightMode -> context.getString(R.string.night)
}
}

fun tools(context: Context): List<QuickActionType> {
return listOfNotNull(
if (FlashlightSubsystem.getInstance(context).isAvailable()) QuickActionType.Flashlight else null,
QuickActionType.Whistle,
QuickActionType.LowPowerMode,
QuickActionType.SunsetAlert,
QuickActionType.WhiteNoise,
QuickActionType.NightMode
)
}

fun navigation(context: Context): List<QuickActionType> {
val list = mutableListOf(
QuickActionType.None,
Expand All @@ -48,7 +61,8 @@ object QuickActionUtils {
QuickActionType.Whistle,
QuickActionType.Ruler,
QuickActionType.LowPowerMode,
QuickActionType.Maps)
QuickActionType.Maps,
QuickActionType.NightMode)

return list.filterNotNull()
}
Expand All @@ -62,7 +76,8 @@ object QuickActionUtils {
QuickActionType.Temperature,
QuickActionType.LowPowerMode,
QuickActionType.Thunder,
QuickActionType.Climate
QuickActionType.Climate,
QuickActionType.NightMode
)
}

Expand All @@ -73,7 +88,8 @@ object QuickActionUtils {
QuickActionType.Whistle,
QuickActionType.WhiteNoise,
QuickActionType.LowPowerMode,
QuickActionType.SunsetAlert
QuickActionType.SunsetAlert,
QuickActionType.NightMode
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.kylecorry.trail_sense.shared.extensions.putIntArray
import com.kylecorry.trail_sense.shared.extensions.putLongArray
import com.kylecorry.trail_sense.shared.preferences.PreferencesSubsystem
import com.kylecorry.trail_sense.shared.sharing.MapSite
import com.kylecorry.trail_sense.tools.flashlight.infrastructure.FlashlightSubsystem
import com.kylecorry.trail_sense.tools.ui.sort.ToolSortType
import com.kylecorry.trail_sense.weather.infrastructure.WeatherPreferences
import java.time.Duration
Expand Down Expand Up @@ -144,20 +145,44 @@ class UserPreferences(private val context: Context) : IDeclinationPreferences {
false
)

val theme: Theme
var lastTheme: Theme by StringEnumPreference(
cache,
"pref_last_theme",
mapOf(
"light" to Theme.Light,
"dark" to Theme.Dark,
"black" to Theme.Black,
"sunrise_sunset" to Theme.SunriseSunset,
"night" to Theme.Night,
"system" to Theme.System
),
Theme.System
)

private var _theme: Theme by StringEnumPreference(
cache,
context.getString(R.string.pref_theme),
mapOf(
"light" to Theme.Light,
"dark" to Theme.Dark,
"black" to Theme.Black,
"sunrise_sunset" to Theme.SunriseSunset,
"night" to Theme.Night,
"system" to Theme.System
),
Theme.System
)

var theme: Theme
get() {
if (isLowPowerModeOn) {
return Theme.Black
}

return when (cache.getString(context.getString(R.string.pref_theme))) {
"light" -> Theme.Light
"dark" -> Theme.Dark
"black" -> Theme.Black
"sunrise_sunset" -> Theme.SunriseSunset
"night" -> Theme.Night
else -> Theme.System
}
return _theme
}
set(value) {
_theme = value
}

val useDynamicColors = false
Expand Down Expand Up @@ -310,8 +335,10 @@ class UserPreferences(private val context: Context) : IDeclinationPreferences {
var toolQuickActions: List<QuickActionType>
get() {
val ids = cache.getIntArray(context.getString(R.string.pref_tool_quick_actions))
?: return listOf(
QuickActionType.Flashlight,
?: return listOfNotNull(
if (FlashlightSubsystem.getInstance(context)
.isAvailable()
) QuickActionType.Flashlight else null,
QuickActionType.Whistle,
QuickActionType.LowPowerMode,
)
Expand Down

0 comments on commit 209109a

Please sign in to comment.