Fix category rearrange bug

When rearranging categories in the Manga library, all categories in Anime and Novel were being deleted.
This commit is contained in:
NBA2K1 2025-12-30 00:27:49 +01:00
parent 821cbfa0dd
commit 4e2d8b0038

View file

@ -93,16 +93,8 @@ class CategoriesTab extends ConsumerStatefulWidget {
class _CategoriesTabState extends ConsumerState<CategoriesTab> {
List<Category> _entries = [];
Future<void> _updateCategoriesOrder(List<Category> categories) async {
await isar.writeTxn(() async {
await isar.categorys.clear();
await isar.categorys.putAll(categories);
final cats = await isar.categorys.filter().posIsNull().findAll();
for (var category in cats) {
category.pos = category.id;
}
await isar.categorys.putAll(cats);
});
Future<void> _updateCategoriesOrder(Category a, Category b) async {
await isar.writeTxn(() async => await isar.categorys.putAll([a, b]));
}
@override
@ -182,16 +174,16 @@ class _CategoriesTabState extends ConsumerState<CategoriesTab> {
),
onPressed: index > 0
? () async {
final item = _entries[index - 1];
_entries.removeAt(index);
_entries.removeAt(index - 1);
int? currentPos = category.pos;
int? pos = item.pos;
await _updateCategoriesOrder([
..._entries,
category..pos = pos,
item..pos = currentPos,
]);
final above = _entries[index - 1];
_entries[index - 1] = category;
_entries[index] = above;
final temp = category.pos;
category.pos = above.pos;
above.pos = temp;
await _updateCategoriesOrder(
category,
above,
);
setState(() {});
}
: null,
@ -202,16 +194,16 @@ class _CategoriesTabState extends ConsumerState<CategoriesTab> {
),
onPressed: index < _entries.length - 1
? () async {
final item = _entries[index + 1];
_entries.removeAt(index + 1);
_entries.removeAt(index);
int? currentPos = category.pos;
int? pos = item.pos;
await _updateCategoriesOrder([
..._entries,
category..pos = pos,
item..pos = currentPos,
]);
final below = _entries[index + 1];
_entries[index + 1] = category;
_entries[index] = below;
final temp = category.pos;
category.pos = below.pos;
below.pos = temp;
await _updateCategoriesOrder(
category,
below,
);
setState(() {});
}
: null,