chore: Follow up todo list feature

This commit is contained in:
krille-chan 2023-10-29 07:55:00 +01:00
parent d0dbaa5e72
commit 5d387145c8
No known key found for this signature in database
2 changed files with 233 additions and 191 deletions

View file

@ -99,18 +99,19 @@ class TasksController extends State<TasksPage> {
update: (todos) => todos..removeWhere((t) => t.done), update: (todos) => todos..removeWhere((t) => t.done),
); );
void onReorder(int oldindex, int newindex) => updateTodos( void onReorder(int oldindex, int newindex) {
update: (todos) {
if (newindex > oldindex) { if (newindex > oldindex) {
newindex -= 1; newindex -= 1;
} }
updateTodos(
update: (todos) {
final todo = todos.removeAt(oldindex); final todo = todos.removeAt(oldindex);
todos.insert(newindex, todo); todos.insert(newindex, todo);
return todos; return todos;
}, },
tmpTodo: true, tmpTodo: true,
); );
}
void updateTodos({ void updateTodos({
required List<MatrixTodo> Function(List<MatrixTodo>) update, required List<MatrixTodo> Function(List<MatrixTodo>) update,
@ -122,6 +123,7 @@ class TasksController extends State<TasksPage> {
}); });
try { try {
final newTodos = update(todos); final newTodos = update(todos);
assert(todos != newTodos);
if (tmpTodo) { if (tmpTodo) {
setState(() { setState(() {
_tmpTodos = newTodos; _tmpTodos = newTodos;
@ -130,17 +132,23 @@ class TasksController extends State<TasksPage> {
_tmpTodos = null; _tmpTodos = null;
}); });
} }
await widget.room.updateMatrixTodos(newTodos); await widget.room
.updateMatrixTodos(newTodos)
.timeout(const Duration(seconds: 30));
onSuccess?.call(); onSuccess?.call();
} on MatrixException catch (e) { } on MatrixException catch (e) {
if (e.error != MatrixError.M_LIMIT_EXCEEDED) rethrow; final retryAfterMs = e.retryAfterMs;
Logs().w('Rate limit! Try again in ${e.raw['retry_after_ms']}ms'); if (retryAfterMs == null) rethrow;
await Future.delayed( Logs().w('Rate limit! Try again in $retryAfterMs ms');
Duration(milliseconds: e.raw['retry_after_ms'] as int), await Future.delayed(Duration(milliseconds: retryAfterMs));
);
updateTodos(update: update, onSuccess: onSuccess); updateTodos(update: update, onSuccess: onSuccess);
} catch (e, s) { } 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( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
duration: const Duration(seconds: 20), duration: const Duration(seconds: 20),
@ -151,7 +159,13 @@ class TasksController extends State<TasksPage> {
color: Theme.of(context).colorScheme.background, color: Theme.of(context).colorScheme.background,
), ),
const SizedBox(width: 16), const SizedBox(width: 16),
Text(e.toLocalizedString(context)), Expanded(
child: Text(
e.toLocalizedString(context),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
], ],
), ),
action: e is TodoListChangedException action: e is TodoListChangedException
@ -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 { void editTodoDueDate(int i, MatrixTodo todo) async {
final now = DateTime.now(); final now = DateTime.now();
final date = await showDatePicker( final date = await showDatePicker(

View file

@ -13,6 +13,10 @@ class TasksView extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final tag = Localizations.maybeLocaleOf(context)?.toLanguageTag(); final tag = Localizations.maybeLocaleOf(context)?.toLanguageTag();
return StreamBuilder<Object>(
stream: controller.widget.room.onUpdate.stream,
builder: (context, snapshot) {
final list = controller.todos;
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(L10n.of(context)!.todoLists), title: Text(L10n.of(context)!.todoLists),
@ -35,6 +39,7 @@ class TasksView extends StatelessWidget {
? CrossFadeState.showSecond ? CrossFadeState.showSecond
: CrossFadeState.showFirst, : CrossFadeState.showFirst,
), ),
if (list.any((todo) => todo.done))
IconButton( IconButton(
icon: const Icon(Icons.cleaning_services_outlined), icon: const Icon(Icons.cleaning_services_outlined),
onPressed: controller.cleanUp, onPressed: controller.cleanUp,
@ -46,12 +51,8 @@ class TasksView extends StatelessWidget {
Expanded( Expanded(
child: Opacity( child: Opacity(
opacity: controller.isLoading ? 0.66 : 1, opacity: controller.isLoading ? 0.66 : 1,
child: StreamBuilder( child: list.isEmpty
stream: controller.widget.room.onUpdate.stream, ? Column(
builder: (context, snapshot) {
final list = controller.todos;
if (list.isEmpty) {
return Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center,
children: [ children: [
@ -69,9 +70,8 @@ class TasksView extends StatelessWidget {
), ),
), ),
], ],
); )
} : ReorderableListView.builder(
return ReorderableListView.builder(
onReorder: controller.onReorder, onReorder: controller.onReorder,
itemCount: list.length, itemCount: list.length,
itemBuilder: (context, i) { itemBuilder: (context, i) {
@ -90,15 +90,17 @@ class TasksView extends StatelessWidget {
maxLines: 1, maxLines: 1,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
style: TextStyle( style: TextStyle(
decoration: decoration: todo.done
todo.done ? TextDecoration.lineThrough : null, ? TextDecoration.lineThrough
: null,
), ),
), ),
subtitle: description == null && dueDate == null subtitle: description == null && dueDate == null
? null ? null
: Column( : Column(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment:
CrossAxisAlignment.start,
children: [ children: [
if (description != null) if (description != null)
Text( Text(
@ -110,7 +112,8 @@ class TasksView extends StatelessWidget {
height: 24, height: 24,
child: OutlinedButton.icon( child: OutlinedButton.icon(
style: OutlinedButton.styleFrom( style: OutlinedButton.styleFrom(
padding: const EdgeInsets.symmetric( padding:
const EdgeInsets.symmetric(
horizontal: 6, horizontal: 6,
), ),
), ),
@ -119,8 +122,11 @@ class TasksView extends StatelessWidget {
size: 16, size: 16,
), ),
label: Text( label: Text(
DateFormat.yMMMd(tag).format(dueDate), DateFormat.yMMMd(tag)
style: const TextStyle(fontSize: 12), .format(dueDate),
style: const TextStyle(
fontSize: 12,
),
), ),
onPressed: () => onPressed: () =>
controller.editTodoDueDate( controller.editTodoDueDate(
@ -134,15 +140,27 @@ class TasksView extends StatelessWidget {
onTap: controller.isLoading onTap: controller.isLoading
? null ? null
: () => controller.toggleDone(i), : () => controller.toggleDone(i),
trailing: Padding( trailing: Row(
padding: const EdgeInsets.only(right: 8.0), mainAxisSize: MainAxisSize.min,
child: IconButton( children: [
icon: const Icon(Icons.edit_outlined, size: 16), IconButton(
onPressed: () => controller.editTodo(i, todo), icon: const Icon(
Icons.edit_outlined,
size: 16,
), ),
onPressed: () =>
controller.editTodo(i, todo),
),
IconButton(
icon: const Icon(
Icons.delete_outlined,
size: 16,
),
onPressed: () => controller.deleteTodo(i),
),
const SizedBox(width: 8),
],
), ),
);
},
); );
}, },
), ),
@ -187,7 +205,8 @@ class TasksView extends StatelessWidget {
), ),
suffixIcon: IconButton( suffixIcon: IconButton(
icon: const Icon(Icons.add_outlined), icon: const Icon(Icons.add_outlined),
onPressed: controller.isLoading ? null : controller.addTodo, onPressed:
controller.isLoading ? null : controller.addTodo,
), ),
), ),
), ),
@ -195,5 +214,7 @@ class TasksView extends StatelessWidget {
], ],
), ),
); );
},
);
} }
} }