From c747d3c1ba9ed144c4a2b0f8d4ad5a88bde94c11 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 12 May 2020 19:00:35 +0200 Subject: [PATCH] Create ApplicationPreferences object --- lib/helpers/preferences_helper.dart | 7 +++++++ lib/main.dart | 11 +++++------ lib/models/application_preferences.dart | 13 +++++++++++++ 3 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 lib/models/application_preferences.dart diff --git a/lib/helpers/preferences_helper.dart b/lib/helpers/preferences_helper.dart index ca8ae1d..62e08f7 100644 --- a/lib/helpers/preferences_helper.dart +++ b/lib/helpers/preferences_helper.dart @@ -1,5 +1,6 @@ import 'dart:convert'; +import 'package:comunic/models/application_preferences.dart'; import 'package:comunic/models/login_tokens.dart'; import 'package:shared_preferences/shared_preferences.dart'; @@ -70,6 +71,12 @@ class PreferencesHelper { final v = _sharedPreferences.getBool(_PreferenceKeysName[key]); return v == null ? alternative : v; } + + /// Get all settings as an [ApplicationPreferences] object + ApplicationPreferences get preferences => ApplicationPreferences( + enableDarkMode: + getBool(PreferencesKeyList.ENABLE_DARK_THEME, alternative: null), + ); } PreferencesHelper preferences() { diff --git a/lib/main.dart b/lib/main.dart index 9bb7aaf..81cc7fb 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -24,21 +24,20 @@ void subMain() async { final signedIn = await AccountHelper().signedIn(); runApp(ComunicApplication( - darkMode: (await PreferencesHelper.getInstance()) - .getBool(PreferencesKeyList.ENABLE_DARK_THEME), + preferences: await PreferencesHelper.getInstance(), signedIn: signedIn, )); } class ComunicApplication extends StatelessWidget { - final bool darkMode; + final PreferencesHelper preferences; final bool signedIn; const ComunicApplication({ Key key, - @required this.darkMode, + @required this.preferences, @required this.signedIn, - }) : assert(darkMode != null), + }) : assert(preferences != null), assert(signedIn != null), super(key: key); @@ -47,7 +46,7 @@ class ComunicApplication extends StatelessWidget { return MaterialApp( debugShowCheckedModeBanner: false, home: signedIn ? InitializeWidget() : LoginRoute(), - theme: darkMode ? ThemeData.dark() : ThemeData.light(), + theme: preferences.preferences.enableDarkMode ? ThemeData.dark() : null, ); } } diff --git a/lib/models/application_preferences.dart b/lib/models/application_preferences.dart new file mode 100644 index 0000000..36f221e --- /dev/null +++ b/lib/models/application_preferences.dart @@ -0,0 +1,13 @@ +import 'package:flutter/cupertino.dart'; + +/// Application settings +/// +/// @author Pierre Hubert + +class ApplicationPreferences { + final bool enableDarkMode; + + const ApplicationPreferences({ + @required this.enableDarkMode, + }); +}