Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[236] Fix pop when deleting/restoring from the editor #310

Merged
merged 1 commit into from
Dec 15, 2024
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
29 changes: 16 additions & 13 deletions lib/common/actions/notes/delete.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
// ignore_for_file: unused_import

import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:localmaterialnotes/common/actions/notes/select.dart';
import 'package:localmaterialnotes/common/constants/constants.dart';
import 'package:localmaterialnotes/common/dialogs/confirmation_dialog.dart';
import 'package:localmaterialnotes/common/extensions/build_context_extension.dart';
import 'package:localmaterialnotes/models/note/note.dart';
import 'package:localmaterialnotes/providers/bin/bin_provider.dart';
import 'package:localmaterialnotes/providers/notes/notes_provider.dart';
import 'package:localmaterialnotes/providers/notifiers/notifiers.dart';
import 'package:localmaterialnotes/routing/routes/notes/notes_editor_route.dart';
import 'package:localmaterialnotes/routing/routes/shell/shell_route.dart';

/// Deletes the [note].
///
/// Returns `true` if the [note] was deleted, `false` otherwise.
///
/// First, asks for a confirmation if needed.
/// Finally, pops the route if the note was deleted from the editor page.
Future<bool> deleteNote(BuildContext context, WidgetRef ref, Note? note) async {
Future<bool> deleteNote(BuildContext context, WidgetRef ref, Note? note, [bool pop = false]) async {
if (note == null) {
return false;
}
Expand All @@ -34,15 +29,19 @@ Future<bool> deleteNote(BuildContext context, WidgetRef ref, Note? note) async {
return false;
}

if (context.mounted && pop) {
context.pop();
}

currentNoteNotifier.value = null;

final succeeded = await ref.read(notesProvider.notifier).delete(note);

if (context.mounted && context.location == const NotesEditorRoute.empty().location) {
context.pop();
if (!succeeded) {
return false;
}

return succeeded;
return true;
}

/// Deletes the [notes].
Expand Down Expand Up @@ -75,7 +74,7 @@ Future<bool> deleteNotes(BuildContext context, WidgetRef ref, List<Note> notes)
///
/// First, asks for a confirmation if needed.
/// Finally, pops the route if the note was deleted from the editor page.
Future<bool> permanentlyDeleteNote(BuildContext context, WidgetRef ref, Note? note) async {
Future<bool> permanentlyDeleteNote(BuildContext context, WidgetRef ref, Note? note, [bool pop = false]) async {
if (note == null) {
return false;
}
Expand All @@ -90,15 +89,19 @@ Future<bool> permanentlyDeleteNote(BuildContext context, WidgetRef ref, Note? no
return false;
}

if (context.mounted && pop) {
context.pop();
}

currentNoteNotifier.value = null;

final succeeded = await ref.read(binProvider.notifier).permanentlyDelete(note);

if (context.mounted && context.location == const NotesEditorRoute.empty().location) {
context.pop();
if (!succeeded) {
return false;
}

return succeeded;
return true;
}

/// Permanently deletes the [notes].
Expand Down
15 changes: 8 additions & 7 deletions lib/common/actions/notes/restore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import 'package:go_router/go_router.dart';
import 'package:localmaterialnotes/common/actions/notes/select.dart';
import 'package:localmaterialnotes/common/constants/constants.dart';
import 'package:localmaterialnotes/common/dialogs/confirmation_dialog.dart';
import 'package:localmaterialnotes/common/extensions/build_context_extension.dart';
import 'package:localmaterialnotes/models/note/note.dart';
import 'package:localmaterialnotes/providers/bin/bin_provider.dart';
import 'package:localmaterialnotes/providers/notifiers/notifiers.dart';
import 'package:localmaterialnotes/routing/routes/notes/notes_editor_route.dart';
import 'package:localmaterialnotes/routing/routes/shell/shell_route.dart';

/// Restores the [note].
///
/// Returns `true` if the [note] was restored, `false` otherwise.
///
/// First, asks for a confirmation if needed.
/// Finally, pops the route if the note was restored from the editor page.
Future<bool> restoreNote(BuildContext context, WidgetRef ref, Note? note) async {
Future<bool> restoreNote(BuildContext context, WidgetRef ref, Note? note, [bool pop = false]) async {
if (note == null) {
return false;
}
Expand All @@ -31,15 +28,19 @@ Future<bool> restoreNote(BuildContext context, WidgetRef ref, Note? note) async
return false;
}

if (context.mounted && pop) {
context.pop();
}

currentNoteNotifier.value = null;

final succeeded = await ref.read(binProvider.notifier).restore(note);

if (context.mounted && context.location == const NotesEditorRoute.empty().location) {
context.pop();
if (!succeeded) {
return false;
}

return succeeded;
return true;
}

/// Restores the [notes].
Expand Down
6 changes: 3 additions & 3 deletions lib/common/navigation/app_bars/editor_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class _BackAppBarState extends ConsumerState<EditorAppBar> {
case MenuOption.share:
await shareNote(note);
case MenuOption.delete:
await deleteNote(context, ref, note);
await deleteNote(context, ref, note, true);
case MenuOption.restore:
await restoreNote(context, ref, note);
await restoreNote(context, ref, note, true);
case MenuOption.deletePermanently:
await permanentlyDeleteNote(context, ref, note);
await permanentlyDeleteNote(context, ref, note, true);
case MenuOption.about:
await showModalBottomSheet<void>(
context: context,
Expand Down
5 changes: 0 additions & 5 deletions lib/routing/routes/notes/notes_editor_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ class NotesEditorRoute extends GoRouteData {
required this.autoFocus,
});

/// Constructor without parameters used to get the location of the route.
const NotesEditorRoute.empty()
: readOnly = null,
autoFocus = null;

/// Whether the text fields should be read only.
final bool? readOnly;

Expand Down
Loading