Refactor note handling to use epoch time and improve utility functions for settings management

This commit is contained in:
2025-05-24 00:05:44 +02:00
parent dfe1c2b34c
commit 3c1f31d29b
4 changed files with 182 additions and 84 deletions

View File

@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:journaler/db.dart';
import 'package:journaler/notes.dart';
import 'package:journaler/utils.dart';
import 'package:system_tray/system_tray.dart';
import 'package:window_manager/window_manager.dart';
import 'package:audioplayers/audioplayers.dart';
@@ -483,8 +484,9 @@ class MainPageState extends State<MainPage> with WindowListener {
return;
}
final prev = await getPreviousTo(_currentlyDisplayedNote!.date);
final bool isLatest = _currentlyDisplayedNote!.date == previousNote?.date;
final prev = await getPreviousTo(_currentlyDisplayedNote!.epochTime);
final bool isLatest =
_currentlyDisplayedNote!.epochTime == previousNote?.epochTime;
setState(() {
_canGoPrevious = prev != null;
@@ -498,10 +500,10 @@ class MainPageState extends State<MainPage> with WindowListener {
// Save the current note content before navigating away
if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = _previousEntryController.text;
await updateNote(_currentlyDisplayedNote!.date, _currentlyDisplayedNote!.content);
await updateNote(_currentlyDisplayedNote!);
}
final prevNote = await getPreviousTo(_currentlyDisplayedNote!.date);
final prevNote = await getPreviousTo(_currentlyDisplayedNote!.epochTime);
if (prevNote != null) {
setState(() {
_currentlyDisplayedNote = prevNote;
@@ -517,10 +519,10 @@ class MainPageState extends State<MainPage> with WindowListener {
// Save the current note content before navigating away
if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = _previousEntryController.text;
await updateNote(_currentlyDisplayedNote!.date, _currentlyDisplayedNote!.content);
await updateNote(_currentlyDisplayedNote!);
}
final nextNote = await getNextTo(_currentlyDisplayedNote!.date);
final nextNote = await getNextTo(_currentlyDisplayedNote!.epochTime);
if (nextNote != null) {
setState(() {
_currentlyDisplayedNote = nextNote;
@@ -531,16 +533,13 @@ class MainPageState extends State<MainPage> with WindowListener {
}
void _loadData() async {
String? intervalMinutesStr = await DB.getSetting('popupIntervalMinutes');
String? soundFileStr = await DB.getSetting('notificationSound');
Duration interval = await getPopupInterval();
String soundFile = await getNotificationSound();
int intervalMinutes =
int.tryParse(intervalMinutesStr ?? '') ??
_defaultPopupInterval.inMinutes;
_currentPopupInterval = Duration(minutes: intervalMinutes);
_currentNotificationSound = soundFileStr ?? _defaultNotificationSound;
_currentPopupInterval = interval;
_currentNotificationSound = soundFile;
_intervalController.text = intervalMinutes.toString();
_intervalController.text = interval.inMinutes.toString();
_soundController.text = _currentNotificationSound;
_startPopupTimer();
@@ -560,20 +559,16 @@ class MainPageState extends State<MainPage> with WindowListener {
// Load volume setting from database
Future<void> _loadVolume() async {
String? volumeStr = await DB.getSetting('notificationVolume');
if (volumeStr != null) {
setState(() {
_volume = double.tryParse(volumeStr) ?? 0.7;
_audioPlayer.setVolume(_linearToLogVolume(_volume));
});
} else {
double? volume = await getVolume();
setState(() {
_volume = volume;
_audioPlayer.setVolume(_linearToLogVolume(_volume));
}
});
}
// Save volume setting to database
Future<void> _saveVolume() async {
await DB.setSetting('notificationVolume', _volume.toString());
await setVolume(_volume);
debugPrint("Volume saved: $_volume");
}
@@ -596,7 +591,7 @@ class MainPageState extends State<MainPage> with WindowListener {
// Handle previous/currently displayed note
if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = previousEntry;
await updateNote(_currentlyDisplayedNote!.date, _currentlyDisplayedNote!.content);
await updateNote(_currentlyDisplayedNote!);
// If the note was deleted (due to being empty), update the UI state
if (previousEntry.isEmpty) {
@@ -759,7 +754,7 @@ class MainPageState extends State<MainPage> with WindowListener {
// Sort by date, newest first
filteredResults.sort(
(a, b) => b.date.compareTo(a.date),
(a, b) => b.epochTime.compareTo(a.epochTime),
);
// Important: update the dialog state after search completes
@@ -840,8 +835,7 @@ class MainPageState extends State<MainPage> with WindowListener {
_currentlyDisplayedNote!.content =
_previousEntryController.text;
await updateNote(
_currentlyDisplayedNote!.date,
_currentlyDisplayedNote!.content,
_currentlyDisplayedNote!,
);
}
@@ -916,9 +910,7 @@ class MainPageState extends State<MainPage> with WindowListener {
// Show cleanup dialog
void _showCleanupDialog() async {
double sensitivity = 0.7; // Default 70%
final problematicEntries = await getProblematic(
threshold: sensitivity,
);
final problematicEntries = await getProblematic(threshold: sensitivity);
if (!mounted) return;
@@ -1080,7 +1072,7 @@ class MainPageState extends State<MainPage> with WindowListener {
}
if (shouldDelete) {
await deleteNote(note.date);
await deleteNote(note.id);
dialogSetState(() {
problematicEntries.removeAt(
index,