Skip to content

Commit a93e748

Browse files
committed
feat(alarm): automatically stop alarm after 1 min
Closes: #88
1 parent aa4889a commit a93e748

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

app/src/main/java/org/nsh07/pomodoro/MainActivity.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ class MainActivity : ComponentActivity() {
4545
override fun onCreate(savedInstanceState: Bundle?) {
4646
super.onCreate(savedInstanceState)
4747
enableEdgeToEdge()
48+
4849
appContainer.activityTurnScreenOn = {
4950
setShowWhenLocked(it)
5051
setTurnScreenOn(it)
5152
}
53+
5254
setContent {
5355
val preferencesState by settingsViewModel.preferencesState.collectAsStateWithLifecycle()
5456

app/src/main/java/org/nsh07/pomodoro/service/TimerService.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import androidx.core.app.NotificationCompat
3333
import androidx.core.app.NotificationManagerCompat
3434
import kotlinx.coroutines.CoroutineScope
3535
import kotlinx.coroutines.Dispatchers
36+
import kotlinx.coroutines.Job
3637
import kotlinx.coroutines.SupervisorJob
3738
import kotlinx.coroutines.delay
3839
import kotlinx.coroutines.flow.asStateFlow
@@ -71,9 +72,11 @@ class TimerService : Service() {
7172
private var pauseDuration = 0L
7273

7374
private var job = SupervisorJob()
74-
private val scope = CoroutineScope(Dispatchers.IO + job)
75+
private val timerScope = CoroutineScope(Dispatchers.IO + job)
7576
private val skipScope = CoroutineScope(Dispatchers.IO + job)
7677

78+
private var autoAlarmStopScope: Job? = null
79+
7780
private var alarm: MediaPlayer? = null
7881
private val vibrator by lazy {
7982
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
@@ -154,7 +157,7 @@ class TimerService : Service() {
154157

155158
var iterations = -1
156159

157-
scope.launch {
160+
timerScope.launch {
158161
while (true) {
159162
if (!timerState.value.timerRunning) break
160163
if (startTime == 0L) startTime = SystemClock.elapsedRealtime()
@@ -372,6 +375,11 @@ class TimerService : Service() {
372375

373376
appContainer.activityTurnScreenOn(true)
374377

378+
autoAlarmStopScope = CoroutineScope(Dispatchers.IO).launch {
379+
delay(1 * 60 * 1000)
380+
stopAlarm()
381+
}
382+
375383
if (timerRepository.vibrateEnabled) {
376384
if (!vibrator.hasVibrator()) {
377385
return
@@ -384,6 +392,8 @@ class TimerService : Service() {
384392
}
385393

386394
fun stopAlarm() {
395+
autoAlarmStopScope?.cancel()
396+
387397
if (timerRepository.alarmEnabled) {
388398
alarm?.pause()
389399
alarm?.seekTo(0)

0 commit comments

Comments
 (0)