ListProvider from PR39 with fixes
This commit is contained in:
parent
21ef483c32
commit
9231772185
|
@ -47,10 +47,9 @@ class TaskTileState extends State<TaskTile> {
|
||||||
)),
|
)),
|
||||||
),
|
),
|
||||||
title: Text(widget.task.title),
|
title: Text(widget.task.title),
|
||||||
subtitle:
|
subtitle: widget.task.description == null || widget.task.description.isEmpty
|
||||||
widget.task.description == null || widget.task.description.isEmpty
|
? null
|
||||||
? null
|
: Text(widget.task.description),
|
||||||
: Text(widget.task.description),
|
|
||||||
trailing: IconButton(
|
trailing: IconButton(
|
||||||
icon: Icon(Icons.settings),
|
icon: Icon(Icons.settings),
|
||||||
onPressed: () => widget.onEdit,
|
onPressed: () => widget.onEdit,
|
||||||
|
@ -61,10 +60,9 @@ class TaskTileState extends State<TaskTile> {
|
||||||
title: Text(widget.task.title),
|
title: Text(widget.task.title),
|
||||||
controlAffinity: ListTileControlAffinity.leading,
|
controlAffinity: ListTileControlAffinity.leading,
|
||||||
value: widget.task.done ?? false,
|
value: widget.task.done ?? false,
|
||||||
subtitle:
|
subtitle: widget.task.description == null || widget.task.description.isEmpty
|
||||||
widget.task.description == null || widget.task.description.isEmpty
|
? null
|
||||||
? null
|
: Text(widget.task.description),
|
||||||
: Text(widget.task.description),
|
|
||||||
secondary: IconButton(
|
secondary: IconButton(
|
||||||
icon: Icon(Icons.settings),
|
icon: Icon(Icons.settings),
|
||||||
onPressed: widget.onEdit,
|
onPressed: widget.onEdit,
|
||||||
|
|
|
@ -43,7 +43,7 @@ class VikunjaDateTimePicker extends StatelessWidget {
|
||||||
return showDatePicker(
|
return showDatePicker(
|
||||||
context: context,
|
context: context,
|
||||||
firstDate: DateTime(1900),
|
firstDate: DateTime(1900),
|
||||||
initialDate: currentValue ?? DateTime.now(),
|
initialDate: currentValue.millisecondsSinceEpoch > 0 ? currentValue : DateTime.now(),
|
||||||
lastDate: DateTime(2100));
|
lastDate: DateTime(2100));
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -60,25 +60,25 @@ class Task {
|
||||||
?.toList(),
|
?.toList(),
|
||||||
updated = DateTime.parse(json['updated']),
|
updated = DateTime.parse(json['updated']),
|
||||||
created = DateTime.parse(json['created']),
|
created = DateTime.parse(json['created']),
|
||||||
createdBy = User.fromJson(json['created_by']);
|
createdBy = json['created_by'] == null ? null : User.fromJson(json['created_by']);
|
||||||
|
|
||||||
toJSON() => {
|
toJSON() => {
|
||||||
'id': id,
|
'id': id,
|
||||||
'title': title,
|
'title': title,
|
||||||
'description': description,
|
'description': description,
|
||||||
'done': done ?? false,
|
'done': done ?? false,
|
||||||
'reminderDates': reminderDates
|
'reminder_dates': reminderDates
|
||||||
?.map((date) => datetimeToUnixTimestamp(date))
|
?.map((date) => date?.toIso8601String())
|
||||||
?.toList(),
|
?.toList(),
|
||||||
'dueDate': datetimeToUnixTimestamp(dueDate),
|
'due_date': dueDate?.toIso8601String(),
|
||||||
'startDate': datetimeToUnixTimestamp(startDate),
|
'start_date': startDate?.toIso8601String(),
|
||||||
'endDate': datetimeToUnixTimestamp(endDate),
|
'end_date': endDate?.toIso8601String(),
|
||||||
'priority': priority,
|
'priority': priority,
|
||||||
'repeatAfter': repeatAfter?.inSeconds,
|
'repeat_after': repeatAfter?.inSeconds,
|
||||||
'labels': labels?.map((label) => label.toJSON())?.toList(),
|
'labels': labels?.map((label) => label.toJSON())?.toList(),
|
||||||
'subtasks': subtasks?.map((subtask) => subtask.toJSON())?.toList(),
|
'subtasks': subtasks?.map((subtask) => subtask.toJSON())?.toList(),
|
||||||
'createdBy': createdBy?.toJSON(),
|
'created_by': createdBy?.toJSON(),
|
||||||
'updated': datetimeToUnixTimestamp(updated),
|
'updated': updated?.toIso8601String(),
|
||||||
'created': datetimeToUnixTimestamp(created),
|
'created': created?.toIso8601String(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:provider/provider.dart';
|
||||||
import 'package:vikunja_app/components/AddDialog.dart';
|
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';
|
||||||
|
@ -8,6 +9,8 @@ import 'package:vikunja_app/models/list.dart';
|
||||||
import 'package:vikunja_app/models/task.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/pages/list/task_edit.dart';
|
import 'package:vikunja_app/pages/list/task_edit.dart';
|
||||||
|
import 'package:vikunja_app/stores/list_store.dart';
|
||||||
|
|
||||||
|
|
||||||
class ListPage extends StatefulWidget {
|
class ListPage extends StatefulWidget {
|
||||||
final TaskList taskList;
|
final TaskList taskList;
|
||||||
|
@ -21,7 +24,7 @@ class ListPage extends StatefulWidget {
|
||||||
class _ListPageState extends State<ListPage> {
|
class _ListPageState extends State<ListPage> {
|
||||||
TaskList _list;
|
TaskList _list;
|
||||||
List<Task> _loadingTasks = [];
|
List<Task> _loadingTasks = [];
|
||||||
bool _loading = true;
|
int _currentPage = 1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
|
@ -30,57 +33,71 @@ class _ListPageState extends State<ListPage> {
|
||||||
title: widget.taskList.title,
|
title: widget.taskList.title,
|
||||||
tasks: [],
|
tasks: [],
|
||||||
);
|
);
|
||||||
|
Future.microtask(() => _loadList());
|
||||||
super.initState();
|
super.initState();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
void didChangeDependencies() {
|
|
||||||
super.didChangeDependencies();
|
|
||||||
_loadList();
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
var tasks = (_list?.tasks?.map(_buildTile) ?? []).toList();
|
final taskState = Provider.of<ListProvider>(context);
|
||||||
tasks.addAll(_loadingTasks.map(_buildLoadingTile));
|
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text(_list.title),
|
title: Text(_list.title),
|
||||||
actions: <Widget>[
|
actions: <Widget>[
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: Icon(Icons.edit),
|
icon: Icon(Icons.edit),
|
||||||
onPressed: () => Navigator.push(
|
onPressed: () => Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => ListEditPage(
|
builder: (context) => ListEditPage(
|
||||||
list: _list,
|
list: _list,
|
||||||
|
),
|
||||||
|
)
|
||||||
),
|
),
|
||||||
)
|
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
],
|
|
||||||
),
|
|
||||||
body: !this._loading
|
|
||||||
? RefreshIndicator(
|
|
||||||
onRefresh: _loadList,
|
|
||||||
child: _list.tasks.length > 0
|
|
||||||
? ListView(
|
|
||||||
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
||||||
children: ListTile.divideTiles(
|
|
||||||
context: context,
|
|
||||||
tiles: tasks,
|
|
||||||
).toList(),
|
|
||||||
)
|
|
||||||
: Center(child: Text('This list is empty.')),
|
|
||||||
)
|
|
||||||
: Center(child: CircularProgressIndicator()),
|
|
||||||
floatingActionButton: Builder(
|
|
||||||
builder: (context) => FloatingActionButton(
|
|
||||||
onPressed: () => _addItemDialog(context),
|
|
||||||
child: Icon(Icons.add),
|
|
||||||
),
|
),
|
||||||
),
|
// TODO: it brakes the flow with _loadingTasks and conflicts with the provider
|
||||||
|
body: !taskState.isLoading
|
||||||
|
? RefreshIndicator(
|
||||||
|
child: taskState.tasks.length > 0
|
||||||
|
? ListenableProvider.value(
|
||||||
|
value: taskState,
|
||||||
|
child: ListView.builder(
|
||||||
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
||||||
|
itemBuilder: (context, i) {
|
||||||
|
if (i.isOdd) return Divider();
|
||||||
|
|
||||||
|
if (_loadingTasks.isNotEmpty) {
|
||||||
|
final loadingTask = _loadingTasks.removeLast();
|
||||||
|
return _buildLoadingTile(loadingTask);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (taskState.maxPages == _currentPage &&
|
||||||
|
index == taskState.tasks.length - 1) return null;
|
||||||
|
|
||||||
|
if (index >= taskState.tasks.length &&
|
||||||
|
_currentPage < taskState.maxPages) {
|
||||||
|
_currentPage++;
|
||||||
|
_loadTasksForPage(_currentPage);
|
||||||
|
}
|
||||||
|
return index < taskState.tasks.length
|
||||||
|
? _buildTile(taskState.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)),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,21 +114,10 @@ class _ListPageState extends State<ListPage> {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
onMarkedAsDone: (done) {
|
onMarkedAsDone: (done) {
|
||||||
VikunjaGlobal.of(context)
|
Provider.of<ListProvider>(context, listen: false).updateTask(
|
||||||
.taskService
|
context: context,
|
||||||
.update(Task(
|
id: task.id,
|
||||||
id: task.id,
|
done: done,
|
||||||
done: done,
|
|
||||||
)
|
|
||||||
).then((newTask) => setState(() {
|
|
||||||
// FIXME: This is ugly. We should use a redux to not have to do these kind of things.
|
|
||||||
// This is enough for now (it works™) but we should definitly fix it later.
|
|
||||||
_list.tasks.asMap().forEach((i, t) {
|
|
||||||
if (newTask.id == t.id) {
|
|
||||||
_list.tasks[i] = newTask;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
@ -132,33 +138,32 @@ class _ListPageState extends State<ListPage> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadList() {
|
Future<void> _loadList() async {
|
||||||
return VikunjaGlobal.of(context)
|
_loadTasksForPage(1);
|
||||||
.listService
|
}
|
||||||
.get(widget.taskList.id)
|
|
||||||
.then((list) {
|
void _loadTasksForPage(int page) {
|
||||||
setState(() {
|
Provider.of<ListProvider>(context, listen: false).loadTasks(
|
||||||
_loading = false;
|
context: context,
|
||||||
_list = list;
|
listId: _list.id,
|
||||||
});
|
page: page,
|
||||||
});
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addItemDialog(BuildContext context) {
|
_addItemDialog(BuildContext context) {
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (_) => AddDialog(
|
builder: (_) => AddDialog(
|
||||||
onAdd: (name) => _addItem(name, context),
|
onAdd: (title) => _addItem(title, context),
|
||||||
decoration: new InputDecoration(
|
decoration: InputDecoration(
|
||||||
labelText: 'Task Name',
|
labelText: 'Task Name',
|
||||||
hintText: 'eg. Milk',
|
hintText: 'eg. Milk',
|
||||||
)
|
),
|
||||||
)
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addItem(String title, BuildContext context) {
|
_addItem(String title, BuildContext context) {
|
||||||
// FIXME: Use provider
|
|
||||||
var globalState = VikunjaGlobal.of(context);
|
var globalState = VikunjaGlobal.of(context);
|
||||||
var newTask = Task(
|
var newTask = Task(
|
||||||
id: null,
|
id: null,
|
||||||
|
@ -167,13 +172,12 @@ class _ListPageState extends State<ListPage> {
|
||||||
done: false,
|
done: false,
|
||||||
);
|
);
|
||||||
setState(() => _loadingTasks.add(newTask));
|
setState(() => _loadingTasks.add(newTask));
|
||||||
globalState.taskService.add(_list.id, newTask).then((task) {
|
Provider.of<ListProvider>(context, listen: false)
|
||||||
setState(() {
|
.addTask(
|
||||||
_list.tasks.add(task);
|
context: context,
|
||||||
});
|
newTask: newTask,
|
||||||
}).then((_) {
|
listId: _list.id,
|
||||||
_loadList();
|
).then((_) {
|
||||||
setState(() => _loadingTasks.remove(newTask));
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
|
||||||
content: Text('The task was added successfully!'),
|
content: Text('The task was added successfully!'),
|
||||||
));
|
));
|
||||||
|
|
|
@ -21,6 +21,7 @@ class ListProvider with ChangeNotifier {
|
||||||
List<Task> get tasks => _tasks;
|
List<Task> get tasks => _tasks;
|
||||||
|
|
||||||
void loadTasks({BuildContext context, int listId, int page = 1}) {
|
void loadTasks({BuildContext context, int listId, int page = 1}) {
|
||||||
|
_tasks = [];
|
||||||
_isLoading = true;
|
_isLoading = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
|
@ -38,10 +39,14 @@ class ListProvider with ChangeNotifier {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> addTask({BuildContext context, String title, int listId}) {
|
Future<void> addTaskByTitle({BuildContext context, String title, int listId}) {
|
||||||
var globalState = VikunjaGlobal.of(context);
|
var globalState = VikunjaGlobal.of(context);
|
||||||
var newTask = Task(
|
var newTask = Task(
|
||||||
id: null, title: title, createdBy: globalState.currentUser, done: false);
|
id: null,
|
||||||
|
title: title,
|
||||||
|
createdBy: globalState.currentUser,
|
||||||
|
done: false,
|
||||||
|
);
|
||||||
_isLoading = true;
|
_isLoading = true;
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
|
|
||||||
|
@ -51,4 +56,33 @@ class ListProvider with ChangeNotifier {
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> addTask({BuildContext context, Task newTask, int listId}) {
|
||||||
|
var globalState = VikunjaGlobal.of(context);
|
||||||
|
_isLoading = true;
|
||||||
|
notifyListeners();
|
||||||
|
|
||||||
|
return globalState.taskService.add(listId, newTask).then((task) {
|
||||||
|
_tasks.insert(0, task);
|
||||||
|
_isLoading = false;
|
||||||
|
notifyListeners();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateTask({BuildContext context, int id, bool done}) {
|
||||||
|
var globalState = VikunjaGlobal.of(context);
|
||||||
|
globalState.taskService.update(Task(
|
||||||
|
id: id,
|
||||||
|
done: done,
|
||||||
|
)).then((task) {
|
||||||
|
// FIXME: This is ugly. We should use a redux to not have to do these kind of things.
|
||||||
|
// This is enough for now (it works™) but we should definitly fix it later.
|
||||||
|
_tasks.asMap().forEach((i, t) {
|
||||||
|
if (task.id == t.id) {
|
||||||
|
_tasks[i] = task;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
notifyListeners();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue
Block a user