chore: Follow up todo list feature

pull/633/head
krille-chan 2 years ago
parent d0dbaa5e72
commit 5d387145c8
No known key found for this signature in database

@ -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(
final todo = todos.removeAt(oldindex); update: (todos) {
todos.insert(newindex, todo); final todo = todos.removeAt(oldindex);
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(

@ -13,187 +13,208 @@ 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 Scaffold( return StreamBuilder<Object>(
appBar: AppBar( stream: controller.widget.room.onUpdate.stream,
title: Text(L10n.of(context)!.todoLists), builder: (context, snapshot) {
actions: [ final list = controller.todos;
AnimatedCrossFade( return Scaffold(
duration: FluffyThemes.animationDuration, appBar: AppBar(
firstChild: const SizedBox( title: Text(L10n.of(context)!.todoLists),
width: 32, actions: [
height: 32, AnimatedCrossFade(
), duration: FluffyThemes.animationDuration,
secondChild: const Padding( firstChild: const SizedBox(
padding: EdgeInsets.all(8.0), width: 32,
child: SizedBox( height: 32,
width: 16, ),
height: 16, secondChild: const Padding(
child: CircularProgressIndicator.adaptive(strokeWidth: 2), padding: EdgeInsets.all(8.0),
child: SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator.adaptive(strokeWidth: 2),
),
),
crossFadeState: controller.isLoading
? CrossFadeState.showSecond
: CrossFadeState.showFirst,
), ),
), if (list.any((todo) => todo.done))
crossFadeState: controller.isLoading IconButton(
? CrossFadeState.showSecond icon: const Icon(Icons.cleaning_services_outlined),
: CrossFadeState.showFirst, onPressed: controller.cleanUp,
), ),
IconButton( ],
icon: const Icon(Icons.cleaning_services_outlined),
onPressed: controller.cleanUp,
), ),
], body: Column(
), children: [
body: Column( Expanded(
children: [ child: Opacity(
Expanded( opacity: controller.isLoading ? 0.66 : 1,
child: Opacity( child: list.isEmpty
opacity: controller.isLoading ? 0.66 : 1, ? Column(
child: StreamBuilder( mainAxisAlignment: MainAxisAlignment.center,
stream: controller.widget.room.onUpdate.stream, crossAxisAlignment: CrossAxisAlignment.center,
builder: (context, snapshot) { children: [
final list = controller.todos; Icon(
if (list.isEmpty) { Icons.task_alt,
return Column( size: 80,
mainAxisAlignment: MainAxisAlignment.center, color: Theme.of(context).colorScheme.secondary,
crossAxisAlignment: CrossAxisAlignment.center, ),
children: [ const SizedBox(height: 16),
Icon( SizedBox(
Icons.task_alt, width: 256,
size: 80, child: Text(
color: Theme.of(context).colorScheme.secondary, L10n.of(context)!.noTodosYet,
), textAlign: TextAlign.center,
const SizedBox(height: 16), ),
SizedBox( ),
width: 256, ],
child: Text( )
L10n.of(context)!.noTodosYet, : ReorderableListView.builder(
textAlign: TextAlign.center, onReorder: controller.onReorder,
), itemCount: list.length,
), itemBuilder: (context, i) {
], final todo = list[i];
); final description = todo.description;
} final dueDate = todo.dueDate;
return ReorderableListView.builder( return ListTile(
onReorder: controller.onReorder, key: Key(todo.toJson().toString()),
itemCount: list.length, leading: Icon(
itemBuilder: (context, i) { todo.done
final todo = list[i]; ? Icons.check_circle
final description = todo.description; : Icons.circle_outlined,
final dueDate = todo.dueDate; ),
return ListTile( title: Text(
key: Key(todo.toJson().toString()), todo.title,
leading: Icon( maxLines: 1,
todo.done overflow: TextOverflow.ellipsis,
? Icons.check_circle style: TextStyle(
: Icons.circle_outlined, decoration: todo.done
), ? TextDecoration.lineThrough
title: Text( : null,
todo.title, ),
maxLines: 1, ),
overflow: TextOverflow.ellipsis, subtitle: description == null && dueDate == null
style: TextStyle( ? null
decoration: : Column(
todo.done ? TextDecoration.lineThrough : null, mainAxisSize: MainAxisSize.min,
), crossAxisAlignment:
), CrossAxisAlignment.start,
subtitle: description == null && dueDate == null children: [
? null if (description != null)
: Column( Text(
description,
maxLines: 2,
),
if (dueDate != null)
SizedBox(
height: 24,
child: OutlinedButton.icon(
style: OutlinedButton.styleFrom(
padding:
const EdgeInsets.symmetric(
horizontal: 6,
),
),
icon: const Icon(
Icons.calendar_month,
size: 16,
),
label: Text(
DateFormat.yMMMd(tag)
.format(dueDate),
style: const TextStyle(
fontSize: 12,
),
),
onPressed: () =>
controller.editTodoDueDate(
i,
todo,
),
),
),
],
),
onTap: controller.isLoading
? null
: () => controller.toggleDone(i),
trailing: Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
if (description != null) IconButton(
Text( icon: const Icon(
description, Icons.edit_outlined,
maxLines: 2, size: 16,
), ),
if (dueDate != null) onPressed: () =>
SizedBox( controller.editTodo(i, todo),
height: 24, ),
child: OutlinedButton.icon( IconButton(
style: OutlinedButton.styleFrom( icon: const Icon(
padding: const EdgeInsets.symmetric( Icons.delete_outlined,
horizontal: 6, size: 16,
),
),
icon: const Icon(
Icons.calendar_month,
size: 16,
),
label: Text(
DateFormat.yMMMd(tag).format(dueDate),
style: const TextStyle(fontSize: 12),
),
onPressed: () =>
controller.editTodoDueDate(
i,
todo,
),
),
), ),
onPressed: () => controller.deleteTodo(i),
),
const SizedBox(width: 8),
], ],
), ),
onTap: controller.isLoading );
? null },
: () => controller.toggleDone(i),
trailing: Padding(
padding: const EdgeInsets.only(right: 8.0),
child: IconButton(
icon: const Icon(Icons.edit_outlined, size: 16),
onPressed: () => controller.editTodo(i, todo),
),
), ),
); ),
},
);
},
), ),
), Padding(
), padding: const EdgeInsets.all(12.0),
Padding( child: TextField(
padding: const EdgeInsets.all(12.0), focusNode: controller.focusNode,
child: TextField( readOnly: controller.isLoading,
focusNode: controller.focusNode, controller: controller.textEditingController,
readOnly: controller.isLoading, onSubmitted: controller.addTodo,
controller: controller.textEditingController, maxLength: 64,
onSubmitted: controller.addTodo, decoration: InputDecoration(
maxLength: 64, counterStyle: const TextStyle(height: double.minPositive),
decoration: InputDecoration( counterText: '',
counterStyle: const TextStyle(height: double.minPositive), hintText: L10n.of(context)!.newTodo,
counterText: '', prefixIcon: Row(
hintText: L10n.of(context)!.newTodo, mainAxisSize: MainAxisSize.min,
prefixIcon: Row( children: [
mainAxisSize: MainAxisSize.min, IconButton(
children: [ icon: Icon(
IconButton( controller.newTaskDateTime == null
icon: Icon( ? Icons.calendar_month_outlined
controller.newTaskDateTime == null : Icons.calendar_month,
? Icons.calendar_month_outlined color: controller.newTaskDateTime == null
: Icons.calendar_month, ? null
color: controller.newTaskDateTime == null : Theme.of(context).primaryColor,
? null ),
: Theme.of(context).primaryColor, onPressed: controller.setNewTaskDateTime,
), ),
onPressed: controller.setNewTaskDateTime, IconButton(
icon: Icon(
Icons.text_fields,
color: controller.newTaskDescription == null
? null
: Theme.of(context).primaryColor,
),
onPressed: controller.setNewTaskDescription,
),
],
), ),
IconButton( suffixIcon: IconButton(
icon: Icon( icon: const Icon(Icons.add_outlined),
Icons.text_fields, onPressed:
color: controller.newTaskDescription == null controller.isLoading ? null : controller.addTodo,
? null
: Theme.of(context).primaryColor,
),
onPressed: controller.setNewTaskDescription,
), ),
], ),
),
suffixIcon: IconButton(
icon: const Icon(Icons.add_outlined),
onPressed: controller.isLoading ? null : controller.addTodo,
), ),
), ),
), ],
), ),
], );
), },
); );
} }
} }

Loading…
Cancel
Save