app-mirror-github/lib/pages/landing_page.dart

168 lines
4.7 KiB
Dart
Raw Normal View History

2022-04-28 11:58:27 +00:00
import 'package:after_layout/after_layout.dart';
import 'package:flutter/material.dart';
2022-04-28 11:58:27 +00:00
import 'package:flutter/services.dart';
import 'package:vikunja_app/global.dart';
import 'dart:developer';
import '../components/AddDialog.dart';
2022-04-15 18:14:04 +00:00
import '../components/TaskTile.dart';
import '../models/task.dart';
class LandingPage extends StatefulWidget {
const LandingPage(
2022-08-27 21:04:43 +00:00
{Key? key})
: super(key: key);
@override
State<StatefulWidget> createState() => LandingPageState();
}
2022-04-28 11:58:27 +00:00
class LandingPageState extends State<LandingPage> with AfterLayoutMixin<LandingPage> {
2022-08-27 21:04:43 +00:00
int? defaultList;
List<Task>? _list;
2022-04-28 11:58:27 +00:00
static const platform = const MethodChannel('vikunja');
2022-04-15 18:14:04 +00:00
2022-04-28 11:58:27 +00:00
Future<void> _updateDefaultList() async {
return 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-28 11:58:27 +00:00
Future.delayed(Duration.zero, () =>
_updateDefaultList().then((value) {
try {
platform.invokeMethod("isQuickTile","").then((value) => {
if(value is bool && value)
_addItemDialog(context)
});
} catch (e) {
log(e.toString());
}}));
2022-04-15 18:14:04 +00:00
super.initState();
}
2022-04-28 11:58:27 +00:00
@override
void afterFirstLayout(BuildContext context) {
try {
// This is needed when app is already open and quicktile is clicked
platform.setMethodCallHandler((call) {
switch (call.method) {
case "open_add_task":
_addItemDialog(context);
break;
}
return Future.value();
});
} catch (e) {
log(e.toString());
}
}
@override
Widget build(BuildContext context) {
2022-08-27 21:04:43 +00:00
if(_list == null || _list!.isEmpty)
2022-04-15 18:14:04 +00:00
_loadList(context);
return new Scaffold(
body: RefreshIndicator(
onRefresh: () => _loadList(context),
child: _list != null ? ListView(
scrollDirection: Axis.vertical,
shrinkWrap: true,
padding: EdgeInsets.symmetric(vertical: 8.0),
children: ListTile.divideTiles(
context: context, tiles: _listTasks(context)).toList(),
) : new Center(child: CircularProgressIndicator(),),
),
floatingActionButton: Builder(
builder: (context) =>
FloatingActionButton(
onPressed: () {
_addItemDialog(context);
},
child: const Icon(Icons.add),
)
)
);
}
_addItemDialog(BuildContext context) {
2022-04-28 11:58:27 +00:00
if(defaultList == null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Please select a default list in the settings'),
));
} else {
showDialog(
context: context,
builder: (_) =>
AddDialog(
2022-09-03 15:43:16 +00:00
onAddTask: (title, dueDate) => _addTask(title, dueDate, context),
2022-04-28 11:58:27 +00:00
decoration: new InputDecoration(
labelText: 'Task Name', hintText: 'eg. Milk')));
}
}
2022-09-03 15:43:16 +00:00
Future<void> _addTask(
String title, DateTime? dueDate, BuildContext context) async {
final globalState = VikunjaGlobal.of(context);
if (globalState.currentUser == null) {
return;
}
await globalState.taskService.add(
defaultList!,
Task(
createdBy: globalState.currentUser!,
listId: defaultList!,
),
);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The task was added successfully!'),
));
_loadList(context).then((value) => setState(() {}));
}
2022-04-15 18:14:04 +00:00
List<Widget> _listTasks(BuildContext context) {
2022-08-27 21:04:43 +00:00
var tasks = (_list?.map((task) => _buildTile(task, context)) ?? []).toList();
2022-04-15 18:14:04 +00:00
//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 = [];
// 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) {
//taskList.forEach((task) {task.list = lists.firstWhere((element) => element.id == task.list_id);});
2022-04-15 18:14:04 +00:00
setState(() {
_list = taskList;
});
});
});
}
}