diff --git a/lib/canvas/drawing_page.dart b/lib/canvas/drawing_page.dart index 5465646..d688e4d 100644 --- a/lib/canvas/drawing_page.dart +++ b/lib/canvas/drawing_page.dart @@ -39,6 +39,7 @@ class _DrawingPageState extends State { super.initState(); controller = PaintController(noteFile); + debugPrint('initializing strokes from file'); noteFile.init().then((value) => controller.loadStrokesFromFile()); // todo might be weird behaviour if used with short side diff --git a/lib/canvas/paint_controller.dart b/lib/canvas/paint_controller.dart index 3fb4964..706e69b 100644 --- a/lib/canvas/paint_controller.dart +++ b/lib/canvas/paint_controller.dart @@ -54,7 +54,7 @@ class PaintController extends ChangeNotifier { } 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 if (!a4Page.contains(offset)) return; @@ -69,31 +69,31 @@ class PaintController extends ChangeNotifier { ? const Color(0xFF444444) : Colors.yellow.withOpacity(.5))); file.addStroke(strokeid); - notifyListeners(); } } void pointUpEvent(PointerUpEvent e) { if (activePen == Pen.eraser) return; - - if (_allowedToDraw(e)) { + if (_allowedToDraw(e.kind, 1)) { final lastStroke = strokes.last; if (lastStroke.points.length <= 1) { // 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); file.addPoint(lastStroke.id, lastStroke.points.last); notifyListeners(); + } else { + debugPrint('adding points to db'); + file.addPoints(lastStroke.id, lastStroke.points); } } } /// check if pointer event is allowed to draw points - bool _allowedToDraw(PointerEvent event) { - return (_allowDrawWithFinger && event.kind == PointerDeviceKind.touch) || - event.kind == PointerDeviceKind.stylus || - (event.kind == PointerDeviceKind.mouse && - event.buttons == kPrimaryMouseButton); + bool _allowedToDraw(PointerDeviceKind kind, int button) { + return (_allowDrawWithFinger && kind == PointerDeviceKind.touch) || + kind == PointerDeviceKind.stylus || + (kind == PointerDeviceKind.mouse && button == kPrimaryMouseButton); } void pointMoveEvent(Offset offset, PointerMoveEvent event) { @@ -101,7 +101,7 @@ class PaintController extends ChangeNotifier { return; } - if (_allowedToDraw(event)) { + if (_allowedToDraw(event.kind, event.buttons)) { switch (activePen) { case Pen.eraser: // todo dynamic eraser size @@ -128,16 +128,14 @@ class PaintController extends ChangeNotifier { double newWidth = _calcTiltedWidth(5.0, event.tilt); if (pts.length > 1) { - newWidth = _calcAngleDependentWidth( - pts.last, - pts[pts.length - (pts.length > 5 ? 5 : pts.length - 1)], - newWidth); + newWidth = _calcAngleDependentWidth(pts.last, + pts[pts.length - (pts.length > 5 ? 5 : pts.length)], newWidth); } Point p = Point(offset, newWidth); strokes.last.addPoint(p); - // todo do a batch commit per stroke - file.addPoint(strokes.last.id, p); + // // todo do a batch commit per stroke + // file.addPoint(strokes.last.id, p); break; case Pen.selector: // TODO: Handle this case. @@ -149,6 +147,7 @@ class PaintController extends ChangeNotifier { Future loadStrokesFromFile() async { strokes = await file.loadStrokes(); + debugPrint('finished loading strokes from file'); notifyListeners(); } } diff --git a/lib/context/file_change_notifier.dart b/lib/context/file_change_notifier.dart index 84c5aba..86512af 100644 --- a/lib/context/file_change_notifier.dart +++ b/lib/context/file_change_notifier.dart @@ -26,7 +26,7 @@ class FileChangeNotifier extends ChangeNotifier { return NoteTileData(name, filename, lastmodified); }).toList(); dta.sort( - (a, b) => a.lastModified.compareTo(b.lastModified), + (a, b) => b.lastModified.compareTo(a.lastModified), ); notifyListeners(); tiledata = dta; diff --git a/lib/savesystem/line_loader.dart b/lib/savesystem/line_loader.dart index 34205f6..b8f295a 100644 --- a/lib/savesystem/line_loader.dart +++ b/lib/savesystem/line_loader.dart @@ -40,8 +40,23 @@ extension LineLoading on NoteFile { }); } + Future addPoints(int strokeid, List 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 removeStroke(int id) async { - await db().delete('strokes', where: 'id = $id'); - await db().delete('points', where: 'strokeid = $id'); + final batch = db().batch(); + batch.delete('strokes', where: 'id = $id'); + batch.delete('points', where: 'strokeid = $id'); + await batch.commit(); } }