Refactoring ScannerPage

main
Kai Mannweiler 2023-02-27 17:00:28 +01:00
parent 7a402e464c
commit d401882671
2 changed files with 107 additions and 75 deletions

View File

@ -1,108 +1,58 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:mobile_scanner/mobile_scanner.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
import 'package:smoke_cess_app/models/mood.dart'; import 'package:smoke_cess_app/models/mood.dart';
import 'package:smoke_cess_app/models/relapse.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/settings_service.dart';
import 'package:smoke_cess_app/services/notification_service.dart'; import 'package:smoke_cess_app/services/notification_service.dart';
import 'package:smoke_cess_app/widgets/scanner.dart';
import '../models/sleep.dart'; import '../models/sleep.dart';
import '../providers/settings_provider.dart'; import '../providers/settings_provider.dart';
import '../widgets/missing_config_popup.dart';
import '../globals.dart' as globals; import '../globals.dart' as globals;
class ScannerPage extends StatefulWidget { class ScannerPage extends StatelessWidget {
const ScannerPage({super.key}); const ScannerPage({super.key});
@override void export() async {
State<StatefulWidget> createState() => ScannerPageState(); List<Mood> moods = await globals.databaseService.getMoodRecords();
} List<Sleep> sleeps = await globals.databaseService.getSleepRecords();
List<Relapse> 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<ScannerPage> { void loadJSON(BuildContext context) async {
bool scanning = false; var settingsModel = context.read<SettingsProvider>();
await loadSettingsFromLocalJSON();
settingsModel.initSettings();
NotificationService().setAllNotifications();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
var settings = context.watch<SettingsProvider>();
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
scanning const MyScanner(),
? Expanded(
child: MobileScanner(
fit: BoxFit.contain,
controller: MobileScannerController(
detectionTimeoutMs: 2000,
),
onDetect: (capture) {
//TODO Errorhandling!!
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'),
),
const SizedBox(height: 30), const SizedBox(height: 30),
ElevatedButton( ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)), textStyle: const TextStyle(fontSize: 20)),
onPressed: () { onPressed: () => loadJSON(context),
loadSettingsFromLocalJSON();
Future.delayed(Duration(milliseconds: 100), () {
settings.initSettings();
NotificationService().setAllNotifications();
});
},
child: const Text('Read JSON'), child: const Text('Read JSON'),
), ),
const SizedBox(height: 30), const SizedBox(height: 30),
ElevatedButton( ElevatedButton(
style: ElevatedButton.styleFrom( style: ElevatedButton.styleFrom(
textStyle: const TextStyle(fontSize: 20)), textStyle: const TextStyle(fontSize: 20)),
onPressed: () async { onPressed: export,
List<Mood> moods = await globals.databaseService.getMoodRecords();
List<Sleep> sleeps =
await globals.databaseService.getSleepRecords();
List<Relapse> 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'), child: const Text('Export'),
) )
], ],

View File

@ -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<StatefulWidget> createState() => MyScannerState();
}
class MyScannerState extends State<MyScanner> {
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<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);
}
}
} 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'),
);
}
}