diff --git a/lib/components/TaskTile.dart b/lib/components/TaskTile.dart index f976950..190dfb6 100644 --- a/lib/components/TaskTile.dart +++ b/lib/components/TaskTile.dart @@ -7,26 +7,25 @@ import 'package:vikunja_app/models/task.dart'; class TaskTile extends StatefulWidget { final Task task; final VoidCallback onEdit; + final ValueSetter onMarkedAsDone; final bool loading; const TaskTile( - {Key key, @required this.task, this.onEdit, this.loading = false}) + {Key key, @required this.task, this.onEdit, this.loading = false, this.onMarkedAsDone}) : assert(task != null), super(key: key); @override TaskTileState createState() { - return new TaskTileState(this.task, this.loading); + return new TaskTileState(this.loading); } } class TaskTileState extends State { bool _loading; - Task _currentTask; - TaskTileState(this._currentTask, this._loading) - : assert(_currentTask != null), - assert(_loading != null); + TaskTileState(this._loading) + : assert(_loading != null); @override Widget build(BuildContext context) { @@ -41,11 +40,11 @@ class TaskTileState extends State { strokeWidth: 2.0, )), ), - title: Text(_currentTask.text), + title: Text(widget.task.text), subtitle: - _currentTask.description == null || _currentTask.description.isEmpty + widget.task.description == null || widget.task.description.isEmpty ? null - : Text(_currentTask.description), + : Text(widget.task.description), trailing: IconButton( icon: Icon(Icons.settings), onPressed: () => widget.onEdit, @@ -53,18 +52,18 @@ class TaskTileState extends State { ); } return CheckboxListTile( - title: Text(_currentTask.text), + title: Text(widget.task.text), controlAffinity: ListTileControlAffinity.leading, - value: _currentTask.done ?? false, + value: widget.task.done ?? false, subtitle: - _currentTask.description == null || _currentTask.description.isEmpty + widget.task.description == null || widget.task.description.isEmpty ? null - : Text(_currentTask.description), + : Text(widget.task.description), secondary: IconButton( icon: Icon(Icons.settings), onPressed: widget.onEdit, ), - onChanged: _change, + onChanged: widget.onMarkedAsDone, ); } @@ -72,9 +71,9 @@ class TaskTileState extends State { setState(() { this._loading = true; }); - Task newTask = await _updateTask(_currentTask, value); + Task newTask = await _updateTask(widget.task, value); setState(() { - this._currentTask = newTask; + //this.widget.task = newTask; this._loading = false; }); } diff --git a/lib/pages/list/list.dart b/lib/pages/list/list.dart index f7de17c..fe62f1a 100644 --- a/lib/pages/list/list.dart +++ b/lib/pages/list/list.dart @@ -38,6 +38,10 @@ class _ListPageState extends State { @override Widget build(BuildContext context) { + + var tasks = (_list?.tasks?.map(_buildTile) ?? []).toList(); + tasks.addAll(_loadingTasks.map(_buildLoadingTile)); + return Scaffold( appBar: AppBar( title: new Text(_list.title), @@ -54,20 +58,28 @@ class _ListPageState extends State { ), body: !this._loading ? RefreshIndicator( - child: _list.tasks.length > 0 - ? ListView( + onRefresh: _loadList, + child: /*_list.tasks.length > 0 + ? */ListView( padding: EdgeInsets.symmetric(vertical: 8.0), children: ListTile.divideTiles( - context: context, tiles: _listTasks()) + context: context, + tiles: tasks) .toList(), - ) - : Center(child: Text('This list is empty.')), - onRefresh: _loadList, + ), + //: Center(child: Text('This list is empty.')), ) : Center(child: CircularProgressIndicator()), floatingActionButton: Builder( builder: (context) => FloatingActionButton( - onPressed: () => _addItemDialog(context), child: Icon(Icons.add)), + //onPressed: () => _addItemDialog(context), child: Icon(Icons.add)), + onPressed: () { + print('test'); + setState(() { + tasks = null; + }); + }, child: Icon(Icons.add), + ), )); } @@ -89,6 +101,20 @@ class _ListPageState extends State { ), ), ), + onMarkedAsDone: (done) { + VikunjaGlobal.of(context).taskService.update(Task( + id: task.id, + done: done, + )).then((newTask) => setState(() { + // FIXME: This is ugly. We should use a redux to not have to do these kind of things. + // This is enough for now (it works™) but we should definitly fix it later. + _list.tasks.asMap().forEach((i, t) { + if (newTask.id == t.id) { + _list.tasks[i] = newTask; + } + }); + })); + }, ); } @@ -145,4 +171,12 @@ class _ListPageState extends State { )); }); } + + Future _updateTask(Task task, bool checked) { + // TODO use copyFrom + return VikunjaGlobal.of(context).taskService.update(Task( + id: task.id, + done: checked, + )); + } } diff --git a/lib/pages/namespace/namespace.dart b/lib/pages/namespace/namespace.dart index 07f6f87..e678508 100644 --- a/lib/pages/namespace/namespace.dart +++ b/lib/pages/namespace/namespace.dart @@ -75,12 +75,6 @@ class _NamespacePageState extends State ); } - @override - void didChangeDependencies() { - super.didChangeDependencies(); - _loadLists(); - } - Future _removeList(TaskList list) { return VikunjaGlobal.of(context) .listService