diff --git a/lib/app.dart b/lib/app.dart new file mode 100644 index 0000000..bd5cf91 --- /dev/null +++ b/lib/app.dart @@ -0,0 +1,95 @@ +import 'package:flutter/material.dart'; +import 'package:notes/context/file_change_notifier.dart'; +import 'package:provider/provider.dart'; + +import 'canvas/drawing_page.dart'; +import 'pages/all_notes_page.dart'; +import 'widgets/collapse_drawer.dart'; + +class App extends StatefulWidget { + const App({super.key}); + + @override + State createState() => _AppState(); +} + +class _AppState extends State { + View activePage = View.all; + + @override + Widget build(BuildContext context) { + return ChangeNotifierProvider( + create: (ctx) { + final notifier = FileChangeNotifier(); + notifier.loadAllNotes(); + return notifier; + }, + child: Scaffold( + floatingActionButton: _fab(), + body: Row( + children: [ + CollapseDrawer( + onPageChange: (View newPage) => + setState(() => activePage = newPage), + activePage: activePage, + ), + Expanded( + child: Container( + color: const Color(0xff0d0d0d), + child: _buildPage(), + )) + ], + ), + ), + ); + } + + Widget _fab() { + switch (activePage) { + case View.all: + case View.folders: + return FloatingActionButton( + onPressed: () async { + final now = DateTime.now(); + String filename = + 'note-${now.year}_${now.month}_${now.day}-${now.hour}_${now.minute}.dbnote'; + + Navigator.push( + context, + MaterialPageRoute( + builder: (ctx) => DrawingPage(filePath: filename), + ), + ).then((value) => + Provider.of(context, listen: false) + .loadAllNotes()); + }, + backgroundColor: const Color(0xff3f3f3f), + child: const Icon(Icons.edit_calendar_outlined, color: Colors.orange), + ); + default: + return Container(); + } + } + + Widget _buildPage() { + switch (activePage) { + case View.all: + return const AllNotesPage(); + case View.shared: + return const Text( + 'shared notebooks WIP', + style: TextStyle(color: Colors.white), + ); + case View.recycle: + return const Text( + 'recycle bin WIP', + style: TextStyle(color: Colors.white), + ); + case View.folders: + return const Text( + 'Folders WIP', + style: TextStyle(color: Colors.white), + ); + } + } +} diff --git a/lib/canvas/drawing_page.dart b/lib/canvas/drawing_page.dart index 8da476d..91ff6d6 100644 --- a/lib/canvas/drawing_page.dart +++ b/lib/canvas/drawing_page.dart @@ -8,8 +8,8 @@ import 'my_painter.dart'; import 'paint_controller.dart'; import 'screen_document_mapping.dart'; -import '../icon_material_button.dart'; -import '../tool_bar.dart'; +import '../widgets/icon_material_button.dart'; +import '../widgets/tool_bar.dart'; /// Handles input events and draws canvas element class DrawingPage extends StatefulWidget { diff --git a/lib/context/file_change_notifier.dart b/lib/context/file_change_notifier.dart new file mode 100644 index 0000000..3b21794 --- /dev/null +++ b/lib/context/file_change_notifier.dart @@ -0,0 +1,31 @@ +import 'dart:io'; + +import 'package:flutter/cupertino.dart'; + +import '../savesystem/path.dart'; +import '../widgets/note_tile.dart'; + +class FileChangeNotifier extends ChangeNotifier { + FileChangeNotifier(); + + List tiledata = []; + + Future> loadAllNotes() async { + final path = await getSavePath(); + final dta = await path + .list() + .where((fsentity) => fsentity.path.endsWith('.dbnote')) + .asyncMap((fsentity) async { + final lastmodified = (await fsentity.stat()).modified; + final filename = fsentity.path.split(Platform.pathSeparator).last; + final name = filename.substring(0, filename.length - 7); + return NoteTileData(name, filename, lastmodified); + }).toList(); + dta.sort( + (a, b) => a.lastModified.isAfter(b.lastModified) ? -1 : 1, + ); + notifyListeners(); + tiledata = dta; + return dta; + } +} diff --git a/lib/main.dart b/lib/main.dart index d31108f..d5d101a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,114 +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/sqflite.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'app.dart'; + void main() { if (defaultTargetPlatform != TargetPlatform.android && defaultTargetPlatform != TargetPlatform.iOS) { sqfliteFfiInit(); databaseFactory = databaseFactoryFfi; } - runApp(const MyApp()); -} - -class MyApp extends StatelessWidget { - const MyApp({super.key}); - - // This widget is the root of your application. - @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Flutter Demo', - theme: ThemeData( - // is not restarted. - primarySwatch: Colors.blue, - ), - home: const MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - View activePage = View.all; - - @override - Widget build(BuildContext context) { - return Scaffold( - floatingActionButton: _fab(), - body: Row( - children: [ - CollapseDrawer( - onPageChange: (View newPage) => - setState(() => activePage = newPage), - activePage: activePage, - ), - Expanded( - child: Container( - color: const Color(0xff0d0d0d), - child: _buildPage(), - )) - ], - ), - ); - } - - Widget _fab() { - switch (activePage) { - case View.all: - case View.folders: - return FloatingActionButton( - onPressed: () { - final now = DateTime.now(); - String filename = - 'note-${now.year}_${now.month}_${now.day}-${now.hour}_${now.minute}.dbnote'; - - Navigator.push( - context, - MaterialPageRoute( - builder: (context) => DrawingPage(filePath: filename), - ), - ); - }, - backgroundColor: const Color(0xff3f3f3f), - child: const Icon(Icons.edit_calendar_outlined, color: Colors.orange), - ); - default: - return Container(); - } - } - - Widget _buildPage() { - switch (activePage) { - case View.all: - return const AllNotesPage(); - case View.shared: - return const Text( - 'shared notebooks WIP', - style: TextStyle(color: Colors.white), - ); - case View.recycle: - return const Text( - 'recycle bin WIP', - style: TextStyle(color: Colors.white), - ); - case View.folders: - return const Text( - 'Folders WIP', - style: TextStyle(color: Colors.white), - ); - } - } + runApp(const MaterialApp(home: App())); } diff --git a/lib/all_notes_page.dart b/lib/pages/all_notes_page.dart similarity index 57% rename from lib/all_notes_page.dart rename to lib/pages/all_notes_page.dart index 311b801..ab9e43f 100644 --- a/lib/all_notes_page.dart +++ b/lib/pages/all_notes_page.dart @@ -1,9 +1,8 @@ -import 'dart:io'; - import 'package:flutter/material.dart'; -import 'package:notes/icon_material_button.dart'; -import 'package:notes/note_tile.dart'; -import 'package:notes/savesystem/path.dart'; +import 'package:notes/context/file_change_notifier.dart'; +import 'package:notes/widgets/icon_material_button.dart'; +import 'package:notes/widgets/note_tile.dart'; +import 'package:provider/provider.dart'; class AllNotesPage extends StatefulWidget { const AllNotesPage({Key? key}) : super(key: key); @@ -13,8 +12,6 @@ class AllNotesPage extends StatefulWidget { } class _AllNotesPageState extends State { - List tileData = []; - @override Widget build(BuildContext context) { return Column( @@ -63,40 +60,19 @@ class _AllNotesPageState extends State { Widget _buildNoteTiles() { return Expanded( - child: GridView.builder( - gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: 5, - ), - padding: const EdgeInsets.all(2), - itemBuilder: (BuildContext context, int idx) => - NoteTile(data: tileData[idx]), - itemCount: tileData.length, + child: Consumer( + builder: (BuildContext context, value, Widget? child) { + return GridView.builder( + gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: 5, + ), + padding: const EdgeInsets.all(2), + itemBuilder: (BuildContext context, int idx) => + NoteTile(data: value.tiledata[idx]), + itemCount: value.tiledata.length, + ); + }, ), ); } - - @override - void initState() { - super.initState(); - loadAllNotes(); - } - - Future loadAllNotes() async { - final path = await getSavePath(); - final dta = await path - .list() - .where((fsentity) => fsentity.path.endsWith('.dbnote')) - .asyncMap((fsentity) async { - final lastmodified = (await fsentity.stat()).modified; - final filename = fsentity.path.split(Platform.pathSeparator).last; - final name = filename.substring(0, filename.length - 7); - return NoteTileData(name, filename, lastmodified); - }).toList(); - dta.sort( - (a, b) => a.lastModified.isAfter(b.lastModified) ? -1 : 1, - ); - setState(() { - tileData = dta; - }); - } } diff --git a/lib/savesystem/path.dart b/lib/savesystem/path.dart index e215719..af47043 100644 --- a/lib/savesystem/path.dart +++ b/lib/savesystem/path.dart @@ -2,13 +2,13 @@ import 'dart:io'; import 'package:flutter/foundation.dart'; import 'package:path_provider/path_provider.dart'; -import 'package:sqflite/sqflite.dart'; Future getSavePath() async { Directory dbpath; if (defaultTargetPlatform == TargetPlatform.android || defaultTargetPlatform == TargetPlatform.iOS) { - dbpath = Directory(await getDatabasesPath()); + dbpath = Directory( + '${(await getApplicationDocumentsDirectory()).path}${Platform.pathSeparator}notes'); } else { dbpath = Directory( '${(await getApplicationDocumentsDirectory()).path}${Platform.pathSeparator}notes'); diff --git a/lib/collapse_drawer.dart b/lib/widgets/collapse_drawer.dart similarity index 88% rename from lib/collapse_drawer.dart rename to lib/widgets/collapse_drawer.dart index d9eab0a..3d77ff1 100644 --- a/lib/collapse_drawer.dart +++ b/lib/widgets/collapse_drawer.dart @@ -1,6 +1,8 @@ import 'package:fluentui_system_icons/fluentui_system_icons.dart'; import 'package:flutter/material.dart'; -import 'package:notes/drawer_item.dart'; +import 'package:notes/context/file_change_notifier.dart'; +import 'package:notes/widgets/drawer_item.dart'; +import 'package:provider/provider.dart'; enum View { all, shared, recycle, folders } @@ -98,12 +100,14 @@ class _CollapseDrawerState extends State Expanded( child: ListView( children: [ - DrawerItem( - dta: ItemData('All Notes', Icons.book), - collapsed: collapsed, - endText: '7', - active: widget.activePage == View.all, - onTap: () => widget.onPageChange(View.all)), + Consumer( + builder: (context, value, child) => DrawerItem( + dta: ItemData('All Notes', Icons.book), + collapsed: collapsed, + endText: value.tiledata.length.toString(), + active: widget.activePage == View.all, + onTap: () => widget.onPageChange(View.all)), + ), DrawerItem( dta: ItemData('Shared Notebooks', Icons.person_outline), collapsed: collapsed, diff --git a/lib/drawer_item.dart b/lib/widgets/drawer_item.dart similarity index 100% rename from lib/drawer_item.dart rename to lib/widgets/drawer_item.dart diff --git a/lib/icon_material_button.dart b/lib/widgets/icon_material_button.dart similarity index 100% rename from lib/icon_material_button.dart rename to lib/widgets/icon_material_button.dart diff --git a/lib/note_tile.dart b/lib/widgets/note_tile.dart similarity index 82% rename from lib/note_tile.dart rename to lib/widgets/note_tile.dart index 8651a5b..ef12e5d 100644 --- a/lib/note_tile.dart +++ b/lib/widgets/note_tile.dart @@ -1,6 +1,8 @@ import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; -import 'canvas/drawing_page.dart'; +import '../canvas/drawing_page.dart'; +import '../context/file_change_notifier.dart'; class NoteTileData { final String name; @@ -24,7 +26,9 @@ class NoteTile extends StatelessWidget { MaterialPageRoute( builder: (context) => DrawingPage(filePath: data.relativePath), ), - ); + ).then((value) => + Provider.of(context, listen: false) + .loadAllNotes()); }, child: SizedBox( width: 100, diff --git a/lib/tool_bar.dart b/lib/widgets/tool_bar.dart similarity index 98% rename from lib/tool_bar.dart rename to lib/widgets/tool_bar.dart index 345fd35..3ddb86b 100644 --- a/lib/tool_bar.dart +++ b/lib/widgets/tool_bar.dart @@ -5,7 +5,7 @@ import 'package:iconify_flutter/iconify_flutter.dart'; import 'package:iconify_flutter/icons/emojione_monotone.dart'; import 'package:iconify_flutter/icons/jam.dart'; -import 'canvas/paint_controller.dart'; +import '../canvas/paint_controller.dart'; import 'icon_material_button.dart'; class ToolBar extends StatefulWidget { diff --git a/pubspec.lock b/pubspec.lock index efe9770..3384fbb 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -144,6 +144,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.0" + nested: + dependency: transitive + description: + name: nested + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" path: dependency: transitive description: @@ -242,6 +249,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.2.4" + provider: + dependency: "direct main" + description: + name: provider + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.4" sky_engine: dependency: transitive description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 1865381..65cf7ba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -35,6 +35,7 @@ dependencies: sqflite: ^2.2.0+2 sqflite_common_ffi: ^2.2.0+1 path_provider: ^2.0.11 + provider: ^6.0.4 dev_dependencies: flutter_test: