2022-10-29 19:39:08 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-29 18:06:20 +00:00
|
|
|
|
|
|
|
import '../canvas/document_types.dart';
|
2022-10-30 20:57:13 +00:00
|
|
|
import 'note_file.dart';
|
2022-10-29 18:06:20 +00:00
|
|
|
|
|
|
|
extension LineLoading on NoteFile {
|
|
|
|
Future<List<Stroke>> loadStrokes() async {
|
2022-11-03 14:12:40 +00:00
|
|
|
const sql = '''
|
|
|
|
SELECT x, y, thickness, strokeid, points.id, s.color
|
|
|
|
from points
|
|
|
|
JOIN strokes s on s.id = strokeid
|
|
|
|
ORDER BY strokeid
|
|
|
|
''';
|
|
|
|
final query = await db().rawQuery(sql);
|
2022-10-29 18:06:20 +00:00
|
|
|
int strokeid = -1;
|
|
|
|
List<Stroke> strokes = [];
|
|
|
|
|
|
|
|
for (final i in query) {
|
|
|
|
final int csid = i['strokeid'] as int;
|
|
|
|
if (csid != strokeid) {
|
|
|
|
strokeid = csid;
|
2022-11-03 14:12:40 +00:00
|
|
|
strokes.add(Stroke(strokeid, Color(i['color'] as int)));
|
2022-10-29 18:06:20 +00:00
|
|
|
}
|
|
|
|
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
|
2022-11-03 14:12:40 +00:00
|
|
|
Future<void> addStroke(int newStrokeId, Color color) async {
|
2022-10-29 18:06:20 +00:00
|
|
|
await db().insert(
|
2022-11-03 14:12:40 +00:00
|
|
|
'strokes', {'color': color.value, 'elevation': 0, 'id': newStrokeId});
|
2022-10-29 18:06:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 14:12:40 +00:00
|
|
|
// add one point to a stroke in db
|
2022-10-29 18:06:20 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-11-03 14:12:40 +00:00
|
|
|
// add list of points to a stroke in db
|
2022-11-03 11:13:36 +00:00
|
|
|
Future<void> addPoints(int strokeid, List<Point> pts) async {
|
|
|
|
final batch = db().batch();
|
|
|
|
for (final p in pts) {
|
|
|
|
batch.insert('points', {
|
|
|
|
'x': p.point.dx,
|
|
|
|
'y': p.point.dy,
|
|
|
|
'thickness': p.thickness,
|
|
|
|
'strokeid': strokeid
|
|
|
|
});
|
|
|
|
}
|
|
|
|
await batch.commit();
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:06:20 +00:00
|
|
|
Future<void> removeStroke(int id) async {
|
2022-11-03 11:13:36 +00:00
|
|
|
final batch = db().batch();
|
|
|
|
batch.delete('strokes', where: 'id = $id');
|
|
|
|
batch.delete('points', where: 'strokeid = $id');
|
|
|
|
await batch.commit();
|
2022-10-29 18:06:20 +00:00
|
|
|
}
|
|
|
|
}
|