From 7c0b506b90ada39ea7fcf412698e681f3e3e24e9 Mon Sep 17 00:00:00 2001 From: MarcaDian Date: Sat, 20 Apr 2024 12:21:16 +0300 Subject: [PATCH] refactor fixed brightness reset after soft close comments --- .../SwipeControlsConfigurationProvider.kt | 2 +- .../SwipeControlsHostActivity.kt | 27 +++++++++++------ .../controller/ScreenBrightnessController.kt | 30 +++++++++++-------- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt index 5ec05da9..3e956125 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsConfigurationProvider.kt @@ -111,7 +111,7 @@ class SwipeControlsConfigurationProvider( get() = Settings.SWIPE_ENABLE_LOWEST_VALUE_AUTO_BRIGHTNESS.get() /** - * variable that stores the brightness gesture value + * variable that stores the brightness gesture value in the settings */ var savedScreenBrightnessValue: Float get() = Settings.SWIPE_BRIGHTNESS_VALUE.get() diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt index 9b902a97..55236314 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/SwipeControlsHostActivity.kt @@ -166,22 +166,31 @@ class SwipeControlsHostActivity : Activity() { contentRoot.addView(overlay) } + // Flag that indicates whether the brightness has been saved and restored default brightness + private var isBrightnessSaved = false + /** * called when the player type changes * * @param type the new player type */ private fun onPlayerTypeChanged(type: PlayerType) { - if (config.shouldSaveAndRestoreBrightness) { - when (type) { - PlayerType.WATCH_WHILE_FULLSCREEN -> screen?.restore() - else -> { - screen?.save() - screen?.restoreDefaultBrightness() - } + when { + // If saving and restoring brightness is enabled, and the player type is WATCH_WHILE_FULLSCREEN, + // and brightness has already been saved, then restore the screen brightness + config.shouldSaveAndRestoreBrightness && type == PlayerType.WATCH_WHILE_FULLSCREEN && isBrightnessSaved -> { + screen?.restore() + isBrightnessSaved = false } - } else { - screen?.restoreDefaultBrightness() // here was lost the function of restore the default brightness + // If saving and restoring brightness is enabled, and brightness has not been saved, + // then save the current screen state, restore default brightness, and mark brightness as saved + config.shouldSaveAndRestoreBrightness && !isBrightnessSaved -> { + screen?.save() + screen?.restoreDefaultBrightness() + isBrightnessSaved = true + } + // If saving and restoring brightness is disabled, simply keep the default brightness + else -> screen?.restoreDefaultBrightness() } } diff --git a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt index 773ffaca..e1b4fcc6 100644 --- a/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt +++ b/app/src/main/java/app/revanced/integrations/youtube/swipecontrols/controller/ScreenBrightnessController.kt @@ -12,11 +12,9 @@ import app.revanced.integrations.youtube.swipecontrols.misc.clamp */ class ScreenBrightnessController( private val host: Activity, - val config: SwipeControlsConfigurationProvider, + val config: SwipeControlsConfigurationProvider = SwipeControlsConfigurationProvider(host), ) { - constructor(context: Activity) : this(context, SwipeControlsConfigurationProvider(context)) - /** * the current screen brightness in percent, ranging from 0.0 to 100.0 */ @@ -26,25 +24,31 @@ class ScreenBrightnessController( rawScreenBrightness = (value.toFloat() / 100f).clamp(0f, 1f) } - /** - * restore the screen brightness to the default device brightness - */ - fun restoreDefaultBrightness() { - rawScreenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE - } - /** * is the screen brightness set to device- default? */ val isDefaultBrightness get() = (rawScreenBrightness == WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE) + /** + * restore the screen brightness to the default device brightness + */ + fun restoreDefaultBrightness() { + rawScreenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE + } + + // Flag that indicates whether the brightness has been restored + private var isBrightnessRestored = false + /** * save the current screen brightness into settings, to be brought back using [restore] */ fun save() { - if (config.savedScreenBrightnessValue == -1f) { + if (isBrightnessRestored) { + // Saves the current screen brightness value into settings config.savedScreenBrightnessValue = rawScreenBrightness + // Reset the flag + isBrightnessRestored = false } } @@ -52,8 +56,10 @@ class ScreenBrightnessController( * restore the screen brightness from settings saved using [save] */ fun restore() { + // Restores the screen brightness value from the saved settings rawScreenBrightness = config.savedScreenBrightnessValue - config.savedScreenBrightnessValue = -1f + // Mark that brightness has been restored + isBrightnessRestored = true } /**