diff --git a/assets/config.json b/assets/config.json deleted file mode 100644 index a80b05f..0000000 --- a/assets/config.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "group": "Gruppe 2", - "key": "value" -} diff --git a/lib/pages/main_page.dart b/lib/pages/main_page.dart index 6e479e5..76cec46 100644 --- a/lib/pages/main_page.dart +++ b/lib/pages/main_page.dart @@ -41,7 +41,10 @@ class MyHomePageState extends State { : showDialog( context: context, builder: (BuildContext context) { - return const MissingConfigPopup(); + return const MissingConfigPopup( + title: 'Fehlende Konfiguration', + text: 'Bitte QR Code Scannen!', + ); }); }); } diff --git a/lib/pages/scanner_page.dart b/lib/pages/scanner_page.dart index fc4abe0..9ce6301 100644 --- a/lib/pages/scanner_page.dart +++ b/lib/pages/scanner_page.dart @@ -1,30 +1,71 @@ 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/service/json_service.dart'; import 'package:smoke_cess_app/service/settings_service.dart'; -class ScannerPage extends StatelessWidget { +import '../widgets/missing_config_popup.dart'; + +class ScannerPage extends StatefulWidget { const ScannerPage({super.key}); + @override + State createState() => ScannerPageState(); +} + +class ScannerPageState extends State { + bool scanning = false; + @override Widget build(BuildContext context) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Expanded(child: MobileScanner(fit: BoxFit.contain,onDetect: (capture) { - final List barcodes = capture.barcodes; - for (final barcode in barcodes) { - debugPrint('Barcode found! ${barcode.rawValue}'); - } }, - - )) - , + scanning + ? Expanded( + child: MobileScanner( + fit: BoxFit.contain, + controller: MobileScannerController( + detectionTimeoutMs: 2000, + ), + onDetect: (capture) { + 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 SizedBox(height: 30), ElevatedButton( style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), onPressed: () { - loadSettings(); + loadSettingsFromLocalJSON(); }, child: const Text('Read JSON'), ) diff --git a/lib/service/json_service.dart b/lib/service/json_service.dart index ca1b26a..6d09e9f 100644 --- a/lib/service/json_service.dart +++ b/lib/service/json_service.dart @@ -10,3 +10,5 @@ Future> loadLocalConfigJSON() async { List? jsonPropertyAsList(dynamic property) => property != null ? List.from(property) : null; + +Map stringToJSON(String jsonString) => jsonDecode(jsonString); diff --git a/lib/service/settings_service.dart b/lib/service/settings_service.dart index 891f60b..f83c979 100644 --- a/lib/service/settings_service.dart +++ b/lib/service/settings_service.dart @@ -45,7 +45,7 @@ Future?> _getStringListSetting(String settingKey) => SharedPreferences.getInstance() .then((pref) => pref.getStringList(settingKey)); -Future loadSettings() async { +Future loadSettingsFromLocalJSON() async { Map configJSON = await loadLocalConfigJSON(); Settings settings = Settings.fromJson(configJSON); saveSettings(settings); diff --git a/lib/widgets/missing_config_popup.dart b/lib/widgets/missing_config_popup.dart index 43ed2c8..620774e 100644 --- a/lib/widgets/missing_config_popup.dart +++ b/lib/widgets/missing_config_popup.dart @@ -1,17 +1,20 @@ import 'package:flutter/material.dart'; class MissingConfigPopup extends StatelessWidget { - const MissingConfigPopup({super.key}); + final String title; + final String text; + const MissingConfigPopup( + {super.key, required this.title, required this.text}); @override Widget build(BuildContext context) { return AlertDialog( - title: const Text('Fehlende Konfiguration'), + title: Text(title), content: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.start, - children: const [ - Text("Bitte QR Code Scannen"), + children: [ + Text(text), ], ), ); diff --git a/pubspec.yaml b/pubspec.yaml index 7c98b0f..92964f0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -64,7 +64,6 @@ flutter: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg assets: - - config.json - group1.json - group3.json