Skip to content

Commit 0afb0da

Browse files
committed
refactor: extract effect to set max brightness
1 parent 36420e9 commit 0afb0da

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

app/src/main/java/to/bitkit/ui/screens/wallets/receive/ReceiveQrScreen.kt

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package to.bitkit.ui.screens.wallets.receive
22

3-
import android.app.Activity
43
import android.graphics.Bitmap
5-
import android.view.WindowManager
64
import androidx.compose.animation.AnimatedVisibility
75
import androidx.compose.foundation.layout.Arrangement
86
import androidx.compose.foundation.layout.Column
@@ -24,7 +22,6 @@ import androidx.compose.material3.Icon
2422
import androidx.compose.material3.Switch
2523
import androidx.compose.material3.rememberTooltipState
2624
import androidx.compose.runtime.Composable
27-
import androidx.compose.runtime.DisposableEffect
2825
import androidx.compose.runtime.MutableState
2926
import androidx.compose.runtime.getValue
3027
import androidx.compose.runtime.mutableIntStateOf
@@ -36,6 +33,7 @@ import androidx.compose.ui.Alignment
3633
import androidx.compose.ui.Modifier
3734
import androidx.compose.ui.draw.rotate
3835
import androidx.compose.ui.graphics.painter.Painter
36+
import androidx.compose.ui.keepScreenOn
3937
import androidx.compose.ui.platform.LocalContext
4038
import androidx.compose.ui.platform.testTag
4139
import androidx.compose.ui.res.painterResource
@@ -59,6 +57,7 @@ import to.bitkit.ui.components.PrimaryButton
5957
import to.bitkit.ui.components.QrCodeImage
6058
import to.bitkit.ui.components.Tooltip
6159
import to.bitkit.ui.scaffold.SheetTopBar
60+
import to.bitkit.ui.shared.effects.SetMaxBrightness
6261
import to.bitkit.ui.shared.modifiers.sheetHeight
6362
import to.bitkit.ui.shared.util.gradientBackground
6463
import to.bitkit.ui.shared.util.shareQrCode
@@ -80,32 +79,7 @@ fun ReceiveQrScreen(
8079
onClickReceiveOnSpending: () -> Unit,
8180
modifier: Modifier = Modifier,
8281
) {
83-
val context = LocalContext.current
84-
val window = remember(context) { (context as? Activity)?.window }
85-
86-
// Keep screen on and set brightness to max while this composable is active
87-
DisposableEffect(Unit) {
88-
val originalBrightness = window?.attributes?.screenBrightness
89-
val originalFlags = window?.attributes?.flags
90-
91-
window?.let { win ->
92-
win.attributes?.let { attrs ->
93-
attrs.screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
94-
attrs.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
95-
win.attributes = attrs
96-
}
97-
}
98-
99-
onDispose {
100-
window?.let { win ->
101-
win.attributes?.let { attrs ->
102-
if (originalBrightness != null) attrs.screenBrightness = originalBrightness
103-
if (originalFlags != null) attrs.flags = originalFlags
104-
win.attributes = attrs
105-
}
106-
}
107-
}
108-
}
82+
SetMaxBrightness()
10983

11084
val qrLogoImageRes by remember(walletState, cjitInvoice.value) {
11185
val resId = when {
@@ -124,6 +98,7 @@ fun ReceiveQrScreen(
12498
.fillMaxSize()
12599
.gradientBackground()
126100
.navigationBarsPadding()
101+
.keepScreenOn()
127102
) {
128103
SheetTopBar(stringResource(R.string.wallet__receive_bitcoin))
129104
Column(
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package to.bitkit.ui.shared.effects
2+
3+
import android.view.WindowManager
4+
import androidx.activity.compose.LocalActivity
5+
import androidx.compose.runtime.Composable
6+
import androidx.compose.runtime.DisposableEffect
7+
8+
@Composable
9+
fun SetMaxBrightness() {
10+
val window = (LocalActivity.current)?.window ?: return
11+
12+
DisposableEffect(Unit) {
13+
val originalBrightness = window.attributes?.screenBrightness
14+
15+
window.attributes = window.attributes?.apply {
16+
screenBrightness = WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_FULL
17+
}
18+
19+
onDispose {
20+
window.attributes = window.attributes?.apply {
21+
screenBrightness = originalBrightness ?: WindowManager.LayoutParams.BRIGHTNESS_OVERRIDE_NONE
22+
}
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)