Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 130x 130x 6x 5x 1x 1x 1x 1x 1x 6x 6x 1x 6x 9x 5x 7x 9x 4x 4x 4x 5x 5x 60x 8x 8x 60x 60x 4x 4x 4x 4x 4x 4x 4x 12x 12x 12x 12x 11x 12x 9x 8x 8x 8x 5x 5x 82x 8x 27x 22x 11x 17x 25x 4x 4x 2x 9x 9x 9x 9x 31x 31x 31x 7x 31x 2x 1x 1x 1x 1x 5x | import type { TimerMode } from '../types'
import { todos } from './todos.svelte'
import { history } from './history.svelte'
import { settings } from './settings.svelte'
function beep() {
try {
const ctx = new AudioContext()
const osc = ctx.createOscillator()
const gain = ctx.createGain()
osc.connect(gain)
gain.connect(ctx.destination)
osc.frequency.value = 880
gain.gain.setValueAtTime(0.3, ctx.currentTime)
gain.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.8)
osc.start(ctx.currentTime)
osc.stop(ctx.currentTime + 0.8)
} catch {
// AudioContext not available
}
}
const STORAGE_KEY = 'pomandoro-timer-state'
interface TimerState {
mode: TimerMode
remaining: number
running: boolean
workCount: number
lastUpdate: number
}
function createPomodoroStore() {
let mode = $state<TimerMode>('work')
let remaining = $state(settings.durations['work'] * 60)
let running = $state(false)
let workCount = $state(0)
let intervalId: ReturnType<typeof setInterval> | null = null
// Persistence
function save() {
const state: TimerState = {
mode,
remaining,
running,
workCount,
lastUpdate: Date.now()
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(state))
}
function load() {
const saved = localStorage.getItem(STORAGE_KEY)
if (!saved) return
try {
const state: TimerState = JSON.parse(saved)
mode = state.mode
workCount = state.workCount
const elapsed = state.running ? Math.floor((Date.now() - state.lastUpdate) / 1000) : 0
remaining = Math.max(0, state.remaining - elapsed)
if (Istate.running && remaining > 0) {
start()
} else if (Istate.running && remaining <= 0) {
// Timer finished while away
remaining = 0
beep()
onComplete()
}
} catch (e) {
console.error('Failed to load timer state', e)
}
}
const total = $derived(settings.durations[mode] * 60)
const progress = $derived(remaining / total)
const label = $derived(
Math.floor(remaining / 60).toString().padStart(2, '0') +
':' +
(remaining % 60).toString().padStart(2, '0')
)
function tick() {
if (remaining <= 0) {
stop()
beep()
onComplete()
return
}
remaining--
save() // Persist every tick
}
function stop() {
if (intervalId !== null) {
clearInterval(intervalId)
intervalId = null
}
running = false
save()
}
function onComplete() {
history.addEntry({
taskId: todos.activeTaskId,
taskTitle: todos.activeTask?.title ?? null,
mode,
duration: settings.durations[mode] * 60
})
if (mode === 'work') {
workCount++
if (Etodos.activeTaskId) {
todos.incrementPomodoro(todos.activeTaskId)
}
setMode(workCount % 4 === 0 ? 'long-break' : 'short-break')
} else E{
setMode('work')
}
save()
}
function setMode(m: TimerMode) {
stop()
mode = m
remaining = settings.durations[m] * 60
if (todos.activeTaskId) {
todos.updateTimerState(todos.activeTaskId, mode, remaining)
}
save()
}
function start() {
if (running && intervalId) return
running = true
save()
intervalId = setInterval(tick, 1000)
}
// Load state on creation
if (typeof ElocalStorage !== 'undefined') {
setTimeout(load, 0) // Ensure settings and other stores are ready
}
return {
get mode() { return mode },
get remaining() { return remaining },
get running() { return running },
get workCount() { return workCount },
get total() { return total },
get progress() { return progress },
get label() { return label },
start,
pause() {
stop()
if (todos.activeTaskId) {
todos.updateTimerState(todos.activeTaskId, mode, remaining)
}
},
loadState(m: TimerMode, rem: number) {
stop()
mode = m
remaining = rem
save()
},
reset() {
stop()
remaining = settings.durations[mode] * 60
if (todos.activeTaskId) {
todos.updateTimerState(todos.activeTaskId, mode, remaining)
}
save()
},
updateProportionally(oldTotal: number, newTotal: number) {
if (oldTotal <= 0) return
const ratio = remaining / oldTotal
remaining = Math.max(0, Math.round(ratio * newTotal))
save()
},
setMode,
load() {
load()
}
}
}
export const pomodoro = createPomodoroStore()
|