From 8d8a9ccf2c5c6910ae5963811f24d9ce326ae0e3 Mon Sep 17 00:00:00 2001 From: Crondung <1922635@stud.hs-mannheim.de> Date: Fri, 17 Feb 2023 13:47:52 +0100 Subject: [PATCH] wip --- assets/config.json | 4 +++ lib/main.dart | 2 +- lib/models/settings.dart | 13 ++++++++++ lib/pages/main_page.dart | 36 +++++++++++++++++++-------- lib/pages/scanner_page.dart | 27 ++++++++++++++++++++ lib/pages/settings_page.dart | 10 -------- lib/service/json_service.dart | 9 +++++++ lib/service/settings_service.dart | 24 ++++++++++++++++++ lib/widgets/missing_config_popup.dart | 19 ++++++++++++++ pubspec.yaml | 2 ++ 10 files changed, 125 insertions(+), 21 deletions(-) create mode 100644 assets/config.json create mode 100644 lib/models/settings.dart create mode 100644 lib/pages/scanner_page.dart delete mode 100644 lib/pages/settings_page.dart create mode 100644 lib/service/json_service.dart create mode 100644 lib/widgets/missing_config_popup.dart diff --git a/assets/config.json b/assets/config.json new file mode 100644 index 0000000..a80b05f --- /dev/null +++ b/assets/config.json @@ -0,0 +1,4 @@ +{ + "group": "Gruppe 2", + "key": "value" +} diff --git a/lib/main.dart b/lib/main.dart index bc91015..6e5d7a5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -10,6 +10,6 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - return const MaterialApp(title: _title, home: MyHomePage()); + return MaterialApp(title: _title, home: MyHomePage()); } } diff --git a/lib/models/settings.dart b/lib/models/settings.dart new file mode 100644 index 0000000..4dced6e --- /dev/null +++ b/lib/models/settings.dart @@ -0,0 +1,13 @@ +class Settings { + final String group; + final String key; + //final List relapseCategories; + + Settings(this.group, this.key); //, this.relapseCategories); + + Settings.fromJson(Map json) + : group = json['group'].toString(), + key = json['key'].toString() + //relapseCategories = json['relapse_categories'] + ; +} diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 3241a4c..a1f7213 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -1,9 +1,11 @@ import 'package:flutter/material.dart'; import 'package:smoke_cess_app/pages/mood_page.dart'; import 'package:smoke_cess_app/pages/relapse_page.dart'; -import 'package:smoke_cess_app/pages/settings_page.dart'; +import 'package:smoke_cess_app/pages/scanner_page.dart'; import 'package:smoke_cess_app/pages/sleep_page.dart'; import 'package:smoke_cess_app/pages/timer_page.dart'; +import 'package:smoke_cess_app/service/settings_service.dart'; +import 'package:smoke_cess_app/widgets/missing_config_popup.dart'; class MyHomePage extends StatefulWidget { const MyHomePage({super.key}); @@ -13,24 +15,38 @@ class MyHomePage extends StatefulWidget { } class MyHomePageState extends State { - int _selectedIndex = 2; + int _selectedIndex = 4; + final SettingsService settingsService = SettingsService(); + final List _titles = [ 'Stimmung', 'Schlaf', 'Timer', 'Rückfall', - 'Einstellungen' + 'Scanner' ]; - static const List _widgetOptions = [ - MoodPage(), - SleepPage(), - StopWatchTimerPage(), - RelapsePage(), + static final List _widgetOptions = [ + const MoodPage(), + const SleepPage(), + const StopWatchTimerPage(), + const RelapsePage(), SettingsPage(), ]; void _onItemTapped(int index) { - setState(() => _selectedIndex = index); + setState(() async { + (await settingsService.getGroup() != null) + ? _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(); + }); + }); } @override @@ -58,7 +74,7 @@ class MyHomePageState extends State { icon: Icon(Icons.smoke_free_outlined, color: Colors.black), label: 'Rückfall'), NavigationDestination( - icon: Icon(Icons.settings, color: Colors.black), + icon: Icon(Icons.camera_alt_outlined, color: Colors.black), label: 'Settings'), ], diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart new file mode 100644 index 0000000..f3bf96a --- /dev/null +++ b/lib/pages/scanner_page.dart @@ -0,0 +1,27 @@ +import 'package:flutter/material.dart'; +import 'package:smoke_cess_app/service/settings_service.dart'; + +class SettingsPage extends StatelessWidget { + SettingsPage({super.key}); + SettingsService service = SettingsService(); + + @override + Widget build(BuildContext context) { + return Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + const Text('Hier taucht die Kamera zum Scannen auf'), + const SizedBox(height: 30), + ElevatedButton( + style: ElevatedButton.styleFrom( + textStyle: const TextStyle(fontSize: 20)), + onPressed: () { + service.loadSettings(); + }, + child: const Text('Read JSON'), + ) + ], + )); + } +} diff --git a/lib/pages/settings_page.dart b/lib/pages/settings_page.dart deleted file mode 100644 index 899b163..0000000 --- a/lib/pages/settings_page.dart +++ /dev/null @@ -1,10 +0,0 @@ -import 'package:flutter/material.dart'; - -class SettingsPage extends StatelessWidget { - const SettingsPage({super.key}); - - @override - Widget build(BuildContext context) { - return const Center(child: Text('Hier können Settings eingestellt werden')); - } -} diff --git a/lib/service/json_service.dart b/lib/service/json_service.dart new file mode 100644 index 0000000..d20dacf --- /dev/null +++ b/lib/service/json_service.dart @@ -0,0 +1,9 @@ +import 'package:flutter/services.dart'; +import 'dart:convert'; + +const String configJSONPath = 'assets/config.json'; + +Future> loadLocalConfigJSON() async { + String content = await rootBundle.loadString(configJSONPath); + return jsonDecode(content); +} diff --git a/lib/service/settings_service.dart b/lib/service/settings_service.dart index ebd5b05..7b41f0c 100644 --- a/lib/service/settings_service.dart +++ b/lib/service/settings_service.dart @@ -1,4 +1,6 @@ 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(); @@ -7,6 +9,11 @@ class 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)); @@ -19,7 +26,24 @@ class SettingsService { 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); + } } diff --git a/lib/widgets/missing_config_popup.dart b/lib/widgets/missing_config_popup.dart new file mode 100644 index 0000000..43ed2c8 --- /dev/null +++ b/lib/widgets/missing_config_popup.dart @@ -0,0 +1,19 @@ +import 'package:flutter/material.dart'; + +class MissingConfigPopup extends StatelessWidget { + const MissingConfigPopup({super.key}); + + @override + Widget build(BuildContext context) { + return AlertDialog( + title: const Text('Fehlende Konfiguration'), + content: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.start, + children: const [ + Text("Bitte QR Code Scannen"), + ], + ), + ); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index ea481f5..8502294 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -62,6 +62,8 @@ flutter: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg + assets: + - config.json # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/assets-and-images/#resolution-aware