mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-06-29 02:07:39 +08:00
Make the state global and connect the add note screen
This is a huge work in progress, but it finally seems to kinda work.
This commit is contained in:
86
lib/app.dart
Normal file
86
lib/app.dart
Normal file
@ -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<JournalApp> {
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
@ -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<Directory> Function() getDirectory;
|
||||
|
||||
FileStorage(this.getDirectory);
|
||||
FileStorage({@required this.getDirectory});
|
||||
|
||||
Future<List<Note>> loadNotes() async {
|
||||
final dir = await getDirectory();
|
||||
|
@ -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<List<Note>> fetchNotes() async {
|
||||
final response = await http.get('http://192.168.1.132:8000/notes');
|
||||
@ -20,63 +23,20 @@ Future<List<Note>> 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<List<Note>>(
|
||||
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<Directory> getNotesDir() async {
|
||||
var appDir = await getApplicationDocumentsDirectory();
|
||||
var dir = new Directory(p.join(appDir.path, "notes"));
|
||||
await dir.create();
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
@ -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<Note> notes;
|
||||
|
||||
AppState({
|
||||
this.isLoading = false,
|
||||
this.notes = const [],
|
||||
});
|
||||
|
||||
factory AppState.loading() => AppState(isLoading: true);
|
||||
}
|
||||
|
@ -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<FormFieldState<String>> noteTextKey =
|
||||
GlobalKey<FormFieldState<String>>();
|
||||
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;
|
||||
|
67
lib/screens/home_screen.dart
Normal file
67
lib/screens/home_screen.dart
Normal file
@ -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<List<Note>>(
|
||||
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);
|
||||
}
|
||||
}
|
10
pubspec.lock
10
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"
|
||||
|
@ -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:
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user