Introduced auto refresh for API Data

This commit is contained in:
2026-09-01 14:28:35 +02:00
parent 3ddb9fa066
commit 435698086d
6 changed files with 348 additions and 14 deletions
+23 -7
View File
@@ -4,6 +4,7 @@ import 'package:ac_web/Api/Dtos/AgentTaskDto.dart';
import 'package:ac_web/Widgets/AgentStatusIndicator.dart';
import 'package:ac_web/Widgets/AgentTaskCard.dart';
import 'package:ac_web/Widgets/AgentTypeIcons.dart';
import 'package:ac_web/Widgets/AutoRefreshState.dart';
import 'package:flutter/material.dart';
/// Page that shows every detail of a single agent, including all of its tasks
@@ -18,7 +19,8 @@ class AgentDetails extends StatefulWidget {
State<AgentDetails> createState() => _AgentDetailsState();
}
class _AgentDetailsState extends State<AgentDetails> {
class _AgentDetailsState extends State<AgentDetails>
with AutoRefreshState<AgentDetails> {
late AgentDto mAgent;
@@ -30,17 +32,29 @@ class _AgentDetailsState extends State<AgentDetails> {
mAgent = widget.agent;
}
/// Reload triggered by the user through the app bar or by pulling the page
/// down. Reports what happened, even when nothing changed.
Future<void> ReloadAgent() => _reloadAgent(silent: false);
/// Automatic reload. It runs without the user asking for it, so it neither
/// disables the reload button nor pops up a message - the page simply keeps
/// showing the agent it already has.
@override
Future<void> RefreshData() => _reloadAgent(silent: true);
/// The api only offers the whole list of agents, so the agent this page
/// shows is looked up by its name again.
Future<void> ReloadAgent() async {
Future<void> _reloadAgent({required bool silent}) async {
if (mIsReloading) {
return;
}
setState(() {
mIsReloading = true;
});
mIsReloading = true;
if (!silent) {
setState(() {});
}
try {
final List<AgentDto> agents = await AcApi.instance.agents.getAgents();
@@ -60,7 +74,7 @@ class _AgentDetailsState extends State<AgentDetails> {
}
});
if (updated == null) {
if (updated == null && !silent) {
_showMessage("This agent is no longer reported by the api.");
}
} catch (error) {
@@ -73,7 +87,9 @@ class _AgentDetailsState extends State<AgentDetails> {
mIsReloading = false;
});
_showMessage("Could not reload the agent: $error");
if (!silent) {
_showMessage("Could not reload the agent: $error");
}
}
}