Fixed tasks not being updated

This commit is contained in:
kolaente 2019-03-18 20:17:11 +01:00
parent 45bb3c2c8c
commit 9f289cf11e
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 56 additions and 29 deletions

View File

@ -7,26 +7,25 @@ import 'package:vikunja_app/models/task.dart';
class TaskTile extends StatefulWidget {
final Task task;
final VoidCallback onEdit;
final ValueSetter<bool> 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<TaskTile> {
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<TaskTile> {
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<TaskTile> {
);
}
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<TaskTile> {
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;
});
}

View File

@ -38,6 +38,10 @@ class _ListPageState extends State<ListPage> {
@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<ListPage> {
),
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<ListPage> {
),
),
),
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<ListPage> {
));
});
}
Future<Task> _updateTask(Task task, bool checked) {
// TODO use copyFrom
return VikunjaGlobal.of(context).taskService.update(Task(
id: task.id,
done: checked,
));
}
}

View File

@ -75,12 +75,6 @@ class _NamespacePageState extends State<NamespacePage>
);
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadLists();
}
Future _removeList(TaskList list) {
return VikunjaGlobal.of(context)
.listService