mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Refactor frontend: extract widgets and eliminate duplicated scanner state
- Convert ArpScannerCard to a self-managing StatefulWidget (owns its own 5s refresh + 1s tick timers), removing the duplicated state management that existed in both HomeScreen and StatusScreen - Extract NotificationCard to home/notification_card.dart - Extract DeviceSummaryCard (with its 1-min refresh timer) to widgets/device_summary_card.dart - HomeScreen drops from 515 to 253 lines; StatusScreen from 82 to 19 lines Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
251d8a479e
commit
f113da9cd0
@@ -0,0 +1,138 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../model/device_summary.dart';
|
||||
import '../utils/oott_api.dart';
|
||||
|
||||
class DeviceSummaryCard extends StatefulWidget {
|
||||
const DeviceSummaryCard({super.key});
|
||||
|
||||
@override
|
||||
State<DeviceSummaryCard> createState() => _DeviceSummaryCardState();
|
||||
}
|
||||
|
||||
class _DeviceSummaryCardState extends State<DeviceSummaryCard> {
|
||||
DeviceSummary? _summary;
|
||||
bool _isLoading = true;
|
||||
String? _error;
|
||||
Timer? _timer;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_load();
|
||||
_timer = Timer.periodic(const Duration(minutes: 1), (_) => _load());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _load() async {
|
||||
try {
|
||||
final summary = await BackendAPI.instance.getDeviceSummary();
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_summary = summary;
|
||||
_error = null;
|
||||
_isLoading = false;
|
||||
});
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_error = e.toString();
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text('Devices', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: 12),
|
||||
if (_isLoading)
|
||||
const Center(child: CircularProgressIndicator())
|
||||
else if (_error != null)
|
||||
Text(
|
||||
'Error loading device summary',
|
||||
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
||||
)
|
||||
else if (_summary != null) ...[
|
||||
_SummaryRow(
|
||||
label: 'Registered',
|
||||
value: '${_summary!.totalRegistered}',
|
||||
),
|
||||
const Divider(height: 20),
|
||||
Text(
|
||||
'Seen in the last 24 hours',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.outline,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
_SummaryRow(
|
||||
label: 'Registered',
|
||||
value: '${_summary!.seenLastDayRegistered}',
|
||||
),
|
||||
_SummaryRow(
|
||||
label: 'Unregistered',
|
||||
value: '${_summary!.seenLastDayUnregistered}',
|
||||
),
|
||||
const Divider(height: 20),
|
||||
Text(
|
||||
'Seen in the last 7 days',
|
||||
style: Theme.of(context).textTheme.bodySmall?.copyWith(
|
||||
color: Theme.of(context).colorScheme.outline,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
_SummaryRow(
|
||||
label: 'Registered',
|
||||
value: '${_summary!.seenLastWeekRegistered}',
|
||||
),
|
||||
_SummaryRow(
|
||||
label: 'Unregistered',
|
||||
value: '${_summary!.seenLastWeekUnregistered}',
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SummaryRow extends StatelessWidget {
|
||||
final String label;
|
||||
final String value;
|
||||
|
||||
const _SummaryRow({required this.label, required this.value});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 2),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(label, style: Theme.of(context).textTheme.bodyMedium),
|
||||
Text(
|
||||
value,
|
||||
style: Theme.of(context).textTheme.bodyMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user