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

View File

@@ -17,6 +17,7 @@ const header = {
'Authorization': 'Bearer $apiKey', 'Authorization': 'Bearer $apiKey',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}; };
final alphanum = RegExp(r'[a-zA-Z0-9]', caseSensitive: false);
class MeilisearchQuery { class MeilisearchQuery {
String q; String q;
@@ -63,12 +64,12 @@ class MeilisearchQuery {
} }
class MeilisearchResponse { class MeilisearchResponse {
final List<Map<String, dynamic>> hits; final List<dynamic> hits;
final String query; final String query;
final int processingTimeMs; final int processingTimeMs;
final int limit; final int limit;
final int offset; final int offset;
final double estimatedTotalHits; final int estimatedTotalHits;
MeilisearchResponse({ MeilisearchResponse({
required this.hits, required this.hits,
@@ -98,14 +99,17 @@ Future<void> init() async {
headers: header, headers: header,
body: jsonEncode({'uid': noteIndex, 'primaryKey': 'id'}), body: jsonEncode({'uid': noteIndex, 'primaryKey': 'id'}),
); );
await http.put(
Uri.parse('$endpoint/indexes/$noteIndex/settings/sortable-attributes'),
headers: header,
body: jsonEncode({
'attributes': ['date'],
}),
);
} }
await http.put(
Uri.parse('$endpoint/indexes/$noteIndex/settings/sortable-attributes'),
headers: header,
body: jsonEncode(['date']),
);
await http.put(
Uri.parse('$endpoint/indexes/$noteIndex/settings/filterable-attributes'),
headers: header,
body: jsonEncode(['date', 'topLetter', 'topLetterFrequency']),
);
if (!await indexExists(scratchIndex)) { if (!await indexExists(scratchIndex)) {
await http.post( await http.post(
@@ -113,14 +117,17 @@ Future<void> init() async {
headers: header, headers: header,
body: jsonEncode({'uid': scratchIndex, 'primaryKey': 'id'}), body: jsonEncode({'uid': scratchIndex, 'primaryKey': 'id'}),
); );
await http.put(
Uri.parse('$endpoint/indexes/$scratchIndex/settings/sortable-attributes'),
headers: header,
body: jsonEncode({
'attributes': ['date'],
}),
);
} }
await http.put(
Uri.parse('$endpoint/indexes/$scratchIndex/settings/sortable-attributes'),
headers: header,
body: jsonEncode(['date']),
);
await http.put(
Uri.parse('$endpoint/indexes/$scratchIndex/settings/filterable-attributes'),
headers: header,
body: jsonEncode(['date']),
);
if (!await indexExists(settingsIndex)) { if (!await indexExists(settingsIndex)) {
await http.post( await http.post(
@@ -193,18 +200,18 @@ Future<List<Note>> searchNotes(String query) async {
.map( .map(
(hit) => Note( (hit) => Note(
id: hit['id'] as String, id: hit['id'] as String,
date: hit['date'] as String, epochTime: hit['date'] as int,
content: hit['content'] as String, content: hit['content'] as String,
), ),
) )
.toList(); .toList();
} }
Future<Note?> getPreviousTo(String date) async { Future<Note?> getPreviousTo(int epochTime) async {
final searchCondition = MeilisearchQuery( final searchCondition = MeilisearchQuery(
q: '', q: '',
filter: 'date < $date', filter: 'date < $epochTime',
sort: ['date DESC'], sort: ['date:desc'],
limit: 1, limit: 1,
); );
final response = await http.post( final response = await http.post(
@@ -213,7 +220,9 @@ Future<Note?> getPreviousTo(String date) async {
body: jsonEncode(searchCondition.toJson()), body: jsonEncode(searchCondition.toJson()),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Failed to get previous note'); throw Exception(
'Failed to get previous note, backend responded with ${response.statusCode}',
);
} }
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body)); final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
if (responseJson.hits.isEmpty) { if (responseJson.hits.isEmpty) {
@@ -221,16 +230,16 @@ Future<Note?> getPreviousTo(String date) async {
} }
return Note( return Note(
id: responseJson.hits.first['id'] as String, id: responseJson.hits.first['id'] as String,
date: responseJson.hits.first['date'] as String, epochTime: responseJson.hits.first['date'] as int,
content: responseJson.hits.first['content'] as String, content: responseJson.hits.first['content'] as String,
); );
} }
Future<Note?> getNextTo(String date) async { Future<Note?> getNextTo(int epochTime) async {
final searchCondition = MeilisearchQuery( final searchCondition = MeilisearchQuery(
q: '', q: '',
filter: 'date > $date', filter: 'date > $epochTime',
sort: ['date ASC'], sort: ['date:asc'],
limit: 1, limit: 1,
); );
final response = await http.post( final response = await http.post(
@@ -239,7 +248,9 @@ Future<Note?> getNextTo(String date) async {
body: jsonEncode(searchCondition.toJson()), body: jsonEncode(searchCondition.toJson()),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Failed to get next note'); throw Exception(
'Failed to get next note, backend responded with ${response.statusCode}',
);
} }
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body)); final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
if (responseJson.hits.isEmpty) { if (responseJson.hits.isEmpty) {
@@ -247,7 +258,7 @@ Future<Note?> getNextTo(String date) async {
} }
return Note( return Note(
id: responseJson.hits.first['id'] as String, id: responseJson.hits.first['id'] as String,
date: responseJson.hits.first['date'] as String, epochTime: responseJson.hits.first['date'] as int,
content: responseJson.hits.first['content'] as String, content: responseJson.hits.first['content'] as String,
); );
} }
@@ -264,7 +275,9 @@ Future<Note?> getLatest() async {
body: jsonEncode(searchCondition.toJson()), body: jsonEncode(searchCondition.toJson()),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Failed to get latest note'); throw Exception(
'Failed to get latest note, backend responded with ${response.statusCode}',
);
} }
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body)); final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
if (responseJson.hits.isEmpty) { if (responseJson.hits.isEmpty) {
@@ -272,7 +285,7 @@ Future<Note?> getLatest() async {
} }
return Note( return Note(
id: responseJson.hits.first['id'] as String, id: responseJson.hits.first['id'] as String,
date: responseJson.hits.first['date'] as String, epochTime: responseJson.hits.first['date'] as int,
content: responseJson.hits.first['content'] as String, content: responseJson.hits.first['content'] as String,
); );
} }
@@ -302,7 +315,9 @@ Future<Note> createNote(String content) async {
final letterFrequency = <String, int>{}; final letterFrequency = <String, int>{};
for (final char in trimmedContent.split('')) { for (final char in trimmedContent.split('')) {
letterFrequency[char] = (letterFrequency[char] ?? 0) + 1; if (alphanum.hasMatch(char)) {
letterFrequency[char] = (letterFrequency[char] ?? 0) + 1;
}
} }
final mostFrequentLetter = final mostFrequentLetter =
@@ -311,7 +326,7 @@ Future<Note> createNote(String content) async {
final document = { final document = {
'id': generateRandomString(32), 'id': generateRandomString(32),
'date': DateTime.now().toIso8601String(), 'date': DateTime.now().toUtc().millisecondsSinceEpoch,
'content': content, 'content': content,
'topLetter': mostFrequentLetter, 'topLetter': mostFrequentLetter,
'topLetterFrequency': mostFrequentLetterCount, 'topLetterFrequency': mostFrequentLetterCount,
@@ -326,7 +341,7 @@ Future<Note> createNote(String content) async {
} }
return Note( return Note(
id: document['id'] as String, id: document['id'] as String,
date: document['id'] as String, epochTime: document['date'] as int,
content: document['content'] as String, content: document['content'] as String,
); );
} }
@@ -342,14 +357,16 @@ Future<List<Note>> getProblematic({double threshold = 0.7}) async {
body: jsonEncode(searchCondition.toJson()), body: jsonEncode(searchCondition.toJson()),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Failed to get problematic notes'); throw Exception(
'Failed to get problematic notes, backend responded with ${response.statusCode}',
);
} }
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body)); final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
return responseJson.hits return responseJson.hits
.map( .map(
(hit) => Note( (hit) => Note(
id: hit['id'] as String, id: hit['id'] as String,
date: hit['date'] as String, epochTime: hit['date'] as int,
content: hit['content'] as String, content: hit['content'] as String,
isProblematic: true, isProblematic: true,
problemReason: problemReason:
@@ -359,16 +376,49 @@ Future<List<Note>> getProblematic({double threshold = 0.7}) async {
.toList(); .toList();
} }
Future<void> updateNote(String id, String content) async { // TODO: only update if changed
// TODO: Trim and calculate frequency // How? idk
final document = {'id': id, 'content': content}; Future<void> updateNote(Note note) async {
final lines = note.content.split('\n');
final trimmedLines = <String>[];
for (final line in lines) {
final trimmedContent = line.trim().replaceAll(RegExp(r'\s{2,}'), ' ');
if (trimmedContent.isEmpty) {
continue;
}
trimmedLines.add(trimmedContent);
}
final trimmedContent = trimmedLines.join('\n');
final letterFrequency = <String, int>{};
for (final char in trimmedContent.split('')) {
if (alphanum.hasMatch(char)) {
letterFrequency[char] = (letterFrequency[char] ?? 0) + 1;
}
}
final mostFrequentLetter =
letterFrequency.entries.reduce((a, b) => a.value > b.value ? a : b).key;
final mostFrequentLetterRatio =
letterFrequency[mostFrequentLetter]! / trimmedContent.length;
final document = {
'id': note.id,
'content': trimmedContent,
'date': note.epochTime,
'topLetter': mostFrequentLetter,
'topLetterFrequency': mostFrequentLetterRatio,
};
final response = await http.post( final response = await http.post(
Uri.parse('$endpoint/indexes/$noteIndex/documents'), Uri.parse('$endpoint/indexes/$noteIndex/documents'),
headers: header, headers: header,
body: jsonEncode(document), body: jsonEncode(document),
); );
if (response.statusCode != 200) { if (response.statusCode != 202) {
throw Exception('Failed to update note'); throw Exception(
'Failed to update note, backend responded with ${response.statusCode}',
);
} }
} }
@@ -377,15 +427,17 @@ Future<void> deleteNote(String id) async {
Uri.parse('$endpoint/indexes/$noteIndex/documents/$id'), Uri.parse('$endpoint/indexes/$noteIndex/documents/$id'),
headers: header, headers: header,
); );
if (response.statusCode != 200) { if (response.statusCode != 202) {
throw Exception('Failed to delete note'); throw Exception(
'Failed to delete note, backend responded with ${response.statusCode}',
);
} }
} }
Future<Scratch?> getLatestScratch() async { Future<Scratch?> getLatestScratch() async {
final searchCondition = MeilisearchQuery( final searchCondition = MeilisearchQuery(
q: '', q: '',
sort: ['date DESC'], sort: ['date:desc'],
limit: 1, limit: 1,
); );
final response = await http.post( final response = await http.post(
@@ -394,26 +446,40 @@ Future<Scratch?> getLatestScratch() async {
body: jsonEncode(searchCondition.toJson()), body: jsonEncode(searchCondition.toJson()),
); );
if (response.statusCode != 200) { if (response.statusCode != 200) {
throw Exception('Failed to get latest scratch'); throw Exception(
'Failed to get latest scratch, backend responded with ${response.statusCode}',
);
} }
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body)); final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
if (responseJson.hits.isEmpty) { if (responseJson.hits.isEmpty) {
return null; return null;
} }
return Scratch( return Scratch(
date: responseJson.hits.first['date'] as String, id: responseJson.hits.first['id'] as String,
epochTime: responseJson.hits.first['date'] as int,
content: responseJson.hits.first['content'] as String, content: responseJson.hits.first['content'] as String,
); );
} }
Future<void> createScratch(String content) async { Future<Scratch> createScratch(String content) async {
final document = {'id': DateTime.now().toIso8601String(), 'content': content}; final document = {
'id': generateRandomString(32),
'date': DateTime.now().toUtc().millisecondsSinceEpoch,
'content': content,
};
final response = await http.post( final response = await http.post(
Uri.parse('$endpoint/indexes/$scratchIndex/documents'), Uri.parse('$endpoint/indexes/$scratchIndex/documents'),
headers: header, headers: header,
body: jsonEncode(document), body: jsonEncode(document),
); );
if (response.statusCode != 200) { if (response.statusCode != 202) {
throw Exception('Failed to create scratch'); throw Exception(
'Failed to create scratch, backend responded with ${response.statusCode}',
);
} }
return Scratch(
id: document['id'] as String,
epochTime: document['date'] as int,
content: document['content'] as String,
);
} }

View File

@@ -2,7 +2,7 @@ import 'package:intl/intl.dart';
class Note { class Note {
final String id; final String id;
final String date; final int epochTime;
late final String displayDate; late final String displayDate;
String content; String content;
String? snippet; String? snippet;
@@ -11,21 +11,22 @@ class Note {
Note({ Note({
required this.id, required this.id,
required this.date, required this.epochTime,
required this.content, required this.content,
this.snippet, this.snippet,
this.isProblematic = false, this.isProblematic = false,
this.problemReason = '', this.problemReason = '',
}) { }) {
final dtUtc = DateFormat('yyyy-MM-dd HH:mm:ss').parse(date, true); final dtUtc = DateTime.fromMillisecondsSinceEpoch(epochTime, isUtc: true);
final dtLocal = dtUtc.toLocal(); final dtLocal = dtUtc.toLocal();
displayDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(dtLocal); displayDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(dtLocal);
} }
} }
class Scratch { class Scratch {
final String date; final int epochTime;
final String id;
String content; String content;
Scratch({required this.date, required this.content}); Scratch({required this.id, required this.epochTime, required this.content});
} }

39
lib/utils.dart Normal file
View File

@@ -0,0 +1,39 @@
import 'package:journaler/meilisearch.dart';
Future<double> getVolume() async {
try {
final volumeStr = await getSetting('notificationVolume');
return double.tryParse(volumeStr ?? '0.7') ?? 0.7;
} catch (e) {
return 0.7;
}
}
Future<void> setVolume(double volume) async {
await setSetting('notificationVolume', volume.toString());
}
Future<Duration> getPopupInterval() async {
try {
final intervalStr = await getSetting('popupIntervalMinutes');
return Duration(minutes: int.tryParse(intervalStr ?? '10') ?? 10);
} catch (e) {
return Duration(minutes: 10);
}
}
Future<void> setPopupInterval(Duration interval) async {
await setSetting('popupIntervalMinutes', interval.inMinutes.toString());
}
Future<String> getNotificationSound() async {
try {
return await getSetting('notificationSound') ?? 'MeetTheSniper.mp3';
} catch (e) {
return 'MeetTheSniper.mp3';
}
}
Future<void> setNotificationSound(String sound) async {
await setSetting('notificationSound', sound);
}