Enable deleting notes by deleting all their content

This commit is contained in:
2025-04-23 20:56:01 +02:00
parent 6c8340d768
commit e8b9f0ba49
2 changed files with 91 additions and 20 deletions

View File

@@ -48,9 +48,7 @@ Future<bool> alreadyRunning() async {
final executable = Platform.resolvedExecutable; final executable = Platform.resolvedExecutable;
final executableName = path.basename(executable); final executableName = path.basename(executable);
final journalers = final journalers =
processes processes.where((process) => process == executableName).toList();
.where((process) => process == executableName)
.toList();
debugPrint("Journalers: $journalers"); debugPrint("Journalers: $journalers");
return journalers.length > 1; return journalers.length > 1;
} }
@@ -359,8 +357,9 @@ class MainPageState extends State<MainPage> with WindowListener {
} }
@override @override
void onWindowClose() { void onWindowClose() async {
_saveData(); // Save data when window is closed
await _saveData();
windowManager.hide(); windowManager.hide();
} }
@@ -494,6 +493,12 @@ class MainPageState extends State<MainPage> with WindowListener {
Future<void> _goToPreviousNote() async { Future<void> _goToPreviousNote() async {
if (!_canGoPrevious || _currentlyDisplayedNote == null) return; if (!_canGoPrevious || _currentlyDisplayedNote == null) return;
// Save the current note content before navigating away
if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = _previousEntryController.text;
await updateNote(_currentlyDisplayedNote!);
}
final prevNote = await getPreviousNote(_currentlyDisplayedNote!.date); final prevNote = await getPreviousNote(_currentlyDisplayedNote!.date);
if (prevNote != null) { if (prevNote != null) {
setState(() { setState(() {
@@ -507,6 +512,12 @@ class MainPageState extends State<MainPage> with WindowListener {
Future<void> _goToNextNote() async { Future<void> _goToNextNote() async {
if (!_canGoNext || _currentlyDisplayedNote == null) return; if (!_canGoNext || _currentlyDisplayedNote == null) return;
// Save the current note content before navigating away
if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = _previousEntryController.text;
await updateNote(_currentlyDisplayedNote!);
}
final nextNote = await getNextNote(_currentlyDisplayedNote!.date); final nextNote = await getNextNote(_currentlyDisplayedNote!.date);
if (nextNote != null) { if (nextNote != null) {
setState(() { setState(() {
@@ -565,18 +576,40 @@ class MainPageState extends State<MainPage> with WindowListener {
debugPrint("Volume saved: $_volume"); debugPrint("Volume saved: $_volume");
} }
void _saveData() async { Future<void> _saveData() async {
String previousEntry = _previousEntryController.text; String previousEntry = _previousEntryController.text;
String currentEntry = _currentEntryController.text; String currentEntry = _currentEntryController.text;
String scratchContent = _scratchController.text; String scratchContent = _scratchController.text;
String intervalStr = _intervalController.text; String intervalStr = _intervalController.text;
String soundStr = _soundController.text; String soundStr = _soundController.text;
// Handle current entry
if (currentEntry.isNotEmpty) {
await createNote(currentEntry); await createNote(currentEntry);
}
// Handle scratch pad
await createScratch(scratchContent); await createScratch(scratchContent);
if (previousNote != null) {
previousNote!.content = previousEntry; // Handle previous/currently displayed note
await updateNote(previousNote!); if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = previousEntry;
await updateNote(_currentlyDisplayedNote!);
// If the note was deleted (due to being empty), update the UI state
if (previousEntry.isEmpty) {
// Check if we need to navigate to another note
Note? nextNote = await getLatestNote();
setState(() {
_currentlyDisplayedNote = nextNote;
if (nextNote != null) {
_previousEntryController.text = nextNote.content;
} else {
_previousEntryController.text = "";
}
});
await _checkNavigation();
}
} }
int newIntervalMinutes = int newIntervalMinutes =
@@ -716,9 +749,15 @@ class MainPageState extends State<MainPage> with WindowListener {
try { try {
final results = await searchNotes(trimmedQuery); final results = await searchNotes(trimmedQuery);
// Filter out empty notes (which may exist in the search index but were deleted)
final filteredResults =
results
.where((note) => note.content.isNotEmpty)
.toList();
// Important: update the dialog state after search completes // Important: update the dialog state after search completes
dialogSetState(() { dialogSetState(() {
_searchResults = results; _searchResults = filteredResults;
_isSearching = false; _isSearching = false;
}); });
} catch (e) { } catch (e) {
@@ -787,7 +826,18 @@ class MainPageState extends State<MainPage> with WindowListener {
), // Smaller font for content ), // Smaller font for content
), ),
isThreeLine: true, isThreeLine: true,
onTap: () { onTap: () async {
// Save current note if needed
if (_currentlyDisplayedNote !=
null) {
_currentlyDisplayedNote!.content =
_previousEntryController.text;
await updateNote(
_currentlyDisplayedNote!,
);
}
// Navigate to the selected note
Navigator.of(context).pop(); Navigator.of(context).pop();
this.setState(() { this.setState(() {
_currentlyDisplayedNote = note; _currentlyDisplayedNote = note;
@@ -1027,9 +1077,7 @@ class MainPageState extends State<MainPage> with WindowListener {
Expanded( Expanded(
child: TextField( child: TextField(
controller: _previousEntryController, controller: _previousEntryController,
readOnly: readOnly: false, // Always allow editing
_currentlyDisplayedNote?.date !=
previousNote?.date,
maxLines: null, maxLines: null,
expands: true, expands: true,
style: Theme.of(context).textTheme.bodyMedium, style: Theme.of(context).textTheme.bodyMedium,
@@ -1037,7 +1085,7 @@ class MainPageState extends State<MainPage> with WindowListener {
hintText: hintText:
_currentlyDisplayedNote?.date != _currentlyDisplayedNote?.date !=
previousNote?.date previousNote?.date
? 'Viewing note from ${_currentlyDisplayedNote?.date} (Read-Only)' ? 'Viewing note from ${_currentlyDisplayedNote?.date} (Editable)'
: 'Latest Note', : 'Latest Note',
border: const OutlineInputBorder(), border: const OutlineInputBorder(),
filled: filled:

View File

@@ -30,16 +30,26 @@ Future<Note?> getLatestNote() async {
} }
Future<void> createNote(String content) async { Future<void> createNote(String content) async {
if (content.isEmpty) { // Trim the content to avoid saving just whitespace
final trimmedContent = content.trim();
if (trimmedContent.isEmpty) {
return; return;
} }
await DB.db.insert('notes', {'content': content}); await DB.db.insert('notes', {'content': trimmedContent});
} }
Future<void> updateNote(Note note) async { Future<void> updateNote(Note note) async {
// Trim the content to avoid saving just whitespace
final trimmedContent = note.content.trim();
if (trimmedContent.isEmpty) {
// Delete the note if content is empty
await DB.db.delete('notes', where: 'date = ?', whereArgs: [note.date]);
return;
}
await DB.db.update( await DB.db.update(
'notes', 'notes',
{'content': note.content}, {'content': trimmedContent},
where: 'date = ?', where: 'date = ?',
whereArgs: [note.date], whereArgs: [note.date],
); );
@@ -61,7 +71,9 @@ Future<Scratch?> getLatestScratch() async {
} }
Future<void> createScratch(String content) async { Future<void> createScratch(String content) async {
await DB.db.insert('scratches', {'content': content}); // Trim content but allow empty scratch notes (they might be intentionally cleared)
final trimmedContent = content.trim();
await DB.db.insert('scratches', {'content': trimmedContent});
} }
// Get the note immediately older than the given date // Get the note immediately older than the given date
@@ -103,6 +115,17 @@ Future<Note?> getNextNote(String currentDate) async {
return getLatestNote(); return getLatestNote();
} }
/// Delete a note by its date
Future<bool> deleteNote(String date) async {
final result = await DB.db.delete(
'notes',
where: 'date = ?',
whereArgs: [date],
);
return result > 0; // Return true if a note was deleted
}
// Search notes using full-text search // Search notes using full-text search
Future<List<Note>> searchNotes(String query) async { Future<List<Note>> searchNotes(String query) async {
if (query.isEmpty) { if (query.isEmpty) {