Merge branch 'refactroring-ScannerPage' into 32-success-or-failure-alert-after-save
commit
f0e3a1ff6b
|
@ -1,7 +1,7 @@
|
||||||
import 'package:awesome_dialog/awesome_dialog.dart';
|
import 'package:awesome_dialog/awesome_dialog.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.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/providers/settings_provider.dart';
|
||||||
|
|
||||||
class MyHomePage extends StatefulWidget {
|
class MyHomePage extends StatefulWidget {
|
||||||
|
|
|
@ -1,98 +1,21 @@
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
class ScannerPageState extends State<ScannerPage> {
|
|
||||||
bool scanning = false;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
var settings = context.watch<SettingsProvider>();
|
|
||||||
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<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),
|
|
||||||
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<Mood> moods = await globals.databaseService.getMoodRecords();
|
List<Mood> moods = await globals.databaseService.getMoodRecords();
|
||||||
List<Sleep> sleeps =
|
List<Sleep> sleeps = await globals.databaseService.getSleepRecords();
|
||||||
await globals.databaseService.getSleepRecords();
|
List<Relapse> relapses = await globals.databaseService.getRelapseRecords();
|
||||||
List<Relapse> relapses =
|
|
||||||
await globals.databaseService.getRelapseRecords();
|
|
||||||
for (Mood mood in moods) {
|
for (Mood mood in moods) {
|
||||||
print(mood.toCSV());
|
print(mood.toCSV());
|
||||||
}
|
}
|
||||||
|
@ -102,7 +25,34 @@ class ScannerPageState extends State<ScannerPage> {
|
||||||
for (Relapse relapse in relapses) {
|
for (Relapse relapse in relapses) {
|
||||||
print(relapse.toCSV());
|
print(relapse.toCSV());
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
|
void loadJSON(BuildContext context) async {
|
||||||
|
var settingsModel = context.read<SettingsProvider>();
|
||||||
|
await loadSettingsFromLocalJSON();
|
||||||
|
settingsModel.initSettings();
|
||||||
|
NotificationService().setAllNotifications();
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Center(
|
||||||
|
child: Column(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
children: [
|
||||||
|
const MyScanner(),
|
||||||
|
const SizedBox(height: 30),
|
||||||
|
ElevatedButton(
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
textStyle: const TextStyle(fontSize: 20)),
|
||||||
|
onPressed: () => loadJSON(context),
|
||||||
|
child: const Text('Read JSON'),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 30),
|
||||||
|
ElevatedButton(
|
||||||
|
style: ElevatedButton.styleFrom(
|
||||||
|
textStyle: const TextStyle(fontSize: 20)),
|
||||||
|
onPressed: export,
|
||||||
child: const Text('Export'),
|
child: const Text('Export'),
|
||||||
)
|
)
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'interval_page.dart';
|
import '../pages/interval_page.dart';
|
||||||
import 'mood_page.dart';
|
import '../pages/mood_page.dart';
|
||||||
import 'relapse_page.dart';
|
import '../pages/relapse_page.dart';
|
||||||
import 'scanner_page.dart';
|
import '../pages/scanner_page.dart';
|
||||||
import 'sleep_page.dart';
|
import '../pages/sleep_page.dart';
|
||||||
|
|
||||||
const pages = {
|
const pages = {
|
||||||
'Stimmung': {
|
'Stimmung': {
|
|
@ -0,0 +1,89 @@
|
||||||
|
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 {
|
||||||
|
const MyScanner({super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
State<StatefulWidget> createState() => MyScannerState();
|
||||||
|
}
|
||||||
|
|
||||||
|
class MyScannerState extends State<MyScanner> {
|
||||||
|
bool scanning = false;
|
||||||
|
|
||||||
|
void handleError() {
|
||||||
|
setState(() {
|
||||||
|
scanning = false;
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return const MissingConfigPopup(
|
||||||
|
title: 'Scanfehler',
|
||||||
|
text:
|
||||||
|
'Beim Scanen gab es wohl einen Fehler. Bitte erneut versuchen',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void handleSucces(String? rawValue) {
|
||||||
|
String qrText = rawValue!;
|
||||||
|
Map<String, dynamic> json = stringToJSON(qrText);
|
||||||
|
Settings settings = Settings.fromJson(json);
|
||||||
|
saveSettings(settings);
|
||||||
|
var settingsModel = context.read<SettingsProvider>();
|
||||||
|
settingsModel.initSettings();
|
||||||
|
NotificationService().setAllNotifications();
|
||||||
|
setState(() {
|
||||||
|
scanning = false;
|
||||||
|
showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return MissingConfigPopup(
|
||||||
|
title: 'Konfiguration erfolgreich',
|
||||||
|
text: 'Du gehörst zu Gruppe ${settings.group}',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
void onDetect(capture) {
|
||||||
|
try {
|
||||||
|
final List<Barcode> barcodes = capture.barcodes;
|
||||||
|
for (final barcode in barcodes) {
|
||||||
|
if (barcode.rawValue != null) {
|
||||||
|
handleSucces(barcode.rawValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
handleError();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@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'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue