Added better Entry handling, also added Entry.id
This commit is contained in:
parent
2a9901f9d8
commit
07000c1709
4 changed files with 81 additions and 51 deletions
|
@ -1,17 +1,20 @@
|
|||
import 'package:intl/intl.dart';
|
||||
|
||||
class Entry {
|
||||
final int id;
|
||||
final DateTime date;
|
||||
final String whatYouDid;
|
||||
|
||||
Entry(this.date, this.whatYouDid);
|
||||
Entry(this.id, this.date, this.whatYouDid);
|
||||
|
||||
Entry.fromJson(Map<String, dynamic> json)
|
||||
: date = DateTime.parse(json['date']),
|
||||
whatYouDid = json['whatyoudid'];
|
||||
: id = json['id'],
|
||||
date = DateTime.parse(json['date']),
|
||||
whatYouDid = json['whatyoudid'];
|
||||
|
||||
Map<String, dynamic> toJson() =>
|
||||
{
|
||||
'id': id,
|
||||
'date': date.toString(),
|
||||
'whatyoudid': whatYouDid,
|
||||
};
|
||||
|
|
|
@ -15,6 +15,12 @@ class FileLoader {
|
|||
return new File('$path/days.json');
|
||||
}
|
||||
|
||||
Future<bool> checkForFile() async {
|
||||
final file = await _localFile;
|
||||
|
||||
return file.exists();
|
||||
}
|
||||
|
||||
Future<File> writeFile(String data) async {
|
||||
final file = await _localFile;
|
||||
|
||||
|
|
|
@ -25,49 +25,32 @@ class NoZeroDays extends StatefulWidget {
|
|||
|
||||
class _NoZeroDaysState extends State<NoZeroDays> {
|
||||
static List<Entry> _firstEntry = <Entry>[
|
||||
Entry(new DateTime.now(), "Made this!")
|
||||
Entry(0, new DateTime.now(), "Made this!")
|
||||
];
|
||||
FileLoader f = new FileLoader();
|
||||
|
||||
List<Entry> _zerodayslist = new List<Entry>.from(_firstEntry);
|
||||
|
||||
Future<DateTime> _currentDatePicker() {
|
||||
int currentYear = new DateTime.now().year;
|
||||
return showDatePicker(
|
||||
context: (context),
|
||||
initialDate: new DateTime.now(),
|
||||
firstDate: new DateTime(currentYear),
|
||||
lastDate: new DateTime(currentYear + 1));
|
||||
void _newEntry(DateTime date, String whatyoudid){
|
||||
int highestID = 0;
|
||||
for (var i = 0; i < _zerodayslist.length; ++i) {
|
||||
if (_zerodayslist[i].id > highestID){
|
||||
highestID = _zerodayslist[i].id;
|
||||
}
|
||||
}
|
||||
|
||||
setState(() => _zerodayslist.add(new Entry(highestID + 1, date, whatyoudid)));
|
||||
|
||||
_save();
|
||||
}
|
||||
|
||||
Widget _buildFAB() {
|
||||
return new FloatingActionButton(
|
||||
onPressed: () {
|
||||
Future<DateTime> pickedDate = _currentDatePicker();
|
||||
pickedDate.then((value) {
|
||||
setState(() {
|
||||
if (value != null) _zerodayslist.add(Entry(value, "nothing"));
|
||||
});
|
||||
}).catchError((err) {
|
||||
Scaffold
|
||||
.of(context)
|
||||
.showSnackBar(new SnackBar(content: new Text(err.toString())));
|
||||
});
|
||||
},
|
||||
tooltip: 'Make a new entry',
|
||||
child: new Icon(Icons.add),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRow(Entry entry) {
|
||||
return new ListTile(
|
||||
title: new Text(entry.toString()),
|
||||
onTap: () {
|
||||
_editEntry(entry);
|
||||
},
|
||||
trailing: new Icon(Icons.edit),
|
||||
);
|
||||
void _removeEntry(Entry entry) {
|
||||
_zerodayslist.removeWhere((testEntry) => testEntry.id == entry.id);
|
||||
_save();
|
||||
}
|
||||
|
||||
void _editEntry(Entry entry) {
|
||||
_removeEntry(entry);
|
||||
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
|
||||
final t = new Form(
|
||||
child: new Column(
|
||||
|
@ -83,9 +66,7 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
|||
newDate.then((newValue) {
|
||||
if (newValue != null) {
|
||||
setState(() {
|
||||
_zerodayslist.remove(entry);
|
||||
_zerodayslist
|
||||
.add(new Entry(newValue, entry.whatYouDid));
|
||||
_newEntry(newValue, entry.whatYouDid);
|
||||
});
|
||||
}
|
||||
Navigator.pop(context);
|
||||
|
@ -98,8 +79,7 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
|||
initialValue: entry.whatYouDid,
|
||||
onFieldSubmitted: (newValue) {
|
||||
setState(() {
|
||||
_zerodayslist.remove(entry);
|
||||
_zerodayslist.add(new Entry(entry.date, newValue));
|
||||
_newEntry(entry.date, newValue);
|
||||
});
|
||||
Navigator.pop(context);
|
||||
},
|
||||
|
@ -122,18 +102,50 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
|||
}));
|
||||
}
|
||||
|
||||
Future<DateTime> _currentDatePicker() {
|
||||
int currentYear = new DateTime.now().year;
|
||||
return showDatePicker(
|
||||
context: (context),
|
||||
initialDate: new DateTime.now(),
|
||||
firstDate: new DateTime(currentYear),
|
||||
lastDate: new DateTime(currentYear + 1));
|
||||
}
|
||||
|
||||
Widget _buildFAB() {
|
||||
return new FloatingActionButton(
|
||||
onPressed: () {
|
||||
Future<DateTime> pickedDate = _currentDatePicker();
|
||||
pickedDate.then((value) {
|
||||
setState(() {
|
||||
if (value != null) _newEntry(value, "nothing");
|
||||
});
|
||||
}).catchError((err) {
|
||||
Scaffold
|
||||
.of(context)
|
||||
.showSnackBar(new SnackBar(content: new Text(err.toString())));
|
||||
});
|
||||
},
|
||||
tooltip: 'Make a new entry',
|
||||
child: new Icon(Icons.add),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRow(Entry entry) {
|
||||
return new ListTile(
|
||||
title: new Text(entry.toString()),
|
||||
onTap: () {
|
||||
_editEntry(entry);
|
||||
},
|
||||
trailing: new Icon(Icons.edit),
|
||||
);
|
||||
}
|
||||
|
||||
void _save() {
|
||||
FileLoader f = new FileLoader();
|
||||
|
||||
f.writeFile(json.encode(_zerodayslist));
|
||||
|
||||
}
|
||||
|
||||
void _load() {
|
||||
FileLoader f = new FileLoader();
|
||||
|
||||
f.readFile().then((s) {
|
||||
print(s);
|
||||
setState(() {
|
||||
List<Entry> newList = <Entry>[];
|
||||
List<dynamic> tempList = json.decode(s);
|
||||
|
@ -164,6 +176,15 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
|||
),
|
||||
body: new Scaffold(body: new ListView.builder(
|
||||
itemBuilder: (context, entry) {
|
||||
f.checkForFile().then((value) {
|
||||
if (value) {
|
||||
_load();
|
||||
}
|
||||
else {
|
||||
f.writeFile("");
|
||||
}
|
||||
});
|
||||
|
||||
if (entry < _zerodayslist.length) {
|
||||
return _buildRow(_zerodayslist[entry]);
|
||||
} else
|
||||
|
|
|
@ -63,7 +63,7 @@ packages:
|
|||
name: csslib
|
||||
url: "https://pub.dartlang.org"
|
||||
source: hosted
|
||||
version: "0.14.3"
|
||||
version: "0.14.4"
|
||||
cupertino_icons:
|
||||
dependency: "direct main"
|
||||
description:
|
||||
|
@ -381,5 +381,5 @@ packages:
|
|||
source: hosted
|
||||
version: "2.1.13"
|
||||
sdks:
|
||||
dart: ">=2.0.0-dev.52.0 <=2.0.0-edge.af1436931b93e755d38223c487d33a0a1f5eadf5"
|
||||
dart: ">=2.0.0-dev.52.0 <=2.0.0-dev.54.0.flutter-46ab040e58"
|
||||
flutter: ">=0.1.4 <2.0.0"
|
||||
|
|
Loading…
Add table
Reference in a new issue