11package app.revanced.extension.youtube.swipecontrols
22
3- import android.annotation.SuppressLint
43import android.graphics.Color
54import app.revanced.extension.shared.Logger
65import app.revanced.extension.shared.StringRef.str
76import app.revanced.extension.shared.Utils
7+ import app.revanced.extension.shared.settings.StringSetting
88import app.revanced.extension.youtube.settings.Settings
99import app.revanced.extension.youtube.shared.PlayerType
1010
@@ -51,105 +51,112 @@ class SwipeControlsConfigurationProvider {
5151 /* *
5252 * Indicates whether press-to-swipe mode is enabled, requiring a press before swiping to activate controls.
5353 */
54- val shouldEnablePressToSwipe: Boolean
55- get() = Settings .SWIPE_PRESS_TO_ENGAGE .get()
54+ val shouldEnablePressToSwipe = Settings .SWIPE_PRESS_TO_ENGAGE .get()
5655
5756 /* *
5857 * The threshold for detecting swipe gestures, in pixels.
5958 * Loaded once to ensure consistent behavior during rapid scroll events.
6059 */
61- val swipeMagnitudeThreshold: Int
62- get() = Settings .SWIPE_MAGNITUDE_THRESHOLD .get()
60+ val swipeMagnitudeThreshold = Settings .SWIPE_MAGNITUDE_THRESHOLD .get()
6361
6462 /* *
6563 * The sensitivity of volume swipe gestures, determining how much volume changes per swipe.
6664 * Resets to default if set to 0, as it would disable swiping.
6765 */
68- val volumeSwipeSensitivity: Int
69- get() {
70- val sensitivity = Settings .SWIPE_VOLUME_SENSITIVITY .get()
66+ val volumeSwipeSensitivity: Int by lazy {
67+ val sensitivity = Settings .SWIPE_VOLUME_SENSITIVITY .get()
7168
72- if (sensitivity < 1 ) {
73- return Settings .SWIPE_VOLUME_SENSITIVITY .resetToDefault()
74- }
75-
76- return sensitivity
69+ if (sensitivity < 1 ) {
70+ return @lazy Settings .SWIPE_VOLUME_SENSITIVITY .resetToDefault()
7771 }
72+
73+ sensitivity
74+ }
7875 // endregion
7976
8077 // region overlay adjustments
8178 /* *
8279 * Indicates whether haptic feedback should be enabled for swipe control interactions.
8380 */
84- val shouldEnableHapticFeedback: Boolean
85- get() = Settings .SWIPE_HAPTIC_FEEDBACK .get()
81+ val shouldEnableHapticFeedback = Settings .SWIPE_HAPTIC_FEEDBACK .get()
8682
8783 /* *
8884 * The duration in milliseconds that the overlay should remain visible after a change.
8985 */
90- val overlayShowTimeoutMillis: Long
91- get() = Settings .SWIPE_OVERLAY_TIMEOUT .get()
86+ val overlayShowTimeoutMillis = Settings .SWIPE_OVERLAY_TIMEOUT .get()
9287
9388 /* *
9489 * The background opacity of the overlay, converted from a percentage (0-100) to an alpha value (0-255).
9590 * Resets to default and shows a toast if the value is out of range.
9691 */
97- val overlayBackgroundOpacity: Int
98- get() {
99- var opacity = Settings .SWIPE_OVERLAY_OPACITY .get()
100-
101- if (opacity < 0 || opacity > 100 ) {
102- Utils .showToastLong(str(" revanced_swipe_overlay_background_opacity_invalid_toast" ))
103- opacity = Settings .SWIPE_OVERLAY_OPACITY .resetToDefault()
104- }
92+ val overlayBackgroundOpacity: Int by lazy {
93+ var opacity = Settings .SWIPE_OVERLAY_OPACITY .get()
10594
106- opacity = opacity * 255 / 100
107- return Color .argb(opacity, 0 , 0 , 0 )
95+ if (opacity < 0 || opacity > 100 ) {
96+ Utils .showToastLong(str(" revanced_swipe_overlay_background_opacity_invalid_toast" ))
97+ opacity = Settings .SWIPE_OVERLAY_OPACITY .resetToDefault()
10898 }
10999
100+ opacity = opacity * 255 / 100
101+ Color .argb(opacity, 0 , 0 , 0 )
102+ }
103+
110104 /* *
111- * The color of the progress bar in the overlay.
105+ * The color of the progress bar in the overlay for brightness .
112106 * Resets to default and shows a toast if the color string is invalid or empty.
113107 */
114- val overlayProgressColor: Int
115- get() {
116- try {
117- @SuppressLint(" UseKtx" )
118- val color = Color .parseColor(Settings .SWIPE_OVERLAY_PROGRESS_COLOR .get())
119- return (0xBF000000 .toInt() or (color and 0xFFFFFF ))
120- } catch (ex: IllegalArgumentException ) {
121- Logger .printDebug({ " Could not parse color" }, ex)
122- Utils .showToastLong(str(" revanced_swipe_overlay_progress_color_invalid_toast" ))
123- Settings .SWIPE_OVERLAY_PROGRESS_COLOR .resetToDefault()
124- return overlayProgressColor // Recursively return.
125- }
108+ val overlayBrightnessProgressColor: Int by lazy {
109+ // Use lazy to avoid repeat parsing. Changing color requires app restart.
110+ getSettingColor(Settings .SWIPE_OVERLAY_BRIGHTNESS_COLOR )
111+ }
112+
113+ /* *
114+ * The color of the progress bar in the overlay for volume.
115+ * Resets to default and shows a toast if the color string is invalid or empty.
116+ */
117+ val overlayVolumeProgressColor: Int by lazy {
118+ getSettingColor(Settings .SWIPE_OVERLAY_VOLUME_COLOR )
119+ }
120+
121+ private fun getSettingColor (setting : StringSetting ): Int {
122+ try {
123+ // noinspection UseKtx
124+ val color = Color .parseColor(setting.get())
125+ return (0xBF000000 .toInt() or (color and 0x00FFFFFF ))
126+ } catch (ex: IllegalArgumentException ) {
127+ // This code should never be reached.
128+ // Color picker rejects and will not save bad colors to a setting.
129+ // If a user imports bad data, the color picker preference resets the
130+ // bad color before this method can be called.
131+ Logger .printDebug({ " Could not parse color: $setting " }, ex)
132+ Utils .showToastLong(str(" revanced_settings_color_invalid" ))
133+ setting.resetToDefault()
134+ return getSettingColor(setting) // Recursively return.
126135 }
136+ }
127137
128138 /* *
129139 * The background color used for the filled portion of the progress bar in the overlay.
130140 */
131- val overlayFillBackgroundPaint: Int
132- get() = 0x80D3D3D3 .toInt()
141+ val overlayFillBackgroundPaint = 0x80D3D3D3 .toInt()
133142
134143 /* *
135144 * The color used for text and icons in the overlay.
136145 */
137- val overlayTextColor: Int
138- get() = Color .WHITE
146+ val overlayTextColor = Color .WHITE
139147
140148 /* *
141149 * The text size in the overlay, in density-independent pixels (dp).
142150 * Must be between 1 and 30 dp; resets to default and shows a toast if invalid.
143151 */
144- val overlayTextSize: Int
145- get() {
146- val size = Settings .SWIPE_OVERLAY_TEXT_SIZE .get()
147- if (size < 1 || size > 30 ) {
148- Utils .showToastLong(str(" revanced_swipe_text_overlay_size_invalid_toast" ))
149- return Settings .SWIPE_OVERLAY_TEXT_SIZE .resetToDefault()
150- }
151- return size
152+ val overlayTextSize: Int by lazy {
153+ val size = Settings .SWIPE_OVERLAY_TEXT_SIZE .get()
154+ if (size < 1 || size > 30 ) {
155+ Utils .showToastLong(str(" revanced_swipe_text_overlay_size_invalid_toast" ))
156+ return @lazy Settings .SWIPE_OVERLAY_TEXT_SIZE .resetToDefault()
152157 }
158+ size
159+ }
153160
154161 /* *
155162 * Defines the style of the swipe controls overlay, determining its layout and appearance.
@@ -199,28 +206,25 @@ class SwipeControlsConfigurationProvider {
199206 /* *
200207 * A minimal vertical progress bar.
201208 */
202- VERTICAL_MINIMAL (isMinimal = true , isVertical = true )
209+ VERTICAL_MINIMAL (isMinimal = true , isVertical = true )
203210 }
204211
205212 /* *
206213 * The current style of the overlay, determining its layout and appearance.
207214 */
208- val overlayStyle: SwipeOverlayStyle
209- get() = Settings .SWIPE_OVERLAY_STYLE .get()
215+ val overlayStyle = Settings .SWIPE_OVERLAY_STYLE .get()
210216 // endregion
211217
212218 // region behaviour
213219 /* *
214220 * Indicates whether the brightness level should be saved and restored when entering or exiting fullscreen mode.
215221 */
216- val shouldSaveAndRestoreBrightness: Boolean
217- get() = Settings .SWIPE_SAVE_AND_RESTORE_BRIGHTNESS .get()
222+ val shouldSaveAndRestoreBrightness = Settings .SWIPE_SAVE_AND_RESTORE_BRIGHTNESS .get()
218223
219224 /* *
220225 * Indicates whether auto-brightness should be enabled when the brightness gesture reaches its lowest value.
221226 */
222- val shouldLowestValueEnableAutoBrightness: Boolean
223- get() = Settings .SWIPE_LOWEST_VALUE_ENABLE_AUTO_BRIGHTNESS .get()
227+ val shouldLowestValueEnableAutoBrightness = Settings .SWIPE_LOWEST_VALUE_ENABLE_AUTO_BRIGHTNESS .get()
224228
225229 /* *
226230 * The saved brightness value for the swipe gesture, used to restore brightness in fullscreen mode.
@@ -229,4 +233,4 @@ class SwipeControlsConfigurationProvider {
229233 get() = Settings .SWIPE_BRIGHTNESS_VALUE .get()
230234 set(value) = Settings .SWIPE_BRIGHTNESS_VALUE .save(value)
231235 // endregion
232- }
236+ }
0 commit comments