Skip to content

Commit a45790a

Browse files
committed
Enhance time entry management and tracking: Initialize storage for time entries, improve logging during entry addition, and implement focus timeout for tracking state.
1 parent c2e9bd9 commit a45790a

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

src/database.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ export class Database {
1717

1818
constructor(context: vscode.ExtensionContext) {
1919
this.context = context;
20+
// Initialize storage if empty
21+
if (!this.context.globalState.get('timeEntries')) {
22+
this.context.globalState.update('timeEntries', []);
23+
}
24+
25+
// Debug logging
26+
const entries = this.getEntries();
27+
console.log('Database initialized with entries:', entries);
28+
2029
// Log the storage URI path
2130
const storagePath = this.context.storageUri?.fsPath;
2231
if (storagePath) {
@@ -28,17 +37,27 @@ export class Database {
2837
async addEntry(date: Date, project: string, timeSpent: number) {
2938
const dateString = date.toISOString().split('T')[0];
3039
const entries = this.getEntries();
40+
41+
console.log('Adding entry:', { date: dateString, project, timeSpent });
42+
console.log('Current entries:', entries);
43+
3144
const existingEntryIndex = entries.findIndex(entry => entry.date === dateString && entry.project === project);
3245

3346
if (existingEntryIndex !== -1) {
34-
// Update existing entry
3547
entries[existingEntryIndex].timeSpent += timeSpent;
48+
console.log('Updated existing entry:', entries[existingEntryIndex]);
3649
} else {
37-
// Add new entry
3850
entries.push({ date: dateString, project, timeSpent });
51+
console.log('Added new entry');
3952
}
4053

41-
await this.context.globalState.update('timeEntries', entries);
54+
try {
55+
await this.context.globalState.update('timeEntries', entries);
56+
console.log('Successfully saved entries:', this.getEntries());
57+
} catch (error) {
58+
console.error('Error saving entry:', error);
59+
vscode.window.showErrorMessage('Failed to save time entry');
60+
}
4261
}
4362

4463
getEntries(): TimeEntry[] {

src/extension.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,28 @@ export function activate(context: vscode.ExtensionContext) {
5050
timeTracker.startTracking();
5151
}
5252

53+
// Variable to store the focus timeout handle
54+
let focusTimeoutHandle: NodeJS.Timeout | null = null;
55+
5356
vscode.window.onDidChangeWindowState((e: vscode.WindowState) => {
5457
if (e.focused) {
58+
if (focusTimeoutHandle) {
59+
clearTimeout(focusTimeoutHandle);
60+
focusTimeoutHandle = null;
61+
}
5562
timeTracker.startTracking();
5663
} else {
57-
timeTracker.stopTracking();
64+
const config = vscode.workspace.getConfiguration('simpleCodingTimeTracker');
65+
const focusTimeoutSeconds = config.get('focusTimeout', 60);
66+
67+
// Only stop tracking after the focus timeout
68+
if (focusTimeoutHandle) {
69+
clearTimeout(focusTimeoutHandle);
70+
}
71+
72+
focusTimeoutHandle = setTimeout(() => {
73+
timeTracker.stopTracking();
74+
}, focusTimeoutSeconds * 1000);
5875
}
5976
});
6077

0 commit comments

Comments
 (0)