Skip to content

Commit 95701b1

Browse files
committed
add pause implementation for pomodoro
1 parent 2c6ba52 commit 95701b1

File tree

15 files changed

+127
-64
lines changed

15 files changed

+127
-64
lines changed

data_local/src/main/java/com/example/util/simpletimetracker/data_local/prefs/PrefsRepoImpl.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ class PrefsRepoImpl @Inject constructor(
209209
KEY_POMODORO_MODE_STARTED_TIMESTAMP, 0,
210210
)
211211

212+
override var pomodoroModePausedTimestamp: Long by prefs.delegate(
213+
KEY_POMODORO_MODE_PAUSED_TIMESTAMP, 0,
214+
)
215+
212216
override var pomodoroFocusTime: Long by prefs.delegate(
213217
KEY_POMODORO_FOCUS_TIME, POMODORO_DEFAULT_FOCUS_TIME_SEC,
214218
)
@@ -710,6 +714,7 @@ class PrefsRepoImpl @Inject constructor(
710714
private const val KEY_AUTOMATIC_EXPORT_ERROR = "automaticExportError"
711715
private const val KEY_AUTOMATIC_EXPORT_LAST_SAVE_TIME = "automaticExportLastSaveTime"
712716
private const val KEY_POMODORO_MODE_STARTED_TIMESTAMP = "pomodoroModeStartedTimestamp"
717+
private const val KEY_POMODORO_MODE_PAUSED_TIMESTAMP = "pomodoroModePausedTimestamp"
713718
private const val KEY_WIDGET = "widget_"
714719
private const val KEY_STATISTICS_WIDGET_FILTERED_TYPES = "statistics_widget_filtered_types_"
715720
private const val KEY_STATISTICS_WIDGET_FILTERED_CATEGORIES = "statistics_widget_filtered_categories_"
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.example.util.simpletimetracker.domain.pomodoro.interactor
2+
3+
import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteractor
4+
import javax.inject.Inject
5+
6+
class GetPomodoroStateInteractor @Inject constructor(
7+
private val prefsInteractor: PrefsInteractor,
8+
) {
9+
10+
suspend fun execute(): State {
11+
val timeStarted = prefsInteractor.getPomodoroModeStartedTimestampMs()
12+
val timePaused = prefsInteractor.getPomodoroModePausedTimestampMs()
13+
14+
return when {
15+
timeStarted == 0L -> State.Stopped
16+
timePaused != 0L -> State.Paused
17+
else -> State.Running
18+
}
19+
}
20+
21+
sealed interface State {
22+
data object Stopped : State
23+
data object Paused : State
24+
data object Running : State
25+
}
26+
}

domain/src/main/java/com/example/util/simpletimetracker/domain/pomodoro/interactor/PomodoroNextCycleInteractor.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ class PomodoroNextCycleInteractor @Inject constructor(
99
private val prefsInteractor: PrefsInteractor,
1010
private val currentTimestampProvider: CurrentTimestampProvider,
1111
private val pomodoroCycleDurationsMapper: PomodoroCycleDurationsMapper,
12+
private val getPomodoroStateInteractor: GetPomodoroStateInteractor,
1213
private val getPomodoroSettingsInteractor: GetPomodoroSettingsInteractor,
1314
private val pomodoroCycleNotificationInteractor: PomodoroCycleNotificationInteractor,
1415
) {
1516

1617
suspend fun executePrev() {
1718
if (!prefsInteractor.getEnablePomodoroMode()) return
18-
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
19-
if (timeStartedMs == 0L) return
19+
val state = getPomodoroStateInteractor.execute()
20+
if (state !is GetPomodoroStateInteractor.State.Running) return
2021

22+
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
2123
val settings = getPomodoroSettingsInteractor.execute()
2224
val result = pomodoroCycleDurationsMapper.map(
2325
timeStartedMs = timeStartedMs,
@@ -43,9 +45,10 @@ class PomodoroNextCycleInteractor @Inject constructor(
4345

4446
suspend fun executeNext() {
4547
if (!prefsInteractor.getEnablePomodoroMode()) return
46-
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
47-
if (timeStartedMs == 0L) return
48+
val state = getPomodoroStateInteractor.execute()
49+
if (state !is GetPomodoroStateInteractor.State.Running) return
4850

51+
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
4952
val settings = getPomodoroSettingsInteractor.execute()
5053
val result = pomodoroCycleDurationsMapper.map(
5154
timeStartedMs = timeStartedMs,

domain/src/main/java/com/example/util/simpletimetracker/domain/pomodoro/interactor/PomodoroPauseCycleInteractor.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.example.util.simpletimetracker.domain.pomodoro.interactor
2+
3+
import com.example.util.simpletimetracker.domain.prefs.interactor.PrefsInteractor
4+
import javax.inject.Inject
5+
6+
class PomodoroPauseInteractor @Inject constructor(
7+
private val prefsInteractor: PrefsInteractor,
8+
private val pomodoroCycleNotificationInteractor: PomodoroCycleNotificationInteractor,
9+
private val getPomodoroStateInteractor: GetPomodoroStateInteractor,
10+
) {
11+
12+
suspend fun pause() {
13+
if (!prefsInteractor.getEnablePomodoroMode()) return
14+
val state = getPomodoroStateInteractor.execute()
15+
if (state !is GetPomodoroStateInteractor.State.Running) return
16+
17+
val current = System.currentTimeMillis()
18+
prefsInteractor.setPomodoroModePausedTimestampMs(current)
19+
pomodoroCycleNotificationInteractor.cancel()
20+
}
21+
22+
suspend fun startAfterPause() {
23+
if (!prefsInteractor.getEnablePomodoroMode()) return
24+
val state = getPomodoroStateInteractor.execute()
25+
if (state !is GetPomodoroStateInteractor.State.Paused) return
26+
27+
val current = System.currentTimeMillis()
28+
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
29+
val timePausedMs = prefsInteractor.getPomodoroModePausedTimestampMs()
30+
val newTimeStarted = timeStartedMs + current - timePausedMs
31+
prefsInteractor.setPomodoroModeStartedTimestampMs(newTimeStarted)
32+
prefsInteractor.setPomodoroModePausedTimestampMs(0)
33+
pomodoroCycleNotificationInteractor.checkAndReschedule()
34+
}
35+
}

domain/src/main/java/com/example/util/simpletimetracker/domain/pomodoro/interactor/PomodoroRestartCycleInteractor.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import javax.inject.Inject
77
class PomodoroRestartCycleInteractor @Inject constructor(
88
private val prefsInteractor: PrefsInteractor,
99
private val pomodoroCycleDurationsMapper: PomodoroCycleDurationsMapper,
10+
private val getPomodoroStateInteractor: GetPomodoroStateInteractor,
1011
private val getPomodoroSettingsInteractor: GetPomodoroSettingsInteractor,
1112
private val pomodoroCycleNotificationInteractor: PomodoroCycleNotificationInteractor,
1213
) {
1314

1415
suspend fun execute() {
1516
if (!prefsInteractor.getEnablePomodoroMode()) return
16-
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
17-
if (timeStartedMs == 0L) return
17+
val state = getPomodoroStateInteractor.execute()
18+
if (state !is GetPomodoroStateInteractor.State.Running) return
1819

20+
val timeStartedMs = prefsInteractor.getPomodoroModeStartedTimestampMs()
1921
val result = pomodoroCycleDurationsMapper.map(
2022
timeStartedMs = timeStartedMs,
2123
settings = getPomodoroSettingsInteractor.execute(),

domain/src/main/java/com/example/util/simpletimetracker/domain/pomodoro/interactor/PomodoroStartInteractor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import javax.inject.Inject
55

66
class PomodoroStartInteractor @Inject constructor(
77
private val prefsInteractor: PrefsInteractor,
8+
private val getPomodoroStateInteractor: GetPomodoroStateInteractor,
89
private val pomodoroCycleNotificationInteractor: PomodoroCycleNotificationInteractor,
910
) {
1011

@@ -16,8 +17,9 @@ class PomodoroStartInteractor @Inject constructor(
1617

1718
suspend fun checkAndStart(typeId: Long) {
1819
if (!prefsInteractor.getEnablePomodoroMode()) return
20+
val state = getPomodoroStateInteractor.execute()
1921

20-
if (prefsInteractor.getPomodoroModeStartedTimestampMs() == 0L &&
22+
if (state is GetPomodoroStateInteractor.State.Stopped &&
2123
typeId in prefsInteractor.getAutostartPomodoroActivities()
2224
) {
2325
start()

domain/src/main/java/com/example/util/simpletimetracker/domain/pomodoro/interactor/PomodoroStopInteractor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class PomodoroStopInteractor @Inject constructor(
1212

1313
suspend fun stop() {
1414
prefsInteractor.setPomodoroModeStartedTimestampMs(0)
15+
prefsInteractor.setPomodoroModePausedTimestampMs(0)
1516
pomodoroCycleNotificationInteractor.cancel()
1617
}
1718

domain/src/main/java/com/example/util/simpletimetracker/domain/prefs/interactor/PrefsInteractor.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,14 @@ class PrefsInteractor @Inject constructor(
390390
prefsRepo.pomodoroModeStartedTimestamp = timestampMs
391391
}
392392

393+
suspend fun getPomodoroModePausedTimestampMs(): Long = withContext(Dispatchers.IO) {
394+
prefsRepo.pomodoroModePausedTimestamp
395+
}
396+
397+
suspend fun setPomodoroModePausedTimestampMs(timestampMs: Long) = withContext(Dispatchers.IO) {
398+
prefsRepo.pomodoroModePausedTimestamp = timestampMs
399+
}
400+
393401
suspend fun getPomodoroFocusTime(): Long = withContext(Dispatchers.IO) {
394402
prefsRepo.pomodoroFocusTime
395403
}

domain/src/main/java/com/example/util/simpletimetracker/domain/prefs/repo/PrefsRepo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ interface PrefsRepo {
7878

7979
var pomodoroModeStartedTimestamp: Long // in milliseconds, 0 - disabled
8080

81+
var pomodoroModePausedTimestamp: Long // in milliseconds, 0 - disabled
82+
8183
var pomodoroFocusTime: Long // in seconds, 0 - disabled
8284

8385
var pomodoroBreakTime: Long // in seconds, 0 - disabled

0 commit comments

Comments
 (0)