Skip to content

Commit

Permalink
v1.8.1 (#262)
Browse files Browse the repository at this point in the history
* [259] feat: improve preferences (#260)

* Prepare v1.8.1 (#261)
  • Loading branch information
maelchiotti authored Nov 23, 2024
1 parent aced7e9 commit 7d324d8
Show file tree
Hide file tree
Showing 24 changed files with 163 additions and 106 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.8.1 - 2024-11-13

### Fixed

- Crash at startup because of the sorting setting

## 1.8.0 - 2024-11-23

### Added
Expand Down
1 change: 0 additions & 1 deletion RELEASING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
- [ ] Upgrade Flutter and Dart versions
- [ ] Bump application version
- [ ] Bump dependencies versions
- [ ] Re-generate generated files

### Changelogs

Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/en-US/changelogs/180.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
FIXED
- Crash at startup because of the sorting setting
2 changes: 1 addition & 1 deletion fastlane/metadata/android/fr-FR/changelogs/170.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AJOUT
AJOUTÉ
- Possibilité de catégoriser les notes avec des étiquettes
- Possibilité de trier les notes en fonction de leur date de création
- Possibilité d'utiliser un texte blanc avec le thème sombre
Expand Down
2 changes: 2 additions & 0 deletions fastlane/metadata/android/fr-FR/changelogs/180.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CORRIGÉ
- Crash au démarrage à cause du paramètre de tri
17 changes: 17 additions & 0 deletions lib/common/extensions/iterable_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/// Extends the [Iterable] class with some utilities functions.
extension IterableExtension<T extends Enum> on Iterable<T> {
/// Returns the enum value for the [name] or `null` if it doesn't exist.
T? byNameOrNull(String? name) {
if (name == null) {
return null;
}

for (var value in this) {
if (value.name == name) {
return value;
}
}

return null;
}
}
6 changes: 3 additions & 3 deletions lib/common/logs/app_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class AppLogger {
}

/// Logs an information message.
void i(String message, {Object? exception, StackTrace? stackTrace}) {
void i(String message, [Object? exception, StackTrace? stackTrace]) {
_consoleLogger.i(message, error: exception, stackTrace: stackTrace);
_fileLogger.i(message.firstLine, error: exception, stackTrace: stackTrace);
}

/// Logs a warning message.
void w(String message, {Object? exception, StackTrace? stackTrace}) {
void w(String message, [Object? exception, StackTrace? stackTrace]) {
_consoleLogger.w(message, error: exception, stackTrace: stackTrace);
_fileLogger.w(message.firstLine, error: exception, stackTrace: stackTrace);
}
Expand All @@ -98,7 +98,7 @@ class AppLogger {
}

/// Logs a fatal message.
void f(String message, {Object? exception, StackTrace? stackTrace}) {
void f(String message, [Object? exception, StackTrace? stackTrace]) {
_consoleLogger.f(message, error: exception, stackTrace: stackTrace);
_fileLogger.f(message.firstLine, error: exception, stackTrace: stackTrace);
}
Expand Down
11 changes: 5 additions & 6 deletions lib/common/navigation/app_bars/notes_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:localmaterialnotes/common/extensions/build_context_extension.dar
import 'package:localmaterialnotes/common/preferences/enums/layout.dart';
import 'package:localmaterialnotes/common/preferences/enums/sort_method.dart';
import 'package:localmaterialnotes/common/preferences/preference_key.dart';
import 'package:localmaterialnotes/common/preferences/preferences_utils.dart';
import 'package:localmaterialnotes/common/widgets/notes/note_tile.dart';
import 'package:localmaterialnotes/common/widgets/placeholders/empty_placeholder.dart';
import 'package:localmaterialnotes/models/note/note.dart';
Expand Down Expand Up @@ -68,7 +67,7 @@ class NotesAppBar extends ConsumerWidget {
void _toggleLayout() {
final newLayout = layoutNotifier.value == Layout.list ? Layout.grid : Layout.list;

PreferencesUtils().set<String>(PreferenceKey.layout, newLayout.name);
PreferenceKey.layout.set<String>(newLayout.name);

layoutNotifier.value = newLayout;
}
Expand All @@ -79,20 +78,20 @@ class NotesAppBar extends ConsumerWidget {
if (sortMethod == SortMethod.ascending) {
final oldAscendingPreference = PreferenceKey.sortAscending.getPreferenceOrDefault<bool>();

PreferencesUtils().set<bool>(PreferenceKey.sortAscending, !oldAscendingPreference);
PreferenceKey.sortAscending.set<bool>(!oldAscendingPreference);
}

// The 'Date' or 'Title' menu items were taped
else if (sortMethod != null) {
final forceAscending = sortMethod == SortMethod.title;

PreferencesUtils().set<String>(PreferenceKey.sortMethod, sortMethod.name);
PreferencesUtils().set<bool>(PreferenceKey.sortAscending, forceAscending);
PreferenceKey.sortMethod.set<String>(sortMethod.name);
PreferenceKey.sortAscending.set<bool>(forceAscending);
}

// The checkbox of the 'Ascending' menu item was toggled
else if (ascending != null) {
PreferencesUtils().set<bool>(PreferenceKey.sortAscending, ascending);
PreferenceKey.sortAscending.set<bool>(ascending);

Navigator.pop(context);
}
Expand Down
17 changes: 12 additions & 5 deletions lib/common/preferences/enums/confirmations.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:localmaterialnotes/common/constants/constants.dart';
import 'package:localmaterialnotes/common/extensions/iterable_extension.dart';
import 'package:localmaterialnotes/common/preferences/preference_key.dart';
import 'package:localmaterialnotes/common/preferences/preferences_utils.dart';

/// Lists the options for the confirmations asked for user actions such as pining and deleting notes.
enum Confirmations {
Expand All @@ -16,11 +16,18 @@ enum Confirmations {

/// The value of the preference if set, or its default value otherwise.
factory Confirmations.fromPreference() {
final preference = PreferencesUtils().get<String>(PreferenceKey.confirmations);
final confirmations = Confirmations.values.byNameOrNull(
PreferenceKey.confirmations.getPreference<String>(),
);

return preference != null
? Confirmations.values.byName(preference)
: PreferenceKey.confirmations.defaultValue as Confirmations;
// Reset the malformed preference to its default value
if (confirmations == null) {
PreferenceKey.confirmations.setToDefault();

return PreferenceKey.confirmations.defaultValue as Confirmations;
}

return confirmations;
}

/// Returns the title of the preference for the settings page.
Expand Down
15 changes: 12 additions & 3 deletions lib/common/preferences/enums/layout.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:localmaterialnotes/common/extensions/iterable_extension.dart';
import 'package:localmaterialnotes/common/preferences/preference_key.dart';
import 'package:localmaterialnotes/common/preferences/preferences_utils.dart';

/// Lists the layouts of the notes list.
enum Layout {
Expand All @@ -12,8 +12,17 @@ enum Layout {

/// Returns the value of the preference if set, or its default value otherwise.
factory Layout.fromPreference() {
final preference = PreferencesUtils().get<String>(PreferenceKey.layout);
final layout = Layout.values.byNameOrNull(
PreferenceKey.layout.getPreference<String>(),
);

return preference != null ? Layout.values.byName(preference) : PreferenceKey.layout.defaultValue as Layout;
// Reset the malformed preference to its default value
if (layout == null) {
PreferenceKey.layout.setToDefault();

return PreferenceKey.layout.defaultValue as Layout;
}

return layout;
}
}
17 changes: 12 additions & 5 deletions lib/common/preferences/enums/sort_method.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:localmaterialnotes/common/extensions/iterable_extension.dart';
import 'package:localmaterialnotes/common/preferences/preference_key.dart';
import 'package:localmaterialnotes/common/preferences/preferences_utils.dart';

/// Lists the methods to sort the notes in the notes list.
enum SortMethod {
Expand All @@ -20,10 +20,17 @@ enum SortMethod {

/// Returns the value of the preference if set, or its default value otherwise.
factory SortMethod.fromPreference() {
final preference = PreferencesUtils().get<String>(PreferenceKey.sortMethod);
final sortMethod = SortMethod.values.byNameOrNull(
PreferenceKey.sortMethod.getPreference<String>(),
);

return preference != null
? SortMethod.values.byName(preference)
: PreferenceKey.sortMethod.defaultValue as SortMethod;
// Reset the malformed preference to its default value
if (sortMethod == null) {
PreferenceKey.sortMethod.setToDefault();

return PreferenceKey.sortMethod.defaultValue as SortMethod;
}

return sortMethod;
}
}
32 changes: 23 additions & 9 deletions lib/common/preferences/enums/swipe_action.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:localmaterialnotes/common/constants/constants.dart';
import 'package:localmaterialnotes/common/extensions/iterable_extension.dart';
import 'package:localmaterialnotes/common/preferences/preference_key.dart';
import 'package:localmaterialnotes/common/preferences/preferences_utils.dart';

/// Lists the actions to trigger when swiping on a note tile.
enum SwipeAction {
Expand Down Expand Up @@ -43,20 +43,34 @@ enum SwipeAction {

/// Returns the value of the right swipe action preference if set, or its default value otherwise.
factory SwipeAction.rightFromPreference() {
final preference = PreferencesUtils().get<String>(PreferenceKey.swipeRightAction);
final swipeRightAction = SwipeAction.values.byNameOrNull(
PreferenceKey.swipeRightAction.getPreference<String>(),
);

return preference != null
? SwipeAction.values.byName(preference)
: PreferenceKey.swipeRightAction.defaultValue as SwipeAction;
// Reset the malformed preference to its default value
if (swipeRightAction == null) {
PreferenceKey.swipeRightAction.setToDefault();

return PreferenceKey.swipeRightAction.defaultValue as SwipeAction;
}

return swipeRightAction;
}

/// Returns the value of the left swipe action preference if set, or its default value otherwise.
factory SwipeAction.leftFromPreference() {
final preference = PreferencesUtils().get<String>(PreferenceKey.swipeLeftAction);
final swipeRightAction = SwipeAction.values.byNameOrNull(
PreferenceKey.swipeLeftAction.getPreference<String>(),
);

// Reset the malformed preference to its default value
if (swipeRightAction == null) {
PreferenceKey.swipeLeftAction.setToDefault();

return PreferenceKey.swipeLeftAction.defaultValue as SwipeAction;
}

return preference != null
? SwipeAction.values.byName(preference)
: PreferenceKey.swipeLeftAction.defaultValue as SwipeAction;
return swipeRightAction;
}

/// Returns whether the swipe action is enabled.
Expand Down
38 changes: 20 additions & 18 deletions lib/common/preferences/preference_key.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:localmaterialnotes/common/constants/constants.dart';
import 'package:localmaterialnotes/common/preferences/enums/confirmations.dart';
import 'package:localmaterialnotes/common/preferences/enums/layout.dart';
import 'package:localmaterialnotes/common/preferences/enums/sort_method.dart';
Expand Down Expand Up @@ -54,7 +53,7 @@ enum PreferenceKey {

// Notes
sortMethod(SortMethod.editedDate),
sortAscending(false),
sortAscending(bool),
layout(Layout.list),
;

Expand All @@ -71,29 +70,32 @@ enum PreferenceKey {
/// if the preference should be securely stored, it can be marked as [secure].
const PreferenceKey(this.defaultValue, {this.secure = false});

/// Returns the value of the preference if set, or its default value otherwise.
///
/// The type [T] of the value should be a basic type: `bool`, `int`, `double`, `String` or `List<String>`.
T getPreferenceOrDefault<T>() {
if (T == dynamic) {
throw ArgumentError('The type T is required.');
}
/// Sets the preference to the [value] with the type [T].
Future<void> set<T>(T value) async {
await PreferencesUtils().set<T>(this, value);
}

if (T != bool && T != int && T != double && T != String && T != List<String>) {
throw ArgumentError('The type T should be a native type (bool, int, double, String or List<String>), not $T.');
}
/// Resets the preference to its [defaultValue].
Future<void> setToDefault() async {
await PreferencesUtils().set(this, defaultValue);
}

try {
return PreferencesUtils().get<T>(this) ?? defaultValue as T;
} catch (exception, stackTrace) {
logger.e(exception.toString(), exception, stackTrace);
/// Returns the value of the preference if set, or its default value otherwise.
T? getPreference<T>() {
return PreferencesUtils().get<T>(this);
}

return defaultValue as T;
}
/// Returns the value of the preference if set, or its default value otherwise.
T getPreferenceOrDefault<T>() {
return PreferencesUtils().get<T>(this) ?? defaultValue as T;
}

/// Returns the value of the securely stored preference if set, or its default value otherwise.
Future<String> getPreferenceOrDefaultSecure() async {
return await PreferencesUtils().getSecure(this) ?? defaultValue as String;
}

Future<void> remove() async {
await PreferencesUtils().remove(this);
}
}
12 changes: 8 additions & 4 deletions lib/common/preferences/preferences_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ class PreferencesUtils {
///
/// The type [T] of the value should be a basic type: `bool`, `int`, `double`, `String` or `List<String>`.
T? get<T>(PreferenceKey preferenceKey) {
if (preferenceKey.secure) {
throw ArgumentError('The preference is securely stored, use getSecure() instead');
}

if (T == dynamic) {
throw ArgumentError('The type T is required.');
}

if (T != bool && T != int && T != double && T != String && T != List<String>) {
throw ArgumentError('The type T should be a native type (bool, int, double, String or List<String>), not $T.');
}

if (preferenceKey.secure) {
throw ArgumentError('The preference is securely stored, use getSecure() instead');
}

return _preferences.get(preferenceKey.name) as T?;
}

Expand Down
5 changes: 2 additions & 3 deletions lib/pages/settings/pages/settings_accessibility_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:localmaterialnotes/common/constants/paddings.dart';
import 'package:localmaterialnotes/common/navigation/app_bars/basic_app_bar.dart';
import 'package:localmaterialnotes/common/navigation/top_navigation.dart';
import 'package:localmaterialnotes/common/preferences/preference_key.dart';
import 'package:localmaterialnotes/common/preferences/preferences_utils.dart';
import 'package:localmaterialnotes/providers/notifiers.dart';
import 'package:localmaterialnotes/utils/keys.dart';
import 'package:localmaterialnotes/utils/locale_utils.dart';
Expand All @@ -29,7 +28,7 @@ class _SettingsAppearancePageState extends State<SettingsAccessibilityPage> {
/// Sets the text scaling to the new [textScaling].
void _submittedTextScaling(double textScaling) {
setState(() {
PreferencesUtils().set<double>(PreferenceKey.textScaling, textScaling);
PreferenceKey.textScaling.set<double>(textScaling);
});

textScalingNotifier.value = textScaling;
Expand All @@ -46,7 +45,7 @@ class _SettingsAppearancePageState extends State<SettingsAccessibilityPage> {
/// Toggles whether to use white text in dark mode.
void _toggleUseWhiteTextDarkMode(bool toggled) {
setState(() {
PreferencesUtils().set<bool>(PreferenceKey.useWhiteTextDarkMode, toggled);
PreferenceKey.useWhiteTextDarkMode.set<bool>(toggled);
});

useWhiteTextDarkModeNotifier.value = toggled;
Expand Down
Loading

0 comments on commit 7d324d8

Please sign in to comment.