From 449b3e3e3a14d6d716f13006a7f09795eda20927 Mon Sep 17 00:00:00 2001 From: "balazsmanus@chameleon" Date: Thu, 27 Feb 2025 17:10:23 +0100 Subject: [PATCH] fixed todo deletion --- .../lib/pages/notes/notes_page.dart | 101 ++++++++++-------- 1 file changed, 55 insertions(+), 46 deletions(-) diff --git a/refilc_mobile_ui/lib/pages/notes/notes_page.dart b/refilc_mobile_ui/lib/pages/notes/notes_page.dart index 7605beca..ece4dd35 100644 --- a/refilc_mobile_ui/lib/pages/notes/notes_page.dart +++ b/refilc_mobile_ui/lib/pages/notes/notes_page.dart @@ -71,7 +71,32 @@ class NotesPageState extends State with TickerProviderStateMixin { final TextEditingController _taskName = TextEditingController(); final TextEditingController _taskContent = TextEditingController(); - void generateTiles() async { + @override + void initState() { + super.initState(); + WidgetsBinding.instance.addPostFrameCallback((_) { + generateTiles(); + }); + } + +Future deleteTodoItem(TodoItem item) async { + todoItems.removeWhere((element) => element.id == item.id); + await databaseProvider.userStore.storeSelfTodoItems(todoItems, userId: user.id!); + + Provider.of(context, listen: false).restore(); + Provider.of(context, listen: false).restoreTodo(); + + setState(() {}); +} + + Future generateTiles() async { + if (!mounted) return; + + user = Provider.of(context, listen: false); + databaseProvider = Provider.of(context, listen: false); + updateProvider = Provider.of(context, listen: false); + selfNoteProvider = Provider.of(context, listen: false); + doneItems = await databaseProvider.userQuery.toDoItems(userId: user.id!); todoItems = await databaseProvider.userQuery.getTodoItems(userId: user.id!); @@ -80,14 +105,11 @@ class NotesPageState extends State with TickerProviderStateMixin { List hw = Provider.of(context, listen: false) .homework .where((e) => e.deadline.isAfter(DateTime.now())) - // e.deadline.isBefore(DateTime(DateTime.now().year, - // DateTime.now().month, DateTime.now().day + 3))) .toList(); - // todo tiles + // Build todo tiles List toDoTiles = []; - // TODO: FIX THIS ASAP if (hw.isNotEmpty && Provider.of(context, listen: false) .hasScope(PremiumScopes.unlimitedSelfNotes)) { @@ -98,51 +120,39 @@ class NotesPageState extends State with TickerProviderStateMixin { '${(e.subject.isRenamed ? e.subject.renamedTo : e.subject.name) ?? ''}, ${e.content.escapeHtml()}', isTicked: doneItems[e.id] ?? false, onTap: (p0) async { - // print(p0); - // print(doneItems); - if (!doneItems.containsKey(e.id)) { - doneItems.addAll({e.id: p0}); - } else { - doneItems[e.id] = p0; - } - // print(doneItems); - // print(doneItems[e.id]); - // print(user.id); + doneItems[e.id] = p0; await databaseProvider.userStore .storeToDoItem(doneItems, userId: user.id!); - setState(() {}); - - // print( - // await databaseProvider.userQuery.toDoItems(userId: user.id!)); }, ))); } - if (selfNoteProvider.todos.isNotEmpty) { - toDoTiles.addAll(selfNoteProvider.todos.map((e) => TickTile( - padding: EdgeInsets.zero, - title: e.title, - description: e.content, - isTicked: e.done, - onTap: (p0) async { - final todoItemIndex = - todoItems.indexWhere((element) => element.id == e.id); - if (todoItemIndex != -1) { - TodoItem todoItem = todoItems[todoItemIndex]; - Map todoItemJson = todoItem.toJson; - todoItemJson['done'] = p0; - todoItem = TodoItem.fromJson(todoItemJson); - todoItems[todoItemIndex] = todoItem; - await databaseProvider.userStore - .storeSelfTodoItems(todoItems, userId: user.id!); - } - - // await databaseProvider.userStore - // .storeSelfTodoItems(todoItems, userId: user.id!); - }, - ))); - } +if (selfNoteProvider.todos.isNotEmpty) { + toDoTiles.addAll(selfNoteProvider.todos.map((e) => GestureDetector( + onLongPress: () async { + final todoItem = todoItems.firstWhere((item) => item.id == e.id); + await deleteTodoItem(todoItem); + }, + child: TickTile( + padding: EdgeInsets.zero, + title: e.title, + description: e.content, + isTicked: e.done, + onTap: (p0) async { + final todoItemIndex = todoItems.indexWhere((element) => element.id == e.id); + if (todoItemIndex != -1) { + TodoItem todoItem = todoItems[todoItemIndex]; + Map todoItemJson = todoItem.toJson; + todoItemJson['done'] = p0; + todoItem = TodoItem.fromJson(todoItemJson); + todoItems[todoItemIndex] = todoItem; + await databaseProvider.userStore.storeSelfTodoItems(todoItems, userId: user.id!); + } + }, + ), + ))); +} if (toDoTiles.isNotEmpty) { tiles.add(const SizedBox( @@ -328,9 +338,8 @@ class NotesPageState extends State with TickerProviderStateMixin { child: ProfileImage( heroTag: "profile", name: firstName, - backgroundColor: Theme.of(context) - .colorScheme - .tertiary, //ColorUtils.stringToColor(user.displayName ?? "?"), + backgroundColor: Theme.of(context).colorScheme.tertiary, + //ColorUtils.stringToColor(user.displayName ?? "?"), badge: updateProvider.available, role: user.role, profilePictureString: user.picture,