Skip to content

Commit

Permalink
chore: Follow up todo list feature
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Oct 29, 2023
1 parent d0dbaa5 commit 5d38714
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 186 deletions.
61 changes: 41 additions & 20 deletions lib/pages/tasks/tasks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,19 @@ class TasksController extends State<TasksPage> {
update: (todos) => todos..removeWhere((t) => t.done),
);

void onReorder(int oldindex, int newindex) => updateTodos(
update: (todos) {
if (newindex > oldindex) {
newindex -= 1;
}
final todo = todos.removeAt(oldindex);
todos.insert(newindex, todo);

return todos;
},
tmpTodo: true,
);
void onReorder(int oldindex, int newindex) {
if (newindex > oldindex) {
newindex -= 1;
}
updateTodos(
update: (todos) {
final todo = todos.removeAt(oldindex);
todos.insert(newindex, todo);
return todos;
},
tmpTodo: true,
);
}

void updateTodos({
required List<MatrixTodo> Function(List<MatrixTodo>) update,
Expand All @@ -122,6 +123,7 @@ class TasksController extends State<TasksPage> {
});
try {
final newTodos = update(todos);
assert(todos != newTodos);
if (tmpTodo) {
setState(() {
_tmpTodos = newTodos;
Expand All @@ -130,17 +132,23 @@ class TasksController extends State<TasksPage> {
_tmpTodos = null;
});
}
await widget.room.updateMatrixTodos(newTodos);
await widget.room
.updateMatrixTodos(newTodos)
.timeout(const Duration(seconds: 30));
onSuccess?.call();
} on MatrixException catch (e) {
if (e.error != MatrixError.M_LIMIT_EXCEEDED) rethrow;
Logs().w('Rate limit! Try again in ${e.raw['retry_after_ms']}ms');
await Future.delayed(
Duration(milliseconds: e.raw['retry_after_ms'] as int),
);
final retryAfterMs = e.retryAfterMs;
if (retryAfterMs == null) rethrow;
Logs().w('Rate limit! Try again in $retryAfterMs ms');
await Future.delayed(Duration(milliseconds: retryAfterMs));
updateTodos(update: update, onSuccess: onSuccess);
} catch (e, s) {
Logs().w('Unable to toggle done', e, s);
Logs().w('Unable to update todo list', e, s);
if (_tmpTodos != null) {
setState(() {
_tmpTodos = null;
});
}
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
duration: const Duration(seconds: 20),
Expand All @@ -151,7 +159,13 @@ class TasksController extends State<TasksPage> {
color: Theme.of(context).colorScheme.background,
),
const SizedBox(width: 16),
Text(e.toLocalizedString(context)),
Expanded(
child: Text(
e.toLocalizedString(context),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
],
),
action: e is TodoListChangedException
Expand Down Expand Up @@ -207,6 +221,13 @@ class TasksController extends State<TasksPage> {
);
}

void deleteTodo(int i) => updateTodos(
update: (list) {
list.removeAt(i);
return list;
},
);

void editTodoDueDate(int i, MatrixTodo todo) async {
final now = DateTime.now();
final date = await showDatePicker(
Expand Down
Loading

0 comments on commit 5d38714

Please sign in to comment.