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/components/TaskTile.dart';
|
||||||
import 'package:vikunja_app/global.dart';
|
import 'package:vikunja_app/global.dart';
|
||||||
import 'package:vikunja_app/models/list.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/pages/list/list_edit.dart';
|
||||||
import 'package:vikunja_app/stores/list_store.dart';
|
import 'package:vikunja_app/stores/list_store.dart';
|
||||||
|
|
||||||
|
@ -21,8 +20,6 @@ class ListPage extends StatefulWidget {
|
||||||
|
|
||||||
class _ListPageState extends State<ListPage> {
|
class _ListPageState extends State<ListPage> {
|
||||||
TaskList _list;
|
TaskList _list;
|
||||||
List<Task> _tasks = [];
|
|
||||||
List<Task> _loadingTasks = [];
|
|
||||||
int _currentPage = 1;
|
int _currentPage = 1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -104,24 +101,15 @@ class _ListPageState extends State<ListPage> {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (_) => AddDialog(
|
builder: (_) => AddDialog(
|
||||||
onAdd: (name) => _addItem(name, context),
|
onAdd: (title) => _addItem(title, context),
|
||||||
decoration: new InputDecoration(
|
decoration: new InputDecoration(
|
||||||
labelText: 'Task Name', hintText: 'eg. Milk')));
|
labelText: 'Task Name', hintText: 'eg. Milk')));
|
||||||
}
|
}
|
||||||
|
|
||||||
_addItem(String name, BuildContext context) {
|
_addItem(String title, BuildContext context) {
|
||||||
// FIXME: Use provider
|
Provider.of<ListProvider>(context, listen: false)
|
||||||
var globalState = VikunjaGlobal.of(context);
|
.addTask(context: context, title: title, listId: _list.id)
|
||||||
var newTask = Task(
|
.then((_) {
|
||||||
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));
|
|
||||||
Scaffold.of(context).showSnackBar(SnackBar(
|
Scaffold.of(context).showSnackBar(SnackBar(
|
||||||
content: Text('The task was added successfully!'),
|
content: Text('The task was added successfully!'),
|
||||||
));
|
));
|
||||||
|
|
|
@ -4,6 +4,7 @@ import 'package:vikunja_app/global.dart';
|
||||||
|
|
||||||
class ListProvider with ChangeNotifier {
|
class ListProvider with ChangeNotifier {
|
||||||
bool _isLoading = false;
|
bool _isLoading = false;
|
||||||
|
|
||||||
// TODO: Streams
|
// TODO: Streams
|
||||||
List<Task> _tasks = [];
|
List<Task> _tasks = [];
|
||||||
|
|
||||||
|
@ -30,4 +31,18 @@ class ListProvider with ChangeNotifier {
|
||||||
notifyListeners();
|
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