Implement scrolling through previous notes

This commit is contained in:
2025-04-22 00:35:03 +02:00
parent 900bcd866c
commit 5a27ac75c7
2 changed files with 164 additions and 26 deletions

View File

@@ -6,6 +6,7 @@ import 'package:journaler/notes.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';
import 'package:flutter/gestures.dart';
// TODO: Add an icon to the executable, simply use the existing tray icon // TODO: Add an icon to the executable, simply use the existing tray icon
// TODO: Implement some sort of scroll through notes // TODO: Implement some sort of scroll through notes
@@ -137,9 +138,13 @@ class MainPageState extends State<MainPage> with WindowListener {
final TextEditingController _soundController = TextEditingController(); final TextEditingController _soundController = TextEditingController();
Note? previousNote; Note? previousNote;
Note? _currentlyDisplayedNote;
Duration _currentPopupInterval = _defaultPopupInterval; Duration _currentPopupInterval = _defaultPopupInterval;
String _currentNotificationSound = _defaultNotificationSound; String _currentNotificationSound = _defaultNotificationSound;
bool _canGoPrevious = false;
bool _canGoNext = false;
Timer? _popupTimer; Timer? _popupTimer;
Timer? _debounceTimer; Timer? _debounceTimer;
@@ -237,6 +242,50 @@ class MainPageState extends State<MainPage> with WindowListener {
} }
} }
Future<void> _checkNavigation() async {
if (_currentlyDisplayedNote == null) {
setState(() {
_canGoPrevious = false;
_canGoNext = false;
});
return;
}
final prev = await getPreviousNote(_currentlyDisplayedNote!.date);
final bool isLatest = _currentlyDisplayedNote!.date == previousNote?.date;
setState(() {
_canGoPrevious = prev != null;
_canGoNext = !isLatest;
});
}
Future<void> _goToPreviousNote() async {
if (!_canGoPrevious || _currentlyDisplayedNote == null) return;
final prevNote = await getPreviousNote(_currentlyDisplayedNote!.date);
if (prevNote != null) {
setState(() {
_currentlyDisplayedNote = prevNote;
_previousEntryController.text = prevNote.content;
});
await _checkNavigation();
}
}
Future<void> _goToNextNote() async {
if (!_canGoNext || _currentlyDisplayedNote == null) return;
final nextNote = await getNextNote(_currentlyDisplayedNote!.date);
if (nextNote != null) {
setState(() {
_currentlyDisplayedNote = nextNote;
_previousEntryController.text = nextNote.content;
});
await _checkNavigation();
}
}
void _loadData() async { void _loadData() async {
String? intervalMinutesStr = await DB.getSetting('popupIntervalMinutes'); String? intervalMinutesStr = await DB.getSetting('popupIntervalMinutes');
String? soundFileStr = await DB.getSetting('notificationSound'); String? soundFileStr = await DB.getSetting('notificationSound');
@@ -252,12 +301,15 @@ class MainPageState extends State<MainPage> with WindowListener {
final note = await getLatestNote(); final note = await getLatestNote();
previousNote = note; previousNote = note;
_previousEntryController.text = note?.content ?? ""; _currentlyDisplayedNote = note;
_previousEntryController.text = _currentlyDisplayedNote?.content ?? "";
final scratch = await getLatestScratch(); final scratch = await getLatestScratch();
_scratchController.text = scratch?.content ?? ""; _scratchController.text = scratch?.content ?? "";
_currentEntryController.text = ""; _currentEntryController.text = "";
await _checkNavigation();
debugPrint("Data loaded."); debugPrint("Data loaded.");
} }
@@ -391,35 +443,82 @@ class MainPageState extends State<MainPage> with WindowListener {
children: [ children: [
Expanded( Expanded(
flex: 9, flex: 9,
child: Column( child: Listener(
crossAxisAlignment: CrossAxisAlignment.stretch, behavior: HitTestBehavior.opaque,
children: [ onPointerSignal: (pointerSignal) {
Expanded( if (pointerSignal is PointerScrollEvent) {
child: TextField( if (pointerSignal.scrollDelta.dy < 0) {
controller: _previousEntryController, if (_canGoPrevious) {
maxLines: null, _goToPreviousNote();
expands: true, }
style: Theme.of(context).textTheme.bodyMedium, } else if (pointerSignal.scrollDelta.dy > 0) {
decoration: const InputDecoration( if (_canGoNext) {
labelText: 'Previous Entry', _goToNextNote();
}
}
}
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Combine Label, Buttons, and TextField for Previous Entry
Row(
children: [
Expanded(
child: Text(
_currentlyDisplayedNote?.date == previousNote?.date
? 'Previous Entry (Latest)'
: 'Entry: ${_currentlyDisplayedNote?.date ?? 'N/A'}',
style: TextStyle(fontSize: 18, color: Colors.grey),
),
),
IconButton(
icon: const Icon(Icons.arrow_back),
tooltip: 'Previous Note',
onPressed: _canGoPrevious ? _goToPreviousNote : null,
),
IconButton(
icon: const Icon(Icons.arrow_forward),
tooltip: 'Next Note',
onPressed: _canGoNext ? _goToNextNote : null,
),
],
),
Expanded(
child: TextField(
controller: _previousEntryController,
readOnly: _currentlyDisplayedNote?.date != previousNote?.date,
maxLines: null,
expands: true,
style: Theme.of(context).textTheme.bodyMedium,
decoration: InputDecoration(
hintText: _currentlyDisplayedNote?.date != previousNote?.date
? 'Viewing note from ${_currentlyDisplayedNote?.date} (Read-Only)'
: 'Latest Note',
border: const OutlineInputBorder(),
filled: _currentlyDisplayedNote?.date != previousNote?.date,
fillColor: _currentlyDisplayedNote?.date != previousNote?.date
? Colors.grey.withOpacity(0.1)
: null,
),
), ),
), ),
), const SizedBox(height: 8),
const SizedBox(height: 8), Expanded(
Expanded( child: TextField(
child: TextField( controller: _currentEntryController,
controller: _currentEntryController, focusNode: _currentEntryFocusNode,
focusNode: _currentEntryFocusNode, maxLines: null,
maxLines: null, expands: true,
expands: true, autofocus: true,
autofocus: true, style: Theme.of(context).textTheme.bodyMedium,
style: Theme.of(context).textTheme.bodyMedium, decoration: const InputDecoration(
decoration: const InputDecoration( labelText: 'Current Entry (What\'s on your mind?)',
labelText: 'Current Entry (What\'s on your mind?)', ),
), ),
), ),
), ],
], ),
), ),
), ),
const SizedBox(width: 8), const SizedBox(width: 8),

View File

@@ -61,3 +61,42 @@ Future<Scratch?> getLatestScratch() async {
Future<void> createScratch(String content) async { Future<void> createScratch(String content) async {
await DB.db.insert('scratches', {'content': content}); await DB.db.insert('scratches', {'content': content});
} }
// Get the note immediately older than the given date
Future<Note?> getPreviousNote(String currentDate) async {
final List<Map<String, dynamic>> notes = await DB.db.query(
'notes',
where: 'date < ?',
whereArgs: [currentDate],
orderBy: 'date DESC',
limit: 1,
);
if (notes.isNotEmpty) {
return Note(
date: notes.first['date'] as String,
content: notes.first['content'] as String,
);
}
return null;
}
// Get the note immediately newer than the given date
Future<Note?> getNextNote(String currentDate) async {
final List<Map<String, dynamic>> notes = await DB.db.query(
'notes',
where: 'date > ?',
whereArgs: [currentDate],
orderBy: 'date ASC',
limit: 1,
);
if (notes.isNotEmpty) {
return Note(
date: notes.first['date'] as String,
content: notes.first['content'] as String,
);
}
// If there's no newer note, it means we might be at the latest
// but let's double-check by explicitly getting the latest again.
// This handles the case where the `currentDate` might not be the absolute latest.
return getLatestNote();
}