147 lines
4.6 KiB
Dart
147 lines
4.6 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
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';
|
|
|
|
class ListPage extends StatefulWidget {
|
|
final TaskList taskList;
|
|
|
|
ListPage({this.taskList}) : super(key: Key(taskList.id.toString()));
|
|
|
|
@override
|
|
_ListPageState createState() => _ListPageState();
|
|
}
|
|
|
|
class _ListPageState extends State<ListPage> {
|
|
TaskList _list;
|
|
List<Task> _tasks = [];
|
|
List<Task> _loadingTasks = [];
|
|
bool _loading = true;
|
|
int _currentPage = 1;
|
|
|
|
@override
|
|
void initState() {
|
|
_list = TaskList(id: widget.taskList.id, title: widget.taskList.title);
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
_loadList();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: new Text(_list.title),
|
|
actions: <Widget>[
|
|
IconButton(
|
|
icon: Icon(Icons.edit),
|
|
onPressed: () => Navigator.push(
|
|
context,
|
|
MaterialPageRoute(
|
|
builder: (context) => ListEditPage(
|
|
list: _list,
|
|
))))
|
|
],
|
|
),
|
|
body: !this._loading
|
|
? RefreshIndicator(
|
|
child: _tasks.length > 0
|
|
? ListView.builder(
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
itemBuilder: (context, i) {
|
|
if (i.isOdd) return Divider();
|
|
|
|
final index = i ~/ 2;
|
|
|
|
// This handles the case if there are no more elements in the list left which can be provided by the api
|
|
if (VikunjaGlobal.of(context).taskService.maxPages ==
|
|
_currentPage &&
|
|
index == _tasks.length - 1) return null;
|
|
|
|
if (index >= _tasks.length &&
|
|
_currentPage <
|
|
VikunjaGlobal.of(context)
|
|
.taskService
|
|
.maxPages) {
|
|
_currentPage++;
|
|
_loadTasksForPage(_currentPage);
|
|
}
|
|
return index < _tasks.length
|
|
? TaskTile(
|
|
task: _tasks[index],
|
|
)
|
|
: null;
|
|
})
|
|
: Center(child: Text('This list is empty.')),
|
|
onRefresh: _loadList,
|
|
)
|
|
: Center(child: CircularProgressIndicator()),
|
|
floatingActionButton: Builder(
|
|
builder: (context) => FloatingActionButton(
|
|
onPressed: () => _addItemDialog(context), child: Icon(Icons.add)),
|
|
));
|
|
}
|
|
|
|
Future<void> _loadList() {
|
|
return VikunjaGlobal.of(context)
|
|
.listService
|
|
.get(widget.taskList.id)
|
|
.then((list) {
|
|
setState(() {
|
|
_loading = true;
|
|
_list = list;
|
|
});
|
|
_loadTasksForPage(_currentPage);
|
|
});
|
|
}
|
|
|
|
Future<void> _loadTasksForPage(int page) {
|
|
return VikunjaGlobal.of(context).taskService.getAll(_list.id, {
|
|
"sort_by": "done",
|
|
"order_by": "asc",
|
|
"page": page.toString()
|
|
}).then((tasks) {
|
|
setState(() {
|
|
_loading = false;
|
|
_tasks.addAll(tasks);
|
|
});
|
|
});
|
|
}
|
|
|
|
_addItemDialog(BuildContext context) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (_) => AddDialog(
|
|
onAdd: (name) => _addItem(name, context),
|
|
decoration: new InputDecoration(
|
|
labelText: 'Task Name', hintText: 'eg. Milk')));
|
|
}
|
|
|
|
_addItem(String name, BuildContext context) {
|
|
var globalState = VikunjaGlobal.of(context);
|
|
var newTask =
|
|
Task(id: null, text: 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(
|
|
content: Text('The task was added successfully!'),
|
|
));
|
|
});
|
|
}
|
|
}
|