mobile_nebula/lib/screens/siteConfig/StaticHostsScreen.dart

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

156 lines
4.6 KiB
Dart
Raw Normal View History

2020-07-27 20:43:58 +00:00
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobile_nebula/components/FormPage.dart';
import 'package:mobile_nebula/components/config/ConfigButtonItem.dart';
import 'package:mobile_nebula/components/config/ConfigPageItem.dart';
import 'package:mobile_nebula/components/config/ConfigSection.dart';
import 'package:mobile_nebula/models/Hostmap.dart';
import 'package:mobile_nebula/models/IPAndPort.dart';
import 'package:mobile_nebula/models/StaticHosts.dart';
import 'package:mobile_nebula/screens/siteConfig/StaticHostmapScreen.dart';
2020-07-27 20:43:58 +00:00
import 'package:mobile_nebula/services/utils.dart';
//TODO: wire up the focus nodes, add a done/next/prev to the keyboard
class _Hostmap {
final FocusNode focusNode;
String nebulaIp;
List<IPAndPort> destinations;
bool lighthouse;
_Hostmap({
required this.focusNode,
required this.nebulaIp,
required this.destinations,
required this.lighthouse,
});
2020-07-27 20:43:58 +00:00
}
class StaticHostsScreen extends StatefulWidget {
const StaticHostsScreen({
Key? key,
required this.hostmap,
required this.onSave,
}) : super(key: key);
2020-07-27 20:43:58 +00:00
final Map<String, StaticHost> hostmap;
final ValueChanged<Map<String, StaticHost>>? onSave;
2020-07-27 20:43:58 +00:00
@override
_StaticHostsScreenState createState() => _StaticHostsScreenState();
}
class _StaticHostsScreenState extends State<StaticHostsScreen> {
Map<Key, _Hostmap> _hostmap = {};
bool changed = false;
@override
void initState() {
widget.hostmap.forEach((key, map) {
2020-07-27 20:43:58 +00:00
_hostmap[UniqueKey()] =
_Hostmap(focusNode: FocusNode(), nebulaIp: key, destinations: map.destinations, lighthouse: map.lighthouse);
});
super.initState();
}
@override
Widget build(BuildContext context) {
return FormPage(
title: 'Static Hosts',
changed: changed,
onSave: _onSave,
child: ConfigSection(
children: _buildHosts(),
));
}
_onSave() {
Navigator.pop(context);
if (widget.onSave != null) {
Map<String, StaticHost> map = {};
_hostmap.forEach((_, host) {
2024-08-28 18:46:03 +00:00
map[host.nebulaIp] = StaticHost(destinations: host.destinations, lighthouse: host.lighthouse);
});
widget.onSave!(map);
}
2020-07-27 20:43:58 +00:00
}
List<Widget> _buildHosts() {
final double ipWidth = Utils.textSize("000.000.000.000", CupertinoTheme.of(context).textTheme.textStyle).width + 32;
List<Widget> items = [];
_hostmap.forEach((key, host) {
items.add(ConfigPageItem(
label: Row(children: <Widget>[
Padding(
child: Icon(host.lighthouse ? Icons.lightbulb_outline : Icons.computer,
color: CupertinoColors.placeholderText.resolveFrom(context)),
padding: EdgeInsets.only(right: 10)),
Text(host.nebulaIp),
]),
labelWidth: ipWidth,
content: Text(host.destinations.length.toString() + ' items', textAlign: TextAlign.end),
onPressed: () {
Utils.openPage(context, (context) {
return StaticHostmapScreen(
nebulaIp: host.nebulaIp,
destinations: host.destinations,
lighthouse: host.lighthouse,
2024-08-28 18:46:03 +00:00
onSave: widget.onSave == null
? null
: (map) {
setState(() {
changed = true;
host.nebulaIp = map.nebulaIp;
host.destinations = map.destinations;
host.lighthouse = map.lighthouse;
});
},
onDelete: widget.onSave == null
? null
: () {
setState(() {
changed = true;
_hostmap.remove(key);
});
});
2020-07-27 20:43:58 +00:00
});
},
));
});
if (widget.onSave != null) {
items.add(ConfigButtonItem(
content: Text('Add a new entry'),
onPressed: () {
Utils.openPage(context, (context) {
return StaticHostmapScreen(onSave: (map) {
setState(() {
changed = true;
_addHostmap(map);
});
2020-07-27 20:43:58 +00:00
});
});
},
));
}
2020-07-27 20:43:58 +00:00
return items;
}
_addHostmap(Hostmap map) {
_hostmap[UniqueKey()] = (_Hostmap(
focusNode: FocusNode(), nebulaIp: map.nebulaIp, destinations: map.destinations, lighthouse: map.lighthouse));
}
@override
void dispose() {
_hostmap.forEach((key, host) {
host.focusNode.dispose();
});
super.dispose();
}
}