import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; import '../providers/app_state.dart'; import '../models/machine.dart'; import '../components/machine_dialog.dart'; import '../components/searchable_selection.dart'; class MachinesScreen extends StatelessWidget { const MachinesScreen({super.key}); @override Widget build(BuildContext context) { return Consumer( builder: (context, appState, child) { if (appState.isLoading) { return const Center(child: CircularProgressIndicator()); } return Scaffold( body: appState.machines.isEmpty ? const Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.kitchen, size: 64, color: Colors.grey), SizedBox(height: 16), Text('No equipment in your collection yet'), Text('Tap the + button to browse and add machines'), ], ), ) : ListView.builder( padding: const EdgeInsets.all(16), itemCount: appState.machines.length, itemBuilder: (context, index) { final machine = appState.machines[index]; return _buildMachineCard(context, machine); }, ), floatingActionButton: FloatingActionButton( onPressed: () => _showAddMachineDialog(context), child: const Icon(Icons.add), ), ); }, ); } Widget _buildMachineCard(BuildContext context, Machine machine) { return Card( margin: const EdgeInsets.only(bottom: 16), child: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Expanded( child: Text( '${machine.manufacturer} ${machine.model}', style: Theme.of(context).textTheme.headlineSmall, ), ), PopupMenuButton( itemBuilder: (context) => [ const PopupMenuItem( value: 'edit', child: Row( children: [ Icon(Icons.edit), SizedBox(width: 8), Text('Edit'), ], ), ), const PopupMenuItem( value: 'delete', child: Row( children: [ Icon(Icons.delete, color: Colors.red), SizedBox(width: 8), Text('Delete', style: TextStyle(color: Colors.red)), ], ), ), ], onSelected: (value) { if (value == 'edit') { _showEditMachineDialog(context, machine); } else if (value == 'delete') { _showDeleteMachineDialog(context, machine); } }, ), ], ), const SizedBox(height: 8), Text('Type: ${machine.type.name}'), Text('Year: ${machine.year}'), if (machine.steamWand) const Row( children: [ Icon(Icons.check, size: 16, color: Colors.green), SizedBox(width: 4), Text('Steam Wand'), ], ), const SizedBox(height: 8), Text( machine.details, style: Theme.of(context).textTheme.bodyMedium, ), ], ), ), ); } void _showAddMachineDialog(BuildContext context) { _showMachineCatalog(context); } void _showMachineCatalog(BuildContext context) async { final appState = Provider.of(context, listen: false); // Get all available machines from catalog final availableMachines = await appState.getAllAvailableMachines(); if (availableMachines.isEmpty) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('No machines available in catalog')), ); return; } Navigator.of(context).push( MaterialPageRoute( builder: (context) => SearchableSelection( items: availableMachines, title: 'Browse Machine Catalog', searchHint: 'Search machines...', displayText: (machine) => '${machine.manufacturer} ${machine.model}', onItemSelected: (machine) async { Navigator.of(context).pop(); // Check if machine is already in user's collection if (appState.machines.any((m) => m.id == machine.id)) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('${machine.manufacturer} ${machine.model} is already in your collection')), ); return; } // Add machine to user's collection try { await appState.addMachine(machine); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('${machine.manufacturer} ${machine.model} added to your collection!')), ); } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Error adding machine: $e')), ); } }, onAddCustom: () { Navigator.of(context).pop(); // Close the search screen showDialog( context: context, builder: (context) => const MachineDialog(), ); }, ), ), ); } void _showEditMachineDialog(BuildContext context, Machine machine) { showDialog( context: context, builder: (context) => MachineDialog(machine: machine), ); } void _showDeleteMachineDialog(BuildContext context, Machine machine) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Delete Machine'), content: Text('Are you sure you want to delete "${machine.manufacturer} ${machine.model}"?'), actions: [ TextButton( onPressed: () => Navigator.of(context).pop(), child: const Text('Cancel'), ), ElevatedButton( onPressed: () async { try { await Provider.of(context, listen: false) .deleteMachine(machine.id); if (context.mounted) { Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Machine deleted successfully!')), ); } } catch (e) { if (context.mounted) { Navigator.of(context).pop(); ScaffoldMessenger.of(context).showSnackBar( SnackBar(content: Text('Error deleting machine: $e')), ); } } }, style: ElevatedButton.styleFrom(backgroundColor: Colors.red), child: const Text('Delete'), ), ], ), ); } }