3 Commits

2 changed files with 78 additions and 50 deletions

View File

@@ -507,8 +507,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!);
if (_currentlyDisplayedNote!.content != _previousEntryController.text) {
_currentlyDisplayedNote!.content = _previousEntryController.text;
await updateNote(_currentlyDisplayedNote!);
}
}
final prevNote = await getPreviousTo(_currentlyDisplayedNote!.epochTime);
@@ -526,8 +528,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!);
if (_currentlyDisplayedNote!.content != _previousEntryController.text) {
_currentlyDisplayedNote!.content = _previousEntryController.text;
await updateNote(_currentlyDisplayedNote!);
}
}
final nextNote = await getNextTo(_currentlyDisplayedNote!.epochTime);
@@ -593,13 +597,17 @@ class MainPageState extends State<MainPage> with WindowListener {
_currentEntryController.clear(); // Clear the input field after saving
}
// Handle scratch pad
await createScratch(scratchContent);
if (scratchContent != _scratchController.text) {
// Was modified
await createScratch(scratchContent);
}
// Handle previous/currently displayed note
if (_currentlyDisplayedNote != null) {
_currentlyDisplayedNote!.content = previousEntry;
await updateNote(_currentlyDisplayedNote!);
if (_currentlyDisplayedNote!.content != previousEntry) {
_currentlyDisplayedNote!.content = previousEntry;
await updateNote(_currentlyDisplayedNote!);
}
// If the note was deleted (due to being empty), update the UI state
if (previousEntry.isEmpty) {
@@ -662,9 +670,7 @@ class MainPageState extends State<MainPage> with WindowListener {
spans.add(
TextSpan(
text: highlightedText.substring(lastIndex, match.start),
style: const TextStyle(
fontSize: 13,
),
style: const TextStyle(fontSize: 13),
),
);
}
@@ -825,11 +831,18 @@ class MainPageState extends State<MainPage> with WindowListener {
// Save current note if needed
if (_currentlyDisplayedNote !=
null) {
_currentlyDisplayedNote!.content =
_previousEntryController.text;
await updateNote(
_currentlyDisplayedNote!,
);
if (_currentlyDisplayedNote!
.content !=
_previousEntryController
.text) {
_currentlyDisplayedNote!
.content =
_previousEntryController
.text;
await updateNote(
_currentlyDisplayedNote!,
);
}
}
// Navigate to the selected note
@@ -1107,19 +1120,23 @@ class MainPageState extends State<MainPage> with WindowListener {
String? errorMessage;
// Load current values
getMeilisearchEndpoint().then((value) {
endpointController.text = value;
isLoading = false;
}).catchError((e) {
errorMessage = 'Failed to load endpoint: $e';
isLoading = false;
});
getMeilisearchEndpoint()
.then((value) {
endpointController.text = value;
isLoading = false;
})
.catchError((e) {
errorMessage = 'Failed to load endpoint: $e';
isLoading = false;
});
getMeilisearchApiKey().then((value) {
apiKeyController.text = value;
}).catchError((e) {
errorMessage = 'Failed to load API key: $e';
});
getMeilisearchApiKey()
.then((value) {
apiKeyController.text = value;
})
.catchError((e) {
errorMessage = 'Failed to load API key: $e';
});
showDialog(
context: context,
@@ -1168,29 +1185,34 @@ class MainPageState extends State<MainPage> with WindowListener {
child: const Text('Cancel'),
),
TextButton(
onPressed: isLoading ? null : () async {
try {
setState(() {
isLoading = true;
errorMessage = null;
});
onPressed:
isLoading
? null
: () async {
try {
setState(() {
isLoading = true;
errorMessage = null;
});
await setMeilisearchEndpoint(endpointController.text);
await setMeilisearchApiKey(apiKeyController.text);
await setMeilisearchEndpoint(
endpointController.text,
);
await setMeilisearchApiKey(apiKeyController.text);
// Try to reinitialize Meilisearch with new settings
await init();
// Try to reinitialize Meilisearch with new settings
await init();
if (mounted) {
Navigator.of(context).pop();
}
} catch (e) {
setState(() {
errorMessage = 'Failed to save settings: $e';
isLoading = false;
});
}
},
if (mounted) {
Navigator.of(context).pop();
}
} catch (e) {
setState(() {
errorMessage = 'Failed to save settings: $e';
isLoading = false;
});
}
},
child: const Text('Save'),
),
],

View File

@@ -345,11 +345,13 @@ Future<Note> createNote(String content) async {
final mostFrequentLetter =
letterFrequency.entries.reduce((a, b) => a.value > b.value ? a : b).key;
final mostFrequentLetterCount = letterFrequency[mostFrequentLetter];
final mostFrequentLetterCount =
letterFrequency[mostFrequentLetter]! / trimmedContent.length;
final document = {
'id': generateRandomString(32),
'date': DateTime.now().toUtc().millisecondsSinceEpoch,
'dateISO': DateTime.now().toUtc().toIso8601String(),
'content': content,
'topLetter': mostFrequentLetter,
'topLetterFrequency': mostFrequentLetterCount,
@@ -433,6 +435,10 @@ Future<void> updateNote(Note note) async {
'id': note.id,
'content': trimmedContent,
'date': note.epochTime,
'dateISO':
DateTime.fromMillisecondsSinceEpoch(
note.epochTime,
).toUtc().toIso8601String(),
'topLetter': mostFrequentLetter,
'topLetterFrequency': mostFrequentLetterRatio,
};