diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index a1f7213..711efb1 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -16,7 +16,6 @@ class MyHomePage extends StatefulWidget { class MyHomePageState extends State { int _selectedIndex = 4; - final SettingsService settingsService = SettingsService(); final List _titles = [ 'Stimmung', @@ -33,17 +32,15 @@ class MyHomePageState extends State { SettingsPage(), ]; - void _onItemTapped(int index) { - setState(() async { - (await settingsService.getGroup() != null) + Future _onItemTapped(int index) async { + bool isConfigured = (await getGroup()) != null; + print('Gruppe: ${await getGroup()}, isConfigured: $isConfigured'); + setState(() { + isConfigured ? _selectedIndex = index : showDialog( context: context, builder: (BuildContext context) { - Future.delayed( - const Duration(seconds: 3), - () => Navigator.of(context) - .pop(true)); //Dismiss popup after 3 seconds return const MissingConfigPopup(); }); }); diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index f3bf96a..73a6a45 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -3,7 +3,6 @@ import 'package:smoke_cess_app/service/settings_service.dart'; class SettingsPage extends StatelessWidget { SettingsPage({super.key}); - SettingsService service = SettingsService(); @override Widget build(BuildContext context) { @@ -17,7 +16,7 @@ class SettingsPage extends StatelessWidget { style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), onPressed: () { - service.loadSettings(); + loadSettings(); }, child: const Text('Read JSON'), ) diff --git a/lib/pages/timer_page.dart b/lib/pages/timer_page.dart index 691d15d..a2e6f49 100644 --- a/lib/pages/timer_page.dart +++ b/lib/pages/timer_page.dart @@ -11,7 +11,6 @@ class StopWatchTimerPage extends StatefulWidget { } class StopWatchTimerPageState extends State { - SettingsService settings = SettingsService(); Duration duration = const Duration(minutes: 1); Timer? timer; @@ -19,21 +18,11 @@ class StopWatchTimerPageState extends State { @override void initState() { - setDurationWithSetting(); super.initState(); } - void setDurationWithSetting() { - settings.getIntSetting('workout_duration_minutes').then((workoutMinutes) => - {setState(() => duration = Duration(minutes: workoutMinutes ?? 10))}); - } - void reset() { - if (countDown) { - setDurationWithSetting(); - } else { - setState(() => duration = const Duration()); - } + setState(() => duration = const Duration()); } void startTimer() { diff --git a/lib/service/settings_service.dart b/lib/service/settings_service.dart index 7b41f0c..cc7a0f6 100644 --- a/lib/service/settings_service.dart +++ b/lib/service/settings_service.dart @@ -2,48 +2,42 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/service/json_service.dart'; -class SettingsService { - final Future _prefs = SharedPreferences.getInstance(); +final Future _prefs = SharedPreferences.getInstance(); - SettingsService() { - setIntSetting('workout_duration_minutes', 5); - } - - //access group setting which was saved in local storage - Future getGroup() { - return getStringSetting('group'); - } - - void setStringSetting(String settingKey, String settingValue) => - _prefs.then((pref) => pref.setString(settingKey, settingValue)); - - Future getStringSetting(String settingKey) => - _prefs.then((pref) => pref.getString(settingKey)); - - void setIntSetting(String settingKey, int settingValue) => - _prefs.then((pref) => pref.setInt(settingKey, settingValue)); - - Future getIntSetting(String settingKey) => - _prefs.then((pref) => pref.getInt(settingKey)); - - void setStringList(String settingKey, List list) => - _prefs.then((pref) => pref.setStringList(settingKey, list)); - - //Add other setters and getters if needed - //other possible SharedPreferences Types: Int, Bool, Double, StringList - //see https://pub.dev/packages/shared_preferences - - Future loadSettings() async { - Map configJSON = await loadLocalConfigJSON(); - Settings settings = Settings.fromJson(configJSON); - print(settings.group); - saveSettings(settings); - String gruppe = (await getGroup())!; - print('Gruppe: $gruppe'); - } - - void saveSettings(Settings settings) { - setStringSetting('group', settings.group); - setStringSetting('key', settings.key); - } +//access group setting which was saved in local storage +Future getGroup() { + return getStringSetting('group'); +} + +void setStringSetting(String settingKey, String settingValue) => + _prefs.then((pref) => pref.setString(settingKey, settingValue)); + +Future getStringSetting(String settingKey) => + _prefs.then((pref) => pref.getString(settingKey)); + +void setIntSetting(String settingKey, int settingValue) => + _prefs.then((pref) => pref.setInt(settingKey, settingValue)); + +Future getIntSetting(String settingKey) => + _prefs.then((pref) => pref.getInt(settingKey)); + +void setStringList(String settingKey, List list) => + _prefs.then((pref) => pref.setStringList(settingKey, list)); + +//Add other setters and getters if needed +//other possible SharedPreferences Types: Int, Bool, Double, StringList +//see https://pub.dev/packages/shared_preferences + +Future loadSettings() async { + Map configJSON = await loadLocalConfigJSON(); + Settings settings = Settings.fromJson(configJSON); + print(settings.group); + saveSettings(settings); + String gruppe = (await getGroup())!; + print('Gruppe: $gruppe'); +} + +void saveSettings(Settings settings) { + setStringSetting('group', settings.group); + setStringSetting('key', settings.key); }