From 0cb36b298138daf2ec07631a8cd7a1ede28e761e Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Mon, 21 May 2018 16:51:29 +0200 Subject: [PATCH] Make the state global and connect the add note screen This is a huge work in progress, but it finally seems to kinda work. --- lib/app.dart | 86 ++++++++++++++++++++++++++++++++++++ lib/file_storage.dart | 3 +- lib/main.dart | 82 +++++++++------------------------- lib/note.dart | 16 +++++++ lib/note_editor.dart | 33 +++++++++++++- lib/screens/home_screen.dart | 67 ++++++++++++++++++++++++++++ pubspec.lock | 10 ++++- pubspec.yaml | 2 + test/file_storage_test.dart | 2 +- 9 files changed, 235 insertions(+), 66 deletions(-) create mode 100644 lib/app.dart create mode 100644 lib/screens/home_screen.dart diff --git a/lib/app.dart b/lib/app.dart new file mode 100644 index 00000000..a82c31e5 --- /dev/null +++ b/lib/app.dart @@ -0,0 +1,86 @@ +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:journal/file_storage.dart'; +import 'package:journal/note.dart'; +import 'package:journal/screens/home_screen.dart'; + +class JournalApp extends StatefulWidget { + final FileStorage fileStorage; + + JournalApp({@required this.fileStorage}); + + @override + JournalAppState createState() { + return new JournalAppState(); + } +} + +class JournalAppState extends State { + AppState appState = AppState.loading(); + + @override + void initState() { + super.initState(); + + widget.fileStorage.loadNotes().then((loadedNotes) { + setState(() { + appState = AppState(notes: loadedNotes); + }); + }).catchError((err) { + setState(() { + print("Got Error"); + print(err); + appState.isLoading = false; + }); + }); + } + + @override + Widget build(BuildContext context) { + return new MaterialApp( + title: 'Journal', + home: new HomeScreen( + appState: appState, + noteAdder: addNote, + ), + theme: new ThemeData( + brightness: Brightness.dark, + primaryColor: Colors.lightBlue[800], + accentColor: Colors.cyan[600], + ), + ); + } + + @override + void setState(VoidCallback fn) { + super.setState(fn); + + widget.fileStorage.saveNotes(appState.notes); + } + + void addNote(Note note) { + print("Adding a note " + note.toString()); + setState(() { + try { + appState.notes.add(note); + } catch (err) { + print("WTF"); + print(err); + } + }); + } + + void removeNote(Note note) { + setState(() { + appState.notes.remove(note); + }); + } + + // FIXME: Implement this! + void updateNote(Note note) { + setState(() { + //appState.notes. + //appState.notes.remove(note); + }); + } +} diff --git a/lib/file_storage.dart b/lib/file_storage.dart index b7371691..9d7fe467 100644 --- a/lib/file_storage.dart +++ b/lib/file_storage.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'dart:io'; import 'dart:convert'; +import 'package:flutter/foundation.dart'; import 'package:path/path.dart' as p; import './note.dart'; @@ -9,7 +10,7 @@ import './note.dart'; class FileStorage { final Future Function() getDirectory; - FileStorage(this.getDirectory); + FileStorage({@required this.getDirectory}); Future> loadNotes() async { final dir = await getDirectory(); diff --git a/lib/main.dart b/lib/main.dart index 027e4a06..9a388ec2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,13 +1,16 @@ import 'dart:async'; -import 'dart:convert'; +import 'dart:io'; import 'package:flutter/material.dart'; -import 'package:http/http.dart' as http; +//import 'package:http/http.dart' as http; +import 'package:journal/app.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:path/path.dart' as p; +import 'package:journal/file_storage.dart'; + +/* import 'note.dart'; -import 'note_editor.dart'; -import 'note_viewer.dart'; -import 'journal_list.dart'; Future> fetchNotes() async { final response = await http.get('http://192.168.1.132:8000/notes'); @@ -20,63 +23,20 @@ Future> fetchNotes() async { return notes; } +*/ -void main() => runApp(new MyApp()); - -class HomeScreen extends StatelessWidget { - @override - Widget build(BuildContext context) { - var createButton = new FloatingActionButton( - onPressed: () => _newPost(context), - child: new Icon(Icons.add), - ); - - return new Scaffold( - appBar: new AppBar( - title: new Text('Journal'), - ), - floatingActionButton: createButton, - body: new FutureBuilder>( - future: fetchNotes(), - builder: (context, snapshot) { - if (snapshot.hasData) { - var notes = snapshot.data; - return new JournalList( - notes: notes, - noteSelectedFunction: (note) => _noteSelected(note, context), - ); - } else if (snapshot.hasError) { - return new Text("${snapshot.error}"); - } - - return new CircularProgressIndicator(); - }), - ); - } - - void _noteSelected(Note note, BuildContext context) { - var route = - new MaterialPageRoute(builder: (context) => new NoteViewer(note: note)); - Navigator.of(context).push(route); - } - - void _newPost(BuildContext context) { - var route = new MaterialPageRoute(builder: (context) => new NoteEditor()); - Navigator.of(context).push(route); - } +void main() { + runApp(new JournalApp( + fileStorage: FileStorage( + getDirectory: getNotesDir, + ), + )); } -class MyApp extends StatelessWidget { - @override - Widget build(BuildContext context) { - return new MaterialApp( - title: 'Journal', - home: new HomeScreen(), - theme: new ThemeData( - brightness: Brightness.dark, - primaryColor: Colors.lightBlue[800], - accentColor: Colors.cyan[600], - ), - ); - } +Future getNotesDir() async { + var appDir = await getApplicationDocumentsDirectory(); + var dir = new Directory(p.join(appDir.path, "notes")); + await dir.create(); + + return dir; } diff --git a/lib/note.dart b/lib/note.dart index 0c9f7ff3..a6922bd9 100644 --- a/lib/note.dart +++ b/lib/note.dart @@ -1,3 +1,7 @@ +typedef NoteAdder(Note note); +typedef NoteRemover(Note note); +typedef NoteUpdator(Note note); + class Note implements Comparable { final String id; final DateTime createdAt; @@ -41,3 +45,15 @@ class Note implements Comparable { @override int compareTo(other) => createdAt.compareTo(other.createdAt); } + +class AppState { + bool isLoading; + List notes; + + AppState({ + this.isLoading = false, + this.notes = const [], + }); + + factory AppState.loading() => AppState(isLoading: true); +} diff --git a/lib/note_editor.dart b/lib/note_editor.dart index aaae27d2..2a73f745 100644 --- a/lib/note_editor.dart +++ b/lib/note_editor.dart @@ -1,10 +1,23 @@ import 'package:flutter/material.dart'; +import 'package:flutter/foundation.dart'; +import 'package:intl/intl.dart'; +import 'package:journal/note.dart'; class NoteEditor extends StatelessWidget { + static final GlobalKey> noteTextKey = + GlobalKey>(); + final NoteAdder noteAdder; + final DateTime _createdAt; + + NoteEditor({ + @required this.noteAdder, + }) : _createdAt = new DateTime.now(); + @override Widget build(BuildContext context) { var bodyWidget = new Container( - child: new TextField( + child: new TextFormField( + key: noteTextKey, autofocus: true, keyboardType: TextInputType.multiline, maxLines: 5000, @@ -15,11 +28,27 @@ class NoteEditor extends StatelessWidget { padding: const EdgeInsets.all(8.0), ); + var formatter = new DateFormat('dd MMM, yyyy'); + var title = formatter.format(_createdAt); + var newJournalScreen = new Scaffold( appBar: new AppBar( - title: new Text("May 15, 17:35"), + title: new Text(title), ), body: bodyWidget, + floatingActionButton: FloatingActionButton( + child: Icon(Icons.add), + onPressed: () { + var body = noteTextKey.currentState.value; + var note = new Note( + id: "1", + createdAt: _createdAt, + body: body, + ); + noteAdder(note); + + Navigator.pop(context); + }), ); return newJournalScreen; diff --git a/lib/screens/home_screen.dart b/lib/screens/home_screen.dart new file mode 100644 index 00000000..11bbd795 --- /dev/null +++ b/lib/screens/home_screen.dart @@ -0,0 +1,67 @@ +import 'package:flutter/material.dart'; +import 'package:flutter/foundation.dart'; + +import 'package:journal/journal_list.dart'; +import 'package:journal/note.dart'; +import 'package:journal/note_editor.dart'; +import 'package:journal/note_viewer.dart'; + +class HomeScreen extends StatelessWidget { + final AppState appState; + final NoteAdder noteAdder; + + HomeScreen({ + @required this.appState, + @required this.noteAdder, + }); + + @override + Widget build(BuildContext context) { + var createButton = new FloatingActionButton( + onPressed: () => _newPost(context), + child: new Icon(Icons.add), + ); + + return new Scaffold( + appBar: new AppBar( + title: new Text('Journal'), + ), + floatingActionButton: createButton, + /* + body: new FutureBuilder>( + future: fetchNotes(), + builder: (context, snapshot) { + if (snapshot.hasData) { + var notes = snapshot.data; + return new JournalList( + notes: notes, + noteSelectedFunction: (note) => _noteSelected(note, context), + ); + } else if (snapshot.hasError) { + return new Text("${snapshot.error}"); + } + + return new CircularProgressIndicator(); + }), + */ + body: new JournalList( + notes: appState.notes, + noteSelectedFunction: (note) => _noteSelected(note, context), + ), + ); + } + + void _noteSelected(Note note, BuildContext context) { + var route = + new MaterialPageRoute(builder: (context) => new NoteViewer(note: note)); + Navigator.of(context).push(route); + } + + void _newPost(BuildContext context) { + var route = new MaterialPageRoute( + builder: (context) => new NoteEditor( + noteAdder: noteAdder, + )); + Navigator.of(context).push(route); + } +} diff --git a/pubspec.lock b/pubspec.lock index a7c2b42e..f537d2f0 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -228,6 +228,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.5.1" + path_provider: + dependency: "direct main" + description: + name: path_provider + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.0" plugin: dependency: transitive description: @@ -388,4 +395,5 @@ packages: source: hosted version: "2.1.13" sdks: - dart: ">=2.0.0-dev.23.0 <=2.0.0-edge.af1436931b93e755d38223c487d33a0a1f5eadf5" + dart: ">=2.0.0-dev.28.0 <=2.0.0-edge.af1436931b93e755d38223c487d33a0a1f5eadf5" + flutter: ">=0.1.4 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 331eddc2..e240a01e 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,6 +7,8 @@ dependencies: http: "^0.11.3+16" intl: "^0.15.6" path: "^1.5.1" + path_provider: "^0.4.0" + dev_dependencies: flutter_test: diff --git a/test/file_storage_test.dart b/test/file_storage_test.dart index a8fd4118..b5fc6756 100644 --- a/test/file_storage_test.dart +++ b/test/file_storage_test.dart @@ -14,7 +14,7 @@ main() { ]; final directory = Directory.systemTemp.createTemp('__storage_test__'); - final storage = FileStorage(() => directory); + final storage = FileStorage(getDirectory: () => directory); tearDownAll(() async { final tempDirectory = await directory;