reload files after poping from drawing page

add delete item in top menu bar
add feature to rename file by tapping on title bar
This commit is contained in:
lukas-heiligenbrunner 2022-11-12 19:35:16 +01:00
parent 192884c902
commit f532deb5ad
8 changed files with 132 additions and 74 deletions

View File

@ -1,9 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'canvas/document_types.dart'; import 'canvas/document_types.dart';
import 'canvas/drawing_page.dart'; import 'canvas/drawing_page.dart';
import 'context/file_change_notifier.dart';
import 'pages/all_notes_page.dart'; import 'pages/all_notes_page.dart';
import 'widgets/collapse_drawer.dart'; import 'widgets/collapse_drawer.dart';
@ -19,11 +17,7 @@ class _AppState extends State<App> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ChangeNotifierProvider( return Scaffold(
create: (ctx) {
return FileChangeNotifier()..loadAllNotes();
},
child: Scaffold(
floatingActionButton: _fab(), floatingActionButton: _fab(),
body: Row( body: Row(
children: [ children: [
@ -39,7 +33,6 @@ class _AppState extends State<App> {
)) ))
], ],
), ),
),
); );
} }
@ -47,8 +40,7 @@ class _AppState extends State<App> {
switch (activePage) { switch (activePage) {
case View.all: case View.all:
case View.folders: case View.folders:
return Consumer<FileChangeNotifier>( return FloatingActionButton(
builder: (ctx, notifier, child) => FloatingActionButton(
onPressed: () async { onPressed: () async {
final now = DateTime.now(); final now = DateTime.now();
final name = final name =
@ -56,18 +48,20 @@ class _AppState extends State<App> {
final filename = '$name.dbnote'; final filename = '$name.dbnote';
Navigator.push( Navigator.push(
ctx, context,
MaterialPageRoute( MaterialPageRoute(
builder: (ctx) => DrawingPage( builder: (ctx) => DrawingPage(
meta: NoteMetaData(filename, name, DateTime.now()), meta: NoteMetaData(
name: name,
relativePath: filename,
lastModified: DateTime.now()),
), ),
), ),
).then((v) => notifier.loadAllNotes()); );
}, },
backgroundColor: const Color(0xff3f3f3f), backgroundColor: const Color(0xff3f3f3f),
child: const Icon(Icons.edit_calendar_outlined, child: const Icon(Icons.edit_calendar_outlined,
color: Color(0xffff7300)), color: Color(0xffff7300)),
),
); );
default: default:
return Container(); return Container();

View File

@ -51,5 +51,8 @@ class NoteMetaData {
final String relativePath; final String relativePath;
final DateTime lastModified; final DateTime lastModified;
NoteMetaData(this.name, this.relativePath, this.lastModified); NoteMetaData(
{required this.name,
required this.relativePath,
required this.lastModified});
} }

View File

@ -2,7 +2,9 @@ import 'dart:ui';
import 'package:flutter/gestures.dart'; import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../context/file_change_notifier.dart';
import '../savesystem/note_file.dart'; import '../savesystem/note_file.dart';
import '../widgets/drawing_page_top_actions.dart'; import '../widgets/drawing_page_top_actions.dart';
import '../widgets/tool_bar.dart'; import '../widgets/tool_bar.dart';
@ -29,6 +31,8 @@ class _DrawingPageState extends State<DrawingPage> {
late PaintController controller; late PaintController controller;
late NoteFile noteFile = NoteFile(widget.meta.relativePath); late NoteFile noteFile = NoteFile(widget.meta.relativePath);
late TextEditingController titleController =
TextEditingController(text: widget.meta.name);
@override @override
void initState() { void initState() {
@ -45,17 +49,36 @@ class _DrawingPageState extends State<DrawingPage> {
} }
@override @override
void dispose() { void dispose() async {
super.dispose(); super.dispose();
titleController.dispose();
noteFile.close();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return WillPopScope(
appBar: AppBar(title: Text(widget.meta.name), actions: [ onWillPop: () async {
DrawingPageTopActions(controller: controller, noteMetaData: widget.meta) await noteFile.close().then((value) =>
Provider.of<FileChangeNotifier>(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( body: Row(
children: [ children: [
@ -67,6 +90,7 @@ class _DrawingPageState extends State<DrawingPage> {
Expanded(child: RepaintBoundary(child: _buildCanvas())), Expanded(child: RepaintBoundary(child: _buildCanvas())),
], ],
), ),
),
); );
} }

View File

@ -23,7 +23,8 @@ class FileChangeNotifier extends ChangeNotifier {
final lastmodified = (await fsentity.stat()).modified; final lastmodified = (await fsentity.stat()).modified;
final filename = fsentity.path.split(Platform.pathSeparator).last; final filename = fsentity.path.split(Platform.pathSeparator).last;
final name = filename.substring(0, filename.length - 7); final name = filename.substring(0, filename.length - 7);
return NoteMetaData(name, filename, lastmodified); return NoteMetaData(
name: name, relativePath: filename, lastModified: lastmodified);
}).toList(); }).toList();
dta.sort( dta.sort(
(a, b) => b.lastModified.compareTo(a.lastModified), (a, b) => b.lastModified.compareTo(a.lastModified),

View File

@ -1,10 +1,12 @@
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart'; import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
import 'package:sqflite/sqflite.dart'; import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart';
import 'app.dart'; import 'app.dart';
import 'context/file_change_notifier.dart';
void main() async { void main() async {
if (defaultTargetPlatform != TargetPlatform.android && if (defaultTargetPlatform != TargetPlatform.android &&
@ -22,8 +24,13 @@ void main() async {
} }
} }
runApp(MaterialApp( runApp(ChangeNotifierProvider(
create: (ctx) {
return FileChangeNotifier()..loadAllNotes();
},
child: MaterialApp(
home: const App(), home: const App(),
theme: ThemeData(appBarTheme: const AppBarTheme(color: Colors.blueGrey)), theme:
)); ThemeData(appBarTheme: const AppBarTheme(color: Colors.blueGrey)),
)));
} }

View File

@ -7,16 +7,20 @@ import 'path.dart';
class NoteFile { class NoteFile {
late Database _db; late Database _db;
String filepath; String filename;
late String _basePath;
String? _newFileName;
Database db() { Database db() {
return _db; return _db;
} }
NoteFile(this.filepath); NoteFile(this.filename);
Future<void> init() async { Future<void> init() async {
final path = (await getSavePath()).path + Platform.pathSeparator + filepath; _basePath = (await getSavePath()).path;
final path = _basePath + Platform.pathSeparator + filename;
_db = await openDatabase( _db = await openDatabase(
path, path,
onCreate: (db, version) async { onCreate: (db, version) async {
@ -38,8 +42,13 @@ class NoteFile {
); );
} }
void delete() { Future<void> delete() async {
// todo remove db file await close();
await File(_basePath + Platform.pathSeparator + filename).delete();
}
void rename(String newname) {
_newFileName = newname;
} }
Future<void> close() async { Future<void> close() async {
@ -50,5 +59,13 @@ class NoteFile {
} else { } else {
debugPrint('db file unexpectedly closed before shrinking'); 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;
}
} }
} }

View File

@ -3,12 +3,14 @@ import 'dart:math';
import 'package:fluentui_system_icons/fluentui_system_icons.dart'; import 'package:fluentui_system_icons/fluentui_system_icons.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart'; import 'package:fluttertoast/fluttertoast.dart';
import 'wip_toast.dart'; import 'package:provider/provider.dart';
import '../canvas/document_types.dart'; import '../canvas/document_types.dart';
import '../canvas/paint_controller.dart'; import '../canvas/paint_controller.dart';
import '../context/file_change_notifier.dart';
import '../export/export_pdf.dart'; import '../export/export_pdf.dart';
import 'icon_material_button.dart'; import 'icon_material_button.dart';
import 'wip_toast.dart';
class DrawingPageTopActions extends StatefulWidget { class DrawingPageTopActions extends StatefulWidget {
const DrawingPageTopActions( const DrawingPageTopActions(
@ -159,6 +161,20 @@ class _DrawingPageTopActionsState extends State<DrawingPageTopActions> {
); );
}, },
), ),
PopupMenuItem<int>(
value: 0,
child: const Text(
'Delete',
style: TextStyle(color: Color(0xffe5e5e5)),
),
onTap: () {
widget.controller.file.delete().then((value) {
Provider.of<FileChangeNotifier>(context, listen: false)
.loadAllNotes();
Navigator.of(context).pop();
});
},
),
]); ]);
}, },
), ),

View File

@ -1,9 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../canvas/document_types.dart'; import '../canvas/document_types.dart';
import '../canvas/drawing_page.dart'; import '../canvas/drawing_page.dart';
import '../context/file_change_notifier.dart';
class NoteTile extends StatelessWidget { class NoteTile extends StatelessWidget {
const NoteTile({Key? key, required this.data}) : super(key: key); const NoteTile({Key? key, required this.data}) : super(key: key);
@ -19,9 +17,7 @@ class NoteTile extends StatelessWidget {
MaterialPageRoute( MaterialPageRoute(
builder: (context) => DrawingPage(meta: data), builder: (context) => DrawingPage(meta: data),
), ),
).then((value) => );
Provider.of<FileChangeNotifier>(context, listen: false)
.loadAllNotes());
}, },
child: SizedBox( child: SizedBox(
width: 100, width: 100,