mobile_nebula/lib/screens/SiteTunnelsScreen.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

140 lines
4.2 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mobile_nebula/components/SimplePage.dart';
import 'package:mobile_nebula/components/config/ConfigPageItem.dart';
import 'package:mobile_nebula/components/config/ConfigSection.dart';
import 'package:mobile_nebula/models/HostInfo.dart';
import 'package:mobile_nebula/models/Site.dart';
import 'package:mobile_nebula/screens/HostInfoScreen.dart';
import 'package:mobile_nebula/services/utils.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
class SiteTunnelsScreen extends StatefulWidget {
const SiteTunnelsScreen({
super.key,
required this.site,
required this.tunnels,
required this.pending,
required this.onChanged,
required this.supportsQRScanning,
});
final Site site;
final List<HostInfo> tunnels;
final bool pending;
final Function(List<HostInfo>)? onChanged;
final bool supportsQRScanning;
@override
_SiteTunnelsScreenState createState() => _SiteTunnelsScreenState();
}
class _SiteTunnelsScreenState extends State<SiteTunnelsScreen> {
late Site site;
late List<HostInfo> tunnels;
RefreshController refreshController = RefreshController(initialRefresh: false);
@override
void initState() {
site = widget.site;
tunnels = widget.tunnels;
_sortTunnels();
super.initState();
}
@override
void dispose() {
refreshController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final double ipWidth = Utils.textSize("000.000.000.000", CupertinoTheme.of(context).textTheme.textStyle).width + 32;
final List<ConfigPageItem> children =
tunnels.map((hostInfo) {
final isLh = site.staticHostmap[hostInfo.vpnIp]?.lighthouse ?? false;
final icon = switch (isLh) {
true => Icon(Icons.lightbulb_outline, color: CupertinoColors.placeholderText.resolveFrom(context)),
false => Icon(Icons.computer, color: CupertinoColors.placeholderText.resolveFrom(context)),
};
return (ConfigPageItem(
onPressed:
() => Utils.openPage(
context,
(context) => HostInfoScreen(
isLighthouse: isLh,
hostInfo: hostInfo,
pending: widget.pending,
site: widget.site,
onChanged: () {
_listHostmap();
},
supportsQRScanning: widget.supportsQRScanning,
),
),
label: Row(
children: <Widget>[Padding(padding: EdgeInsets.only(right: 10), child: icon), Text(hostInfo.vpnIp)],
),
labelWidth: ipWidth,
content: Container(alignment: Alignment.centerRight, child: Text(hostInfo.cert?.details.name ?? "")),
));
}).toList();
final Widget child = switch (children.length) {
0 => Center(child: Padding(padding: EdgeInsets.only(top: 30), child: Text('No tunnels to show'))),
_ => ConfigSection(children: children),
};
final title = widget.pending ? 'Pending' : 'Active';
return SimplePage(
title: Text('$title Tunnels'),
refreshController: refreshController,
onRefresh: () async {
await _listHostmap();
refreshController.refreshCompleted();
},
child: child,
);
}
_sortTunnels() {
tunnels.sort((a, b) {
final aLh = _isLighthouse(a.vpnIp), bLh = _isLighthouse(b.vpnIp);
if (aLh && !bLh) {
return -1;
} else if (!aLh && bLh) {
return 1;
}
return Utils.ip2int(a.vpnIp) - Utils.ip2int(b.vpnIp);
});
}
bool _isLighthouse(String vpnIp) {
return site.staticHostmap[vpnIp]?.lighthouse ?? false;
}
_listHostmap() async {
try {
if (widget.pending) {
tunnels = await site.listPendingHostmap();
} else {
tunnels = await site.listHostmap();
}
_sortTunnels();
if (widget.onChanged != null) {
widget.onChanged!(tunnels);
}
setState(() {});
} catch (err) {
Utils.popError(context, 'Error while fetching hostmap', err.toString());
}
}
}