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.
This commit is contained in:
NBA2K1 2025-06-15 17:20:51 +02:00
parent eaed214354
commit 585372816d

View file

@ -284,32 +284,10 @@ class _CategoriesTabState extends ConsumerState<CategoriesTab> {
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<CategoriesTab> {
);
}
Future<void> _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<int>.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);