From f532deb5ada2f3eebcae3231ef44657a169778ec Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Sat, 12 Nov 2022 19:35:16 +0100 Subject: [PATCH] reload files after poping from drawing page add delete item in top menu bar add feature to rename file by tapping on title bar --- lib/app.dart | 76 +++++++++++------------ lib/canvas/document_types.dart | 5 +- lib/canvas/drawing_page.dart | 56 ++++++++++++----- lib/context/file_change_notifier.dart | 3 +- lib/main.dart | 15 +++-- lib/savesystem/note_file.dart | 27 ++++++-- lib/widgets/drawing_page_top_actions.dart | 18 +++++- lib/widgets/note_tile.dart | 6 +- 8 files changed, 132 insertions(+), 74 deletions(-) diff --git a/lib/app.dart b/lib/app.dart index bb63f09..1e603c9 100644 --- a/lib/app.dart +++ b/lib/app.dart @@ -1,9 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; import 'canvas/document_types.dart'; import 'canvas/drawing_page.dart'; -import 'context/file_change_notifier.dart'; import 'pages/all_notes_page.dart'; import 'widgets/collapse_drawer.dart'; @@ -19,26 +17,21 @@ class _AppState extends State { @override Widget build(BuildContext context) { - return ChangeNotifierProvider( - create: (ctx) { - return FileChangeNotifier()..loadAllNotes(); - }, - child: Scaffold( - floatingActionButton: _fab(), - body: Row( - children: [ - CollapseDrawer( - onPageChange: (View newPage) => - setState(() => activePage = newPage), - activePage: activePage, - ), - Expanded( - child: Container( - color: const Color(0xff000000), - child: _buildPage(), - )) - ], - ), + return Scaffold( + floatingActionButton: _fab(), + body: Row( + children: [ + CollapseDrawer( + onPageChange: (View newPage) => + setState(() => activePage = newPage), + activePage: activePage, + ), + Expanded( + child: Container( + color: const Color(0xff000000), + child: _buildPage(), + )) + ], ), ); } @@ -47,27 +40,28 @@ class _AppState extends State { switch (activePage) { case View.all: case View.folders: - return Consumer( - builder: (ctx, notifier, child) => FloatingActionButton( - onPressed: () async { - final now = DateTime.now(); - final name = - 'note-${now.year}_${now.month}_${now.day}-${now.hour}_${now.minute}_${now.second}'; - final filename = '$name.dbnote'; + return FloatingActionButton( + onPressed: () async { + final now = DateTime.now(); + final name = + 'note-${now.year}_${now.month}_${now.day}-${now.hour}_${now.minute}_${now.second}'; + final filename = '$name.dbnote'; - Navigator.push( - ctx, - MaterialPageRoute( - builder: (ctx) => DrawingPage( - meta: NoteMetaData(filename, name, DateTime.now()), - ), + Navigator.push( + context, + MaterialPageRoute( + builder: (ctx) => DrawingPage( + meta: NoteMetaData( + name: name, + relativePath: filename, + lastModified: DateTime.now()), ), - ).then((v) => notifier.loadAllNotes()); - }, - backgroundColor: const Color(0xff3f3f3f), - child: const Icon(Icons.edit_calendar_outlined, - color: Color(0xffff7300)), - ), + ), + ); + }, + backgroundColor: const Color(0xff3f3f3f), + child: const Icon(Icons.edit_calendar_outlined, + color: Color(0xffff7300)), ); default: return Container(); diff --git a/lib/canvas/document_types.dart b/lib/canvas/document_types.dart index 76520ea..ee3cda2 100644 --- a/lib/canvas/document_types.dart +++ b/lib/canvas/document_types.dart @@ -51,5 +51,8 @@ class NoteMetaData { final String relativePath; final DateTime lastModified; - NoteMetaData(this.name, this.relativePath, this.lastModified); + NoteMetaData( + {required this.name, + required this.relativePath, + required this.lastModified}); } diff --git a/lib/canvas/drawing_page.dart b/lib/canvas/drawing_page.dart index 8f5b1a4..9ee9395 100644 --- a/lib/canvas/drawing_page.dart +++ b/lib/canvas/drawing_page.dart @@ -2,7 +2,9 @@ import 'dart:ui'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import '../context/file_change_notifier.dart'; import '../savesystem/note_file.dart'; import '../widgets/drawing_page_top_actions.dart'; import '../widgets/tool_bar.dart'; @@ -29,6 +31,8 @@ class _DrawingPageState extends State { late PaintController controller; late NoteFile noteFile = NoteFile(widget.meta.relativePath); + late TextEditingController titleController = + TextEditingController(text: widget.meta.name); @override void initState() { @@ -45,27 +49,47 @@ class _DrawingPageState extends State { } @override - void dispose() { + void dispose() async { super.dispose(); - - noteFile.close(); + titleController.dispose(); } @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar(title: Text(widget.meta.name), actions: [ - DrawingPageTopActions(controller: controller, noteMetaData: widget.meta) - ]), - body: Row( - children: [ - ToolBar( - onPenChange: (pen) { - controller.changePen(pen); - }, - ), - Expanded(child: RepaintBoundary(child: _buildCanvas())), - ], + return WillPopScope( + onWillPop: () async { + await noteFile.close().then((value) => + Provider.of(context, listen: false) + .loadAllNotes()); + + return true; + }, + child: Scaffold( + appBar: AppBar( + title: TextField( + onChanged: (value) { + noteFile.rename('$value.dbnote'); + }, + controller: titleController, + style: const TextStyle(color: Colors.white, fontSize: 20), + decoration: const InputDecoration( + border: InputBorder.none, + ), + ), + actions: [ + DrawingPageTopActions( + controller: controller, noteMetaData: widget.meta) + ]), + body: Row( + children: [ + ToolBar( + onPenChange: (pen) { + controller.changePen(pen); + }, + ), + Expanded(child: RepaintBoundary(child: _buildCanvas())), + ], + ), ), ); } diff --git a/lib/context/file_change_notifier.dart b/lib/context/file_change_notifier.dart index 7c2e5f7..b3a2936 100644 --- a/lib/context/file_change_notifier.dart +++ b/lib/context/file_change_notifier.dart @@ -23,7 +23,8 @@ class FileChangeNotifier extends ChangeNotifier { final lastmodified = (await fsentity.stat()).modified; final filename = fsentity.path.split(Platform.pathSeparator).last; final name = filename.substring(0, filename.length - 7); - return NoteMetaData(name, filename, lastmodified); + return NoteMetaData( + name: name, relativePath: filename, lastModified: lastmodified); }).toList(); dta.sort( (a, b) => b.lastModified.compareTo(a.lastModified), diff --git a/lib/main.dart b/lib/main.dart index 391927f..c7f78b0 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,10 +1,12 @@ import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:permission_handler/permission_handler.dart'; +import 'package:provider/provider.dart'; import 'package:sqflite/sqflite.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'app.dart'; +import 'context/file_change_notifier.dart'; void main() async { if (defaultTargetPlatform != TargetPlatform.android && @@ -22,8 +24,13 @@ void main() async { } } - runApp(MaterialApp( - home: const App(), - theme: ThemeData(appBarTheme: const AppBarTheme(color: Colors.blueGrey)), - )); + runApp(ChangeNotifierProvider( + create: (ctx) { + return FileChangeNotifier()..loadAllNotes(); + }, + child: MaterialApp( + home: const App(), + theme: + ThemeData(appBarTheme: const AppBarTheme(color: Colors.blueGrey)), + ))); } diff --git a/lib/savesystem/note_file.dart b/lib/savesystem/note_file.dart index f87501a..0d193d0 100644 --- a/lib/savesystem/note_file.dart +++ b/lib/savesystem/note_file.dart @@ -7,16 +7,20 @@ import 'path.dart'; class NoteFile { late Database _db; - String filepath; + String filename; + late String _basePath; + + String? _newFileName; Database db() { return _db; } - NoteFile(this.filepath); + NoteFile(this.filename); Future init() async { - final path = (await getSavePath()).path + Platform.pathSeparator + filepath; + _basePath = (await getSavePath()).path; + final path = _basePath + Platform.pathSeparator + filename; _db = await openDatabase( path, onCreate: (db, version) async { @@ -38,8 +42,13 @@ class NoteFile { ); } - void delete() { - // todo remove db file + Future delete() async { + await close(); + await File(_basePath + Platform.pathSeparator + filename).delete(); + } + + void rename(String newname) { + _newFileName = newname; } Future close() async { @@ -50,5 +59,13 @@ class NoteFile { } else { debugPrint('db file unexpectedly closed before shrinking'); } + + // perform qued file renaming operations + if (_newFileName != null) { + File(_basePath + Platform.pathSeparator + filename) + .rename(_basePath + Platform.pathSeparator + _newFileName!); + filename = _newFileName!; + _newFileName = null; + } } } diff --git a/lib/widgets/drawing_page_top_actions.dart b/lib/widgets/drawing_page_top_actions.dart index 962f746..be703ce 100644 --- a/lib/widgets/drawing_page_top_actions.dart +++ b/lib/widgets/drawing_page_top_actions.dart @@ -3,12 +3,14 @@ import 'dart:math'; import 'package:fluentui_system_icons/fluentui_system_icons.dart'; import 'package:flutter/material.dart'; import 'package:fluttertoast/fluttertoast.dart'; -import 'wip_toast.dart'; +import 'package:provider/provider.dart'; import '../canvas/document_types.dart'; import '../canvas/paint_controller.dart'; +import '../context/file_change_notifier.dart'; import '../export/export_pdf.dart'; import 'icon_material_button.dart'; +import 'wip_toast.dart'; class DrawingPageTopActions extends StatefulWidget { const DrawingPageTopActions( @@ -159,6 +161,20 @@ class _DrawingPageTopActionsState extends State { ); }, ), + PopupMenuItem( + value: 0, + child: const Text( + 'Delete', + style: TextStyle(color: Color(0xffe5e5e5)), + ), + onTap: () { + widget.controller.file.delete().then((value) { + Provider.of(context, listen: false) + .loadAllNotes(); + Navigator.of(context).pop(); + }); + }, + ), ]); }, ), diff --git a/lib/widgets/note_tile.dart b/lib/widgets/note_tile.dart index 89a7661..f49b651 100644 --- a/lib/widgets/note_tile.dart +++ b/lib/widgets/note_tile.dart @@ -1,9 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:provider/provider.dart'; import '../canvas/document_types.dart'; import '../canvas/drawing_page.dart'; -import '../context/file_change_notifier.dart'; class NoteTile extends StatelessWidget { const NoteTile({Key? key, required this.data}) : super(key: key); @@ -19,9 +17,7 @@ class NoteTile extends StatelessWidget { MaterialPageRoute( builder: (context) => DrawingPage(meta: data), ), - ).then((value) => - Provider.of(context, listen: false) - .loadAllNotes()); + ); }, child: SizedBox( width: 100,