ListProvider from PR39 with fixes

This commit is contained in:
Aleksandr Borisenko 2021-06-10 10:58:25 +03:00
parent 21ef483c32
commit 9231772185
5 changed files with 141 additions and 105 deletions

View File

@ -47,10 +47,9 @@ class TaskTileState extends State<TaskTile> {
)),
),
title: Text(widget.task.title),
subtitle:
widget.task.description == null || widget.task.description.isEmpty
? null
: Text(widget.task.description),
subtitle: widget.task.description == null || widget.task.description.isEmpty
? null
: Text(widget.task.description),
trailing: IconButton(
icon: Icon(Icons.settings),
onPressed: () => widget.onEdit,
@ -61,10 +60,9 @@ class TaskTileState extends State<TaskTile> {
title: Text(widget.task.title),
controlAffinity: ListTileControlAffinity.leading,
value: widget.task.done ?? false,
subtitle:
widget.task.description == null || widget.task.description.isEmpty
? null
: Text(widget.task.description),
subtitle: widget.task.description == null || widget.task.description.isEmpty
? null
: Text(widget.task.description),
secondary: IconButton(
icon: Icon(Icons.settings),
onPressed: widget.onEdit,

View File

@ -43,7 +43,7 @@ class VikunjaDateTimePicker extends StatelessWidget {
return showDatePicker(
context: context,
firstDate: DateTime(1900),
initialDate: currentValue ?? DateTime.now(),
initialDate: currentValue.millisecondsSinceEpoch > 0 ? currentValue : DateTime.now(),
lastDate: DateTime(2100));
},
);

View File

@ -60,25 +60,25 @@ class Task {
?.toList(),
updated = DateTime.parse(json['updated']),
created = DateTime.parse(json['created']),
createdBy = User.fromJson(json['created_by']);
createdBy = json['created_by'] == null ? null : User.fromJson(json['created_by']);
toJSON() => {
'id': id,
'title': title,
'description': description,
'done': done ?? false,
'reminderDates': reminderDates
?.map((date) => datetimeToUnixTimestamp(date))
'reminder_dates': reminderDates
?.map((date) => date?.toIso8601String())
?.toList(),
'dueDate': datetimeToUnixTimestamp(dueDate),
'startDate': datetimeToUnixTimestamp(startDate),
'endDate': datetimeToUnixTimestamp(endDate),
'due_date': dueDate?.toIso8601String(),
'start_date': startDate?.toIso8601String(),
'end_date': endDate?.toIso8601String(),
'priority': priority,
'repeatAfter': repeatAfter?.inSeconds,
'repeat_after': repeatAfter?.inSeconds,
'labels': labels?.map((label) => label.toJSON())?.toList(),
'subtasks': subtasks?.map((subtask) => subtask.toJSON())?.toList(),
'createdBy': createdBy?.toJSON(),
'updated': datetimeToUnixTimestamp(updated),
'created': datetimeToUnixTimestamp(created),
'created_by': createdBy?.toJSON(),
'updated': updated?.toIso8601String(),
'created': created?.toIso8601String(),
};
}

View File

@ -1,6 +1,7 @@
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:vikunja_app/components/AddDialog.dart';
import 'package:vikunja_app/components/TaskTile.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/pages/list/list_edit.dart';
import 'package:vikunja_app/pages/list/task_edit.dart';
import 'package:vikunja_app/stores/list_store.dart';
class ListPage extends StatefulWidget {
final TaskList taskList;
@ -21,7 +24,7 @@ class ListPage extends StatefulWidget {
class _ListPageState extends State<ListPage> {
TaskList _list;
List<Task> _loadingTasks = [];
bool _loading = true;
int _currentPage = 1;
@override
void initState() {
@ -30,57 +33,71 @@ class _ListPageState extends State<ListPage> {
title: widget.taskList.title,
tasks: [],
);
Future.microtask(() => _loadList());
super.initState();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_loadList();
}
@override
Widget build(BuildContext context) {
var tasks = (_list?.tasks?.map(_buildTile) ?? []).toList();
tasks.addAll(_loadingTasks.map(_buildLoadingTile));
final taskState = Provider.of<ListProvider>(context);
return Scaffold(
appBar: AppBar(
title: Text(_list.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListEditPage(
list: _list,
appBar: AppBar(
title: Text(_list.title),
actions: <Widget>[
IconButton(
icon: Icon(Icons.edit),
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ListEditPage(
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) {
VikunjaGlobal.of(context)
.taskService
.update(Task(
id: task.id,
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;
}
});
})
Provider.of<ListProvider>(context, listen: false).updateTask(
context: context,
id: task.id,
done: done,
);
},
);
@ -132,33 +138,32 @@ class _ListPageState extends State<ListPage> {
);
}
Future<void> _loadList() {
return VikunjaGlobal.of(context)
.listService
.get(widget.taskList.id)
.then((list) {
setState(() {
_loading = false;
_list = list;
});
});
Future<void> _loadList() async {
_loadTasksForPage(1);
}
void _loadTasksForPage(int page) {
Provider.of<ListProvider>(context, listen: false).loadTasks(
context: context,
listId: _list.id,
page: page,
);
}
_addItemDialog(BuildContext context) {
showDialog(
context: context,
builder: (_) => AddDialog(
onAdd: (name) => _addItem(name, context),
decoration: new InputDecoration(
labelText: 'Task Name',
hintText: 'eg. Milk',
)
)
context: context,
builder: (_) => AddDialog(
onAdd: (title) => _addItem(title, context),
decoration: InputDecoration(
labelText: 'Task Name',
hintText: 'eg. Milk',
),
),
);
}
_addItem(String title, BuildContext context) {
// FIXME: Use provider
var globalState = VikunjaGlobal.of(context);
var newTask = Task(
id: null,
@ -167,13 +172,12 @@ class _ListPageState extends State<ListPage> {
done: false,
);
setState(() => _loadingTasks.add(newTask));
globalState.taskService.add(_list.id, newTask).then((task) {
setState(() {
_list.tasks.add(task);
});
}).then((_) {
_loadList();
setState(() => _loadingTasks.remove(newTask));
Provider.of<ListProvider>(context, listen: false)
.addTask(
context: context,
newTask: newTask,
listId: _list.id,
).then((_) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('The task was added successfully!'),
));

View File

@ -21,6 +21,7 @@ class ListProvider with ChangeNotifier {
List<Task> get tasks => _tasks;
void loadTasks({BuildContext context, int listId, int page = 1}) {
_tasks = [];
_isLoading = true;
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 newTask = Task(
id: null, title: title, createdBy: globalState.currentUser, done: false);
id: null,
title: title,
createdBy: globalState.currentUser,
done: false,
);
_isLoading = true;
notifyListeners();
@ -51,4 +56,33 @@ class ListProvider with ChangeNotifier {
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();
});
}
}