Introduced auto refresh for API Data
This commit is contained in:
@@ -3,6 +3,7 @@ import 'package:ac_web/Api/Dtos/AgentDto.dart';
|
||||
import 'package:ac_web/Pages/AgentDetails.dart';
|
||||
import 'package:ac_web/Widgets/AgentStatusIndicator.dart';
|
||||
import 'package:ac_web/Widgets/AgentTypeIcons.dart';
|
||||
import 'package:ac_web/Widgets/AutoRefreshState.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Fetches all agents from the AgentsController and lists them with a colored
|
||||
@@ -14,7 +15,8 @@ class AgentAvailabilityColumn extends StatefulWidget{
|
||||
State<AgentAvailabilityColumn> createState() => _AgentAvailabilityColumnState();
|
||||
}
|
||||
|
||||
class _AgentAvailabilityColumnState extends State<AgentAvailabilityColumn>{
|
||||
class _AgentAvailabilityColumnState extends State<AgentAvailabilityColumn>
|
||||
with AutoRefreshState<AgentAvailabilityColumn> {
|
||||
|
||||
late Future<List<AgentDto>> mAgentsFuture;
|
||||
|
||||
@@ -24,23 +26,44 @@ class _AgentAvailabilityColumnState extends State<AgentAvailabilityColumn>{
|
||||
mAgentsFuture = AcApi.instance.agents.getAgents();
|
||||
}
|
||||
|
||||
/// Reload triggered by the user. Shows the loading indicator again because
|
||||
/// the user asked for the request and should see that it is running.
|
||||
void ReloadAgents(){
|
||||
setState(() {
|
||||
mAgentsFuture = AcApi.instance.agents.getAgents();
|
||||
});
|
||||
}
|
||||
|
||||
/// Automatic reload. The agents that are already on screen stay visible
|
||||
/// while the request runs, the future is only replaced once it succeeded.
|
||||
@override
|
||||
Future<void> RefreshData() async {
|
||||
|
||||
final List<AgentDto> agents = await AcApi.instance.agents.getAgents();
|
||||
|
||||
if (!mounted) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
mAgentsFuture = Future<List<AgentDto>>.value(agents);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return FutureBuilder<List<AgentDto>>(
|
||||
future: mAgentsFuture,
|
||||
builder: (context, snapshot) {
|
||||
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
// Not `connectionState`: a refresh puts the builder back into the
|
||||
// waiting state although the previous agents are still available, and
|
||||
// those should stay on screen instead of flashing a spinner.
|
||||
if (!snapshot.hasData && !snapshot.hasError) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
|
||||
if (snapshot.hasError) {
|
||||
if (snapshot.hasError && !snapshot.hasData) {
|
||||
return Center(child: Text('Could not load agents: ${snapshot.error}'));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user