Files
notas/lib/data/app_database.dart
T
Marcos 96f8f95924 Add Windows runner files for high DPI support and console output
- Created runner.exe.manifest to enable DPI awareness and dark mode support.
- Implemented utility functions in utils.cpp and utils.h for console creation and command line argument handling.
- Developed Win32Window class in win32_window.cpp and win32_window.h to manage high DPI-aware windows, including theme updates and message handling.
2026-05-13 12:03:40 +02:00

104 lines
2.7 KiB
Dart

import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';
part 'app_database.g.dart';
@DataClassName('DbNote')
class Notes extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text()();
TextColumn get body => text()();
DateTimeColumn get createdAt => dateTime()();
DateTimeColumn get updatedAt => dateTime()();
IntColumn get sortIndex => integer().named('sort_index')();
}
@DriftDatabase(tables: [Notes])
class AppDatabase extends _$AppDatabase {
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
Future<List<DbNote>> getAllNotes() {
return (select(notes)..orderBy([
(note) => OrderingTerm(expression: note.sortIndex),
])).get();
}
Future<int> insertNoteAtTop(NotesCompanion note) {
return transaction(() async {
await customStatement('UPDATE notes SET sort_index = sort_index + 1');
return into(notes).insert(note.copyWith(sortIndex: const Value<int>(0)));
});
}
Future<void> replaceAllNotes(List<NotesCompanion> noteList) {
return transaction(() async {
await delete(notes).go();
for (final NotesCompanion note in noteList) {
await into(notes).insert(note);
}
});
}
Future<void> updateNoteRow(DbNote note) {
return update(notes).replace(note);
}
Future<void> deleteNoteAndShift({
required int id,
required int removedIndex,
}) {
return transaction(() async {
await (delete(notes)..where((note) => note.id.equals(id))).go();
await customStatement(
'UPDATE notes SET sort_index = sort_index - 1 WHERE sort_index > ?',
[removedIndex],
);
});
}
Future<void> moveNote({
required int id,
required int oldIndex,
required int newIndex,
}) {
if (oldIndex == newIndex) {
return Future<void>.value();
}
return transaction(() async {
if (oldIndex < newIndex) {
await customStatement(
'UPDATE notes SET sort_index = sort_index - 1 WHERE sort_index > ? AND sort_index <= ?',
[oldIndex, newIndex],
);
} else {
await customStatement(
'UPDATE notes SET sort_index = sort_index + 1 WHERE sort_index >= ? AND sort_index < ?',
[newIndex, oldIndex],
);
}
await customStatement(
'UPDATE notes SET sort_index = ? WHERE id = ?',
[newIndex, id],
);
});
}
}
LazyDatabase _openConnection() {
return LazyDatabase(() async {
final Directory supportDir = await getApplicationSupportDirectory();
final File file = File(p.join(supportDir.path, 'notes.sqlite'));
return NativeDatabase(file);
});
}