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 '../models/sleep.dart'; import '../providers/settings_provider.dart'; import '../widgets/missing_config_popup.dart'; import '../globals.dart' as globals; class ScannerPage extends StatefulWidget { const ScannerPage({super.key}); @override State createState() => ScannerPageState(); } class ScannerPageState extends State { bool scanning = false; @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 SizedBox(height: 30), ElevatedButton( style: ElevatedButton.styleFrom( textStyle: const TextStyle(fontSize: 20)), onPressed: () { loadSettingsFromLocalJSON(); Future.delayed(Duration(milliseconds: 100), () { settings.initSettings(); NotificationService().setAllNotifications(); }); }, 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()); } }, child: const Text('Export'), ) ], )); } }