mobile_nebula/lib/screens/siteConfig/StaticHostsScreen.dart
Caleb Jasik 2b844d27dd
Add Flutter lint (#253)
* Enable `flutter_lints` linting

* Fix unmarked deps, we aren't on web so we don't need a URL strategy

* ` dart fix --apply --code=use_super_parameters`

* `dart fix --apply --code=use_key_in_widget_constructors`

* `dart fix --apply --code=use_function_type_syntax_for_parameters`

* Ignore code-generated `lib/services/theme.dart` file

* `dart fix --apply --code=unnecessary_this`

* `dart fix --apply --code=unnecessary_null_in_if_null_operators`

* `dart fix --apply --code=unnecessary_new`

* `dart fix --apply --code=sort_child_properties_last`

* `dart fix --apply --code=sized_box_for_whitespace`

* `dart fix --apply --code=prefer_typing_uninitialized_variables`

* `dart fix --apply --code=prefer_is_empty`

* `dart fix --apply --code=prefer_interpolation_to_compose_strings`

* `dart fix --apply --code=prefer_final_fields`

* `dart fix --apply --code=prefer_const_constructors_in_immutables`

* `dart fix --apply --code=prefer_collection_literals`

* `dart fix --apply --code=no_leading_underscores_for_local_identifiers`

* `dart fix --apply --code=curly_braces_in_flow_control_structures`

* `dart fix --apply --code=avoid_function_literals_in_foreach_calls`

* `dart fix --apply --code=annotate_overrides`

* Add CI for dart linting

* `dart format lib/`

* Re-enable the `usePathUrlStrategy` call, with proper deps

https://docs.flutter.dev/ui/navigation/url-strategies#configuring-the-url-strategy
2025-03-04 11:29:23 -06:00

167 lines
4.8 KiB
Dart

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';
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});
}
class StaticHostsScreen extends StatefulWidget {
const StaticHostsScreen({super.key, required this.hostmap, required this.onSave});
final Map<String, StaticHost> hostmap;
final ValueChanged<Map<String, StaticHost>>? onSave;
@override
_StaticHostsScreenState createState() => _StaticHostsScreenState();
}
class _StaticHostsScreenState extends State<StaticHostsScreen> {
final Map<Key, _Hostmap> _hostmap = {};
bool changed = false;
@override
void initState() {
widget.hostmap.forEach((key, map) {
_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) {
map[host.nebulaIp] = StaticHost(destinations: host.destinations, lighthouse: host.lighthouse);
});
widget.onSave!(map);
}
}
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(
padding: EdgeInsets.only(right: 10),
child: Icon(
host.lighthouse ? Icons.lightbulb_outline : Icons.computer,
color: CupertinoColors.placeholderText.resolveFrom(context),
),
),
Text(host.nebulaIp),
],
),
labelWidth: ipWidth,
content: Text('${host.destinations.length} items', textAlign: TextAlign.end),
onPressed: () {
Utils.openPage(context, (context) {
return StaticHostmapScreen(
nebulaIp: host.nebulaIp,
destinations: host.destinations,
lighthouse: host.lighthouse,
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);
});
},
);
});
},
),
);
});
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);
});
},
);
});
},
),
);
}
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();
}
}