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';
|
import 'package:intl/intl.dart';
|
||||||
|
|
||||||
class Entry {
|
class Entry {
|
||||||
|
final int id;
|
||||||
final DateTime date;
|
final DateTime date;
|
||||||
final String whatYouDid;
|
final String whatYouDid;
|
||||||
|
|
||||||
Entry(this.date, this.whatYouDid);
|
Entry(this.id, this.date, this.whatYouDid);
|
||||||
|
|
||||||
Entry.fromJson(Map<String, dynamic> json)
|
Entry.fromJson(Map<String, dynamic> json)
|
||||||
: date = DateTime.parse(json['date']),
|
: id = json['id'],
|
||||||
whatYouDid = json['whatyoudid'];
|
date = DateTime.parse(json['date']),
|
||||||
|
whatYouDid = json['whatyoudid'];
|
||||||
|
|
||||||
Map<String, dynamic> toJson() =>
|
Map<String, dynamic> toJson() =>
|
||||||
{
|
{
|
||||||
|
'id': id,
|
||||||
'date': date.toString(),
|
'date': date.toString(),
|
||||||
'whatyoudid': whatYouDid,
|
'whatyoudid': whatYouDid,
|
||||||
};
|
};
|
||||||
|
|
|
@ -15,6 +15,12 @@ class FileLoader {
|
||||||
return new File('$path/days.json');
|
return new File('$path/days.json');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<bool> checkForFile() async {
|
||||||
|
final file = await _localFile;
|
||||||
|
|
||||||
|
return file.exists();
|
||||||
|
}
|
||||||
|
|
||||||
Future<File> writeFile(String data) async {
|
Future<File> writeFile(String data) async {
|
||||||
final file = await _localFile;
|
final file = await _localFile;
|
||||||
|
|
||||||
|
|
|
@ -25,49 +25,32 @@ class NoZeroDays extends StatefulWidget {
|
||||||
|
|
||||||
class _NoZeroDaysState extends State<NoZeroDays> {
|
class _NoZeroDaysState extends State<NoZeroDays> {
|
||||||
static List<Entry> _firstEntry = <Entry>[
|
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);
|
List<Entry> _zerodayslist = new List<Entry>.from(_firstEntry);
|
||||||
|
|
||||||
Future<DateTime> _currentDatePicker() {
|
void _newEntry(DateTime date, String whatyoudid){
|
||||||
int currentYear = new DateTime.now().year;
|
int highestID = 0;
|
||||||
return showDatePicker(
|
for (var i = 0; i < _zerodayslist.length; ++i) {
|
||||||
context: (context),
|
if (_zerodayslist[i].id > highestID){
|
||||||
initialDate: new DateTime.now(),
|
highestID = _zerodayslist[i].id;
|
||||||
firstDate: new DateTime(currentYear),
|
}
|
||||||
lastDate: new DateTime(currentYear + 1));
|
}
|
||||||
|
|
||||||
|
setState(() => _zerodayslist.add(new Entry(highestID + 1, date, whatyoudid)));
|
||||||
|
|
||||||
|
_save();
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildFAB() {
|
void _removeEntry(Entry entry) {
|
||||||
return new FloatingActionButton(
|
_zerodayslist.removeWhere((testEntry) => testEntry.id == entry.id);
|
||||||
onPressed: () {
|
_save();
|
||||||
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 _editEntry(Entry entry) {
|
void _editEntry(Entry entry) {
|
||||||
|
_removeEntry(entry);
|
||||||
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
|
Navigator.of(context).push(new MaterialPageRoute(builder: (context) {
|
||||||
final t = new Form(
|
final t = new Form(
|
||||||
child: new Column(
|
child: new Column(
|
||||||
|
@ -83,9 +66,7 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
||||||
newDate.then((newValue) {
|
newDate.then((newValue) {
|
||||||
if (newValue != null) {
|
if (newValue != null) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_zerodayslist.remove(entry);
|
_newEntry(newValue, entry.whatYouDid);
|
||||||
_zerodayslist
|
|
||||||
.add(new Entry(newValue, entry.whatYouDid));
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
|
@ -98,8 +79,7 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
||||||
initialValue: entry.whatYouDid,
|
initialValue: entry.whatYouDid,
|
||||||
onFieldSubmitted: (newValue) {
|
onFieldSubmitted: (newValue) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_zerodayslist.remove(entry);
|
_newEntry(entry.date, newValue);
|
||||||
_zerodayslist.add(new Entry(entry.date, newValue));
|
|
||||||
});
|
});
|
||||||
Navigator.pop(context);
|
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() {
|
void _save() {
|
||||||
FileLoader f = new FileLoader();
|
|
||||||
|
|
||||||
f.writeFile(json.encode(_zerodayslist));
|
f.writeFile(json.encode(_zerodayslist));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _load() {
|
void _load() {
|
||||||
FileLoader f = new FileLoader();
|
|
||||||
|
|
||||||
f.readFile().then((s) {
|
f.readFile().then((s) {
|
||||||
print(s);
|
|
||||||
setState(() {
|
setState(() {
|
||||||
List<Entry> newList = <Entry>[];
|
List<Entry> newList = <Entry>[];
|
||||||
List<dynamic> tempList = json.decode(s);
|
List<dynamic> tempList = json.decode(s);
|
||||||
|
@ -164,6 +176,15 @@ class _NoZeroDaysState extends State<NoZeroDays> {
|
||||||
),
|
),
|
||||||
body: new Scaffold(body: new ListView.builder(
|
body: new Scaffold(body: new ListView.builder(
|
||||||
itemBuilder: (context, entry) {
|
itemBuilder: (context, entry) {
|
||||||
|
f.checkForFile().then((value) {
|
||||||
|
if (value) {
|
||||||
|
_load();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
f.writeFile("");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
if (entry < _zerodayslist.length) {
|
if (entry < _zerodayslist.length) {
|
||||||
return _buildRow(_zerodayslist[entry]);
|
return _buildRow(_zerodayslist[entry]);
|
||||||
} else
|
} else
|
||||||
|
|
|
@ -63,7 +63,7 @@ packages:
|
||||||
name: csslib
|
name: csslib
|
||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.14.3"
|
version: "0.14.4"
|
||||||
cupertino_icons:
|
cupertino_icons:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -381,5 +381,5 @@ packages:
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "2.1.13"
|
version: "2.1.13"
|
||||||
sdks:
|
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"
|
flutter: ">=0.1.4 <2.0.0"
|
||||||
|
|
Loading…
Add table
Reference in a new issue