Skip to content
Merged
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
17 changes: 9 additions & 8 deletions lib/common/actions/notes/archive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -38,15 +37,16 @@ Future<bool> 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),
);
}

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

final succeeded = await ref
.read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier)
.read(notesProvider(status: notes.first.status, label: currentLabelFilter).notifier)
.setArchived(notes, true);

if (context.mounted) {
exitNotesSelectionMode(context, ref, notesStatus: NoteStatus.available);
exitNotesSelectionMode(context, ref, notesStatus: notes.first.status);
}

if (succeeded && cancel && context.mounted) {
SnackBarUtils().show(
context,
text: context.l.snack_bar_archived(notes.length),
onCancel: (globalRef) async => await unarchiveNotes(context, globalRef, notes: notes, cancel: false),
onCancel: (globalRef) async =>
await unarchiveNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false),
);
}

Expand Down
18 changes: 9 additions & 9 deletions lib/common/actions/notes/delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ Future<bool> 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),
);
}

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

final succeeded = await ref
.read(notesProvider(status: NoteStatus.available, label: currentLabelFilter).notifier)
.read(notesProvider(status: notes.first.status, label: currentLabelFilter).notifier)
.setDeleted(notes, true);

if (context.mounted) {
exitNotesSelectionMode(context, ref, notesStatus: NoteStatus.available);
exitNotesSelectionMode(context, ref, notesStatus: notes.first.status);
}

if (succeeded && cancel && context.mounted) {
SnackBarUtils().show(
context,
text: context.l.snack_bar_deleted(notes.length),
onCancel: (globalRef) async => 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),
);
}

Expand Down
6 changes: 4 additions & 2 deletions lib/common/actions/notes/restore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ Future<bool> 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),
);
}

Expand Down Expand Up @@ -74,7 +75,8 @@ Future<bool> restoreNotes(BuildContext context, WidgetRef ref, {required List<No
SnackBarUtils().show(
context,
text: context.l.snack_bar_restored(notes.length),
onCancel: (globalRef) async => await deleteNotes(context, globalRef, notes: notes, cancel: false),
onCancel: (globalRef) async =>
await deleteNotes(rootNavigatorKey.currentContext!, globalRef, notes: notes, cancel: false),
);
}

Expand Down
6 changes: 4 additions & 2 deletions lib/common/actions/notes/unarchive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ Future<bool> 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),
);
}

Expand Down Expand Up @@ -83,7 +84,8 @@ Future<bool> 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),
);
}

Expand Down
28 changes: 13 additions & 15 deletions lib/providers/notes/notes_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> setDeleted(List<Note> 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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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();
}
}