refactor pesantissimo dei Customer Files
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/theme/theme.dart';
|
||||
import 'package:flux/features/customers/data/customer_repository.dart';
|
||||
import 'package:flux/core/widgets/image_viewer_widget.dart';
|
||||
import 'package:flux/core/widgets/pdf_viewer_widget.dart';
|
||||
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:flux/features/customers/models/customer_file_model.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
|
||||
class CustomerDetailScreen extends StatefulWidget {
|
||||
final CustomerModel customer;
|
||||
@@ -15,36 +17,19 @@ class CustomerDetailScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _CustomerDetailScreenState extends State<CustomerDetailScreen> {
|
||||
final _repository = GetIt.I<CustomerRepository>();
|
||||
List<CustomerFileModel> _files = [];
|
||||
bool _isLoadingFiles = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadFiles();
|
||||
}
|
||||
|
||||
Future<void> _loadFiles() async {
|
||||
try {
|
||||
final files = await _repository.getCustomerFiles(
|
||||
widget.customer.id.toString(),
|
||||
);
|
||||
setState(() {
|
||||
_files = files;
|
||||
_isLoadingFiles = false;
|
||||
});
|
||||
} catch (e) {
|
||||
setState(() => _isLoadingFiles = false);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(e.toString())));
|
||||
}
|
||||
}
|
||||
void _loadFiles() {
|
||||
context.read<CustomerFilesBloc>().add(LoadCustomerFilesEvent());
|
||||
}
|
||||
|
||||
Future<void> _pickAndUpload() async {
|
||||
CustomerFilesBloc customerFilesBloc = context.read<CustomerFilesBloc>();
|
||||
|
||||
// Chiamata statica pulita
|
||||
FilePickerResult? result = await FilePicker.pickFiles(
|
||||
allowMultiple: true,
|
||||
@@ -55,11 +40,9 @@ class _CustomerDetailScreenState extends State<CustomerDetailScreen> {
|
||||
if (result != null) {
|
||||
for (var pickedFile in result.files) {
|
||||
try {
|
||||
final newFile = await _repository.uploadAndRegisterFile(
|
||||
customerId: widget.customer.id.toString(),
|
||||
pickedFile: pickedFile,
|
||||
customerFilesBloc.add(
|
||||
UploadCustomerFileEvent(pickedFile: pickedFile),
|
||||
);
|
||||
setState(() => _files.add(newFile));
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -158,46 +141,51 @@ class _CustomerDetailScreenState extends State<CustomerDetailScreen> {
|
||||
}
|
||||
|
||||
Widget _buildDocumentSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
return BlocBuilder<CustomerFilesBloc, CustomerFilesState>(
|
||||
builder: (context, state) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
"DOCUMENTI",
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: context.accent,
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
"DOCUMENTI",
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: context.accent,
|
||||
),
|
||||
),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _pickAndUpload,
|
||||
icon: const Icon(Icons.add_circle_outline),
|
||||
label: const Text("CARICA FILE"),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
if (state.status == CustomerFilesStatus.loading)
|
||||
const Center(child: CircularProgressIndicator())
|
||||
else if (state.customerFiles.isEmpty)
|
||||
const Center(child: Text("Nessun documento presente"))
|
||||
else
|
||||
Expanded(
|
||||
child: GridView.builder(
|
||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent: 200,
|
||||
mainAxisSpacing: 16,
|
||||
crossAxisSpacing: 16,
|
||||
childAspectRatio: 1.2,
|
||||
),
|
||||
itemCount: state.customerFiles.length,
|
||||
itemBuilder: (context, index) =>
|
||||
_FileCard(file: state.customerFiles[index], state: state),
|
||||
),
|
||||
),
|
||||
),
|
||||
ElevatedButton.icon(
|
||||
onPressed: _pickAndUpload,
|
||||
icon: const Icon(Icons.add_circle_outline),
|
||||
label: const Text("CARICA FILE"),
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
if (_isLoadingFiles)
|
||||
const Center(child: CircularProgressIndicator())
|
||||
else if (_files.isEmpty)
|
||||
const Center(child: Text("Nessun documento presente"))
|
||||
else
|
||||
Expanded(
|
||||
child: GridView.builder(
|
||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||
maxCrossAxisExtent: 200,
|
||||
mainAxisSpacing: 16,
|
||||
crossAxisSpacing: 16,
|
||||
childAspectRatio: 1.2,
|
||||
),
|
||||
itemCount: _files.length,
|
||||
itemBuilder: (context, index) => _FileCard(file: _files[index]),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -227,30 +215,54 @@ class _CustomerDetailScreenState extends State<CustomerDetailScreen> {
|
||||
|
||||
class _FileCard extends StatelessWidget {
|
||||
final CustomerFileModel file;
|
||||
const _FileCard({required this.file});
|
||||
final CustomerFilesState state;
|
||||
const _FileCard({required this.file, required this.state});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
decoration: BoxDecoration(
|
||||
color: context.background,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: context.accent.withValues(alpha: 0.1)),
|
||||
return GestureDetector(
|
||||
onTap: () => context.read<CustomerFilesBloc>().add(
|
||||
ToggleCustomerFileSelectionEvent(file),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
onDoubleTap: () => _handleDoubleClickOnFile(context, file),
|
||||
child: Stack(
|
||||
children: [
|
||||
Icon(_getFileIcon(file.extension), size: 48, color: context.accent),
|
||||
const SizedBox(height: 8),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Text(
|
||||
file.name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: context.background,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: context.accent.withValues(alpha: 0.1)),
|
||||
),
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
_getFileIcon(file.extension),
|
||||
size: 48,
|
||||
color: context.accent,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8),
|
||||
child: Text(
|
||||
file.name,
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
if (state.selectedFiles.contains(file))
|
||||
Positioned(
|
||||
top: 10,
|
||||
left: 10,
|
||||
child: Icon(Icons.check_circle, color: context.accent, size: 24),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
@@ -268,4 +280,25 @@ class _FileCard extends StatelessWidget {
|
||||
return Icons.insert_drive_file;
|
||||
}
|
||||
}
|
||||
|
||||
void _handleDoubleClickOnFile(BuildContext context, CustomerFileModel file) {
|
||||
showDialog(
|
||||
context: context,
|
||||
barrierDismissible: true,
|
||||
builder: (ctx) => Dialog(
|
||||
insetPadding: const EdgeInsets.all(16),
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
height: MediaQuery.of(context).size.height * 0.8,
|
||||
child: file.isPdf
|
||||
? PdfViewerWidget(storagePath: file.storagePath)
|
||||
: ImageViewerWidget(storagePath: file.storagePath),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flux/core/blocs/session/session_cubit.dart';
|
||||
import 'package:flux/core/theme/theme.dart';
|
||||
import 'package:flux/features/customers/blocs/customer_cubit.dart';
|
||||
import 'package:flux/features/customers/blocs/customer_files_bloc.dart';
|
||||
import 'package:flux/features/customers/models/customer_model.dart';
|
||||
import 'package:flux/features/customers/ui/customer_form.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
@@ -37,39 +38,6 @@ class _CustomersContentState extends State<CustomersContent> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Funzione unica per gestire Creazione e Modifica
|
||||
void _openCustomerForm({CustomerModel? customer}) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
backgroundColor: context.background,
|
||||
content: SizedBox(
|
||||
width: 500, // Larghezza ottimale per desktop
|
||||
child: CustomerForm(
|
||||
customer: customer,
|
||||
onSave: (customerFromForm) {
|
||||
final session = context.read<SessionCubit>().state;
|
||||
final companyId = session.company?.id;
|
||||
|
||||
if (companyId == null) return;
|
||||
|
||||
if (customer == null) {
|
||||
// CASO NUOVO: Iniettiamo il companyId e inviamo l'evento create
|
||||
context.read<CustomerCubit>().createCustomer(
|
||||
customerFromForm.copyWith(companyId: companyId),
|
||||
);
|
||||
} else {
|
||||
// CASO MODIFICA: L'ID e il companyId sono già nel modello
|
||||
context.read<CustomerCubit>().updateCustomer(customerFromForm);
|
||||
}
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
@@ -85,7 +53,7 @@ class _CustomersContentState extends State<CustomersContent> {
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(right: 16),
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () => _openCustomerForm(),
|
||||
onPressed: () => openCustomerForm(context: context),
|
||||
icon: const Icon(Icons.person_add_alt_1_rounded, size: 20),
|
||||
label: const Text('NUOVO'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
@@ -244,8 +212,48 @@ class _CustomerTile extends StatelessWidget {
|
||||
],
|
||||
),
|
||||
),
|
||||
trailing: Icon(Icons.edit_note_rounded, color: context.accent),
|
||||
trailing: IconButton(
|
||||
onPressed: () =>
|
||||
openCustomerForm(context: context, customer: customer),
|
||||
icon: Icon(Icons.edit_note_rounded, color: context.accent),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Funzione unica per gestire Creazione e Modifica
|
||||
void openCustomerForm({
|
||||
CustomerModel? customer,
|
||||
required BuildContext context,
|
||||
}) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (dialogContext) => AlertDialog(
|
||||
backgroundColor: context.background,
|
||||
content: SizedBox(
|
||||
width: 500, // Larghezza ottimale per desktop
|
||||
child: CustomerForm(
|
||||
customer: customer,
|
||||
onSave: (customerFromForm) {
|
||||
final session = context.read<SessionCubit>().state;
|
||||
final companyId = session.company?.id;
|
||||
|
||||
if (companyId == null) return;
|
||||
|
||||
if (customer == null) {
|
||||
// CASO NUOVO: Iniettiamo il companyId e inviamo l'evento create
|
||||
context.read<CustomerCubit>().createCustomer(
|
||||
customerFromForm.copyWith(companyId: companyId),
|
||||
);
|
||||
} else {
|
||||
// CASO MODIFICA: L'ID e il companyId sono già nel modello
|
||||
context.read<CustomerCubit>().updateCustomer(customerFromForm);
|
||||
}
|
||||
Navigator.pop(dialogContext);
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user