add saving system with sqlite
This commit is contained in:
47
lib/savesystem/line_loader.dart
Normal file
47
lib/savesystem/line_loader.dart
Normal file
@ -0,0 +1,47 @@
|
||||
import 'dart:ui';
|
||||
|
||||
import 'note_file.dart';
|
||||
import '../canvas/document_types.dart';
|
||||
|
||||
extension LineLoading on NoteFile {
|
||||
Future<List<Stroke>> loadStrokes() async {
|
||||
final query = await db().query('points',
|
||||
orderBy: 'strokeid',
|
||||
columns: ['x', 'y', 'thickness', 'strokeid', 'id']);
|
||||
int strokeid = -1;
|
||||
List<Stroke> strokes = [];
|
||||
|
||||
for (final i in query) {
|
||||
final int csid = i['strokeid'] as int;
|
||||
if (csid != strokeid) {
|
||||
strokeid = csid;
|
||||
strokes.add(Stroke(strokeid));
|
||||
}
|
||||
final Point p = Point(
|
||||
Offset(i['x'] as double, i['y'] as double), i['thickness'] as double);
|
||||
strokes.last.points.add(p);
|
||||
}
|
||||
|
||||
return strokes;
|
||||
}
|
||||
|
||||
// create new stroke in file and return strokeid
|
||||
Future<void> addStroke(int newStrokeId) async {
|
||||
await db().insert(
|
||||
'strokes', {'color': 0xffffff, 'elevation': 0, 'id': newStrokeId});
|
||||
}
|
||||
|
||||
Future<void> addPoint(int strokeid, Point p) async {
|
||||
await db().insert('points', {
|
||||
'x': p.point.dx,
|
||||
'y': p.point.dy,
|
||||
'thickness': p.thickness,
|
||||
'strokeid': strokeid
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> removeStroke(int id) async {
|
||||
await db().delete('strokes', where: 'id = $id');
|
||||
await db().delete('points', where: 'strokeid = $id');
|
||||
}
|
||||
}
|
48
lib/savesystem/note_file.dart
Normal file
48
lib/savesystem/note_file.dart
Normal file
@ -0,0 +1,48 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
|
||||
|
||||
class NoteFile {
|
||||
late Database _db;
|
||||
String filename;
|
||||
|
||||
Database db() {
|
||||
return _db;
|
||||
}
|
||||
|
||||
NoteFile(this.filename);
|
||||
|
||||
Future<void> init() async {
|
||||
String dbpath = filename;
|
||||
if (defaultTargetPlatform == TargetPlatform.android ||
|
||||
defaultTargetPlatform == TargetPlatform.iOS) {
|
||||
dbpath = '${await getDatabasesPath()}/$filename';
|
||||
} else {
|
||||
// Change the default factory
|
||||
databaseFactory = databaseFactoryFfi;
|
||||
}
|
||||
|
||||
_db = await openDatabase(
|
||||
dbpath,
|
||||
onCreate: (db, version) {
|
||||
return db.execute(
|
||||
'CREATE TABLE strokes(id integer primary key autoincrement, color INTEGER, elevation INTEGER);'
|
||||
'CREATE TABLE points(id integer primary key autoincrement, x INTEGER, y INTEGER, thickness REAL, strokeid INTEGER)',
|
||||
);
|
||||
},
|
||||
// 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
|
||||
await _db.execute('VACUUM');
|
||||
_db.close();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user