refined document sequence management

This commit is contained in:
2026-05-16 09:04:18 +02:00
parent b5ccb0428d
commit a166992b04
6 changed files with 170 additions and 78 deletions

View File

@@ -1,17 +1,34 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flux/features/settings/document_sequence/blocs/document_sequence_cubit.dart';
import 'package:flux/features/settings/document_sequence/models/document_sequence_model.dart';
class DocumentSequenceSection extends StatelessWidget {
const DocumentSequenceSection({super.key});
// La nostra lista "Ninja" di tutti i documenti gestiti dall'app
// Se domani aggiungo le fatture, basta aggiungere una riga qui!
// e all'enum DocumentType nel model
static final supportedDocumentTypes = [
{
'type': DocumentType.ticket.name,
'label': 'TICKET',
'defaultPrefix': 'TCK',
},
{
'type': DocumentType.shipment.name,
'label': 'DDT (Documento di Trasporto)',
'defaultPrefix': 'DDT',
},
];
@override
Widget build(BuildContext context) {
final year = DateTime.now().year;
return BlocBuilder<DocumentSequenceCubit, DocumentSequenceState>(
builder: (context, state) {
if (state.isLoading) {
if (state.status == DocumentSequenceStatus.loading) {
return const Center(child: CircularProgressIndicator());
}
@@ -27,10 +44,26 @@ class DocumentSequenceSection extends StatelessWidget {
).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.bold),
),
),
...state.sequences.map((seq) {
// Anteprima dinamica
// Invece di mappare state.sequences, mappiamo i documenti supportati
...supportedDocumentTypes.map((docDef) {
final docType = docDef['type']!;
// Cerchiamo se c'è già una configurazione nello stato per questo documento
final existingList = state.sequences
.where((s) => s.docType == docType)
.toList();
final existingSeq = existingList.isNotEmpty
? existingList.first
: null;
// Se esiste usiamo i suoi valori, altrimenti i default
final prefix = existingSeq?.prefix ?? docDef['defaultPrefix']!;
final nextValue = existingSeq?.nextValue ?? 1;
// Anteprima dinamica (aggiornata a 4 zeri come nel DB!)
final preview =
"${seq.prefix.isNotEmpty ? '${seq.prefix}-' : ''}$year-${seq.nextValue.toString().padLeft(6, '0')}";
"${prefix.isNotEmpty ? '$prefix-' : ''}$year-${nextValue.toString().padLeft(4, '0')}";
return Card(
margin: const EdgeInsets.only(bottom: 12),
@@ -40,7 +73,7 @@ class DocumentSequenceSection extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
seq.docType.toUpperCase(),
docDef['label']!,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.blue,
@@ -52,24 +85,21 @@ class DocumentSequenceSection extends StatelessWidget {
Expanded(
flex: 2,
child: TextFormField(
initialValue: seq.prefix,
initialValue: prefix,
decoration: const InputDecoration(
labelText: 'Prefisso',
hintText: 'es. TCK',
),
onChanged: (val) => context
.read<DocumentSequenceCubit>()
.updateLocalSequence(
seq.docType,
prefix: val,
),
.updateLocalSequence(docType, prefix: val),
),
),
const SizedBox(width: 16),
Expanded(
flex: 3,
child: TextFormField(
initialValue: seq.nextValue.toString(),
initialValue: nextValue.toString(),
keyboardType: TextInputType.number,
decoration: const InputDecoration(
labelText: 'Prossimo Numero',
@@ -77,7 +107,7 @@ class DocumentSequenceSection extends StatelessWidget {
onChanged: (val) => context
.read<DocumentSequenceCubit>()
.updateLocalSequence(
seq.docType,
docType,
nextValue: int.tryParse(val) ?? 1,
),
),
@@ -88,7 +118,9 @@ class DocumentSequenceSection extends StatelessWidget {
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Colors.grey.shade100,
color: Colors
.grey
.shade100, // Se hai un tema scuro potresti voler usare Theme.of(context).colorScheme.surfaceContainer
borderRadius: BorderRadius.circular(8),
),
child: Row(
@@ -102,7 +134,9 @@ class DocumentSequenceSection extends StatelessWidget {
Text(
"Anteprima prossimo: ",
style: TextStyle(
color: Colors.grey.shade700,
color: Colors
.grey
.shade700, // Idem per la dark mode
fontSize: 12,
),
),