Use provider to add a new task
continuous-integration/drone/pr Build is passing
Details
continuous-integration/drone/pr Build is passing
Details
This commit is contained in:
parent
a89068bbdf
commit
d4dbc5b4ae
|
@ -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<ListPage> {
|
||||
TaskList _list;
|
||||
List<Task> _tasks = [];
|
||||
List<Task> _loadingTasks = [];
|
||||
int _currentPage = 1;
|
||||
|
||||
@override
|
||||
|
@ -104,24 +101,15 @@ class _ListPageState extends State<ListPage> {
|
|||
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<ListProvider>(context, listen: false)
|
||||
.addTask(context: context, title: title, listId: _list.id)
|
||||
.then((_) {
|
||||
Scaffold.of(context).showSnackBar(SnackBar(
|
||||
content: Text('The task was added successfully!'),
|
||||
));
|
||||
|
|
|
@ -4,6 +4,7 @@ import 'package:vikunja_app/global.dart';
|
|||
|
||||
class ListProvider with ChangeNotifier {
|
||||
bool _isLoading = false;
|
||||
|
||||
// TODO: Streams
|
||||
List<Task> _tasks = [];
|
||||
|
||||
|
@ -30,4 +31,18 @@ class ListProvider with ChangeNotifier {
|
|||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> 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();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Reference in New Issue