import 'dart:convert'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/widgets.dart'; import 'package:mobile_nebula/components/FormPage.dart'; import 'package:mobile_nebula/components/PlatformTextFormField.dart'; import 'package:mobile_nebula/components/SpecialSelectableText.dart'; import 'package:mobile_nebula/components/config/ConfigPageItem.dart'; import 'package:mobile_nebula/components/config/ConfigItem.dart'; import 'package:mobile_nebula/components/config/ConfigSection.dart'; import 'package:mobile_nebula/models/Site.dart'; import 'package:mobile_nebula/screens/siteConfig/AddCertificateScreen.dart'; import 'package:mobile_nebula/screens/siteConfig/AdvancedScreen.dart'; import 'package:mobile_nebula/screens/siteConfig/CAListScreen.dart'; import 'package:mobile_nebula/screens/siteConfig/CertificatesScreen.dart'; import 'package:mobile_nebula/screens/siteConfig/StaticHostsScreen.dart'; import 'package:mobile_nebula/services/utils.dart'; //TODO: Add a config test mechanism class SiteConfigScreen extends StatefulWidget { const SiteConfigScreen({Key key, this.site, this.onSave}) : super(key: key); final Site site; // This is called after the target OS has saved the configuration final ValueChanged onSave; @override _SiteConfigScreenState createState() => _SiteConfigScreenState(); } class _SiteConfigScreenState extends State { bool changed = false; bool newSite = false; bool debug = false; Site site; final nameController = TextEditingController(); @override void initState() { if (widget.site == null) { newSite = true; site = Site(); } else { site = widget.site; nameController.text = site.name; } super.initState(); } @override Widget build(BuildContext context) { return FormPage( title: newSite ? 'New Site' : 'Edit Site', changed: changed, onSave: () async { site.name = nameController.text; try { await site.save(); } catch (error) { return Utils.popError(context, 'Failed to save the site configuration', error.toString()); } Navigator.pop(context); if (widget.onSave != null) { widget.onSave(site); } }, child: Column( children: [ _main(), _keys(), _hosts(), _advanced(), kDebugMode ? _debugConfig() : Container(height: 0), ], )); } Widget _debugConfig() { var data = ""; try { final encoder = new JsonEncoder.withIndent(' '); data = encoder.convert(site); } catch (err) { data = err.toString(); } return ConfigSection(label: 'DEBUG', children: [ConfigItem(labelWidth: 0, content: SpecialSelectableText(data))]); } Widget _main() { return ConfigSection(children: [ ConfigItem( label: Text("Name"), content: PlatformTextFormField( placeholder: 'Required', controller: nameController, )) ]); } Widget _keys() { final certError = site.primaryCertInfo == null || !site.primaryCertInfo.validity.valid; var caError = site.caInfos.length == 0; if (!caError) { site.caInfos.forEach((ca) { if (!ca.validity.valid) { caError = true; } }); } return ConfigSection( label: "IDENTITY", children: [ ConfigPageItem( label: Text('Certificate'), content: Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: [ certError ? Padding( child: Icon(Icons.error, color: CupertinoColors.systemRed.resolveFrom(context), size: 20), padding: EdgeInsets.only(right: 5)) : Container(), certError ? Text('Needs attention') : Text(site.primaryCertInfo.cert.details.name) ]), onPressed: () { Utils.openPage(context, (context) { if (site.certInfos.length > 0) { return CertificatesScreen( site: site, onSave: (newSite) { setState(() { changed = true; site = site; }); }); } return AddCertificateScreen(onSave: (certInfo) { //TODO: onSave we may want to route them to the certificates screen somehow changed = true; certInfo.primary = true; site.primaryCertInfo = certInfo; site.certInfos.add(certInfo); setState(() {}); }); }); }, ), ConfigPageItem( label: Text("CA"), content: Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: [ caError ? Padding( child: Icon(Icons.error, color: CupertinoColors.systemRed.resolveFrom(context), size: 20), padding: EdgeInsets.only(right: 5)) : Container(), caError ? Text('Needs attention') : Text(Utils.itemCountFormat(site.caInfos.length)) ]), onPressed: () { Utils.openPage(context, (context) { return CAListScreen( cas: site.caInfos, onSave: (ca) { setState(() { changed = true; site.caInfos = ca; }); }); }); }) ], ); } Widget _hosts() { return ConfigSection( label: "Set up static hosts and lighthouses", children: [ ConfigPageItem( label: Text('Hosts'), content: Wrap(alignment: WrapAlignment.end, crossAxisAlignment: WrapCrossAlignment.center, children: [ site.staticHostmap.length == 0 ? Padding( child: Icon(Icons.error, color: CupertinoColors.systemRed.resolveFrom(context), size: 20), padding: EdgeInsets.only(right: 5)) : Container(), site.staticHostmap.length == 0 ? Text('Needs attention') : Text(Utils.itemCountFormat(site.staticHostmap.length)) ]), onPressed: () { Utils.openPage(context, (context) { return StaticHostsScreen( hostmap: site.staticHostmap, onSave: (map) { setState(() { changed = true; site.staticHostmap = map; }); }); }); }, ), ], ); } Widget _advanced() { return ConfigSection( children: [ ConfigPageItem( label: Text('Advanced'), onPressed: () { Utils.openPage(context, (context) { return AdvancedScreen( site: site, onSave: (settings) { setState(() { changed = true; site.cipher = settings.cipher; site.lhDuration = settings.lhDuration; site.port = settings.port; site.logVerbosity = settings.verbosity; site.unsafeRoutes = settings.unsafeRoutes; site.mtu = settings.mtu; }); }); }); }) ], ); } }