Release v1.0.8: Timer Accuracy Fix
This commit is contained in:
132
src/main.ts
132
src/main.ts
@@ -273,56 +273,14 @@ export default class FocusTaskPlugin extends Plugin {
|
||||
|
||||
// Sync timer based on timestamp when app returns from background
|
||||
syncTimerFromTimestamp() {
|
||||
if (!this.isTimerRunning || this.timerStartTimestamp === 0) return;
|
||||
// Since all intervals now calculate from timestamps directly,
|
||||
// we just need to trigger an update when coming back to foreground
|
||||
if (!this.isTimerRunning) return;
|
||||
|
||||
const now = Date.now();
|
||||
const elapsedMs = now - this.timerStartTimestamp;
|
||||
const elapsedSeconds = Math.floor(elapsedMs / 1000);
|
||||
|
||||
// If less than 1 second elapsed, no need to sync
|
||||
if (elapsedSeconds < 1) return;
|
||||
|
||||
if (this.isBreakMode) {
|
||||
// Break mode: countdown timer
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
|
||||
if (this.currentTimerSeconds <= 0) {
|
||||
this.handlePomodoroEnd();
|
||||
}
|
||||
} else {
|
||||
// Work mode: check if it's pomodoro (countdown) or stopwatch (count up)
|
||||
const task = this.data.tasks.find(t => t.id === this.activeTaskId);
|
||||
if (!task) return;
|
||||
|
||||
if (this.pausedTimeRemaining > 0) {
|
||||
// Pomodoro countdown mode
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
|
||||
// Update actual minutes worked
|
||||
this.secondsWorkedOnCurrentTask += elapsedSeconds;
|
||||
task.actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
this.focusSecondsToday += elapsedSeconds;
|
||||
|
||||
if (this.currentTimerSeconds <= 0) {
|
||||
this.handlePomodoroEnd();
|
||||
}
|
||||
} else {
|
||||
// Stopwatch count up mode
|
||||
this.currentTimerSeconds += elapsedSeconds;
|
||||
this.secondsWorkedOnCurrentTask += elapsedSeconds;
|
||||
task.actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
this.focusSecondsToday += elapsedSeconds;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset timestamp to now to prevent double-counting on next sync
|
||||
this.timerStartTimestamp = now;
|
||||
this.pausedTimeRemaining = this.currentTimerSeconds;
|
||||
|
||||
// Update displays
|
||||
// The interval will automatically calculate correct values on next tick
|
||||
// Just update the display immediately to show current state
|
||||
this.updateStatusBar();
|
||||
this.updateTimerDisplay();
|
||||
this.saveAllData();
|
||||
}
|
||||
|
||||
startTimer(taskId: string) {
|
||||
@@ -344,25 +302,39 @@ export default class FocusTaskPlugin extends Plugin {
|
||||
this.timerStartTimestamp = Date.now();
|
||||
this.pausedTimeRemaining = 0; // 0 indicates stopwatch mode
|
||||
|
||||
// Store initial values
|
||||
const initialSecondsWorked = this.secondsWorkedOnCurrentTask;
|
||||
let alertShown = false;
|
||||
|
||||
// Full refresh to show the active task card
|
||||
this.refreshView();
|
||||
this.updateStatusBar();
|
||||
|
||||
// Start interval (count up mode - stopwatch)
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
this.currentTimerSeconds++;
|
||||
this.secondsWorkedOnCurrentTask++;
|
||||
// Calculate elapsed time from timestamp
|
||||
const now = Date.now();
|
||||
const elapsedMs = now - this.timerStartTimestamp;
|
||||
const elapsedSeconds = Math.floor(elapsedMs / 1000);
|
||||
|
||||
// Update timer (count up)
|
||||
this.currentTimerSeconds = elapsedSeconds;
|
||||
|
||||
// Update actual time worked
|
||||
this.secondsWorkedOnCurrentTask = initialSecondsWorked + elapsedSeconds;
|
||||
task.actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
|
||||
// Track focus time
|
||||
this.focusSecondsToday++;
|
||||
// Update focus time
|
||||
const newFocusSeconds = Math.floor((this.data.totalFocusMinutesToday || 0) * 60) + elapsedSeconds;
|
||||
this.focusSecondsToday = newFocusSeconds;
|
||||
|
||||
// Light update - only timer display, no full refresh
|
||||
this.updateStatusBar();
|
||||
this.updateTimerDisplay();
|
||||
|
||||
// Check if over estimate
|
||||
if (this.currentTimerSeconds === task.estimatedMinutes * 60) {
|
||||
// Check if over estimate (only alert once)
|
||||
if (!alertShown && this.currentTimerSeconds >= task.estimatedMinutes * 60) {
|
||||
alertShown = true;
|
||||
if (this.settings.enableSounds) {
|
||||
this.playAlertSound();
|
||||
}
|
||||
@@ -392,23 +364,34 @@ export default class FocusTaskPlugin extends Plugin {
|
||||
this.timerStartTimestamp = Date.now();
|
||||
this.pausedTimeRemaining = this.currentTimerSeconds;
|
||||
|
||||
// Store the initial seconds worked to calculate delta
|
||||
const initialSecondsWorked = this.secondsWorkedOnCurrentTask;
|
||||
|
||||
// Full refresh to show the active task card
|
||||
this.refreshView();
|
||||
this.updateStatusBar();
|
||||
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
this.currentTimerSeconds--;
|
||||
// Calculate elapsed time from timestamp (more accurate than counting ticks)
|
||||
const now = Date.now();
|
||||
const elapsedMs = now - this.timerStartTimestamp;
|
||||
const elapsedSeconds = Math.floor(elapsedMs / 1000);
|
||||
|
||||
// Update countdown timer
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
|
||||
if (!this.isBreakMode) {
|
||||
this.secondsWorkedOnCurrentTask++;
|
||||
// Calculate actual minutes from total seconds worked
|
||||
// Update actual time worked based on real elapsed time
|
||||
this.secondsWorkedOnCurrentTask = initialSecondsWorked + elapsedSeconds;
|
||||
const actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
// Only update if changed to avoid unnecessary updates
|
||||
|
||||
if (task.actualMinutes !== actualMinutes) {
|
||||
task.actualMinutes = actualMinutes;
|
||||
}
|
||||
// Increment focus time by 1 second
|
||||
this.focusSecondsToday++;
|
||||
|
||||
// Update focus time based on elapsed seconds
|
||||
const newFocusSeconds = Math.floor((this.data.totalFocusMinutesToday || 0) * 60) + elapsedSeconds;
|
||||
this.focusSecondsToday = newFocusSeconds;
|
||||
}
|
||||
|
||||
// Light update - only timer display, no full refresh
|
||||
@@ -483,7 +466,14 @@ export default class FocusTaskPlugin extends Plugin {
|
||||
|
||||
if (!this.timerInterval) {
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
this.currentTimerSeconds--;
|
||||
// Calculate elapsed time from timestamp
|
||||
const now = Date.now();
|
||||
const elapsedMs = now - this.timerStartTimestamp;
|
||||
const elapsedSeconds = Math.floor(elapsedMs / 1000);
|
||||
|
||||
// Update countdown timer
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
|
||||
// Light update - only timer display
|
||||
this.updateStatusBar();
|
||||
this.updateTimerDisplay();
|
||||
@@ -510,14 +500,28 @@ export default class FocusTaskPlugin extends Plugin {
|
||||
this.timerStartTimestamp = Date.now();
|
||||
const task = this.data.tasks.find(t => t.id === this.activeTaskId);
|
||||
|
||||
// Store initial values for resume
|
||||
const initialSecondsWorked = this.secondsWorkedOnCurrentTask;
|
||||
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
this.currentTimerSeconds--;
|
||||
// Calculate elapsed time from timestamp
|
||||
const now = Date.now();
|
||||
const elapsedMs = now - this.timerStartTimestamp;
|
||||
const elapsedSeconds = Math.floor(elapsedMs / 1000);
|
||||
|
||||
// Update timer (countdown from paused position)
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
|
||||
if (task && !this.isBreakMode) {
|
||||
this.secondsWorkedOnCurrentTask++;
|
||||
// Update actual time worked
|
||||
this.secondsWorkedOnCurrentTask = initialSecondsWorked + elapsedSeconds;
|
||||
task.actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
// Track focus time
|
||||
this.focusSecondsToday++;
|
||||
|
||||
// Update focus time
|
||||
const newFocusSeconds = Math.floor((this.data.totalFocusMinutesToday || 0) * 60) + elapsedSeconds;
|
||||
this.focusSecondsToday = newFocusSeconds;
|
||||
}
|
||||
|
||||
// Light update - only timer display
|
||||
this.updateStatusBar();
|
||||
this.updateTimerDisplay();
|
||||
|
||||
Reference in New Issue
Block a user