Release v1.0.6: Background Timer Fix
This commit is contained in:
53
main.js
53
main.js
@@ -536,6 +536,9 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
this.isBreakMode = false;
|
||||
this.activeTaskId = null;
|
||||
this.pomodoroCount = 0;
|
||||
// Timestamp-based tracking for reliable background timing
|
||||
this.timerStartTimestamp = 0;
|
||||
this.pausedTimeRemaining = 0;
|
||||
// Focus time tracking (in seconds for accuracy)
|
||||
this.focusSecondsToday = 0;
|
||||
this.secondsWorkedOnCurrentTask = 0;
|
||||
@@ -545,6 +548,11 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
async onload() {
|
||||
await this.loadAllData();
|
||||
this.checkDailyReset();
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
if (!document.hidden && this.isTimerRunning) {
|
||||
this.syncTimerFromTimestamp();
|
||||
}
|
||||
});
|
||||
this.registerView(
|
||||
VIEW_TYPE_FOCUS_TASK,
|
||||
(leaf) => new FocusTaskView(leaf, this)
|
||||
@@ -701,6 +709,41 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
}
|
||||
}
|
||||
// ============ Timer Management ============
|
||||
// Sync timer based on timestamp when app returns from background
|
||||
syncTimerFromTimestamp() {
|
||||
if (!this.isTimerRunning || this.timerStartTimestamp === 0)
|
||||
return;
|
||||
const now = Date.now();
|
||||
const elapsedMs = now - this.timerStartTimestamp;
|
||||
const elapsedSeconds = Math.floor(elapsedMs / 1e3);
|
||||
if (this.isBreakMode) {
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
if (this.currentTimerSeconds <= 0) {
|
||||
this.handlePomodoroEnd();
|
||||
}
|
||||
} else {
|
||||
const task = this.data.tasks.find((t) => t.id === this.activeTaskId);
|
||||
if (!task)
|
||||
return;
|
||||
if (this.pausedTimeRemaining > 0) {
|
||||
this.currentTimerSeconds = Math.max(0, this.pausedTimeRemaining - elapsedSeconds);
|
||||
this.secondsWorkedOnCurrentTask += elapsedSeconds;
|
||||
task.actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
this.focusSecondsToday += elapsedSeconds;
|
||||
if (this.currentTimerSeconds <= 0) {
|
||||
this.handlePomodoroEnd();
|
||||
}
|
||||
} else {
|
||||
this.currentTimerSeconds += elapsedSeconds;
|
||||
this.secondsWorkedOnCurrentTask += elapsedSeconds;
|
||||
task.actualMinutes = Math.floor(this.secondsWorkedOnCurrentTask / 60);
|
||||
this.focusSecondsToday += elapsedSeconds;
|
||||
}
|
||||
}
|
||||
this.updateStatusBar();
|
||||
this.updateTimerDisplay();
|
||||
this.saveAllData();
|
||||
}
|
||||
startTimer(taskId) {
|
||||
const task = this.data.tasks.find((t) => t.id === taskId);
|
||||
if (!task)
|
||||
@@ -712,6 +755,8 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
this.currentTimerSeconds = 0;
|
||||
this.isTimerRunning = true;
|
||||
this.secondsWorkedOnCurrentTask = task.actualMinutes * 60;
|
||||
this.timerStartTimestamp = Date.now();
|
||||
this.pausedTimeRemaining = 0;
|
||||
this.refreshView();
|
||||
this.updateStatusBar();
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
@@ -741,6 +786,8 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
this.currentTimerSeconds = this.settings.pomodoroWorkMinutes * 60;
|
||||
this.isTimerRunning = true;
|
||||
this.secondsWorkedOnCurrentTask = task.actualMinutes * 60;
|
||||
this.timerStartTimestamp = Date.now();
|
||||
this.pausedTimeRemaining = this.currentTimerSeconds;
|
||||
this.refreshView();
|
||||
this.updateStatusBar();
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
@@ -792,6 +839,8 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
this.isTimerRunning = true;
|
||||
const isLongBreak = this.pomodoroCount % this.settings.longBreakInterval === 0;
|
||||
this.currentTimerSeconds = (isLongBreak ? this.settings.longBreakMinutes : this.settings.pomodoroBreakMinutes) * 60;
|
||||
this.timerStartTimestamp = Date.now();
|
||||
this.pausedTimeRemaining = this.currentTimerSeconds;
|
||||
new import_obsidian3.Notice(isLongBreak ? "\u2615 Long break time!" : "\u2615 Short break time!");
|
||||
this.refreshView();
|
||||
if (!this.timerInterval) {
|
||||
@@ -811,8 +860,10 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
window.clearInterval(this.timerInterval);
|
||||
this.timerInterval = null;
|
||||
this.isTimerRunning = false;
|
||||
this.pausedTimeRemaining = this.currentTimerSeconds;
|
||||
} else if (this.activeTaskId) {
|
||||
this.isTimerRunning = true;
|
||||
this.timerStartTimestamp = Date.now();
|
||||
const task = this.data.tasks.find((t) => t.id === this.activeTaskId);
|
||||
this.timerInterval = window.setInterval(() => {
|
||||
this.currentTimerSeconds--;
|
||||
@@ -847,6 +898,8 @@ var FocusTaskPlugin = class extends import_obsidian3.Plugin {
|
||||
this.isTimerRunning = false;
|
||||
this.activeTaskId = null;
|
||||
this.secondsWorkedOnCurrentTask = 0;
|
||||
this.timerStartTimestamp = 0;
|
||||
this.pausedTimeRemaining = 0;
|
||||
this.updateStatusBar();
|
||||
this.saveAllData();
|
||||
this.refreshView();
|
||||
|
||||
Reference in New Issue
Block a user