feat(alarm): automatically stop alarm after 1 min

Closes: #88
This commit is contained in:
Nishant Mishra
2025-10-23 09:33:00 +05:30
parent aa4889a1fb
commit a93e748e04
2 changed files with 14 additions and 2 deletions

View File

@@ -45,10 +45,12 @@ class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
appContainer.activityTurnScreenOn = {
setShowWhenLocked(it)
setTurnScreenOn(it)
}
setContent {
val preferencesState by settingsViewModel.preferencesState.collectAsStateWithLifecycle()

View File

@@ -33,6 +33,7 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.asStateFlow
@@ -71,9 +72,11 @@ class TimerService : Service() {
private var pauseDuration = 0L
private var job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO + job)
private val timerScope = CoroutineScope(Dispatchers.IO + job)
private val skipScope = CoroutineScope(Dispatchers.IO + job)
private var autoAlarmStopScope: Job? = null
private var alarm: MediaPlayer? = null
private val vibrator by lazy {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@@ -154,7 +157,7 @@ class TimerService : Service() {
var iterations = -1
scope.launch {
timerScope.launch {
while (true) {
if (!timerState.value.timerRunning) break
if (startTime == 0L) startTime = SystemClock.elapsedRealtime()
@@ -372,6 +375,11 @@ class TimerService : Service() {
appContainer.activityTurnScreenOn(true)
autoAlarmStopScope = CoroutineScope(Dispatchers.IO).launch {
delay(1 * 60 * 1000)
stopAlarm()
}
if (timerRepository.vibrateEnabled) {
if (!vibrator.hasVibrator()) {
return
@@ -384,6 +392,8 @@ class TimerService : Service() {
}
fun stopAlarm() {
autoAlarmStopScope?.cancel()
if (timerRepository.alarmEnabled) {
alarm?.pause()
alarm?.seekTo(0)