Skip to content

Commit

Permalink
keep screen on in timer screen (#323)
Browse files Browse the repository at this point in the history
* keep screen alive while timer or stopwatch

* removed unnecessary code

* refactor: remove ScreenControlHelper object

---------

Co-authored-by: Suhas Dissanayake <[email protected]>
  • Loading branch information
dominik-masson and SuhasDissa authored Mar 28, 2024
1 parent 341c4a2 commit 0985645
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
17 changes: 2 additions & 15 deletions app/src/main/java/com/bnyro/clock/ui/screens/StopwatchScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,21 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.bnyro.clock.R
import com.bnyro.clock.extensions.addZero
import com.bnyro.clock.obj.WatchState
import com.bnyro.clock.ui.model.StopwatchModel
import com.bnyro.clock.ui.nav.TopBarScaffold
import com.bnyro.clock.util.KeepScreenOn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

Expand Down Expand Up @@ -318,16 +317,4 @@ private fun TimeDisplay(
)
}
}
}

// https://stackoverflow.com/a/71293123/9652621
@Composable
fun KeepScreenOn() {
val currentView = LocalView.current
DisposableEffect(Unit) {
currentView.keepScreenOn = true
onDispose {
currentView.keepScreenOn = false
}
}
}
}
7 changes: 6 additions & 1 deletion app/src/main/java/com/bnyro/clock/ui/screens/TimerScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.bnyro.clock.ui.components.TimePickerDial
import com.bnyro.clock.ui.components.TimerItem
import com.bnyro.clock.ui.model.TimerModel
import com.bnyro.clock.ui.nav.TopBarScaffold
import com.bnyro.clock.util.KeepScreenOn
import com.bnyro.clock.util.Preferences

@OptIn(ExperimentalFoundationApi::class)
Expand Down Expand Up @@ -203,4 +204,8 @@ fun TimerScreen(onClickSettings: () -> Unit, timerModel: TimerModel) {
}
}
}
}

if (timerModel.scheduledObjects.isNotEmpty()) {
KeepScreenOn()
}
}
18 changes: 18 additions & 0 deletions app/src/main/java/com/bnyro/clock/util/KeepScreenOn.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bnyro.clock.util

import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.ui.platform.LocalView

// https://stackoverflow.com/a/71293123/9652621
@Composable
fun KeepScreenOn() {
val currentView = LocalView.current
DisposableEffect(Unit) {
currentView.keepScreenOn = true

onDispose {
currentView.keepScreenOn = false
}
}
}

0 comments on commit 0985645

Please sign in to comment.