cpd_2022_zi/lib/pages/scanner_page.dart

100 lines
3.5 KiB
Dart
Raw Normal View History

2023-02-17 13:47:52 +01:00
import 'package:flutter/material.dart';
2023-02-20 16:23:49 +01:00
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:smoke_cess_app/models/mood.dart';
2023-02-20 19:32:23 +01:00
import 'package:smoke_cess_app/models/settings.dart';
2023-02-26 14:10:06 +01:00
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';
2023-02-17 13:47:52 +01:00
import '../models/sleep.dart';
2023-02-20 19:32:23 +01:00
import '../widgets/missing_config_popup.dart';
import '../globals.dart' as globals;
2023-02-20 19:32:23 +01:00
class ScannerPage extends StatefulWidget {
2023-02-19 17:54:30 +01:00
const ScannerPage({super.key});
2023-02-17 13:47:52 +01:00
2023-02-20 19:32:23 +01:00
@override
State<StatefulWidget> createState() => ScannerPageState();
}
class ScannerPageState extends State<ScannerPage> {
bool scanning = false;
2023-02-17 13:47:52 +01:00
@override
Widget build(BuildContext context) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
2023-02-20 19:32:23 +01:00
scanning
? Expanded(
child: MobileScanner(
fit: BoxFit.contain,
controller: MobileScannerController(
detectionTimeoutMs: 2000,
),
onDetect: (capture) {
//TODO Errorhandling!!
2023-02-20 19:32:23 +01:00
final List<Barcode> barcodes = capture.barcodes;
for (final barcode in barcodes) {
if (barcode.rawValue != null) {
String qrText = barcode.rawValue!;
Map<String, dynamic> 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'),
),
2023-02-17 13:47:52 +01:00
const SizedBox(height: 30),
ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () {
2023-02-20 19:32:23 +01:00
loadSettingsFromLocalJSON();
2023-02-24 20:30:28 +01:00
NotificationService().setAllNotifications();
2023-02-17 13:47:52 +01:00
},
child: const Text('Read JSON'),
),
const SizedBox(height: 30),
ElevatedButton(
style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)),
onPressed: () async {
List<Mood> moods = await globals.databaseService.getMoodRecords();
List<Sleep> sleeps =
await globals.databaseService.getSleepRecords();
for (Mood mood in moods) {
print(mood.toCSV());
}
for (Sleep sleep in sleeps) {
print(sleep.toCSV());
}
},
child: const Text('Export'),
2023-02-17 13:47:52 +01:00
)
],
));
}
}