2022-04-12 23:02:16 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:vikunja_app/global.dart';
|
|
|
|
|
2022-04-25 21:48:07 +00:00
|
|
|
import 'dart:developer';
|
|
|
|
|
2022-04-12 23:02:16 +00:00
|
|
|
import '../components/AddDialog.dart';
|
2022-04-15 18:14:04 +00:00
|
|
|
import '../components/TaskTile.dart';
|
2022-04-12 23:02:16 +00:00
|
|
|
import '../models/task.dart';
|
|
|
|
|
|
|
|
class LandingPage extends StatefulWidget {
|
2022-04-25 21:48:07 +00:00
|
|
|
|
|
|
|
const LandingPage(
|
|
|
|
{Key key})
|
|
|
|
: super(key: key);
|
|
|
|
|
2022-04-12 23:02:16 +00:00
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => LandingPageState();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class LandingPageState extends State<LandingPage> {
|
|
|
|
int defaultList;
|
2022-04-15 18:14:04 +00:00
|
|
|
List<Task> _list;
|
|
|
|
|
2022-04-25 21:48:07 +00:00
|
|
|
void _updateDefaultList() {
|
|
|
|
VikunjaGlobal.of(context)
|
|
|
|
.listService
|
|
|
|
.getDefaultList()
|
|
|
|
.then((value) => setState(() => defaultList = value == null ? null : int.tryParse(value)));
|
|
|
|
}
|
|
|
|
|
2022-04-15 18:14:04 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
2022-04-25 21:48:07 +00:00
|
|
|
Future.delayed(Duration.zero, () => _updateDefaultList());
|
2022-04-15 18:14:04 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
2022-04-25 21:48:07 +00:00
|
|
|
|
|
|
|
|
2022-04-12 23:02:16 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-04-15 18:14:04 +00:00
|
|
|
if(_list == null)
|
|
|
|
_loadList(context);
|
2022-04-19 22:12:50 +00:00
|
|
|
VikunjaGlobal.of(context).scheduleDueNotifications();
|
2022-04-12 23:02:16 +00:00
|
|
|
return new Scaffold(
|
2022-04-21 19:35:34 +00:00
|
|
|
body: _list != null ? RefreshIndicator(child: ListView(
|
2022-04-15 18:14:04 +00:00
|
|
|
scrollDirection: Axis.vertical,
|
|
|
|
shrinkWrap: true,
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
children: ListTile.divideTiles(
|
|
|
|
context: context, tiles: _listTasks(context)).toList(),
|
2022-04-21 19:35:34 +00:00
|
|
|
), onRefresh: () => _loadList(context),)
|
|
|
|
|
|
|
|
: new Center(child: CircularProgressIndicator()),
|
2022-04-12 23:02:16 +00:00
|
|
|
floatingActionButton: Builder(
|
|
|
|
builder: (context) =>
|
|
|
|
defaultList == null ?
|
|
|
|
FloatingActionButton(
|
|
|
|
backgroundColor: Colors.grey,
|
|
|
|
onPressed: () {
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text('Please select a default list in the settings'),
|
|
|
|
));},
|
|
|
|
child: const Icon(Icons.add))
|
|
|
|
:
|
|
|
|
FloatingActionButton(
|
|
|
|
onPressed: () {
|
|
|
|
_addItemDialog(context);
|
|
|
|
},
|
|
|
|
child: const Icon(Icons.add),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
_addItemDialog(BuildContext context) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (_) => AddDialog(
|
2022-04-15 21:48:23 +00:00
|
|
|
onAddTask: (task) => _addTask(task, context),
|
2022-04-12 23:02:16 +00:00
|
|
|
decoration: new InputDecoration(
|
|
|
|
labelText: 'Task Name', hintText: 'eg. Milk')));
|
|
|
|
}
|
|
|
|
|
2022-04-15 21:48:23 +00:00
|
|
|
_addTask(Task task, BuildContext context) {
|
2022-04-12 23:02:16 +00:00
|
|
|
var globalState = VikunjaGlobal.of(context);
|
2022-04-15 21:48:23 +00:00
|
|
|
globalState.taskService.add(defaultList, task).then((_) {
|
2022-04-12 23:02:16 +00:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
|
|
|
content: Text('The task was added successfully!'),
|
|
|
|
));
|
2022-04-15 18:14:04 +00:00
|
|
|
_loadList(context).then((value) => setState((){}));
|
2022-04-12 23:02:16 +00:00
|
|
|
});
|
|
|
|
}
|
2022-04-15 18:14:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
List<Widget> _listTasks(BuildContext context) {
|
|
|
|
var tasks = (_list.map((task) => _buildTile(task, context)) ?? []).toList();
|
|
|
|
//tasks.addAll(_loadingTasks.map(_buildLoadingTile));
|
|
|
|
return tasks;
|
|
|
|
}
|
|
|
|
|
|
|
|
TaskTile _buildTile(Task task, BuildContext context) {
|
|
|
|
// key: UniqueKey() seems like a weird workaround to fix the loading issue
|
|
|
|
// is there a better way?
|
|
|
|
return TaskTile(key: UniqueKey(), task: task,onEdit: () => _loadList(context), showInfo: true,);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _loadList(BuildContext context) {
|
|
|
|
_list = [];
|
2022-04-21 19:45:54 +00:00
|
|
|
// FIXME: loads and reschedules tasks each time list is updated
|
|
|
|
VikunjaGlobal.of(context).scheduleDueNotifications();
|
2022-04-15 18:14:04 +00:00
|
|
|
return VikunjaGlobal.of(context)
|
|
|
|
.taskService
|
|
|
|
.getByOptions(VikunjaGlobal.of(context).taskServiceOptions)
|
|
|
|
.then((taskList) {
|
|
|
|
VikunjaGlobal.of(context)
|
|
|
|
.listService
|
|
|
|
.getAll()
|
|
|
|
.then((lists) {
|
2022-04-20 20:57:21 +00:00
|
|
|
//taskList.forEach((task) {task.list = lists.firstWhere((element) => element.id == task.list_id);});
|
2022-04-15 18:14:04 +00:00
|
|
|
setState(() {
|
|
|
|
_list = taskList;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-04-12 23:02:16 +00:00
|
|
|
}
|