From 585372816d2dcceb7752ddaf1eebd5acda810d4d Mon Sep 17 00:00:00 2001 From: NBA2K1 <78034913+NBA2K1@users.noreply.github.com> Date: Sun, 15 Jun 2025 17:20:51 +0200 Subject: [PATCH] Fix Category Removal Bug When removing a category, the items inside the category were not being updated, removing the link to the removed category. That lead to the items disappearance from the library. Now the reference from the item to the deleted category is deleted. Those items show up in the "Default" category. --- .../more/categories/categories_screen.dart | 63 +++++++++++-------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/lib/modules/more/categories/categories_screen.dart b/lib/modules/more/categories/categories_screen.dart index 71ff1637..5b43eab3 100644 --- a/lib/modules/more/categories/categories_screen.dart +++ b/lib/modules/more/categories/categories_screen.dart @@ -284,32 +284,10 @@ class _CategoriesTabState extends ConsumerState { const SizedBox(width: 15), TextButton( onPressed: () async { - await isar.writeTxn( - () async { - await isar.categorys - .delete( - category.id!, - ); - }, + await _removeCategory( + category, + context, ); - await ref - .read( - synchingProvider( - syncId: 1, - ).notifier, - ) - .addChangedPartAsync( - ActionType - .removeCategory, - category.id, - "{}", - true, - ); - if (context.mounted) { - Navigator.pop( - context, - ); - } }, child: Text(l10n.ok), ), @@ -480,6 +458,41 @@ class _CategoriesTabState extends ConsumerState { ); } + Future _removeCategory(Category category, BuildContext context) async { + await isar.writeTxn(() async { + // All Items with this category + final allItems = await isar.mangas + .filter() + .categoriesElementEqualTo(category.id!) + .findAll(); + // Remove the category ID from each item's category list + final updatedItems = allItems.map((manga) { + final cats = List.from(manga.categories ?? []); + cats.remove(category.id!); + manga.categories = cats; + return manga; + }).toList(); + + // Save updated items back to the database + await isar.mangas.putAll(updatedItems); + + // Delete category + await isar.categorys.delete(category.id!); + }); + + await ref + .read(synchingProvider(syncId: 1).notifier) + .addChangedPartAsync( + ActionType.removeCategory, + category.id, + "{}", + true, + ); + if (context.mounted) { + Navigator.pop(context); + } + } + void _renameCategory(Category category) { bool isExist = false; final controller = TextEditingController(text: category.name);