Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/core/widgets/context_menu_region.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,14 @@ class ContextMenuRegion extends StatelessWidget {
final Widget child;
final ValueChanged<ContextMenuAction> onAction;
final List<PopupMenuEntry<ContextMenuAction>>? menuItems;
final bool enableLongPress;

const ContextMenuRegion({
super.key,
required this.onAction,
required this.child,
this.menuItems,
this.enableLongPress = true,
});

Future<void> _show(BuildContext context, {Offset? globalPosition}) async {
Expand Down Expand Up @@ -81,10 +83,12 @@ class ContextMenuRegion extends StatelessWidget {
return GestureDetector(
onSecondaryTapDown: (details) =>
_show(context, globalPosition: details.globalPosition),
onLongPressStart: (details) {
HapticFeedback.heavyImpact();
_show(context, globalPosition: details.globalPosition);
},
onLongPressStart: enableLongPress
? (details) {
HapticFeedback.heavyImpact();
_show(context, globalPosition: details.globalPosition);
}
: null,
child: child,
);
}
Expand Down
48 changes: 48 additions & 0 deletions lib/features/cards/data/card_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,54 @@ class CardRepository {
);
}

/// Bulk-moves non-deleted cards to [newDeckId] in a single transaction.
Future<void> moveCards(List<String> cardIds, String newDeckId) async {
if (cardIds.isEmpty) return;

final db = await _dbHelper.database;
final now = DateTime.now().toUtc().toIso8601String();
final placeholders = List.filled(cardIds.length, '?').join(', ');

await db.transaction((txn) async {
await txn.update(
DatabaseConstants.tableCards,
{
DatabaseConstants.colDeckId: newDeckId,
DatabaseConstants.colUpdatedAt: now,
DatabaseConstants.colSyncStatus: SyncStatus.pending.name,
},
where:
'${DatabaseConstants.colCardId} IN ($placeholders) '
'AND ${DatabaseConstants.colIsDeleted} = 0',
whereArgs: cardIds,
);
});
}

/// Bulk soft-delete for cards.
Future<void> bulkDelete(List<String> cardIds) async {
if (cardIds.isEmpty) return;

final db = await _dbHelper.database;
final now = DateTime.now().toUtc().toIso8601String();
final placeholders = List.filled(cardIds.length, '?').join(', ');

await db.transaction((txn) async {
await txn.update(
DatabaseConstants.tableCards,
{
DatabaseConstants.colIsDeleted: 1,
DatabaseConstants.colUpdatedAt: now,
DatabaseConstants.colSyncStatus: SyncStatus.pending.name,
},
where:
'${DatabaseConstants.colCardId} IN ($placeholders) '
'AND ${DatabaseConstants.colIsDeleted} = 0',
whereArgs: cardIds,
);
});
}

/// Returns all cards with pending sync status.
Future<List<Flashcard>> getUnsynced() async {
final db = await _dbHelper.database;
Expand Down
56 changes: 55 additions & 1 deletion lib/features/decks/data/deck_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,38 @@ class DeckRepository {
/// Soft-deletes [deckId], all descendant decks, and all their cards
/// in a single transaction.
Future<void> delete(String deckId) async {
final allIds = await getDescendantIds(deckId);
await bulkDelete([deckId]);
}

/// Bulk soft-delete for decks and each selected deck's descendants.
Future<void> bulkDelete(List<String> deckIds) async {
if (deckIds.isEmpty) return;

final db = await _dbHelper.database;
final seedPlaceholders = List.filled(deckIds.length, '?').join(', ');
final descendantRows = await db.rawQuery(
'''
WITH RECURSIVE descendants(${DatabaseConstants.colDeckId}) AS (
SELECT ${DatabaseConstants.colDeckId}
FROM ${DatabaseConstants.tableDecks}
WHERE ${DatabaseConstants.colDeckId} IN ($seedPlaceholders)
AND ${DatabaseConstants.colIsDeleted} = 0
UNION ALL
SELECT d.${DatabaseConstants.colDeckId}
FROM ${DatabaseConstants.tableDecks} d
INNER JOIN descendants dt
ON d.${DatabaseConstants.colParentId} = dt.${DatabaseConstants.colDeckId}
WHERE d.${DatabaseConstants.colIsDeleted} = 0
)
SELECT DISTINCT ${DatabaseConstants.colDeckId} FROM descendants
''',
deckIds,
);
final allIds = descendantRows
.map((row) => row[DatabaseConstants.colDeckId] as String)
.toList(growable: false);
if (allIds.isEmpty) return;

final now = DateTime.now().toUtc().toIso8601String();
final deletedFields = {
DatabaseConstants.colIsDeleted: 1,
Expand All @@ -318,6 +348,30 @@ class DeckRepository {
});
}

/// Bulk-moves non-deleted decks to [newParentId] in a single transaction.
Future<void> moveDecks(List<String> deckIds, String? newParentId) async {
if (deckIds.isEmpty) return;

final db = await _dbHelper.database;
final now = DateTime.now().toUtc().toIso8601String();
final placeholders = List.filled(deckIds.length, '?').join(', ');

await db.transaction((txn) async {
await txn.update(
DatabaseConstants.tableDecks,
{
DatabaseConstants.colParentId: newParentId,
DatabaseConstants.colUpdatedAt: now,
DatabaseConstants.colSyncStatus: SyncStatus.pending.name,
},
where:
'${DatabaseConstants.colDeckId} IN ($placeholders) '
'AND ${DatabaseConstants.colIsDeleted} = 0',
whereArgs: deckIds,
);
});
}

/// Finds a deck by its full path (e.g., "Parent::Child::Grandchild").
/// Creates the deck hierarchy if it doesn't exist.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lapse/core/sync/sync_service.dart';
import 'package:lapse/features/cards/data/card_repository_provider.dart';
import 'package:lapse/features/decks/data/deck_repository_provider.dart';
import 'package:lapse/features/decks/domain/deck.dart';
import 'package:lapse/features/decks/presentation/providers/deck_detail_state.dart';
import 'package:lapse/features/decks/presentation/providers/deck_list_provider.dart';

Expand Down Expand Up @@ -97,14 +96,26 @@ class DeckDetailNotifier extends AsyncNotifier<DeckDetailState> {
ref.invalidate(deckListProvider);
}

Future<void> moveChildDeck(String childDeckId, String? newParentId) async {
Future<void> deleteChildDecks(List<String> childDeckIds) async {
if (childDeckIds.isEmpty) return;
final deckRepo = ref.read(deckRepositoryProvider);
final deck = await deckRepo.getById(childDeckId);
if (deck == null) return;
await deckRepo.bulkDelete(childDeckIds);
ref.invalidateSelf();
ref.invalidate(deckListProvider);
ref.read(syncServiceProvider.notifier).schedulePush();
}

await deckRepo.update(
deck.copyWith(parentId: Optional.value(newParentId)),
);
Future<void> moveChildDeck(String childDeckId, String? newParentId) async {
await moveChildDecks([childDeckId], newParentId);
}

Future<void> moveChildDecks(
List<String> childDeckIds,
String? newParentId,
) async {
if (childDeckIds.isEmpty) return;
final deckRepo = ref.read(deckRepositoryProvider);
await deckRepo.moveDecks(childDeckIds, newParentId);
ref.invalidateSelf();
if (newParentId != null) {
ref.invalidate(deckDetailProvider(newParentId));
Expand All @@ -114,11 +125,21 @@ class DeckDetailNotifier extends AsyncNotifier<DeckDetailState> {
}

Future<void> moveCard(String cardId, String newDeckId) async {
await moveCards([cardId], newDeckId);
}

Future<void> moveCards(List<String> cardIds, String newDeckId) async {
if (cardIds.isEmpty) return;
final cardRepo = ref.read(cardRepositoryProvider);
final card = await cardRepo.getById(cardId);
if (card == null) return;
await cardRepo.moveCards(cardIds, newDeckId);
ref.invalidateSelf();
ref.invalidate(deckListProvider);
ref.read(syncServiceProvider.notifier).schedulePush();
}

await cardRepo.update(card.copyWith(deckId: newDeckId));
Future<void> deleteCards(List<String> cardIds) async {
if (cardIds.isEmpty) return;
await ref.read(cardRepositoryProvider).bulkDelete(cardIds);
ref.invalidateSelf();
ref.invalidate(deckListProvider);
ref.read(syncServiceProvider.notifier).schedulePush();
Expand Down
Loading