add saving system with sqlite

This commit is contained in:
2022-10-29 20:06:20 +02:00
parent 4c54265f89
commit 8ea4dc281e
9 changed files with 309 additions and 33 deletions

View File

@ -8,6 +8,8 @@ class Stroke {
double _miny = double.infinity;
double _maxy = double.negativeInfinity;
final int id;
@override
String toString() {
return 'Stroke{points: $points}';
@ -17,18 +19,22 @@ class Stroke {
points.add(point);
// update bounding rect
if (point.point.dx < _minx) _minx = point.point.dx;
if (point.point.dx > _maxx) _maxx = point.point.dx;
if (point.point.dy < _miny) _miny = point.point.dy;
if (point.point.dy > _maxy) _maxy = point.point.dy;
_updateBoundingRect(point);
}
void _updateBoundingRect(Point p) {
if (p.point.dx < _minx) _minx = p.point.dx;
if (p.point.dx > _maxx) _maxx = p.point.dx;
if (p.point.dy < _miny) _miny = p.point.dy;
if (p.point.dy > _maxy) _maxy = p.point.dy;
}
Rect getBoundingRect() {
return Rect.fromPoints(Offset(_minx, _miny), Offset(_maxx, _maxy));
}
Stroke.fromPoints(this.points);
Stroke();
Stroke.fromPoints(this.points, this.id);
Stroke(this.id);
}
class Point {

View File

@ -2,6 +2,7 @@ import 'dart:math';
import 'dart:ui';
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:notes/savesystem/note_file.dart';
import 'my_painter.dart';
import 'paint_controller.dart';
import 'screen_document_mapping.dart';
@ -11,7 +12,10 @@ import '../tool_bar.dart';
/// Handles input events and draws canvas element
class DrawingPage extends StatefulWidget {
const DrawingPage({Key? key}) : super(key: key);
// path to the .dbnote file
final String filePath;
const DrawingPage({Key? key, required this.filePath}) : super(key: key);
@override
State<DrawingPage> createState() => _DrawingPageState();
@ -22,18 +26,29 @@ class _DrawingPageState extends State<DrawingPage> {
double basezoom = 1.0;
Offset offset = const Offset(.0, .0);
PaintController controller = PaintController();
late PaintController controller;
late NoteFile noteFile = NoteFile(widget.filePath);
@override
void initState() {
super.initState();
controller = PaintController(noteFile);
noteFile.init().then((value) => controller.loadStrokesFromFile());
// todo might be weird behaviour if used with short side
final screenWidth =
(window.physicalSize.longestSide / window.devicePixelRatio);
_calcNewPageOffset(const Offset(.0, .0), screenWidth - 45);
}
@override
void dispose() {
super.dispose();
noteFile.close();
}
@override
Widget build(BuildContext context) {
return Scaffold(

View File

@ -2,7 +2,9 @@ import 'dart:math';
import 'dart:ui';
import 'package:flutter/foundation.dart';
import 'package:notes/savesystem/line_loader.dart';
import '../savesystem/note_file.dart';
import 'document_types.dart';
import 'my_painter.dart';
@ -13,6 +15,10 @@ class PaintController extends ChangeNotifier {
List<Stroke> strokes = [];
final bool _allowDrawWithFinger = false;
PaintController(this.file);
final NoteFile file;
void changePen(Pen pen) {
activePen = pen;
notifyListeners();
@ -40,7 +46,8 @@ class PaintController extends ChangeNotifier {
return thickness;
}
void pointDownEvent(Offset offset, PointerDeviceKind pointer, double tilt) {
void pointDownEvent(
Offset offset, PointerDeviceKind pointer, double tilt) async {
if (_allowDrawWithFinger || pointer != PointerDeviceKind.touch) {
// todo line drawn on edge where line left page
if (!a4Page.contains(offset)) return;
@ -48,8 +55,12 @@ class PaintController extends ChangeNotifier {
// todo handle other pens
if (activePen != Pen.pen) return;
strokes
.add(Stroke.fromPoints([Point(offset, _calcTiltedWidth(3.0, tilt))]));
int strokeid = strokes.isNotEmpty ? strokes.last.id + 1 : 0;
strokes.add(Stroke.fromPoints(
[Point(offset, _calcTiltedWidth(3.0, tilt))],
strokes.isNotEmpty ? strokes.last.id + 1 : 0));
file.addStroke(strokeid);
notifyListeners();
}
}
@ -58,10 +69,11 @@ class PaintController extends ChangeNotifier {
if (activePen == Pen.eraser) return;
if (_allowDrawWithFinger || pointer != PointerDeviceKind.touch) {
if (strokes.last.points.length <= 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
// todo maybe solve this in custompainter in future
strokes.last.points.add(strokes.last.points.last);
lastStroke.points.add(lastStroke.points.last);
file.addPoint(lastStroke.id, lastStroke.points.last);
notifyListeners();
}
}
@ -83,6 +95,7 @@ class PaintController extends ChangeNotifier {
// check if eraser hit an point within its range
for (final pt in stroke.points) {
if (eraserrect.contains(pt.point)) {
file.removeStroke(stroke.id);
strokes.remove(stroke);
notifyListeners();
return;
@ -101,7 +114,9 @@ class PaintController extends ChangeNotifier {
pts.last, pts[pts.length - 2], newWidth);
}
strokes.last.addPoint(Point(offset, newWidth));
Point p = Point(offset, newWidth);
strokes.last.addPoint(p);
file.addPoint(strokes.last.id, p);
break;
case Pen.highlighter:
// TODO: Handle this case.
@ -113,4 +128,9 @@ class PaintController extends ChangeNotifier {
notifyListeners();
}
}
Future<void> loadStrokesFromFile() async {
strokes = await file.loadStrokes();
notifyListeners();
}
}

View File

@ -1,4 +1,4 @@
import 'package:flutter/cupertino.dart';
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart';
import 'package:notes/drawer_item.dart';
@ -110,7 +110,8 @@ class _CollapseDrawerState extends State<CollapseDrawer>
active: widget.activePage == View.shared,
onTap: () => widget.onPageChange(View.shared)),
DrawerItem(
dta: ItemData('Recycle bin', CupertinoIcons.trash),
dta: ItemData(
'Recycle bin', FluentIcons.delete_20_filled),
collapsed: collapsed,
active: widget.activePage == View.recycle,
onTap: () => widget.onPageChange(View.recycle)),

View File

@ -1,9 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:notes/collapse_drawer.dart';
import 'package:notes/all_notes_page.dart';
import 'package:notes/canvas/drawing_page.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
void main() {
if (defaultTargetPlatform != TargetPlatform.android &&
defaultTargetPlatform != TargetPlatform.iOS) {
sqfliteFfiInit();
}
runApp(const MyApp());
}
@ -63,10 +69,13 @@ class _MyHomePageState extends State<MyHomePage> {
case View.folders:
return FloatingActionButton(
onPressed: () {
//final now = DateTime.now();
//String filename = 'note-${now.year}_${now.month}_${now.day}-${now.hour}_${now.minute}.dbnote';
String filename = 'test.dbnote';
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const DrawingPage(),
builder: (context) => DrawingPage(filePath: filename),
),
);
},

View 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');
}
}

View 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();
}
}