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,
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user