import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:go_router/go_router.dart'; import '../../../providers/auth_provider.dart'; import '../../../providers/sync_provider.dart'; class SyncPage extends ConsumerStatefulWidget { const SyncPage({super.key}); @override ConsumerState createState() => _SyncPageState(); } class _SyncPageState extends ConsumerState { bool _forceFullSync = false; @override Widget build(BuildContext context) { final authState = ref.watch(authProvider); final syncState = ref.watch(syncProvider); final theme = Theme.of(context); final agente = authState.agente; return Scaffold( appBar: AppBar( title: const Text('Sincronizzazione'), actions: [ IconButton( icon: const Icon(Icons.settings), onPressed: () => context.push('/dashboard/sync/settings'), ), ], ), body: SafeArea( child: Center( child: Padding( padding: const EdgeInsets.all(32), child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon( syncState.isSyncing ? Icons.sync : Icons.sync_outlined, size: 64, color: syncState.isSyncing ? theme.colorScheme.primary : theme.colorScheme.outline, ), const SizedBox(height: 24), Text( syncState.isSyncing ? 'Sincronizzazione in corso...' : 'Sincronizzazione dati', style: theme.textTheme.titleLarge, ), const SizedBox(height: 16), if (syncState.isSyncing) ...[ LinearProgressIndicator(value: syncState.progress), const SizedBox(height: 12), Text(syncState.message, style: theme.textTheme.bodyMedium), const SizedBox(height: 8), Text('${(syncState.progress * 100).toInt()}%', style: theme.textTheme.bodySmall), ] else if (syncState.error != null) ...[ Card( color: theme.colorScheme.errorContainer, child: Padding( padding: const EdgeInsets.all(12), child: Text(syncState.error!, style: TextStyle( color: theme.colorScheme.onErrorContainer)), ), ), ] else if (syncState.progress >= 1.0) ...[ Icon(Icons.check_circle, size: 48, color: Colors.green), const SizedBox(height: 8), const Text('Completato!'), ], const SizedBox(height: 12), CheckboxListTile( value: _forceFullSync, onChanged: syncState.isSyncing ? null : (v) => setState(() => _forceFullSync = v!), title: const Text('Sincronizzazione completa'), subtitle: const Text('Forza il download completo di tutti i dati'), controlAffinity: ListTileControlAffinity.leading, enabled: !syncState.isSyncing, ), const SizedBox(height: 12), FilledButton.icon( onPressed: syncState.isSyncing || agente == null ? null : () => ref.read(syncProvider.notifier).startFullSync( agente.idAgente, agente.codAgente!, forceFullSync: _forceFullSync, ), icon: syncState.isSyncing ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator(strokeWidth: 2)) : const Icon(Icons.sync), label: Text(syncState.isSyncing ? 'In corso...' : 'Avvia Sincronizzazione'), ), ], ), ), ), ), ); } }