103 lines
2.3 KiB
Dart
103 lines
2.3 KiB
Dart
import 'package:esptouch_flutter/esptouch_flutter.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:liteauthconfig/l10n/app_localizations.dart';
|
|
|
|
import '../models/appconfig.dart';
|
|
|
|
class ESPTouchDialog extends StatefulWidget {
|
|
final String name;
|
|
final String ssid;
|
|
final String bssid;
|
|
final String password;
|
|
final int? entryIndex;
|
|
final bool addNewEntry;
|
|
|
|
const ESPTouchDialog({
|
|
super.key,
|
|
required this.name,
|
|
required this.ssid,
|
|
required this.bssid,
|
|
required this.password,
|
|
this.addNewEntry = false,
|
|
this.entryIndex
|
|
});
|
|
|
|
@override
|
|
State<StatefulWidget> createState() => _ESPTouchDialog();
|
|
}
|
|
|
|
class _ESPTouchDialog extends State<ESPTouchDialog> {
|
|
@override
|
|
void initState() {
|
|
execute();
|
|
super.initState();
|
|
}
|
|
|
|
void execute() {
|
|
final task = ESPTouchTask(
|
|
ssid: widget.ssid,
|
|
bssid: widget.bssid,
|
|
password: widget.password,
|
|
packet: ESPTouchPacket.multicast
|
|
);
|
|
|
|
final touchStream = task.execute();
|
|
final sub = touchStream.listen((data) async {
|
|
var conf = AppConfig();
|
|
|
|
final dev = Device(
|
|
name: widget.name,
|
|
routerSsid: widget.ssid,
|
|
routerBssid: widget.bssid,
|
|
networkPassword: widget.password,
|
|
);
|
|
|
|
if (widget.entryIndex != null) {
|
|
final oldDev = conf.devices[widget.entryIndex!];
|
|
dev.token = oldDev.token;
|
|
}
|
|
|
|
dev.ip = data.ip;
|
|
dev.bssid = data.bssid;
|
|
|
|
if (widget.addNewEntry) {
|
|
conf.devices.add(dev);
|
|
} else {
|
|
conf.devices[widget.entryIndex!] = dev;
|
|
}
|
|
await conf.save();
|
|
|
|
if (context.mounted) {
|
|
// ignore: use_build_context_synchronously
|
|
Navigator.pop(context);
|
|
}
|
|
});
|
|
Future.delayed(Duration(minutes: 1), () {
|
|
sub.cancel();
|
|
|
|
if (context.mounted) {
|
|
// ignore: use_build_context_synchronously
|
|
Navigator.pop(context);
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final appLocal = AppLocalizations.of(context)!;
|
|
|
|
return AlertDialog.adaptive(
|
|
title: Text(appLocal.networkInitHeadline),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
spacing: 16,
|
|
children: [
|
|
Text(appLocal.networkInitDetails),
|
|
LinearProgressIndicator(),
|
|
],
|
|
),
|
|
actions: [],
|
|
);
|
|
}
|
|
}
|