forked from vikunja/app
55 lines
1.4 KiB
Dart
55 lines
1.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:vikunja_app/models/task.dart';
|
|
import 'package:vikunja_app/global.dart';
|
|
|
|
class ListProvider with ChangeNotifier {
|
|
bool _isLoading = false;
|
|
int _maxPages = 0;
|
|
|
|
// TODO: Streams
|
|
List<Task> _tasks = [];
|
|
|
|
bool get isLoading => _isLoading;
|
|
|
|
int get maxPages => _maxPages;
|
|
|
|
set tasks(List<Task> tasks) {
|
|
_tasks = tasks;
|
|
notifyListeners();
|
|
}
|
|
|
|
List<Task> get tasks => _tasks;
|
|
|
|
void loadTasks({BuildContext context, int listId, int page = 1}) {
|
|
_isLoading = true;
|
|
notifyListeners();
|
|
|
|
VikunjaGlobal.of(context).taskService.getAll(listId, {
|
|
"sort_by": ["done", "id"],
|
|
"order_by": ["asc", "desc"],
|
|
"page": [page.toString()]
|
|
}).then((response) {
|
|
if (response.headers["x-pagination-total-pages"] != null) {
|
|
_maxPages = int.parse(response.headers["x-pagination-total-pages"]);
|
|
}
|
|
_tasks.addAll(response.body);
|
|
_isLoading = false;
|
|
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();
|
|
});
|
|
}
|
|
}
|