sync_settings_page.dart 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import 'package:go_router/go_router.dart';
  4. import '../../../providers/auth_provider.dart';
  5. import '../../../providers/database_provider.dart';
  6. class SyncSettingsPage extends ConsumerStatefulWidget {
  7. const SyncSettingsPage({super.key});
  8. @override
  9. ConsumerState<SyncSettingsPage> createState() => _SyncSettingsPageState();
  10. }
  11. class _SyncSettingsPageState extends ConsumerState<SyncSettingsPage> {
  12. final _urlCtrl = TextEditingController();
  13. final _formKey = GlobalKey<FormState>();
  14. bool _isLoading = false;
  15. @override
  16. void initState() {
  17. super.initState();
  18. _loadUrl();
  19. }
  20. Future<void> _loadUrl() async {
  21. final url = await ref.read(parametriDaoProvider).read('URL');
  22. if (url != null && url.isNotEmpty) {
  23. _urlCtrl.text = url;
  24. }
  25. }
  26. @override
  27. void dispose() {
  28. _urlCtrl.dispose();
  29. super.dispose();
  30. }
  31. Future<void> _saveUrl() async {
  32. if (!_formKey.currentState!.validate()) return;
  33. setState(() => _isLoading = true);
  34. try {
  35. await ref.read(parametriDaoProvider).set('URL', _urlCtrl.text);
  36. if (mounted) {
  37. ScaffoldMessenger.of(context).showSnackBar(
  38. const SnackBar(content: Text('URL salvato con successo')),
  39. );
  40. }
  41. } catch (e) {
  42. if (mounted) {
  43. ScaffoldMessenger.of(context).showSnackBar(
  44. SnackBar(content: Text('Errore: $e')),
  45. );
  46. }
  47. } finally {
  48. if (mounted) setState(() => _isLoading = false);
  49. }
  50. }
  51. Future<void> _resetArchives() async {
  52. final confirm = await showDialog<bool>(
  53. context: context,
  54. builder: (ctx) => AlertDialog(
  55. title: const Text('Azzeramento archivi'),
  56. content: const Text('Cancellare tutti i dati archiviati? Le impostazioni e il codice agente rimarranno salvati.'),
  57. actions: [
  58. TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Annulla')),
  59. FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Cancella')),
  60. ],
  61. ),
  62. );
  63. if (confirm != true) return;
  64. setState(() => _isLoading = true);
  65. try {
  66. final db = ref.read(databaseProvider);
  67. await db.transaction(() async {
  68. await db.delete(db.articoli).go();
  69. await db.delete(db.clienti).go();
  70. await db.delete(db.ordini).go();
  71. await db.delete(db.righeOrdine).go();
  72. await db.delete(db.filiali).go();
  73. await db.delete(db.listini).go();
  74. await db.delete(db.listiniArticolo).go();
  75. await db.delete(db.listiniCliente).go();
  76. await db.delete(db.sconti).go();
  77. await db.delete(db.scontiCliente).go();
  78. await db.delete(db.pagamenti).go();
  79. await db.delete(db.tabelle).go();
  80. await db.delete(db.promozioni).go();
  81. await db.delete(db.promoArticoli).go();
  82. await db.delete(db.promoListini).go();
  83. await db.delete(db.promoOmaggi).go();
  84. await db.delete(db.clientiAgente).go();
  85. await db.delete(db.scadenze).go();
  86. await db.delete(db.statisticheVendita).go();
  87. });
  88. if (!mounted) return;
  89. final deleteAgent = await showDialog<bool>(
  90. context: context,
  91. builder: (ctx) => AlertDialog(
  92. title: const Text('Cancellazione codice agente'),
  93. content: const Text('Vuoi cancellare anche il codice agente? Dovrai riconfigurare l\'app da capo.'),
  94. actions: [
  95. TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('No')),
  96. FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Sì, cancella')),
  97. ],
  98. ),
  99. );
  100. if (deleteAgent == true) {
  101. await db.delete(db.agenti).go();
  102. ref.read(authProvider.notifier).resetToInit();
  103. if (mounted) context.go('/init');
  104. } else {
  105. ScaffoldMessenger.of(context).showSnackBar(
  106. const SnackBar(content: Text('Archivi azzerati')),
  107. );
  108. }
  109. } catch (e) {
  110. if (mounted) {
  111. ScaffoldMessenger.of(context).showSnackBar(
  112. SnackBar(content: Text('Errore durante l\'azzeramento: $e')),
  113. );
  114. }
  115. } finally {
  116. if (mounted) setState(() => _isLoading = false);
  117. }
  118. }
  119. @override
  120. Widget build(BuildContext context) {
  121. final theme = Theme.of(context);
  122. return Scaffold(
  123. appBar: AppBar(title: const Text('Impostazioni')),
  124. body: Center(
  125. child: SingleChildScrollView(
  126. padding: const EdgeInsets.all(24),
  127. child: ConstrainedBox(
  128. constraints: const BoxConstraints(maxWidth: 400),
  129. child: Form(
  130. key: _formKey,
  131. child: Column(
  132. mainAxisSize: MainAxisSize.min,
  133. crossAxisAlignment: CrossAxisAlignment.stretch,
  134. children: [
  135. Icon(Icons.cloud, size: 64, color: theme.colorScheme.primary),
  136. const SizedBox(height: 16),
  137. Text(
  138. 'Connessione',
  139. textAlign: TextAlign.center,
  140. style: theme.textTheme.titleLarge,
  141. ),
  142. const SizedBox(height: 24),
  143. TextFormField(
  144. controller: _urlCtrl,
  145. decoration: const InputDecoration(
  146. labelText: 'URL Backend',
  147. prefixIcon: Icon(Icons.link),
  148. ),
  149. validator: (v) => v == null || v.isEmpty ? 'Campo obbligatorio' : null,
  150. ),
  151. const SizedBox(height: 16),
  152. FilledButton.icon(
  153. onPressed: _isLoading ? null : _saveUrl,
  154. icon: const Icon(Icons.save),
  155. label: const Text('Salva URL'),
  156. ),
  157. const SizedBox(height: 40),
  158. const Divider(),
  159. const SizedBox(height: 16),
  160. Icon(Icons.delete_sweep, size: 48, color: theme.colorScheme.error),
  161. const SizedBox(height: 8),
  162. Text(
  163. 'Azzeramento dati',
  164. textAlign: TextAlign.center,
  165. style: theme.textTheme.titleMedium,
  166. ),
  167. const SizedBox(height: 8),
  168. Text(
  169. 'Cancella tutti i dati archiviati (clienti, ordini, articoli, ecc.)',
  170. textAlign: TextAlign.center,
  171. style: theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.outline),
  172. ),
  173. const SizedBox(height: 16),
  174. OutlinedButton.icon(
  175. onPressed: _isLoading ? null : _resetArchives,
  176. style: OutlinedButton.styleFrom(foregroundColor: theme.colorScheme.error),
  177. icon: const Icon(Icons.delete),
  178. label: const Text('Azzera Archivi'),
  179. ),
  180. if (_isLoading) ...[
  181. const SizedBox(height: 16),
  182. const LinearProgressIndicator(),
  183. ],
  184. ],
  185. ),
  186. ),
  187. ),
  188. ),
  189. ),
  190. );
  191. }
  192. }