2022-11-17 21:43:16 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/gestures.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
|
2022-11-22 16:12:38 +00:00
|
|
|
import 'package:flutter_svg/svg.dart';
|
2022-11-17 21:43:16 +00:00
|
|
|
|
|
|
|
import 'package:mobile_nebula/components/SimplePage.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
class EnrollmentScreen extends StatefulWidget {
|
|
|
|
final String? code;
|
|
|
|
final StreamController? stream;
|
|
|
|
final bool allowCodeEntry;
|
|
|
|
|
|
|
|
static const routeName = '/v1/mobile-enrollment';
|
|
|
|
|
2022-11-22 16:12:38 +00:00
|
|
|
// Attempts to find an enrollment code in the provided url. If one is not found then assume the input was
|
|
|
|
// an enrollment code. Primarily to support manual dn enrollment where the user can input a code or a url.
|
|
|
|
static String parseCode(String url) {
|
|
|
|
final uri = Uri.parse(url);
|
|
|
|
if (uri.path != EnrollmentScreen.routeName) {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (uri.hasFragment) {
|
|
|
|
final qp = Uri.splitQueryString(uri.fragment);
|
|
|
|
return qp["code"] ?? "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2022-11-17 21:43:16 +00:00
|
|
|
const EnrollmentScreen({super.key, this.code, this.stream, this.allowCodeEntry = false});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_EnrollmentScreenState createState() => _EnrollmentScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _EnrollmentScreenState extends State<EnrollmentScreen> {
|
|
|
|
String? error;
|
|
|
|
var enrolled = false;
|
|
|
|
var enrollInput = TextEditingController();
|
|
|
|
String? code;
|
|
|
|
|
|
|
|
static const platform = MethodChannel('net.defined.mobileNebula/NebulaVpnService');
|
|
|
|
|
|
|
|
void initState() {
|
|
|
|
code = widget.code;
|
|
|
|
super.initState();
|
|
|
|
_enroll();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
enrollInput.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
_enroll() async {
|
|
|
|
try {
|
|
|
|
await platform.invokeMethod("dn.enroll", code);
|
|
|
|
setState(() {
|
|
|
|
enrolled = true;
|
|
|
|
if (widget.stream != null) {
|
|
|
|
// Signal a new site has been added
|
|
|
|
widget.stream!.add(null);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} on PlatformException catch (err) {
|
|
|
|
setState(() {
|
|
|
|
error = err.details ?? err.message;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final colorScheme = Theme.of(context).colorScheme;
|
|
|
|
final textTheme = Theme.of(context).textTheme;
|
|
|
|
final bodyTextStyle = textTheme.bodyLarge!.apply(color: colorScheme.onPrimary);
|
|
|
|
final contactUri = Uri.parse('mailto:support@defined.net');
|
|
|
|
|
|
|
|
Widget child;
|
|
|
|
AlignmentGeometry? alignment;
|
|
|
|
|
|
|
|
if (code == null) {
|
|
|
|
if (widget.allowCodeEntry) {
|
|
|
|
child = _codeEntry();
|
|
|
|
} else {
|
|
|
|
// No code, show the error
|
|
|
|
child = Padding(
|
2024-08-28 18:46:03 +00:00
|
|
|
child: Center(
|
|
|
|
child: Text(
|
2022-11-17 21:43:16 +00:00
|
|
|
'No valid enrollment code was found.\n\nContact your administrator to obtain a new enrollment code.',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
)),
|
2024-08-28 18:46:03 +00:00
|
|
|
padding: EdgeInsets.only(top: 20));
|
2022-11-17 21:43:16 +00:00
|
|
|
}
|
|
|
|
} else if (this.error != null) {
|
|
|
|
// Error while enrolling, display it
|
2024-08-28 18:46:03 +00:00
|
|
|
child = Center(
|
|
|
|
child: Column(
|
2022-11-17 21:43:16 +00:00
|
|
|
children: [
|
|
|
|
Padding(
|
2024-08-28 18:46:03 +00:00
|
|
|
child: SelectableText(
|
|
|
|
'There was an issue while attempting to enroll this device. Contact your administrator to obtain a new enrollment code.'),
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 20)),
|
|
|
|
Padding(
|
|
|
|
child: SelectableText.rich(TextSpan(children: [
|
|
|
|
TextSpan(text: 'If the problem persists, please let us know at '),
|
|
|
|
TextSpan(
|
|
|
|
text: 'support@defined.net',
|
|
|
|
style: bodyTextStyle.apply(color: colorScheme.primary),
|
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = () async {
|
|
|
|
if (await canLaunchUrl(contactUri)) {
|
|
|
|
print(await launchUrl(contactUri));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
TextSpan(text: ' and provide the following error:'),
|
|
|
|
])),
|
|
|
|
padding: EdgeInsets.only(bottom: 10)),
|
2022-11-17 21:43:16 +00:00
|
|
|
Container(
|
|
|
|
child: Padding(child: SelectableText(this.error!), padding: EdgeInsets.all(10)),
|
|
|
|
color: Theme.of(context).colorScheme.errorContainer,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.center,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
));
|
|
|
|
} else if (this.enrolled) {
|
|
|
|
// Enrollment complete!
|
|
|
|
child = Padding(
|
2024-08-28 18:46:03 +00:00
|
|
|
child: Center(
|
|
|
|
child: Text(
|
2022-11-17 21:43:16 +00:00
|
|
|
'Enrollment complete! 🎉',
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
)),
|
2024-08-28 18:46:03 +00:00
|
|
|
padding: EdgeInsets.only(top: 20));
|
2022-11-17 21:43:16 +00:00
|
|
|
} else {
|
|
|
|
// Have a code and actively enrolling
|
|
|
|
alignment = Alignment.center;
|
2024-08-28 18:46:03 +00:00
|
|
|
child = Center(
|
|
|
|
child: Column(children: [
|
|
|
|
Padding(child: Text('Contacting DN for enrollment'), padding: EdgeInsets.only(bottom: 25)),
|
|
|
|
PlatformCircularProgressIndicator(cupertino: (_, __) {
|
|
|
|
return CupertinoProgressIndicatorData(radius: 50);
|
|
|
|
})
|
|
|
|
]));
|
2022-11-17 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
2024-08-28 18:46:03 +00:00
|
|
|
final dnIcon =
|
|
|
|
Theme.of(context).brightness == Brightness.dark ? 'images/dn-logo-dark.svg' : 'images/dn-logo-light.svg';
|
2022-11-22 16:12:38 +00:00
|
|
|
return SimplePage(
|
2024-08-28 18:46:03 +00:00
|
|
|
title: Text('Enroll with Managed Nebula', style: TextStyle(fontWeight: FontWeight.bold)),
|
|
|
|
child: Padding(child: child, padding: EdgeInsets.symmetric(horizontal: 10)),
|
|
|
|
alignment: alignment);
|
2022-11-17 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _codeEntry() {
|
|
|
|
return Column(children: [
|
2022-11-22 16:12:38 +00:00
|
|
|
Padding(
|
2024-08-28 18:46:03 +00:00
|
|
|
padding: EdgeInsets.only(top: 20),
|
|
|
|
child: PlatformTextField(
|
|
|
|
hintText: 'defined.net enrollment code or link',
|
|
|
|
controller: enrollInput,
|
|
|
|
)),
|
2022-11-22 16:12:38 +00:00
|
|
|
PlatformTextButton(
|
|
|
|
child: Text('Submit'),
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
code = EnrollmentScreen.parseCode(enrollInput.text);
|
|
|
|
error = null;
|
|
|
|
_enroll();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
)
|
2022-11-17 21:43:16 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|