sync_settings_page.dart 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. await (db.delete(db.parametri)..where((t) => t.nome.equals('lastUpdate'))).go();
  88. });
  89. if (!mounted) return;
  90. final deleteAgent = await showDialog<bool>(
  91. context: context,
  92. builder: (ctx) => AlertDialog(
  93. title: const Text('Cancellazione codice agente'),
  94. content: const Text('Vuoi cancellare anche il codice agente? Dovrai riconfigurare l\'app da capo.'),
  95. actions: [
  96. TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('No')),
  97. FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Sì, cancella')),
  98. ],
  99. ),
  100. );
  101. if (deleteAgent == true) {
  102. await db.delete(db.agenti).go();
  103. ref.read(authProvider.notifier).resetToInit();
  104. if (mounted) context.go('/init');
  105. } else {
  106. ScaffoldMessenger.of(context).showSnackBar(
  107. const SnackBar(content: Text('Archivi azzerati')),
  108. );
  109. }
  110. } catch (e) {
  111. if (mounted) {
  112. ScaffoldMessenger.of(context).showSnackBar(
  113. SnackBar(content: Text('Errore durante l\'azzeramento: $e')),
  114. );
  115. }
  116. } finally {
  117. if (mounted) setState(() => _isLoading = false);
  118. }
  119. }
  120. @override
  121. Widget build(BuildContext context) {
  122. final theme = Theme.of(context);
  123. return Scaffold(
  124. appBar: AppBar(title: const Text('Impostazioni')),
  125. body: SafeArea(
  126. child: Center(
  127. child: SingleChildScrollView(
  128. padding: const EdgeInsets.all(24),
  129. child: ConstrainedBox(
  130. constraints: const BoxConstraints(maxWidth: 400),
  131. child: Form(
  132. key: _formKey,
  133. child: Column(
  134. mainAxisSize: MainAxisSize.min,
  135. crossAxisAlignment: CrossAxisAlignment.stretch,
  136. children: [
  137. Icon(Icons.cloud, size: 64, color: theme.colorScheme.primary),
  138. const SizedBox(height: 16),
  139. Text(
  140. 'Connessione',
  141. textAlign: TextAlign.center,
  142. style: theme.textTheme.titleLarge,
  143. ),
  144. const SizedBox(height: 24),
  145. TextFormField(
  146. controller: _urlCtrl,
  147. decoration: const InputDecoration(
  148. labelText: 'URL Backend',
  149. prefixIcon: Icon(Icons.link),
  150. ),
  151. validator: (v) => v == null || v.isEmpty ? 'Campo obbligatorio' : null,
  152. ),
  153. const SizedBox(height: 16),
  154. FilledButton.icon(
  155. onPressed: _isLoading ? null : _saveUrl,
  156. icon: const Icon(Icons.save),
  157. label: const Text('Salva URL'),
  158. ),
  159. const SizedBox(height: 40),
  160. const Divider(),
  161. const SizedBox(height: 16),
  162. Icon(Icons.delete_sweep, size: 48, color: theme.colorScheme.error),
  163. const SizedBox(height: 8),
  164. Text(
  165. 'Azzeramento dati',
  166. textAlign: TextAlign.center,
  167. style: theme.textTheme.titleMedium,
  168. ),
  169. const SizedBox(height: 8),
  170. Text(
  171. 'Cancella tutti i dati archiviati (clienti, ordini, articoli, ecc.)',
  172. textAlign: TextAlign.center,
  173. style: theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.outline),
  174. ),
  175. const SizedBox(height: 16),
  176. OutlinedButton.icon(
  177. onPressed: _isLoading ? null : _resetArchives,
  178. style: OutlinedButton.styleFrom(foregroundColor: theme.colorScheme.error),
  179. icon: const Icon(Icons.delete),
  180. label: const Text('Azzera Archivi'),
  181. ),
  182. if (_isLoading) ...[
  183. const SizedBox(height: 16),
  184. const LinearProgressIndicator(),
  185. ],
  186. ],
  187. ),
  188. ),
  189. ),
  190. ),
  191. ),
  192. ),
  193. );
  194. }
  195. }