diff --git a/lib/pages/list/list.dart b/lib/pages/list/list.dart index 4003cf5..d9447b4 100644 --- a/lib/pages/list/list.dart +++ b/lib/pages/list/list.dart @@ -6,7 +6,6 @@ import 'package:vikunja_app/components/AddDialog.dart'; import 'package:vikunja_app/components/TaskTile.dart'; import 'package:vikunja_app/global.dart'; import 'package:vikunja_app/models/list.dart'; -import 'package:vikunja_app/models/task.dart'; import 'package:vikunja_app/pages/list/list_edit.dart'; import 'package:vikunja_app/stores/list_store.dart'; @@ -21,8 +20,6 @@ class ListPage extends StatefulWidget { class _ListPageState extends State { TaskList _list; - List _tasks = []; - List _loadingTasks = []; int _currentPage = 1; @override @@ -104,24 +101,15 @@ class _ListPageState extends State { showDialog( context: context, builder: (_) => AddDialog( - onAdd: (name) => _addItem(name, context), + onAdd: (title) => _addItem(title, context), decoration: new InputDecoration( labelText: 'Task Name', hintText: 'eg. Milk'))); } - _addItem(String name, BuildContext context) { - // FIXME: Use provider - var globalState = VikunjaGlobal.of(context); - var newTask = Task( - id: null, title: name, owner: globalState.currentUser, done: false); - setState(() => _loadingTasks.add(newTask)); - globalState.taskService.add(_list.id, newTask).then((task) { - setState(() { - _tasks.add(task); - }); - }).then((_) { - _loadList(); - setState(() => _loadingTasks.remove(newTask)); + _addItem(String title, BuildContext context) { + Provider.of(context, listen: false) + .addTask(context: context, title: title, listId: _list.id) + .then((_) { Scaffold.of(context).showSnackBar(SnackBar( content: Text('The task was added successfully!'), )); diff --git a/lib/stores/list_store.dart b/lib/stores/list_store.dart index 9918488..cb2643b 100644 --- a/lib/stores/list_store.dart +++ b/lib/stores/list_store.dart @@ -4,6 +4,7 @@ import 'package:vikunja_app/global.dart'; class ListProvider with ChangeNotifier { bool _isLoading = false; + // TODO: Streams List _tasks = []; @@ -30,4 +31,18 @@ class ListProvider with ChangeNotifier { notifyListeners(); }); } + + Future addTask({BuildContext context, String title, int listId}) { + var globalState = VikunjaGlobal.of(context); + var newTask = Task( + id: null, title: title, owner: globalState.currentUser, done: false); + _isLoading = true; + notifyListeners(); + + return globalState.taskService.add(listId, newTask).then((task) { + _tasks.insert(0, task); + _isLoading = false; + notifyListeners(); + }); + } }