batch insert points to db file when stroke finished

This commit is contained in:
lukas-heiligenbrunner 2022-11-03 12:13:36 +01:00
parent 348886b5bb
commit 9be2c69ff5
4 changed files with 34 additions and 19 deletions

View File

@ -39,6 +39,7 @@ class _DrawingPageState extends State<DrawingPage> {
super.initState(); super.initState();
controller = PaintController(noteFile); controller = PaintController(noteFile);
debugPrint('initializing strokes from file');
noteFile.init().then((value) => controller.loadStrokesFromFile()); noteFile.init().then((value) => controller.loadStrokesFromFile());
// todo might be weird behaviour if used with short side // todo might be weird behaviour if used with short side

View File

@ -54,7 +54,7 @@ class PaintController extends ChangeNotifier {
} }
void pointDownEvent(Offset offset, PointerDownEvent e) async { void pointDownEvent(Offset offset, PointerDownEvent e) async {
if (_allowedToDraw(e)) { if (_allowedToDraw(e.kind, e.buttons)) {
// todo line drawn on edge where line left page // todo line drawn on edge where line left page
if (!a4Page.contains(offset)) return; if (!a4Page.contains(offset)) return;
@ -69,31 +69,31 @@ class PaintController extends ChangeNotifier {
? const Color(0xFF444444) ? const Color(0xFF444444)
: Colors.yellow.withOpacity(.5))); : Colors.yellow.withOpacity(.5)));
file.addStroke(strokeid); file.addStroke(strokeid);
notifyListeners(); notifyListeners();
} }
} }
void pointUpEvent(PointerUpEvent e) { void pointUpEvent(PointerUpEvent e) {
if (activePen == Pen.eraser) return; if (activePen == Pen.eraser) return;
if (_allowedToDraw(e.kind, 1)) {
if (_allowedToDraw(e)) {
final lastStroke = strokes.last; final lastStroke = strokes.last;
if (lastStroke.points.length <= 1) { if (lastStroke.points.length <= 1) {
// if the line consists only of one point (point) add endpoint as the same to allow drawing a line // if the line consists only of one point (point) add endpoint as the same to allow drawing a line
lastStroke.points.add(lastStroke.points.last); lastStroke.points.add(lastStroke.points.last);
file.addPoint(lastStroke.id, lastStroke.points.last); file.addPoint(lastStroke.id, lastStroke.points.last);
notifyListeners(); notifyListeners();
} else {
debugPrint('adding points to db');
file.addPoints(lastStroke.id, lastStroke.points);
} }
} }
} }
/// check if pointer event is allowed to draw points /// check if pointer event is allowed to draw points
bool _allowedToDraw(PointerEvent event) { bool _allowedToDraw(PointerDeviceKind kind, int button) {
return (_allowDrawWithFinger && event.kind == PointerDeviceKind.touch) || return (_allowDrawWithFinger && kind == PointerDeviceKind.touch) ||
event.kind == PointerDeviceKind.stylus || kind == PointerDeviceKind.stylus ||
(event.kind == PointerDeviceKind.mouse && (kind == PointerDeviceKind.mouse && button == kPrimaryMouseButton);
event.buttons == kPrimaryMouseButton);
} }
void pointMoveEvent(Offset offset, PointerMoveEvent event) { void pointMoveEvent(Offset offset, PointerMoveEvent event) {
@ -101,7 +101,7 @@ class PaintController extends ChangeNotifier {
return; return;
} }
if (_allowedToDraw(event)) { if (_allowedToDraw(event.kind, event.buttons)) {
switch (activePen) { switch (activePen) {
case Pen.eraser: case Pen.eraser:
// todo dynamic eraser size // todo dynamic eraser size
@ -128,16 +128,14 @@ class PaintController extends ChangeNotifier {
double newWidth = _calcTiltedWidth(5.0, event.tilt); double newWidth = _calcTiltedWidth(5.0, event.tilt);
if (pts.length > 1) { if (pts.length > 1) {
newWidth = _calcAngleDependentWidth( newWidth = _calcAngleDependentWidth(pts.last,
pts.last, pts[pts.length - (pts.length > 5 ? 5 : pts.length)], newWidth);
pts[pts.length - (pts.length > 5 ? 5 : pts.length - 1)],
newWidth);
} }
Point p = Point(offset, newWidth); Point p = Point(offset, newWidth);
strokes.last.addPoint(p); strokes.last.addPoint(p);
// todo do a batch commit per stroke // // todo do a batch commit per stroke
file.addPoint(strokes.last.id, p); // file.addPoint(strokes.last.id, p);
break; break;
case Pen.selector: case Pen.selector:
// TODO: Handle this case. // TODO: Handle this case.
@ -149,6 +147,7 @@ class PaintController extends ChangeNotifier {
Future<void> loadStrokesFromFile() async { Future<void> loadStrokesFromFile() async {
strokes = await file.loadStrokes(); strokes = await file.loadStrokes();
debugPrint('finished loading strokes from file');
notifyListeners(); notifyListeners();
} }
} }

View File

@ -26,7 +26,7 @@ class FileChangeNotifier extends ChangeNotifier {
return NoteTileData(name, filename, lastmodified); return NoteTileData(name, filename, lastmodified);
}).toList(); }).toList();
dta.sort( dta.sort(
(a, b) => a.lastModified.compareTo(b.lastModified), (a, b) => b.lastModified.compareTo(a.lastModified),
); );
notifyListeners(); notifyListeners();
tiledata = dta; tiledata = dta;

View File

@ -40,8 +40,23 @@ extension LineLoading on NoteFile {
}); });
} }
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();
}
Future<void> removeStroke(int id) async { Future<void> removeStroke(int id) async {
await db().delete('strokes', where: 'id = $id'); final batch = db().batch();
await db().delete('points', where: 'strokeid = $id'); batch.delete('strokes', where: 'id = $id');
batch.delete('points', where: 'strokeid = $id');
await batch.commit();
} }
} }