Skip to content

Commit

Permalink
[303] feat: add default note type for shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
maelchiotti committed Jan 20, 2025
1 parent 82662f2 commit 0602a26
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 74 deletions.
10 changes: 1 addition & 9 deletions lib/common/navigation/app_bars/basic_app_bar.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import 'package:flutter/material.dart';

/// Basic app bar.
///
/// Contains:
/// - A back button if built with [BasicAppBar.back].
/// - The title of the current route.
class BasicAppBar extends StatelessWidget {
/// Default constructor.
/// A basic app bar with a title.
const BasicAppBar({
super.key,
required this.title,
this.back = false,
});

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

/// Whether to show the back button.
final bool back;

@override
Widget build(BuildContext context) {
return AppBar(
Expand Down
1 change: 1 addition & 0 deletions lib/common/preferences/preference_key.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum PreferenceKey<T extends Object> {

// Notes creation
availableNotesTypes<List<String>>(['plainText', 'richText']),
defaultShortcutNoteType<String>('plainText'),

// Notes tiles
showTilesBackground<bool>(false),
Expand Down
4 changes: 4 additions & 0 deletions lib/l10n/translations/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
"@navigation_settings_appearance": {
"description": "Title of the settings page regarding the application's appearance."
},
"navigation_settings_notes_types": "Notes types",
"@navigation_settings_notes_types": {
"description": "Title of the settings page regarding the notes types."
},
"navigation_settings_notes_tiles": "Notes tiles",
"@navigation_settings_notes_tiles": {
"description": "Title of the settings page regarding the notes tiles appearance."
Expand Down
20 changes: 18 additions & 2 deletions lib/models/note/notes_types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import '../../common/constants/constants.dart';
import '../../common/preferences/preference_key.dart';
import 'note.dart';

/// The types of notes.
enum NoteType<T extends Note> {
/// Plain text note.
plainText<PlainTextNote>(),

/// Rich text note.
richText<RichTextNote>(),
;

const NoteType();

/// The title of this type.
String get title {
return switch (T) {
== PlainTextNote => l.note_type_plain_text,
Expand All @@ -19,7 +24,10 @@ enum NoteType<T extends Note> {
};
}

/// Returns the list of notes types saved in the preferences as a list of [Type].
/// The types that can be used when creating a new note from a shortcut.
static List<NoteType> get shortcutTypes => [plainText, richText];

/// The list of types available when creating a new note from the notes list.
static List<NoteType> get availableTypes {
final availableTypesPreference = PreferenceKey.availableNotesTypes.getPreferenceOrDefault();

Expand All @@ -28,11 +36,19 @@ enum NoteType<T extends Note> {
}).toList();
}

/// Returns the list of notes types saved in the preferences as a comma-separated [String].
/// The list of types available when creating a new note from the notes list as a comma-separated [String].
static String get availableTypesAsString {
return availableTypes.map((type) => type.title).join(', ').capitalizeFirstLowerRest;
}

/// The default note type to used when creating a new note from a shortcut.
static NoteType get defaultShortcutType {
final defaultShortcutType = PreferenceKey.defaultShortcutNoteType.getPreferenceOrDefault();

return values.byName(defaultShortcutType);
}

/// Returns the [types] as a list of [String] that can be saved to the preferences.
static List<String> toPreference(List<NoteType> types) {
return types.map((type) => type.name).toList();
}
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/labels/labels_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ class LabelsPage extends ConsumerWidget {
return ref.watch(labelsProvider).when(
data: (labels) => Scaffold(
appBar: TopNavigation(
appbar: BasicAppBar(
title: l.navigation_manage_labels_page,
back: true,
),
appbar: BasicAppBar(title: l.navigation_manage_labels_page),
),
drawer: const SideNavigation(),
floatingActionButton: const AddLabelFab(),
Expand Down
14 changes: 6 additions & 8 deletions lib/pages/settings/pages/settings_about_page.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import 'package:flutter/material.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';
import 'package:settings_tiles/settings_tiles.dart';
import 'package:simple_icons/simple_icons.dart';
import 'package:url_launcher/url_launcher.dart';

import '../../../common/constants/constants.dart';
import '../../../common/constants/paddings.dart';
import '../../../common/constants/sizes.dart';
Expand All @@ -8,10 +13,6 @@ import '../../../utils/asset.dart';
import '../../../utils/info_utils.dart';
import '../../../utils/keys.dart';
import '../../../utils/utils.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';
import 'package:settings_tiles/settings_tiles.dart';
import 'package:simple_icons/simple_icons.dart';
import 'package:url_launcher/url_launcher.dart';

/// Settings providing information about the application.
class SettingsAboutPage extends StatelessWidget {
Expand Down Expand Up @@ -109,10 +110,7 @@ class SettingsAboutPage extends StatelessWidget {
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_about,
back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_about),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
8 changes: 3 additions & 5 deletions lib/pages/settings/pages/settings_accessibility_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:dart_helper_utils/dart_helper_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:settings_tiles/settings_tiles.dart';

import '../../../common/constants/constants.dart';
import '../../../common/constants/paddings.dart';
import '../../../common/navigation/app_bars/basic_app_bar.dart';
Expand All @@ -10,7 +12,6 @@ import '../../../common/preferences/watched_preferences.dart';
import '../../../providers/preferences/preferences_provider.dart';
import '../../../utils/keys.dart';
import '../../../utils/locale_utils.dart';
import 'package:settings_tiles/settings_tiles.dart';

/// Accessibility settings.
class SettingsAccessibilityPage extends ConsumerStatefulWidget {
Expand Down Expand Up @@ -79,10 +80,7 @@ class _SettingsAppearancePageState extends ConsumerState<SettingsAccessibilityPa
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_accessibility,
back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_accessibility),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/settings/pages/settings_appearance_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ class _SettingsAppearancePageState extends ConsumerState<SettingsAppearancePage>
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_appearance,
//back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_appearance),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/settings/pages/settings_backup_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,7 @@ class _SettingsBackupPageState extends ConsumerState<SettingsBackupPage> {
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_backup,
back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_backup),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/settings/pages/settings_behavior_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ class _SettingsBehaviorPageState extends ConsumerState<SettingsBehaviorPage> {
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_behavior,
back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_behavior),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
8 changes: 3 additions & 5 deletions lib/pages/settings/pages/settings_editor_page.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'package:flutter/material.dart';
import 'package:settings_tiles/settings_tiles.dart';

import '../../../common/constants/constants.dart';
import '../../../common/constants/paddings.dart';
import '../../../common/navigation/app_bars/basic_app_bar.dart';
import '../../../common/navigation/top_navigation.dart';
import '../../../common/preferences/preference_key.dart';
import '../../../utils/keys.dart';
import 'package:settings_tiles/settings_tiles.dart';

/// Settings related to the notes editor.
class SettingsEditorPage extends StatefulWidget {
Expand Down Expand Up @@ -84,10 +85,7 @@ class _SettingsEditorPageState extends State<SettingsEditorPage> {
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_editor,
back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_editor),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
12 changes: 5 additions & 7 deletions lib/pages/settings/pages/settings_labels_page.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';
import 'package:restart_app/restart_app.dart';
import 'package:settings_tiles/settings_tiles.dart';

import '../../../common/constants/constants.dart';
import '../../../common/constants/paddings.dart';
import '../../../common/navigation/app_bars/basic_app_bar.dart';
import '../../../common/navigation/top_navigation.dart';
import '../../../common/preferences/preference_key.dart';
import '../../../utils/keys.dart';
import 'package:material_symbols_icons/material_symbols_icons.dart';
import 'package:restart_app/restart_app.dart';
import 'package:settings_tiles/settings_tiles.dart';

/// Settings related to the labels.
class SettingsLabelsPage extends StatefulWidget {
Expand Down Expand Up @@ -55,10 +56,7 @@ class _SettingsLabelsPageState extends State<SettingsLabelsPage> {
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_labels,
back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_labels),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/settings/pages/settings_notes_tiles_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,7 @@ class _SettingsNotesTilesPageState extends ConsumerState<SettingsNotesTilesPage>
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_notes_tiles,
//back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_notes_tiles),
),
body: SingleChildScrollView(
child: Padding(
Expand Down
39 changes: 28 additions & 11 deletions lib/pages/settings/pages/settings_notes_types_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,38 @@ class SettingsNotesTypesPage extends ConsumerStatefulWidget {

class _SettingsNotesTypesPageState extends ConsumerState<SettingsNotesTypesPage> {
/// Sets the setting for the available notes types to [availableNotesTypes].
void _onSubmittedAvailableNotesTypes(List<NoteType> availableNotesTypes) {
void onSubmittedAvailableNotesTypes(List<NoteType> availableNotesTypes) {
PreferenceKey.availableNotesTypes.set(NoteType.toPreference(availableNotesTypes));

ref.read(preferencesProvider.notifier).update(WatchedPreferences(availableNotesTypes: availableNotesTypes));
}

/// Sets the setting for the default shortcut note type to [defaultShortcutNoteType].
void onSubmittedDefaultShortcutNoteType(NoteType defaultShortcutNoteType) {
setState(() {
PreferenceKey.defaultShortcutNoteType.set(defaultShortcutNoteType.name);
});
}

@override
Widget build(BuildContext context) {
final notesTypes = NoteType.values
.map(
(type) => (value: type, title: type.title, subtitle: null),
)
.toList();
final notesTypes = NoteType.values.map((type) {
return (value: type, title: type.title, subtitle: null);
}).toList();
final availableNotesTypes = ref.watch(
preferencesProvider.select((preferences) => preferences.availableNotesTypes),
);
final availableNotesTypesString = NoteType.availableTypesAsString;

final shortcutNotesTypes = NoteType.shortcutTypes.map((type) {
return (value: type, title: type.title, subtitle: null);
}).toList();
final defaultShortcutNoteType = NoteType.defaultShortcutType;

return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMainSubpage,
appbar: BasicAppBar(
title: l.navigation_settings_notes_tiles,
//back: true,
),
appbar: BasicAppBar(title: l.navigation_settings_notes_types),
),
body: SingleChildScrollView(
child: Padding(
Expand All @@ -67,7 +74,17 @@ class _SettingsNotesTypesPageState extends ConsumerState<SettingsNotesTypesPage>
options: notesTypes,
initialOptions: availableNotesTypes,
minOptions: 1,
onSubmitted: _onSubmittedAvailableNotesTypes,
onSubmitted: onSubmittedAvailableNotesTypes,
),
SettingSingleOptionTile.detailed(
icon: Icons.app_shortcut,
title: 'Default shortcut type',
value: defaultShortcutNoteType.title,
description: 'The default note type to use when creating a note from a shortcut',
dialogTitle: 'Default shortcut type',
options: shortcutNotesTypes,
initialOption: defaultShortcutNoteType,
onSubmitted: onSubmittedDefaultShortcutNoteType,
),
],
),
Expand Down
5 changes: 1 addition & 4 deletions lib/pages/settings/settings_main_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ class SettingsMainPage extends StatelessWidget {
return Scaffold(
appBar: TopNavigation(
key: Keys.appBarSettingsMain,
appbar: BasicAppBar(
title: l.navigation_settings,
//back: true,
),
appbar: BasicAppBar(title: l.navigation_settings),
),
drawer: const SideNavigation(),
body: SingleChildScrollView(
Expand Down
14 changes: 12 additions & 2 deletions lib/utils/quick_actions_utils.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:quick_actions/quick_actions.dart';

import '../common/actions/notes/add.dart';
import '../models/note/note.dart';
import '../models/note/notes_types.dart';
import 'localizations_utils.dart';
import 'package:quick_actions/quick_actions.dart';

/// Utilities for the quick actions.
class QuickActionsUtils {
Expand All @@ -15,7 +18,14 @@ class QuickActionsUtils {

quickActions.initialize((action) {
if (action == 'add_note') {
addNote(context, ref);
final defaultShortcutNoteType = NoteType.defaultShortcutType;

switch (defaultShortcutNoteType) {
case NoteType.plainText:
addNote<PlainTextNote>(context, ref);
case NoteType.richText:
addNote<RichTextNote>(context, ref);
}
}
});

Expand Down
Loading

0 comments on commit 0602a26

Please sign in to comment.