import 'dart:async'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../settings_page_body.dart'; import 'health_controller.dart'; import 'health_models.dart'; import 'health_sections.dart'; import 'health_header.dart'; import 'health_widgets.dart'; class HealthScreen extends StatefulWidget { const HealthScreen({super.key, required this.gateway}); final ApiGateway gateway; @override State createState() => _HealthScreenState(); } class _HealthScreenState extends State with WidgetsBindingObserver { late HealthController _controller; Timer? _timer; ModalRoute? _route; Animation? _secondaryAnimation; AppLifecycleState? _lifecycleState; bool _autoRefresh = true; @override void initState() { super.initState(); WidgetsBinding.instance.addObserver(this); _lifecycleState = WidgetsBinding.instance.lifecycleState; _connect(); } void _connect() { _controller = HealthController(widget.gateway)..addListener(_changed); unawaited(_controller.load()); } void _changed() { if (mounted) setState(() {}); } @override void didChangeDependencies() { super.didChangeDependencies(); final route = ModalRoute.of(context); if (route == _route) { _updateVisibility(); return; } _secondaryAnimation?.removeListener(_updateVisibility); _route = route; _secondaryAnimation = route?.secondaryAnimation; _secondaryAnimation?.addListener(_updateVisibility); _updateVisibility(); } @override void didChangeAppLifecycleState(AppLifecycleState state) { _lifecycleState = state; _updateVisibility(); } void _updateVisibility() { final route = _route; final visible = route?.isCurrent == true && (_lifecycleState == null || _lifecycleState == AppLifecycleState.resumed) && (_secondaryAnimation?.value ?? 0) == 0; if (visible) { _schedule(); } else { _timer?.cancel(); _timer = null; } } void _schedule() { if (!_autoRefresh || _route?.isCurrent != true) { _timer?.cancel(); _timer = null; return; } if (_timer != null) return; _timer = Timer.periodic( const Duration(seconds: 5), (_) => unawaited(_controller.load()), ); } @override void didUpdateWidget(covariant HealthScreen oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.gateway != widget.gateway) { _controller.dispose(); _connect(); } } @override void dispose() { _timer?.cancel(); _secondaryAnimation?.removeListener(_updateVisibility); WidgetsBinding.instance.removeObserver(this); _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final stats = _controller.stats; final content = SettingsPageBody( children: [ if (_controller.busy && stats == null) const Center(child: CircularProgressIndicator.adaptive()), if (_controller.error case final String error) ...[ Text(error), HealthAction( label: 'Retry', onPressed: _controller.busy ? null : () => unawaited(_controller.load()), ), ], if (stats case final JsonMap data) ...[ HealthHeader( host: healthMap(data['host']), sampled: data['sampled_at'], busy: _controller.busy, autoRefresh: _autoRefresh, onRefresh: () => unawaited(_controller.load()), onAutoRefresh: (value) { setState(() => _autoRefresh = value); _updateVisibility(); }, ), HealthSections(stats: data), ], ], ); return Scaffold( appBar: AppBar(title: const Text('Server health')), body: content, ); } }