Skip to content

Commit

Permalink
[355] Fix labels pages (#359)
Browse files Browse the repository at this point in the history
* [355] fix: add family provider for the labeled notes

* [355] style: make actions parameters named

* [355] misc: remove --no-enable-impeller
  • Loading branch information
maelchiotti authored Jan 10, 2025
1 parent cd4e484 commit e5c85f0
Show file tree
Hide file tree
Showing 40 changed files with 566 additions and 515 deletions.
2 changes: 1 addition & 1 deletion lib/common/actions/notes/add.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Future<void> addNote(BuildContext context, WidgetRef ref, {String? content}) asy

// If some content was provided, immediately save the note without waiting for changes in the editor
if (content != null) {
ref.read(notesProvider.notifier).edit(note);
ref.read(notesProvider(label: currentLabelFilter).notifier).edit(note);
}

currentNoteNotifier.value = note;
Expand Down
5 changes: 3 additions & 2 deletions lib/common/actions/notes/copy.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:flutter/services.dart';
import '../../constants/constants.dart';

import '../../../models/note/note.dart';
import '../../../utils/snack_bar_utils.dart';
import '../../constants/constants.dart';

/// Copies the content of the [note] to the clipboard.
Future<void> copyNote(Note note) async {
Future<void> copyNote({required Note note}) async {
Clipboard.setData(
ClipboardData(text: note.contentPreview),
);
Expand Down
12 changes: 6 additions & 6 deletions lib/common/actions/notes/delete.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'select.dart';
///
/// 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, [bool pop = false]) async {
Future<bool> deleteNote(BuildContext context, WidgetRef ref, {Note? note, bool pop = false}) async {
if (note == null) {
return false;
}
Expand All @@ -35,7 +35,7 @@ Future<bool> deleteNote(BuildContext context, WidgetRef ref, Note? note, [bool p

currentNoteNotifier.value = null;

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

if (!succeeded) {
return false;
Expand All @@ -49,7 +49,7 @@ Future<bool> deleteNote(BuildContext context, WidgetRef ref, Note? note, [bool p
/// Returns `true` if the [notes] were deleted, `false` otherwise.
///
/// First, asks for a confirmation if needed.
Future<bool> deleteNotes(BuildContext context, WidgetRef ref, List<Note> notes) async {
Future<bool> deleteNotes(BuildContext context, WidgetRef ref, {required List<Note> notes}) async {
if (!await askForConfirmation(
context,
l.dialog_delete,
Expand All @@ -59,7 +59,7 @@ Future<bool> deleteNotes(BuildContext context, WidgetRef ref, List<Note> notes)
return false;
}

final succeeded = await ref.read(notesProvider.notifier).deleteAll(notes);
final succeeded = await ref.read(notesProvider(label: currentLabelFilter).notifier).deleteAll(notes);

if (context.mounted) {
exitNotesSelectionMode(context, ref);
Expand All @@ -74,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, [bool pop = false]) async {
Future<bool> permanentlyDeleteNote(BuildContext context, WidgetRef ref, {Note? note, bool pop = false}) async {
if (note == null) {
return false;
}
Expand Down Expand Up @@ -109,7 +109,7 @@ Future<bool> permanentlyDeleteNote(BuildContext context, WidgetRef ref, Note? no
/// Returns `true` if the [notes] were permanently deleted, `false` otherwise.
///
/// First, asks for a confirmation if needed.
Future<bool> permanentlyDeleteNotes(BuildContext context, WidgetRef ref, List<Note> notes) async {
Future<bool> permanentlyDeleteNotes(BuildContext context, WidgetRef ref, {required List<Note> notes}) async {
if (!await askForConfirmation(
context,
l.dialog_permanently_delete,
Expand Down
8 changes: 4 additions & 4 deletions lib/common/actions/notes/labels.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import '../../constants/constants.dart';
import 'select.dart';

/// Asks the user to select the labels for the [note].
Future<List<Label>?> selectLabels(BuildContext context, WidgetRef ref, Note note) async {
Future<List<Label>?> selectLabels(BuildContext context, WidgetRef ref, {required Note note}) async {
if (ref.read(labelsListProvider).value == null || ref.read(labelsListProvider).value!.isEmpty) {
SnackBarUtils.info(l.snack_bar_no_labels).show();

Expand All @@ -29,7 +29,7 @@ Future<List<Label>?> selectLabels(BuildContext context, WidgetRef ref, Note note
return null;
}

await ref.read(notesProvider.notifier).editLabels(note, selectedLabels);
await ref.read(notesProvider(label: currentLabelFilter).notifier).editLabels(note, selectedLabels);

// Forcefully notify the listeners because only the labels of the note have changed
currentNoteNotifier.value = note;
Expand All @@ -39,7 +39,7 @@ Future<List<Label>?> selectLabels(BuildContext context, WidgetRef ref, Note note
}

/// Asks the user to select the labels to add to the [notes].
Future<List<Label>?> addLabels(BuildContext context, WidgetRef ref, List<Note> notes) async {
Future<List<Label>?> addLabels(BuildContext context, WidgetRef ref, {required List<Note> notes}) async {
if (ref.read(labelsListProvider).value == null || ref.read(labelsListProvider).value!.isEmpty) {
SnackBarUtils.info(l.snack_bar_no_labels).show();

Expand All @@ -56,7 +56,7 @@ Future<List<Label>?> addLabels(BuildContext context, WidgetRef ref, List<Note> n
return null;
}

await ref.read(notesProvider.notifier).addLabels(notes, selectedLabels);
await ref.read(notesProvider(label: currentLabelFilter).notifier).addLabels(notes, selectedLabels);

if (context.mounted) {
exitNotesSelectionMode(context, ref);
Expand Down
12 changes: 7 additions & 5 deletions lib/common/actions/notes/pin.dart
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'select.dart';

import '../../../models/note/note.dart';
import '../../../providers/notes/notes_provider.dart';
import '../../../providers/notifiers/notifiers.dart';
import 'select.dart';

/// Toggles the pined status of the [note].
///
/// Returns `true` if the pined status of the [note] was toggled, `false` otherwise.
Future<bool> togglePinNote(BuildContext context, WidgetRef ref, Note? note) async {
Future<bool> togglePinNote(BuildContext context, WidgetRef ref, {Note? note}) async {
if (note == null) {
return false;
}

await ref.read(notesProvider.notifier).togglePin(note);
await ref.read(notesProvider(label: currentLabelFilter).notifier).togglePin(note);

return false;
}

/// Toggles the pined status of the [notes].
Future<void> togglePinNotes(BuildContext context, WidgetRef ref, List<Note> notes) async {
await ref.read(notesProvider.notifier).togglePinAll(notes);
Future<void> togglePinNotes(BuildContext context, WidgetRef ref, {required List<Note> notes}) async {
await ref.read(notesProvider(label: currentLabelFilter).notifier).togglePinAll(notes);

if (context.mounted) {
exitNotesSelectionMode(context, ref);
Expand Down
4 changes: 2 additions & 2 deletions lib/common/actions/notes/restore.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import 'select.dart';
///
/// 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, [bool pop = false]) async {
Future<bool> restoreNote(BuildContext context, WidgetRef ref, {Note? note, bool pop = false}) async {
if (note == null) {
return false;
}
Expand Down Expand Up @@ -47,7 +47,7 @@ Future<bool> restoreNote(BuildContext context, WidgetRef ref, Note? note, [bool
/// Returns `true` if the [notes] were restored, `false` otherwise.
///
/// First, asks for a confirmation if needed.
Future<bool> restoreNotes(BuildContext context, WidgetRef ref, List<Note> notes) async {
Future<bool> restoreNotes(BuildContext context, WidgetRef ref, {required List<Note> notes}) async {
if (!await askForConfirmation(
context,
l.dialog_restore,
Expand Down
14 changes: 10 additions & 4 deletions lib/common/actions/notes/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,32 @@ import '../../../providers/notes/notes_provider.dart';
import '../../../providers/notifiers/notifiers.dart';

/// Toggles the select status of the [note].
void toggleSelectNote(WidgetRef ref, Note note) {
void toggleSelectNote(WidgetRef ref, {required Note note}) {
if (note.deleted) {
note.selected ? ref.read(binProvider.notifier).unselect(note) : ref.read(binProvider.notifier).select(note);
} else {
note.selected ? ref.read(notesProvider.notifier).unselect(note) : ref.read(notesProvider.notifier).select(note);
note.selected
? ref.read(notesProvider(label: currentLabelFilter).notifier).unselect(note)
: ref.read(notesProvider(label: currentLabelFilter).notifier).select(note);
}
}

/// Selects all the notes.
///
/// Depending on the current route, selects either the notes from the notes page or those from the bin page.
void selectAllNotes(BuildContext context, WidgetRef ref, {bool notesPage = true}) {
notesPage ? ref.read(notesProvider.notifier).selectAll() : ref.read(binProvider.notifier).selectAll();
notesPage
? ref.read(notesProvider(label: currentLabelFilter).notifier).selectAll()
: ref.read(binProvider.notifier).selectAll();
}

/// Unselects all the notes.
///
/// Depending on the current route, unselects either the notes from the notes page or those from the bin page.
void unselectAllNotes(BuildContext context, WidgetRef ref, {bool notesPage = true}) {
notesPage ? ref.read(notesProvider.notifier).unselectAll() : ref.read(binProvider.notifier).unselectAll();
notesPage
? ref.read(notesProvider(label: currentLabelFilter).notifier).unselectAll()
: ref.read(binProvider.notifier).unselectAll();
}

/// Exits the notes selection mode.
Expand Down
4 changes: 2 additions & 2 deletions lib/common/actions/notes/share.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import '../../../models/note/note.dart';
import '../../constants/constants.dart';

/// Shares the [note] as text (title and content).
Future<void> shareNote(Note note) async {
Future<void> shareNote({required Note note}) async {
await Share.share(note.shareText, subject: note.title);
}

/// Shares the [notes] as text (title and content), separated by dashes.
Future<void> shareNotes(List<Note> notes) async {
Future<void> shareNotes({required List<Note> notes}) async {
final text = notes.map((note) => note.shareText).join('\n\n----------\n\n');

await Share.share(text, subject: l.action_share_subject(notes.length));
Expand Down
14 changes: 7 additions & 7 deletions lib/common/navigation/app_bars/editor_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class _BackAppBarState extends ConsumerState<EditorAppBar> {

switch (menuOption) {
case NoteMenuOption.togglePin:
await togglePinNote(context, ref, note);
await togglePinNote(context, ref, note: note);
case NoteMenuOption.selectLabels:
selectLabels(context, ref, note);
selectLabels(context, ref, note: note);
case NoteMenuOption.copy:
await copyNote(note);
await copyNote(note: note);
case NoteMenuOption.share:
await shareNote(note);
await shareNote(note: note);
case NoteMenuOption.delete:
await deleteNote(context, ref, note, true);
await deleteNote(context, ref, note: note, pop: true);
case NoteMenuOption.about:
await showModalBottomSheet<void>(
context: context,
Expand All @@ -87,9 +87,9 @@ class _BackAppBarState extends ConsumerState<EditorAppBar> {

switch (menuOption) {
case BinMenuOption.restore:
await restoreNote(context, ref, note, true);
await restoreNote(context, ref, note: note, pop: true);
case BinMenuOption.deletePermanently:
await permanentlyDeleteNote(context, ref, note, true);
await permanentlyDeleteNote(context, ref, note: note, pop: true);
case BinMenuOption.about:
await showModalBottomSheet<void>(
context: context,
Expand Down
27 changes: 15 additions & 12 deletions lib/common/navigation/app_bars/notes_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../models/label/label.dart';
import '../../../models/note/note.dart';
import '../../../providers/bin/bin_provider.dart';
import '../../../providers/notes/notes_provider.dart';
import '../../../providers/notifiers/notifiers.dart';
import '../../../providers/preferences/preferences_provider.dart';
import '../../../services/notes/notes_service.dart';
import '../../../utils/keys.dart';
Expand All @@ -29,19 +29,20 @@ class NotesAppBar extends ConsumerWidget {
/// Default constructor.
const NotesAppBar({
super.key,
required this.title,
this.label,
this.notesPage = true,
});

/// Title to display in the app bar.
final String title;

/// Whether the current page is the notes list.
final bool notesPage;

/// The label on which the notes are filtered if in a label page.
final Label? label;
/// Returns the title of the app bar.
String get title {
if (notesPage) {
return currentLabelFilter?.name ?? l.navigation_notes;
} else {
return l.navigation_bin;
}
}

/// Returns the placeholder for the search button used when the search isn't available.
Widget get searchButtonPlaceholder => IconButton(
Expand Down Expand Up @@ -91,7 +92,9 @@ class NotesAppBar extends ConsumerWidget {
Navigator.pop(context);
}

notesPage ? ref.read(notesProvider.notifier).sort() : ref.read(binProvider.notifier).sort();
notesPage
? ref.read(notesProvider(label: currentLabelFilter).notifier).sort()
: ref.read(binProvider.notifier).sort();
}

/// Searches for the notes that match the [search].
Expand All @@ -100,7 +103,7 @@ class NotesAppBar extends ConsumerWidget {
return [];
}

final notes = await NotesService().search(search, notesPage, label?.name);
final notes = await NotesService().search(search, notesPage, currentLabelFilter?.name);

return notes
.mapIndexed(
Expand Down Expand Up @@ -199,13 +202,13 @@ class NotesAppBar extends ConsumerWidget {
onSelected: (sortMethod) => sort(context, ref, sortMethod: sortMethod),
),
if (notesPage)
ref.watch(notesProvider).when(
ref.watch(notesProvider(label: currentLabelFilter)).when(
data: (notes) => child(context, notes),
error: (error, stackTrace) => const EmptyPlaceholder(),
loading: () => searchButtonPlaceholder,
)
else
ref.watch(notesProvider).when(
ref.watch(notesProvider(label: currentLabelFilter)).when(
data: (notes) => child(context, notes),
error: (error, stackTrace) => const EmptyPlaceholder(),
loading: () => searchButtonPlaceholder,
Expand Down
16 changes: 9 additions & 7 deletions lib/common/navigation/app_bars/notes_selection_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../models/note/note.dart';
import '../../../providers/bin/bin_provider.dart';
import '../../../providers/notes/notes_provider.dart';
import '../../../providers/notifiers/notifiers.dart';
import '../../actions/notes/delete.dart';
import '../../actions/notes/labels.dart';
import '../../actions/notes/pin.dart';
Expand Down Expand Up @@ -59,39 +60,40 @@ class NotesSelectionAppBar extends ConsumerWidget {
IconButton(
icon: const Icon(Icons.share),
tooltip: 'Share',
onPressed: selectedNotes.isNotEmpty ? () => shareNotes(selectedNotes) : null,
onPressed: selectedNotes.isNotEmpty ? () => shareNotes(notes: selectedNotes) : null,
),
IconButton(
icon: const Icon(Icons.push_pin),
tooltip: l.tooltip_toggle_pins,
onPressed: selectedNotes.isNotEmpty ? () => togglePinNotes(context, ref, selectedNotes) : null,
onPressed: selectedNotes.isNotEmpty ? () => togglePinNotes(context, ref, notes: selectedNotes) : null,
),
IconButton(
icon: const Icon(Icons.new_label),
tooltip: 'Add labels',
onPressed: selectedNotes.isNotEmpty ? () => addLabels(context, ref, selectedNotes) : null,
onPressed: selectedNotes.isNotEmpty ? () => addLabels(context, ref, notes: selectedNotes) : null,
),
IconButton(
icon: Icon(
Icons.delete,
color: selectedNotes.isNotEmpty ? Theme.of(context).colorScheme.error : null,
),
tooltip: l.tooltip_delete,
onPressed: selectedNotes.isNotEmpty ? () => deleteNotes(context, ref, selectedNotes) : null,
onPressed: selectedNotes.isNotEmpty ? () => deleteNotes(context, ref, notes: selectedNotes) : null,
),
] else ...[
IconButton(
icon: const Icon(Icons.restore_from_trash),
tooltip: l.tooltip_restore,
onPressed: selectedNotes.isNotEmpty ? () => restoreNotes(context, ref, selectedNotes) : null,
onPressed: selectedNotes.isNotEmpty ? () => restoreNotes(context, ref, notes: selectedNotes) : null,
),
IconButton(
icon: Icon(
Icons.delete_forever,
color: selectedNotes.isNotEmpty ? Theme.of(context).colorScheme.error : null,
),
tooltip: l.tooltip_permanently_delete,
onPressed: selectedNotes.isNotEmpty ? () => permanentlyDeleteNotes(context, ref, selectedNotes) : null,
onPressed:
selectedNotes.isNotEmpty ? () => permanentlyDeleteNotes(context, ref, notes: selectedNotes) : null,
),
],
Padding(padding: Paddings.appBarActionsEnd),
Expand All @@ -101,7 +103,7 @@ class NotesSelectionAppBar extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) => notesPage
? ref.watch(notesProvider).when(
? ref.watch(notesProvider(label: currentLabelFilter)).when(
data: (notes) => buildAppBar(
context,
ref,
Expand Down
Loading

0 comments on commit e5c85f0

Please sign in to comment.