| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- 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/database_provider.dart';
- class SyncSettingsPage extends ConsumerStatefulWidget {
- const SyncSettingsPage({super.key});
- @override
- ConsumerState<SyncSettingsPage> createState() => _SyncSettingsPageState();
- }
- class _SyncSettingsPageState extends ConsumerState<SyncSettingsPage> {
- final _urlCtrl = TextEditingController();
- final _formKey = GlobalKey<FormState>();
- bool _isLoading = false;
- @override
- void initState() {
- super.initState();
- _loadUrl();
- }
- Future<void> _loadUrl() async {
- final url = await ref.read(parametriDaoProvider).read('URL');
- if (url != null && url.isNotEmpty) {
- _urlCtrl.text = url;
- }
- }
- @override
- void dispose() {
- _urlCtrl.dispose();
- super.dispose();
- }
- Future<void> _saveUrl() async {
- if (!_formKey.currentState!.validate()) return;
- setState(() => _isLoading = true);
- try {
- await ref.read(parametriDaoProvider).set('URL', _urlCtrl.text);
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('URL salvato con successo')),
- );
- }
- } catch (e) {
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text('Errore: $e')),
- );
- }
- } finally {
- if (mounted) setState(() => _isLoading = false);
- }
- }
- Future<void> _resetArchives() async {
- final confirm = await showDialog<bool>(
- context: context,
- builder: (ctx) => AlertDialog(
- title: const Text('Azzeramento archivi'),
- content: const Text('Cancellare tutti i dati archiviati? Le impostazioni e il codice agente rimarranno salvati.'),
- actions: [
- TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Annulla')),
- FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Cancella')),
- ],
- ),
- );
- if (confirm != true) return;
- setState(() => _isLoading = true);
- try {
- final db = ref.read(databaseProvider);
- await db.transaction(() async {
- await db.delete(db.articoli).go();
- await db.delete(db.clienti).go();
- await db.delete(db.ordini).go();
- await db.delete(db.righeOrdine).go();
- await db.delete(db.filiali).go();
- await db.delete(db.listini).go();
- await db.delete(db.listiniArticolo).go();
- await db.delete(db.listiniCliente).go();
- await db.delete(db.sconti).go();
- await db.delete(db.scontiCliente).go();
- await db.delete(db.pagamenti).go();
- await db.delete(db.tabelle).go();
- await db.delete(db.promozioni).go();
- await db.delete(db.promoArticoli).go();
- await db.delete(db.promoListini).go();
- await db.delete(db.promoOmaggi).go();
- await db.delete(db.clientiAgente).go();
- await db.delete(db.scadenze).go();
- await db.delete(db.statisticheVendita).go();
- await (db.delete(db.parametri)..where((t) => t.nome.equals('lastUpdate'))).go();
- });
- if (!mounted) return;
- final deleteAgent = await showDialog<bool>(
- context: context,
- builder: (ctx) => AlertDialog(
- title: const Text('Cancellazione codice agente'),
- content: const Text('Vuoi cancellare anche il codice agente? Dovrai riconfigurare l\'app da capo.'),
- actions: [
- TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('No')),
- FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Sì, cancella')),
- ],
- ),
- );
- if (deleteAgent == true) {
- await db.delete(db.agenti).go();
- ref.read(authProvider.notifier).resetToInit();
- if (mounted) context.go('/init');
- } else {
- ScaffoldMessenger.of(context).showSnackBar(
- const SnackBar(content: Text('Archivi azzerati')),
- );
- }
- } catch (e) {
- if (mounted) {
- ScaffoldMessenger.of(context).showSnackBar(
- SnackBar(content: Text('Errore durante l\'azzeramento: $e')),
- );
- }
- } finally {
- if (mounted) setState(() => _isLoading = false);
- }
- }
- @override
- Widget build(BuildContext context) {
- final theme = Theme.of(context);
- return Scaffold(
- appBar: AppBar(title: const Text('Impostazioni')),
- body: SafeArea(
- child: Center(
- child: SingleChildScrollView(
- padding: const EdgeInsets.all(24),
- child: ConstrainedBox(
- constraints: const BoxConstraints(maxWidth: 400),
- child: Form(
- key: _formKey,
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- Icon(Icons.cloud, size: 64, color: theme.colorScheme.primary),
- const SizedBox(height: 16),
- Text(
- 'Connessione',
- textAlign: TextAlign.center,
- style: theme.textTheme.titleLarge,
- ),
- const SizedBox(height: 24),
- TextFormField(
- controller: _urlCtrl,
- decoration: const InputDecoration(
- labelText: 'URL Backend',
- prefixIcon: Icon(Icons.link),
- ),
- validator: (v) => v == null || v.isEmpty ? 'Campo obbligatorio' : null,
- ),
- const SizedBox(height: 16),
- FilledButton.icon(
- onPressed: _isLoading ? null : _saveUrl,
- icon: const Icon(Icons.save),
- label: const Text('Salva URL'),
- ),
- const SizedBox(height: 40),
- const Divider(),
- const SizedBox(height: 16),
- Icon(Icons.delete_sweep, size: 48, color: theme.colorScheme.error),
- const SizedBox(height: 8),
- Text(
- 'Azzeramento dati',
- textAlign: TextAlign.center,
- style: theme.textTheme.titleMedium,
- ),
- const SizedBox(height: 8),
- Text(
- 'Cancella tutti i dati archiviati (clienti, ordini, articoli, ecc.)',
- textAlign: TextAlign.center,
- style: theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.outline),
- ),
- const SizedBox(height: 16),
- OutlinedButton.icon(
- onPressed: _isLoading ? null : _resetArchives,
- style: OutlinedButton.styleFrom(foregroundColor: theme.colorScheme.error),
- icon: const Icon(Icons.delete),
- label: const Text('Azzera Archivi'),
- ),
- if (_isLoading) ...[
- const SizedBox(height: 16),
- const LinearProgressIndicator(),
- ],
- ],
- ),
- ),
- ),
- ),
- ),
- ),
- );
- }
- }
|