diff --git a/lib/common/actions/notes/archive.dart b/lib/common/actions/notes/archive.dart index 0f3cd55c..561cb288 100644 --- a/lib/common/actions/notes/archive.dart +++ b/lib/common/actions/notes/archive.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../../../models/note/note.dart'; -import '../../../models/note/note_status.dart'; import '../../../providers/notes/notes_provider.dart'; import '../../../providers/notifiers/notifiers.dart'; import '../../constants/constants.dart'; @@ -38,15 +37,16 @@ Future archiveNote( currentNoteNotifier.value = null; - final succeeded = await ref - .read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier) - .setArchived([note], true); + final succeeded = await ref.read(notesProvider(status: note.status, label: currentLabelFilter).notifier).setArchived([ + note, + ], true); if (succeeded && cancel && context.mounted) { SnackBarUtils().show( context, text: context.l.snack_bar_archived(1), - onCancel: (globalRef) async => await unarchiveNote(context, globalRef, note: note, cancel: false), + onCancel: (globalRef) async => + await unarchiveNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false), ); } @@ -67,18 +67,19 @@ Future archiveNotes(BuildContext context, WidgetRef ref, {required List await unarchiveNotes(context, globalRef, notes: notes, cancel: false), + onCancel: (globalRef) async => + await unarchiveNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false), ); } diff --git a/lib/common/actions/notes/delete.dart b/lib/common/actions/notes/delete.dart index 2d8813c6..53f0b4ab 100644 --- a/lib/common/actions/notes/delete.dart +++ b/lib/common/actions/notes/delete.dart @@ -41,17 +41,17 @@ Future deleteNote( final wasArchived = note.archived; - final succeeded = await ref - .read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier) - .setDeleted([note], true); + final succeeded = await ref.read(notesProvider(status: note.status, label: currentLabelFilter).notifier).setDeleted([ + note, + ], true); if (succeeded && cancel && context.mounted) { SnackBarUtils().show( context, text: context.l.snack_bar_deleted(1), onCancel: (globalRef) async => wasArchived - ? await archiveNote(context, globalRef, note: note, cancel: false) - : await restoreNote(context, globalRef, note: note, cancel: false), + ? await archiveNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false) + : await restoreNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false), ); } @@ -74,11 +74,11 @@ Future deleteNotes(BuildContext context, WidgetRef ref, {required List deleteNotes(BuildContext context, WidgetRef ref, {required List wereArchived - ? await archiveNotes(context, ref, notes: notes) - : await restoreNotes(context, globalRef, notes: notes, cancel: false), + ? await archiveNotes(rootNavigatorKey.currentContext!, ref, notes: notes) + : await restoreNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false), ); } diff --git a/lib/common/actions/notes/restore.dart b/lib/common/actions/notes/restore.dart index c62dfd65..1d71218a 100644 --- a/lib/common/actions/notes/restore.dart +++ b/lib/common/actions/notes/restore.dart @@ -44,7 +44,8 @@ Future restoreNote( SnackBarUtils().show( context, text: context.l.snack_bar_restored(1), - onCancel: (globalRef) async => await deleteNote(context, globalRef, note: note, cancel: false), + onCancel: (globalRef) async => + await deleteNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false), ); } @@ -74,7 +75,8 @@ Future restoreNotes(BuildContext context, WidgetRef ref, {required List await deleteNotes(context, globalRef, notes: notes, cancel: false), + onCancel: (globalRef) async => + await deleteNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false), ); } diff --git a/lib/common/actions/notes/unarchive.dart b/lib/common/actions/notes/unarchive.dart index 25caa03c..d2a68f94 100644 --- a/lib/common/actions/notes/unarchive.dart +++ b/lib/common/actions/notes/unarchive.dart @@ -46,7 +46,8 @@ Future unarchiveNote( SnackBarUtils().show( context, text: context.l.snack_bar_unarchived(1), - onCancel: (globalRef) async => await archiveNote(context, globalRef, note: note, cancel: false), + onCancel: (globalRef) async => + await archiveNote(rootNavigatorKey.currentContext!, globalRef, note: note, cancel: false), ); } @@ -83,7 +84,8 @@ Future unarchiveNotes( SnackBarUtils().show( context, text: context.l.snack_bar_unarchived(notes.length), - onCancel: (globalRef) async => await archiveNotes(context, globalRef, notes: notes, cancel: false), + onCancel: (globalRef) async => + await archiveNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false), ); } diff --git a/lib/providers/notes/notes_provider.dart b/lib/providers/notes/notes_provider.dart index a390e761..a22fceb6 100644 --- a/lib/providers/notes/notes_provider.dart +++ b/lib/providers/notes/notes_provider.dart @@ -210,21 +210,19 @@ class Notes extends _$Notes { return false; } - final notes = (state.value ?? [])..removeWhere((note) => notesToSet.contains(note)); - - state = AsyncData(notes); + state = AsyncData(await get()); _updateUnlabeledProvider(); - _updateStatusProvider(archived ? NoteStatus.archived : NoteStatus.available); + _updateStatusProvider(NoteStatus.available); + _updateStatusProvider(NoteStatus.archived); + _updateStatusProvider(NoteStatus.deleted); return true; } /// Sets whether the [notesToSet] are deleted to [deleted] in the database. Future setDeleted(List notesToSet, bool deleted) async { - _checkStatus([NoteStatus.available, NoteStatus.deleted]); - - final wereArchived = notesToSet.first.archived; + _checkStatus([NoteStatus.available, NoteStatus.archived, NoteStatus.deleted]); for (final note in notesToSet) { note.pinned = false; @@ -241,16 +239,12 @@ class Notes extends _$Notes { return false; } - final notes = (state.value ?? [])..removeWhere((note) => notesToSet.contains(note)); - - state = AsyncData(notes); + state = AsyncData(await get()); _updateUnlabeledProvider(); - if (deleted) { - _updateStatusProvider(NoteStatus.deleted); - } else { - _updateStatusProvider(wereArchived ? NoteStatus.archived : NoteStatus.available); - } + _updateStatusProvider(NoteStatus.available); + _updateStatusProvider(NoteStatus.archived); + _updateStatusProvider(NoteStatus.deleted); return true; } @@ -350,6 +344,10 @@ class Notes extends _$Notes { /// Updates the notes provider with the [status]. void _updateStatusProvider(NoteStatus status) { + if (this.status == status) { + return; + } + ref.read(notesProvider(status: status).notifier).get(); } }