Many minor improvements
This commit is contained in:
+115
-40
@@ -1,6 +1,10 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:liteauthconfig/appconfig.dart';
|
||||
import 'package:liteauthconfig/models/appconfig.dart';
|
||||
import 'package:liteauthconfig/l10n/app_localizations.dart';
|
||||
import 'package:liteauthconfig/models/device_status.dart';
|
||||
import 'package:liteauthconfig/pages/reconfigure_network.dart';
|
||||
import 'package:liteauthconfig/utils/network.dart';
|
||||
|
||||
@@ -15,9 +19,11 @@ class DeviceInfoPage extends StatefulWidget {
|
||||
|
||||
class _DeviceInfoPageState extends State<DeviceInfoPage> {
|
||||
late Device device;
|
||||
DeviceStatus? deviceStatus;
|
||||
String statusMessage = "";
|
||||
bool online = false;
|
||||
bool loading = false;
|
||||
Timer? refreshTimer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -31,24 +37,67 @@ class _DeviceInfoPageState extends State<DeviceInfoPage> {
|
||||
device = AppConfig().devices[widget.deviceIndex];
|
||||
}
|
||||
|
||||
void checkStatus() async {
|
||||
Future checkStatus() async {
|
||||
statusMessage = "";
|
||||
|
||||
setState(() {
|
||||
loading = true;
|
||||
});
|
||||
var client = await createEspHttpClient();
|
||||
|
||||
try {
|
||||
var resp = await client.get(Uri.parse("https://liteauth.local"));
|
||||
setState(() {
|
||||
online = resp.statusCode >= 200 && resp.statusCode < 300;
|
||||
});
|
||||
var resp = await client.get(
|
||||
Uri.parse("https://liteauth.local/api/status"),
|
||||
);
|
||||
deviceStatus = DeviceStatus.fromJson(jsonDecode(resp.body));
|
||||
online = resp.statusCode >= 200 && resp.statusCode < 300;
|
||||
} catch (e) {
|
||||
statusMessage = e.toString();
|
||||
online = false;
|
||||
refreshTimer ??= Timer.periodic(const Duration(seconds: 5), (
|
||||
timer,
|
||||
) async {
|
||||
await checkStatus();
|
||||
if (online) {
|
||||
timer.cancel();
|
||||
refreshTimer = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setState(() {
|
||||
loading = false;
|
||||
});
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void factoryReset() async {
|
||||
var appLocal = AppLocalizations.of(context)!;
|
||||
|
||||
var reset = await showDialog<bool>(context: context, builder: (ctx) => AlertDialog.adaptive(
|
||||
title: Text(appLocal.factoryReset),
|
||||
content: Text(appLocal.resetConfirm),
|
||||
actions: [
|
||||
TextButton(onPressed: () {
|
||||
Navigator.of(ctx).pop(false);
|
||||
}, child: Text(appLocal.cancel)),
|
||||
TextButton(onPressed: () {
|
||||
Navigator.of(ctx).pop(true);
|
||||
}, child: Text(appLocal.reset)),
|
||||
],
|
||||
)) ?? false;
|
||||
|
||||
if (!reset) return;
|
||||
|
||||
var client = await createEspHttpClient();
|
||||
client.get(Uri.parse("https://liteauth.local/api/factoryReset"));
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
refreshTimer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -76,17 +125,45 @@ class _DeviceInfoPageState extends State<DeviceInfoPage> {
|
||||
controller.open();
|
||||
}
|
||||
},
|
||||
icon: const Icon(Icons.more_vert),
|
||||
icon: Icon(Icons.adaptive.more),
|
||||
);
|
||||
},
|
||||
menuChildren: [
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.refresh),
|
||||
onPressed: checkStatus,
|
||||
child: Text(appLocal.refresh),
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.network_wifi),
|
||||
child: Text(appLocal.reconfigureNetwork),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
ReconfigureNetworkPage(widget.deviceIndex),
|
||||
),
|
||||
).then((result) {
|
||||
if (result) {
|
||||
refreshDevice();
|
||||
checkStatus();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.history),
|
||||
child: Text(appLocal.factoryReset),
|
||||
onPressed: factoryReset,
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.delete),
|
||||
child: Text(appLocal.delete),
|
||||
onPressed: () {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
builder: (context) => AlertDialog.adaptive(
|
||||
title: Text(appLocal.deletion),
|
||||
content: Text(appLocal.deviceDeleteConfirm),
|
||||
actions: [
|
||||
@@ -111,24 +188,6 @@ class _DeviceInfoPageState extends State<DeviceInfoPage> {
|
||||
);
|
||||
},
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.network_wifi),
|
||||
child: Text(appLocal.reconfigureNetwork),
|
||||
onPressed: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
ReconfigureNetworkPage(widget.deviceIndex),
|
||||
),
|
||||
).then((_) => refreshDevice());
|
||||
},
|
||||
),
|
||||
MenuItemButton(
|
||||
leadingIcon: const Icon(Icons.refresh),
|
||||
onPressed: checkStatus,
|
||||
child: Text(appLocal.refresh),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
@@ -144,18 +203,20 @@ class _DeviceInfoPageState extends State<DeviceInfoPage> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(device.name, style: appTheme.textTheme.headlineLarge),
|
||||
Row(children: [
|
||||
Text(
|
||||
appLocal.status,
|
||||
style: appTheme.textTheme.bodyLarge,
|
||||
),
|
||||
Text(
|
||||
online ? appLocal.online : appLocal.offline,
|
||||
style: appTheme.textTheme.bodyLarge?.copyWith(
|
||||
color: online ? Colors.green : Colors.red,
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
appLocal.status,
|
||||
style: appTheme.textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
],),
|
||||
Text(
|
||||
online ? appLocal.online : appLocal.offline,
|
||||
style: appTheme.textTheme.bodyLarge?.copyWith(
|
||||
color: online ? Colors.green : Colors.red,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
if (statusMessage.isNotEmpty) Text(statusMessage),
|
||||
@@ -178,6 +239,20 @@ class _DeviceInfoPageState extends State<DeviceInfoPage> {
|
||||
Text(appLocal.routerSsid(device.routerSsid)),
|
||||
],
|
||||
),
|
||||
Column(
|
||||
children: [
|
||||
Row(
|
||||
spacing: 8,
|
||||
children: [
|
||||
Icon(Icons.list),
|
||||
Text(
|
||||
appLocal.activityLogs,
|
||||
style: appTheme.textTheme.headlineMedium,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
+95
-30
@@ -1,9 +1,13 @@
|
||||
import 'dart:io' show Platform;
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
|
||||
import 'package:liteauthconfig/appconfig.dart';
|
||||
import 'package:liteauthconfig/models/appconfig.dart';
|
||||
import 'package:liteauthconfig/l10n/app_localizations.dart';
|
||||
import 'package:liteauthconfig/pages/deviceinfo.dart';
|
||||
import 'package:liteauthconfig/pages/registerpage.dart';
|
||||
import 'package:nfc_manager/nfc_manager.dart';
|
||||
import 'package:nfc_manager_ndef/nfc_manager_ndef.dart';
|
||||
|
||||
class DeviceListPage extends StatefulWidget {
|
||||
const DeviceListPage({super.key});
|
||||
@@ -14,11 +18,35 @@ class DeviceListPage extends StatefulWidget {
|
||||
|
||||
class _DeviceListPageState extends State<DeviceListPage> {
|
||||
List<Device> devices = [];
|
||||
NfcAvailability? availability;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
refreshList();
|
||||
checkNfc();
|
||||
}
|
||||
|
||||
Future checkNfc() async {
|
||||
if (!(Platform.isAndroid || Platform.isIOS)) return;
|
||||
|
||||
availability = await NfcManager.instance.checkAvailability();
|
||||
|
||||
if (availability != NfcAvailability.enabled) return;
|
||||
|
||||
NfcManager.instance.startSession(
|
||||
pollingOptions: {NfcPollingOption.iso14443},
|
||||
onDiscovered: (NfcTag tag) async {
|
||||
final Ndef? ndef = Ndef.from(tag);
|
||||
|
||||
if (ndef == null) {
|
||||
print("This tag is not compatible with NDEF.");
|
||||
return;
|
||||
}
|
||||
|
||||
print(ndef);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void refreshList() {
|
||||
@@ -34,34 +62,65 @@ class _DeviceListPageState extends State<DeviceListPage> {
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(appLocal.deviceList)),
|
||||
body: Visibility(
|
||||
visible: devices.isEmpty,
|
||||
replacement: ListView.builder(
|
||||
itemBuilder: (context, index) {
|
||||
final dev = devices[index];
|
||||
|
||||
return ListTile(
|
||||
title: Text(dev.name),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => DeviceInfoPage(deviceIndex: index),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: EdgeInsetsGeometry.all(8),
|
||||
child: switch (availability) {
|
||||
null => SizedBox(),
|
||||
NfcAvailability.enabled => Card(
|
||||
child: ListTile(
|
||||
title: Text(appLocal.nfcReady),
|
||||
subtitle: Text(appLocal.nfcReadyDescription),
|
||||
),
|
||||
).then((_) => refreshList());
|
||||
),
|
||||
NfcAvailability.disabled => Card(
|
||||
child: ListTile(
|
||||
title: Text(appLocal.nfcDisabled),
|
||||
subtitle: Text(appLocal.nfcRequired),
|
||||
),
|
||||
),
|
||||
NfcAvailability.unsupported => Card(
|
||||
child: ListTile(
|
||||
title: Text(appLocal.nfcUnsupported),
|
||||
subtitle: Text(appLocal.nfcRequired),
|
||||
),
|
||||
),
|
||||
},
|
||||
);
|
||||
},
|
||||
itemCount: devices.length,
|
||||
shrinkWrap: true,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text(
|
||||
appLocal.noDevices,
|
||||
textAlign: TextAlign.center,
|
||||
style: appTheme.textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
Visibility(
|
||||
visible: devices.isEmpty,
|
||||
replacement: ListView.builder(
|
||||
itemBuilder: (context, index) {
|
||||
final dev = devices[index];
|
||||
|
||||
return ListTile(
|
||||
title: Text(dev.name),
|
||||
onTap: () {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) =>
|
||||
DeviceInfoPage(deviceIndex: index),
|
||||
),
|
||||
).then((_) => refreshList());
|
||||
},
|
||||
);
|
||||
},
|
||||
itemCount: devices.length,
|
||||
shrinkWrap: true,
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Text(
|
||||
appLocal.noDevices,
|
||||
textAlign: TextAlign.center,
|
||||
style: appTheme.textTheme.bodyLarge,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
floatingActionButton: SpeedDial(
|
||||
@@ -77,7 +136,9 @@ class _DeviceListPageState extends State<DeviceListPage> {
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context)
|
||||
.push(MaterialPageRoute(builder: (context) => RegisterPage(true)))
|
||||
.push(
|
||||
MaterialPageRoute(builder: (context) => RegisterPage(true)),
|
||||
)
|
||||
.then((_) => refreshList());
|
||||
},
|
||||
),
|
||||
@@ -90,9 +151,13 @@ class _DeviceListPageState extends State<DeviceListPage> {
|
||||
),
|
||||
onTap: () {
|
||||
Navigator.of(context)
|
||||
.push(MaterialPageRoute(builder: (context) => RegisterPage(false)))
|
||||
.push(
|
||||
MaterialPageRoute(
|
||||
builder: (context) => RegisterPage(false),
|
||||
),
|
||||
)
|
||||
.then((_) => refreshList());
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
child: const Icon(Icons.add),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:liteauthconfig/appconfig.dart';
|
||||
import 'package:liteauthconfig/models/appconfig.dart';
|
||||
import 'package:liteauthconfig/dialogs/esptouchdialog.dart';
|
||||
import 'package:liteauthconfig/l10n/app_localizations.dart';
|
||||
import 'package:network_info_plus/network_info_plus.dart';
|
||||
@@ -115,7 +115,7 @@ class _ReconfigureNetworkPageState extends State<ReconfigureNetworkPage> {
|
||||
);
|
||||
|
||||
if (context.mounted) {
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:liteauthconfig/appconfig.dart';
|
||||
import 'package:liteauthconfig/models/appconfig.dart';
|
||||
import 'package:liteauthconfig/dialogs/esptouchdialog.dart';
|
||||
import 'package:liteauthconfig/l10n/app_localizations.dart';
|
||||
import 'package:multicast_dns/multicast_dns.dart';
|
||||
|
||||
Reference in New Issue
Block a user