Skip to content

Commit a24593a

Browse files
committed
fix: providers updates on archive/delete
1 parent a4272c2 commit a24593a

5 files changed

Lines changed: 39 additions & 36 deletions

File tree

lib/common/actions/notes/archive.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_riverpod/flutter_riverpod.dart';
33

44
import '../../../models/note/note.dart';
5-
import '../../../models/note/note_status.dart';
65
import '../../../providers/notes/notes_provider.dart';
76
import '../../../providers/notifiers/notifiers.dart';
87
import '../../constants/constants.dart';
@@ -38,15 +37,16 @@ Future<bool> archiveNote(
3837

3938
currentNoteNotifier.value = null;
4039

41-
final succeeded = await ref
42-
.read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier)
43-
.setArchived([note], true);
40+
final succeeded = await ref.read(notesProvider(status: note.status, label: currentLabelFilter).notifier).setArchived([
41+
note,
42+
], true);
4443

4544
if (succeeded && cancel && context.mounted) {
4645
SnackBarUtils().show(
4746
context,
4847
text: context.l.snack_bar_archived(1),
49-
onCancel: (globalRef) async => await unarchiveNote(context, globalRef, note: note, cancel: false),
48+
onCancel: (globalRef) async =>
49+
await unarchiveNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false),
5050
);
5151
}
5252

@@ -67,18 +67,19 @@ Future<bool> archiveNotes(BuildContext context, WidgetRef ref, {required List<No
6767
}
6868

6969
final succeeded = await ref
70-
.read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier)
70+
.read(notesProvider(status: notes.first.status, label: currentLabelFilter).notifier)
7171
.setArchived(notes, true);
7272

7373
if (context.mounted) {
74-
exitNotesSelectionMode(context, ref, notesStatus: NoteStatus.available);
74+
exitNotesSelectionMode(context, ref, notesStatus: notes.first.status);
7575
}
7676

7777
if (succeeded && cancel && context.mounted) {
7878
SnackBarUtils().show(
7979
context,
8080
text: context.l.snack_bar_archived(notes.length),
81-
onCancel: (globalRef) async => await unarchiveNotes(context, globalRef, notes: notes, cancel: false),
81+
onCancel: (globalRef) async =>
82+
await unarchiveNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false),
8283
);
8384
}
8485

lib/common/actions/notes/delete.dart

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ Future<bool> deleteNote(
4141

4242
final wasArchived = note.archived;
4343

44-
final succeeded = await ref
45-
.read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier)
46-
.setDeleted([note], true);
44+
final succeeded = await ref.read(notesProvider(status: note.status, label: currentLabelFilter).notifier).setDeleted([
45+
note,
46+
], true);
4747

4848
if (succeeded && cancel && context.mounted) {
4949
SnackBarUtils().show(
5050
context,
5151
text: context.l.snack_bar_deleted(1),
5252
onCancel: (globalRef) async => wasArchived
53-
? await archiveNote(context, globalRef, note: note, cancel: false)
54-
: await restoreNote(context, globalRef, note: note, cancel: false),
53+
? await archiveNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false)
54+
: await restoreNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false),
5555
);
5656
}
5757

@@ -74,20 +74,20 @@ Future<bool> deleteNotes(BuildContext context, WidgetRef ref, {required List<Not
7474
final wereArchived = notes.first.archived;
7575

7676
final succeeded = await ref
77-
.read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier)
77+
.read(notesProvider(status: notes.first.status, label: currentLabelFilter).notifier)
7878
.setDeleted(notes, true);
7979

8080
if (context.mounted) {
81-
exitNotesSelectionMode(context, ref, notesStatus: NoteStatus.available);
81+
exitNotesSelectionMode(context, ref, notesStatus: notes.first.status);
8282
}
8383

8484
if (succeeded && cancel && context.mounted) {
8585
SnackBarUtils().show(
8686
context,
8787
text: context.l.snack_bar_deleted(notes.length),
8888
onCancel: (globalRef) async => wereArchived
89-
? await archiveNotes(context, ref, notes: notes)
90-
: await restoreNotes(context, globalRef, notes: notes, cancel: false),
89+
? await archiveNotes(rootNavigatorKey.currentContext!, ref, notes: notes)
90+
: await restoreNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false),
9191
);
9292
}
9393

lib/common/actions/notes/restore.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Future<bool> restoreNote(
4444
SnackBarUtils().show(
4545
context,
4646
text: context.l.snack_bar_restored(1),
47-
onCancel: (globalRef) async => await deleteNote(context, globalRef, note: note, cancel: false),
47+
onCancel: (globalRef) async =>
48+
await deleteNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false),
4849
);
4950
}
5051

@@ -74,7 +75,8 @@ Future<bool> restoreNotes(BuildContext context, WidgetRef ref, {required List<No
7475
SnackBarUtils().show(
7576
context,
7677
text: context.l.snack_bar_restored(notes.length),
77-
onCancel: (globalRef) async => await deleteNotes(context, globalRef, notes: notes, cancel: false),
78+
onCancel: (globalRef) async =>
79+
await deleteNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false),
7880
);
7981
}
8082

lib/common/actions/notes/unarchive.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ Future<bool> unarchiveNote(
4646
SnackBarUtils().show(
4747
context,
4848
text: context.l.snack_bar_unarchived(1),
49-
onCancel: (globalRef) async => await archiveNote(context, globalRef, note: note, cancel: false),
49+
onCancel: (globalRef) async =>
50+
await archiveNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false),
5051
);
5152
}
5253

@@ -83,7 +84,8 @@ Future<bool> unarchiveNotes(
8384
SnackBarUtils().show(
8485
context,
8586
text: context.l.snack_bar_unarchived(notes.length),
86-
onCancel: (globalRef) async => await archiveNotes(context, globalRef, notes: notes, cancel: false),
87+
onCancel: (globalRef) async =>
88+
await archiveNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false),
8789
);
8890
}
8991

lib/providers/notes/notes_provider.dart

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -210,21 +210,19 @@ class Notes extends _$Notes {
210210
return false;
211211
}
212212

213-
final notes = (state.value ?? [])..removeWhere((note) => notesToSet.contains(note));
214-
215-
state = AsyncData(notes);
213+
state = AsyncData(await get());
216214

217215
_updateUnlabeledProvider();
218-
_updateStatusProvider(archived ? NoteStatus.archived : NoteStatus.available);
216+
_updateStatusProvider(NoteStatus.available);
217+
_updateStatusProvider(NoteStatus.archived);
218+
_updateStatusProvider(NoteStatus.deleted);
219219

220220
return true;
221221
}
222222

223223
/// Sets whether the [notesToSet] are deleted to [deleted] in the database.
224224
Future<bool> setDeleted(List<Note> notesToSet, bool deleted) async {
225-
_checkStatus([NoteStatus.available, NoteStatus.deleted]);
226-
227-
final wereArchived = notesToSet.first.archived;
225+
_checkStatus([NoteStatus.available, NoteStatus.archived, NoteStatus.deleted]);
228226

229227
for (final note in notesToSet) {
230228
note.pinned = false;
@@ -241,16 +239,12 @@ class Notes extends _$Notes {
241239
return false;
242240
}
243241

244-
final notes = (state.value ?? [])..removeWhere((note) => notesToSet.contains(note));
245-
246-
state = AsyncData(notes);
242+
state = AsyncData(await get());
247243

248244
_updateUnlabeledProvider();
249-
if (deleted) {
250-
_updateStatusProvider(NoteStatus.deleted);
251-
} else {
252-
_updateStatusProvider(wereArchived ? NoteStatus.archived : NoteStatus.available);
253-
}
245+
_updateStatusProvider(NoteStatus.available);
246+
_updateStatusProvider(NoteStatus.archived);
247+
_updateStatusProvider(NoteStatus.deleted);
254248

255249
return true;
256250
}
@@ -350,6 +344,10 @@ class Notes extends _$Notes {
350344

351345
/// Updates the notes provider with the [status].
352346
void _updateStatusProvider(NoteStatus status) {
347+
if (this.status == status) {
348+
return;
349+
}
350+
353351
ref.read(notesProvider(status: status).notifier).get();
354352
}
355353
}

0 commit comments

Comments
 (0)