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 executableName = path.basename(executable);
final journalers =
processes
.where((process) => process == executableName)
.toList();
processes.where((process) => process == executableName).toList();
debugPrint("Journalers: $journalers");
return journalers.length > 1;
}
@@ -359,8 +357,9 @@ class MainPageState extends State<MainPage> with WindowListener {
}
@override
void onWindowClose() {
_saveData();
void onWindowClose() async {
// Save data when window is closed
await _saveData();
windowManager.hide();
}
@@ -494,6 +493,12 @@ class MainPageState extends State<MainPage> with WindowListener {
Future<void> _goToPreviousNote() async {
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);
if (prevNote != null) {
setState(() {
@@ -507,6 +512,12 @@ class MainPageState extends State<MainPage> with WindowListener {
Future<void> _goToNextNote() async {
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);
if (nextNote != null) {
setState(() {
@@ -565,18 +576,40 @@ class MainPageState extends State<MainPage> with WindowListener {
debugPrint("Volume saved: $_volume");
}
void _saveData() async {
Future<void> _saveData() async {
String previousEntry = _previousEntryController.text;
String currentEntry = _currentEntryController.text;
String scratchContent = _scratchController.text;
String intervalStr = _intervalController.text;
String soundStr = _soundController.text;
await createNote(currentEntry);
// Handle current entry
if (currentEntry.isNotEmpty) {
await createNote(currentEntry);
}
// Handle scratch pad
await createScratch(scratchContent);
if (previousNote != null) {
previousNote!.content = previousEntry;
await updateNote(previousNote!);
// Handle previous/currently displayed note
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 =
@@ -716,9 +749,15 @@ class MainPageState extends State<MainPage> with WindowListener {
try {
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
dialogSetState(() {
_searchResults = results;
_searchResults = filteredResults;
_isSearching = false;
});
} catch (e) {
@@ -787,7 +826,18 @@ class MainPageState extends State<MainPage> with WindowListener {
), // Smaller font for content
),
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();
this.setState(() {
_currentlyDisplayedNote = note;
@@ -1027,9 +1077,7 @@ class MainPageState extends State<MainPage> with WindowListener {
Expanded(
child: TextField(
controller: _previousEntryController,
readOnly:
_currentlyDisplayedNote?.date !=
previousNote?.date,
readOnly: false, // Always allow editing
maxLines: null,
expands: true,
style: Theme.of(context).textTheme.bodyMedium,
@@ -1037,7 +1085,7 @@ class MainPageState extends State<MainPage> with WindowListener {
hintText:
_currentlyDisplayedNote?.date !=
previousNote?.date
? 'Viewing note from ${_currentlyDisplayedNote?.date} (Read-Only)'
? 'Viewing note from ${_currentlyDisplayedNote?.date} (Editable)'
: 'Latest Note',
border: const OutlineInputBorder(),
filled: