Skip to content
Snippets Groups Projects
Commit d811760c authored by Leon Tappe's avatar Leon Tappe :fire:
Browse files

refactor printer details and add delete button

parent 126c046f
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:http/http.dart' as http;
import '/util.dart';
import '/widgets/cool_card.dart';
import 'toner_indicator.dart';
......@@ -38,7 +39,10 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Details zum Drucker ${widget.printer.hostname}')),
appBar: AppBar(
title: Text('Details zum Drucker ${widget.printer.hostname}'),
actions: [IconButton(onPressed: _onDelete, icon: Icon(Icons.delete_forever_rounded))],
),
body: Center(
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: 1024.0),
......@@ -113,19 +117,7 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
width: 256.0,
child: TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (input) {
final parts = input.split('.');
if (parts.length != 4) {
return 'Eine IP muss im Format aaa.bbb.ccc.ddd vorliegen';
}
if (parts.map((e) => int.tryParse(e)).contains(null)) {
return 'Eine IP muss aus natürlichen Zahlen bestehen';
}
return null;
},
validator: validateIp,
textAlign: TextAlign.end,
initialValue: _printer.ip ?? '',
decoration: InputDecoration(border: InputBorder.none),
......@@ -208,71 +200,6 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
),
),
),
/* CheckboxListTile(
title: Text('Toner überwachen'),
value: _printer.watchToner,
onChanged: (bool input) {
setState(() {
_dirty = true;
_printer.watchToner = input;
});
_onSaveWatchers();
},
),
CheckboxListTile(
title: Text('Papierfach 1 überwachen'),
value: _printer.watchTray1,
onChanged: (bool input) {
setState(() {
_dirty = true;
_printer.watchTray1 = input;
});
_onSaveWatchers();
},
),
CheckboxListTile(
title: Text('Papierfach 2 überwachen'),
value: _printer.watchTray2,
onChanged: (bool input) {
setState(() {
_dirty = true;
_printer.watchTray2 = input;
});
_onSaveWatchers();
},
),
CheckboxListTile(
title: Text('Papierfach 3 überwachen'),
value: _printer.watchTray3,
onChanged: (bool input) {
setState(() {
_dirty = true;
_printer.watchTray3 = input;
});
_onSaveWatchers();
},
),
ListTile(
title: Text('Überwachungsintervall'),
trailing: SizedBox(
width: 256.0,
child: TextFormField(
validator: (input) =>
int.tryParse(input) == null ? 'Nur Zahlen erlaubt' : null,
autovalidateMode: AutovalidateMode.onUserInteraction,
textAlign: TextAlign.end,
initialValue: _printer.watchInterval.toString() ?? '',
decoration:
InputDecoration(border: InputBorder.none, suffixText: 'Sekunden'),
onChanged: (String input) => setState(() {
_dirty = true;
_printer.watchInterval =
int.tryParse(input) ?? _printer.watchInterval;
}),
onFieldSubmitted: (_) => _onSaveWatcherInterval(),
),
),
), */
],
),
CoolCard(
......@@ -356,19 +283,19 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
ListTile(
title: Text('Papierfach 1'),
trailing: Text(
_printer.status.tray1.toString(),
(_printer.status.tray1 ?? 0).toString(),
style: Theme.of(context).textTheme.subtitle1,
)),
ListTile(
title: Text('Papierfach 2'),
trailing: Text(
_printer.status.tray2.toString(),
(_printer.status.tray2 ?? 0).toString(),
style: Theme.of(context).textTheme.subtitle1,
)),
ListTile(
title: Text('Papierfach 3'),
trailing: Text(
_printer.status.tray3.toString(),
(_printer.status.tray3 ?? 0).toString(),
style: Theme.of(context).textTheme.subtitle1,
)),
ListTile(
......@@ -388,9 +315,10 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
color: Colors.pink,
)),
ListTile(
title: Text('Gelb'),
trailing: TonerIndicator(
value: _printer.status.tonerY, color: Colors.yellow)),
title: Text('Gelb'),
trailing: TonerIndicator(
value: _printer.status.tonerY, color: Colors.yellow),
),
ListTile(
title: Text('Schwarz'),
trailing: TonerIndicator(
......@@ -450,9 +378,14 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
printersBloc = BlocProvider.of<PrintersBloc>(context);
printQueueBloc = PrintQueueBloc(http.Client());
_printerListener = printersBloc.stream.listen((PrintersState state) {
_printerListener = printersBloc.stream.where((e) => e.isResult).listen((PrintersState state) {
final results = state.value.where((Printer e) => e.deviceId == widget.printer.deviceId);
if (results.isEmpty) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Der Drucker konnte nicht gefunden werden')));
}
setState(() {
_printer = state.value.singleWhere((Printer e) => e.deviceId == widget.printer.deviceId);
_printer = results.first;
});
});
......@@ -472,6 +405,32 @@ class _PrinterDetailsPageState extends State<PrinterDetailsPage> {
printQueueBloc.onRefresh();
}
Future<void> _onDelete() async {
final result = await showDialog<bool>(
context: context,
builder: (BuildContext context) => AlertDialog(
title: Text('Wirklich löschen?'),
content: Text(
'Der Drucker ${_printer.deviceId} kann danach nicht wiederhergestellt werden.'),
actions: [
MaterialButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text('Löschen'),
),
ElevatedButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text('Abbrechen'),
)
],
));
if (result == null) return;
printersBloc.onDeletePrinter(_printer.deviceId);
Navigator.of(context).pop();
}
void _onEmptyQueue() {
printQueueBloc.onCancelJobs();
}
......
......@@ -69,5 +69,19 @@ String validateEmail(String value) {
return 'Bitte gebe eine richtige E-Mail Adresse ein';
}
String validateIp(String input) {
final parts = input.split('.');
if (parts.length != 4) {
return 'Eine IP muss im Format aaa.bbb.ccc.ddd vorliegen';
}
if (parts.map((e) => int.tryParse(e)).contains(null)) {
return 'Eine IP muss aus natürlichen Zahlen bestehen';
}
return null;
}
String validatePassword(String input) =>
input.length > 5 ? null : 'Das Passwort muss aus mindestens 6 Zeichen bestehen';
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment