From d4018826719b2ec8cd040470ed772854bec4f3d6 Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 27 Feb 2023 17:00:28 +0100 Subject: [PATCH 1/5] Refactoring ScannerPage --- lib/pages/scanner_page.dart | 100 +++++++++--------------------------- lib/widgets/scanner.dart | 82 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 75 deletions(-) create mode 100644 lib/widgets/scanner.dart diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index 82485f8..7e9df8f 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -1,108 +1,58 @@ import 'package:flutter/material.dart'; -import 'package:mobile_scanner/mobile_scanner.dart'; import 'package:provider/provider.dart'; import 'package:smoke_cess_app/models/mood.dart'; import 'package:smoke_cess_app/models/relapse.dart'; -import 'package:smoke_cess_app/models/settings.dart'; -import 'package:smoke_cess_app/services/database_service.dart'; -import 'package:smoke_cess_app/services/json_service.dart'; import 'package:smoke_cess_app/services/settings_service.dart'; import 'package:smoke_cess_app/services/notification_service.dart'; - +import 'package:smoke_cess_app/widgets/scanner.dart'; import '../models/sleep.dart'; import '../providers/settings_provider.dart'; -import '../widgets/missing_config_popup.dart'; import '../globals.dart' as globals; -class ScannerPage extends StatefulWidget { +class ScannerPage extends StatelessWidget { const ScannerPage({super.key}); - @override - State createState() => ScannerPageState(); -} + void export() async { + List moods = await globals.databaseService.getMoodRecords(); + List sleeps = await globals.databaseService.getSleepRecords(); + List relapses = await globals.databaseService.getRelapseRecords(); + for (Mood mood in moods) { + print(mood.toCSV()); + } + for (Sleep sleep in sleeps) { + print(sleep.toCSV()); + } + for (Relapse relapse in relapses) { + print(relapse.toCSV()); + } + } -class ScannerPageState extends State { - bool scanning = false; + void loadJSON(BuildContext context) async { + var settingsModel = context.read(); + await loadSettingsFromLocalJSON(); + settingsModel.initSettings(); + NotificationService().setAllNotifications(); + } @override Widget build(BuildContext context) { - var settings = context.watch(); return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - scanning - ? Expanded( - child: MobileScanner( - fit: BoxFit.contain, - controller: MobileScannerController( - detectionTimeoutMs: 2000, - ), - onDetect: (capture) { - //TODO Errorhandling!! - final List barcodes = capture.barcodes; - for (final barcode in barcodes) { - if (barcode.rawValue != null) { - String qrText = barcode.rawValue!; - Map json = stringToJSON(qrText); - Settings settings = Settings.fromJson(json); - saveSettings(settings); - setState(() { - scanning = false; - showDialog( - context: context, - builder: (BuildContext context) { - return MissingConfigPopup( - title: 'Konfiguration erfolgreich', - text: 'Du gehörst zu Gruppe ${settings.group}', - ); - }); - }); - } - } - }, - )) - : ElevatedButton( - style: ElevatedButton.styleFrom( - textStyle: const TextStyle(fontSize: 20)), - onPressed: () { - setState(() => scanning = true); - }, - child: const Text('Scan QR Code'), - ), + const MyScanner(), const SizedBox(height: 30), ElevatedButton( style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), - onPressed: () { - loadSettingsFromLocalJSON(); - Future.delayed(Duration(milliseconds: 100), () { - settings.initSettings(); - NotificationService().setAllNotifications(); - }); - }, + onPressed: () => loadJSON(context), child: const Text('Read JSON'), ), const SizedBox(height: 30), ElevatedButton( style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), - onPressed: () async { - List moods = await globals.databaseService.getMoodRecords(); - List sleeps = - await globals.databaseService.getSleepRecords(); - List relapses = - await globals.databaseService.getRelapseRecords(); - for (Mood mood in moods) { - print(mood.toCSV()); - } - for (Sleep sleep in sleeps) { - print(sleep.toCSV()); - } - for (Relapse relapse in relapses) { - print(relapse.toCSV()); - } - }, + onPressed: export, child: const Text('Export'), ) ], diff --git a/lib/widgets/scanner.dart b/lib/widgets/scanner.dart new file mode 100644 index 0000000..f8e22d1 --- /dev/null +++ b/lib/widgets/scanner.dart @@ -0,0 +1,82 @@ +import 'package:flutter/material.dart'; +import 'package:mobile_scanner/mobile_scanner.dart'; +import 'package:smoke_cess_app/models/settings.dart'; +import 'package:smoke_cess_app/services/json_service.dart'; +import 'package:smoke_cess_app/services/settings_service.dart'; +import '../widgets/missing_config_popup.dart'; + +class MyScanner extends StatefulWidget { + const MyScanner({super.key}); + + @override + State createState() => MyScannerState(); +} + +class MyScannerState extends State { + bool scanning = false; + + void handleError(String error) { + setState(() { + scanning = false; + showDialog( + context: context, + builder: (BuildContext context) { + return MissingConfigPopup( + title: 'Scanfehler', + text: + 'Beim Scanen gab es folgende Meldung: $error. Bitte erneut versuchen', + ); + }); + }); + } + + void handleSucces(String group) { + setState(() { + scanning = false; + showDialog( + context: context, + builder: (BuildContext context) { + return MissingConfigPopup( + title: 'Konfiguration erfolgreich', + text: 'Du gehörst zu Gruppe $group', + ); + }); + }); + } + + void onDetect(capture) { + try { + final List barcodes = capture.barcodes; + for (final barcode in barcodes) { + if (barcode.rawValue != null) { + String qrText = barcode.rawValue!; + Map json = stringToJSON(qrText); + Settings settings = Settings.fromJson(json); + saveSettings(settings); + } + } + } catch (e) { + handleError(e.toString()); + } + } + + @override + Widget build(BuildContext context) { + return scanning + ? Expanded( + child: MobileScanner( + fit: BoxFit.contain, + controller: MobileScannerController( + detectionTimeoutMs: 2000, + ), + onDetect: onDetect)) + : ElevatedButton( + style: ElevatedButton.styleFrom( + textStyle: const TextStyle(fontSize: 20)), + onPressed: () { + setState(() => scanning = true); + }, + child: const Text('Scan QR Code'), + ); + } +} From 3909bfd66cfc53ea2d4ffcdaeab38b486065bbe1 Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 27 Feb 2023 17:01:44 +0100 Subject: [PATCH 2/5] forgot to add handlesuccess --- lib/widgets/scanner.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/widgets/scanner.dart b/lib/widgets/scanner.dart index f8e22d1..76ec40a 100644 --- a/lib/widgets/scanner.dart +++ b/lib/widgets/scanner.dart @@ -30,7 +30,7 @@ class MyScannerState extends State { }); } - void handleSucces(String group) { + void handleSucces(int group) { setState(() { scanning = false; showDialog( @@ -53,6 +53,7 @@ class MyScannerState extends State { Map json = stringToJSON(qrText); Settings settings = Settings.fromJson(json); saveSettings(settings); + handleSucces(settings.group); } } } catch (e) { From 074ebb1871d2ed0306542567eeb9570f9fc99002 Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 27 Feb 2023 17:09:58 +0100 Subject: [PATCH 3/5] finally instead of catch to secure if nothing is found an message is deplayed --- lib/widgets/scanner.dart | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/widgets/scanner.dart b/lib/widgets/scanner.dart index 76ec40a..b1aff7d 100644 --- a/lib/widgets/scanner.dart +++ b/lib/widgets/scanner.dart @@ -15,22 +15,26 @@ class MyScanner extends StatefulWidget { class MyScannerState extends State { bool scanning = false; - void handleError(String error) { + void handleError() { setState(() { scanning = false; showDialog( context: context, builder: (BuildContext context) { - return MissingConfigPopup( + return const MissingConfigPopup( title: 'Scanfehler', text: - 'Beim Scanen gab es folgende Meldung: $error. Bitte erneut versuchen', + 'Beim Scanen gab es wohl einen Fehler. Bitte erneut versuchen', ); }); }); } - void handleSucces(int group) { + void handleSucces(String? rawValue) { + String qrText = rawValue!; + Map json = stringToJSON(qrText); + Settings settings = Settings.fromJson(json); + saveSettings(settings); setState(() { scanning = false; showDialog( @@ -38,7 +42,7 @@ class MyScannerState extends State { builder: (BuildContext context) { return MissingConfigPopup( title: 'Konfiguration erfolgreich', - text: 'Du gehörst zu Gruppe $group', + text: 'Du gehörst zu Gruppe ${settings.group}', ); }); }); @@ -49,15 +53,11 @@ class MyScannerState extends State { final List barcodes = capture.barcodes; for (final barcode in barcodes) { if (barcode.rawValue != null) { - String qrText = barcode.rawValue!; - Map json = stringToJSON(qrText); - Settings settings = Settings.fromJson(json); - saveSettings(settings); - handleSucces(settings.group); + handleSucces(barcode.rawValue); } } - } catch (e) { - handleError(e.toString()); + } finally { + handleError(); } } From b96604103d4855c4c305d0d7c27067f45a48daca Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 27 Feb 2023 17:15:34 +0100 Subject: [PATCH 4/5] Added initating Settings from QR-Code --- lib/widgets/scanner.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/widgets/scanner.dart b/lib/widgets/scanner.dart index b1aff7d..a97e14d 100644 --- a/lib/widgets/scanner.dart +++ b/lib/widgets/scanner.dart @@ -1,8 +1,11 @@ import 'package:flutter/material.dart'; import 'package:mobile_scanner/mobile_scanner.dart'; +import 'package:provider/provider.dart'; import 'package:smoke_cess_app/models/settings.dart'; import 'package:smoke_cess_app/services/json_service.dart'; import 'package:smoke_cess_app/services/settings_service.dart'; +import '../providers/settings_provider.dart'; +import '../services/notification_service.dart'; import '../widgets/missing_config_popup.dart'; class MyScanner extends StatefulWidget { @@ -35,6 +38,9 @@ class MyScannerState extends State { Map json = stringToJSON(qrText); Settings settings = Settings.fromJson(json); saveSettings(settings); + var settingsModel = context.read(); + settingsModel.initSettings(); + NotificationService().setAllNotifications(); setState(() { scanning = false; showDialog( From 6a6b44a5557f5870ee13632dca422dd16a24a960 Mon Sep 17 00:00:00 2001 From: "k.mannweiler" <2012491@stud.hs-mannheim.de> Date: Mon, 27 Feb 2023 17:27:01 +0100 Subject: [PATCH 5/5] Moved pageservice to service folder --- lib/pages/main_page.dart | 2 +- lib/{pages => services}/pages_service.dart | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) rename lib/{pages => services}/pages_service.dart (77%) diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 5a2af86..48897d0 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -1,6 +1,6 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; -import 'package:smoke_cess_app/pages/pages_service.dart'; +import 'package:smoke_cess_app/services/pages_service.dart'; import 'package:smoke_cess_app/providers/settings_provider.dart'; import 'package:smoke_cess_app/widgets/missing_config_popup.dart'; diff --git a/lib/pages/pages_service.dart b/lib/services/pages_service.dart similarity index 77% rename from lib/pages/pages_service.dart rename to lib/services/pages_service.dart index e530550..c3b1bd8 100644 --- a/lib/pages/pages_service.dart +++ b/lib/services/pages_service.dart @@ -1,9 +1,9 @@ import 'package:flutter/material.dart'; -import 'interval_page.dart'; -import 'mood_page.dart'; -import 'relapse_page.dart'; -import 'scanner_page.dart'; -import 'sleep_page.dart'; +import '../pages/interval_page.dart'; +import '../pages/mood_page.dart'; +import '../pages/relapse_page.dart'; +import '../pages/scanner_page.dart'; +import '../pages/sleep_page.dart'; const pages = { 'Stimmung': {