休息纯计时解耦滑动;重复任务按周期自动生成;统计数字去空格

This commit is contained in:
XIGE
2026-11-28 13:25:28 +08:00
parent ef2fb3a955
commit c3e0c9d67a
11 changed files with 147 additions and 102 deletions

View File

@@ -203,8 +203,9 @@ object TimerCore {
fun setCountUp(on: Boolean) {
if (running || countUp == on) return
countUp = on
// 正/倒计时与专注/休息是两个正交维度:左右滑只切计时方向,不碰 mode。
// 之前 on 时强制 mode = MODE_FOCUS,导致休息页左滑直接被拉回专注。
if (on) {
mode = MODE_FOCUS
accReset()
} else {
totalMs = durationFor(mode)
@@ -244,10 +245,9 @@ object TimerCore {
notifyChanged()
}
/** 切换模式时保留已选任务;专注/休息时长按各自设置重新取 */
/** 切换模式时保留已选任务;专注/休息时长按各自设置重新取。正/倒计时方向保持不变。 */
fun setMode(m: Int, keepRemaining: Boolean = false) {
if (running) return
countUp = false
mode = m
totalMs = durationFor(m)
remainingMs = if (keepRemaining) remainingMs.coerceAtMost(totalMs) else totalMs
@@ -304,7 +304,8 @@ object TimerCore {
fun pause(reason: String = "") {
if (!running) return
if (reason.isNotBlank()) {
// 休息是纯计时:连暂停原因也不落库、不写任务备注
if (reason.isNotBlank() && mode == MODE_FOCUS) {
pendingPauseReason = reason.trim()
pendingPauseStartAt = System.currentTimeMillis()
pendingPauseTaskId = taskId
@@ -328,7 +329,7 @@ object TimerCore {
val startAt = pendingPauseStartAt
val pausedTaskId = pendingPauseTaskId
start()
if (reason.isNotBlank() && startAt > 0L && pausedTaskId > 0L) {
if (mode == MODE_FOCUS && reason.isNotBlank() && startAt > 0L && pausedTaskId > 0L) {
val endAtPause = System.currentTimeMillis()
val seconds = ((endAtPause - startAt) / 1000).toInt().coerceAtLeast(0)
val line = ctx.getString(
@@ -417,6 +418,27 @@ object TimerCore {
return result
}
/**
* 结束休息:休息是纯计时,不写记录、不动轮次、不追加任务备注。
* 结束后回到专注待机,方向重置为倒计时,方便直接开始下一轮。
*/
fun endBreak() {
if (mode == MODE_FOCUS) return
running = false
clearPause()
endAt = 0
endElapsed = 0
startedAt = 0
mode = MODE_FOCUS
countUp = false
totalMs = durationFor(MODE_FOCUS)
remainingMs = totalMs
accReset()
save()
notifyChanged()
TimerService.stop(ctx)
}
/** 跳过:专注跳过不算完成,休息跳过直接进入专注 */
fun skip() {
if (countUp) {
@@ -516,6 +538,9 @@ object TimerCore {
endMs: Long = System.currentTimeMillis(),
startMs: Long = 0L,
) {
// 休息只是纯计时,不写任何库;任何路径(自然到点 / 提前结束 / 跳过 / 进程恢复)
// 都只在专注阶段产生一条记录。
if (mode != MODE_FOCUS) return
val end = endMs
// 正计时暂停后 startedAt 归零,按实际专注时长反推起点,不能拿当前时刻冒充开始时间
val start = if (startMs > 0) startMs else if (startedAt > 0) startedAt else end - elapsedSec * 1000L

View File

@@ -75,7 +75,8 @@ class TimerService : Service() {
syncTicker()
}
ACTION_STOP -> {
TimerCore.stopEarly()
// 休息是纯计时:通知上的"结束"也走 endBreak,不产生任何记录
if (TimerCore.mode == TimerCore.MODE_FOCUS) TimerCore.stopEarly() else TimerCore.endBreak()
stopTicker()
stopSelf()
return START_NOT_STICKY