2022-10-29 19:39:08 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-11-03 10:22:38 +00:00
|
|
|
import 'package:flutter/widgets.dart';
|
2022-10-29 18:06:20 +00:00
|
|
|
import 'package:sqflite/sqflite.dart';
|
|
|
|
|
2022-10-30 20:57:13 +00:00
|
|
|
import 'path.dart';
|
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
class NoteFile {
|
|
|
|
late Database _db;
|
2022-10-29 19:39:08 +00:00
|
|
|
String filepath;
|
2022-10-29 18:06:20 +00:00
|
|
|
|
|
|
|
Database db() {
|
|
|
|
return _db;
|
|
|
|
}
|
|
|
|
|
2022-10-29 19:39:08 +00:00
|
|
|
NoteFile(this.filepath);
|
2022-10-29 18:06:20 +00:00
|
|
|
|
|
|
|
Future<void> init() async {
|
2022-10-29 19:39:08 +00:00
|
|
|
final path = (await getSavePath()).path + Platform.pathSeparator + filepath;
|
2022-10-29 18:06:20 +00:00
|
|
|
_db = await openDatabase(
|
2022-10-29 19:39:08 +00:00
|
|
|
path,
|
2022-10-31 18:29:58 +00:00
|
|
|
onCreate: (db, version) async {
|
2022-11-03 10:22:38 +00:00
|
|
|
final batch = db.batch();
|
2022-10-31 18:29:58 +00:00
|
|
|
|
|
|
|
batch.execute('DROP TABLE IF EXISTS strokes;');
|
|
|
|
batch.execute('DROP TABLE IF EXISTS points;');
|
|
|
|
|
|
|
|
batch.execute(
|
|
|
|
'CREATE TABLE strokes(id integer primary key autoincrement, color INTEGER, elevation INTEGER)');
|
|
|
|
batch.execute(
|
|
|
|
'CREATE TABLE points(id integer primary key autoincrement, x INTEGER, y INTEGER, thickness REAL, strokeid INTEGER)');
|
|
|
|
await batch.commit();
|
|
|
|
return;
|
2022-10-29 18:06:20 +00:00
|
|
|
},
|
|
|
|
// Set the version. This executes the onCreate function and provides a
|
|
|
|
// path to perform database upgrades and downgrades.
|
|
|
|
version: 1,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void delete() {
|
|
|
|
// todo remove db file
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> close() async {
|
|
|
|
// shrink the db file size
|
2022-11-03 10:22:38 +00:00
|
|
|
if (_db.isOpen) {
|
|
|
|
await _db.execute('VACUUM');
|
|
|
|
await _db.close();
|
|
|
|
} else {
|
|
|
|
debugPrint('db file unexpectedly closed before shrinking');
|
|
|
|
}
|
2022-10-29 18:06:20 +00:00
|
|
|
}
|
|
|
|
}
|