initial commit
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
/// Modèle lavage (cycle en cours ou terminé).
|
||||
class Wash {
|
||||
const Wash({
|
||||
required this.uuid,
|
||||
required this.machineUuid,
|
||||
required this.machineName,
|
||||
required this.status,
|
||||
required this.cost,
|
||||
this.startedAt,
|
||||
this.endedAt,
|
||||
this.durationMinutes,
|
||||
});
|
||||
|
||||
final String uuid;
|
||||
final String machineUuid;
|
||||
final String machineName;
|
||||
final String status;
|
||||
final double cost;
|
||||
final DateTime? startedAt;
|
||||
final DateTime? endedAt;
|
||||
final int? durationMinutes;
|
||||
|
||||
factory Wash.fromJson(Map<String, dynamic> json) {
|
||||
final machine = json['machine'] as Map<String, dynamic>?;
|
||||
|
||||
return Wash(
|
||||
uuid: json['uuid'] as String,
|
||||
machineUuid: machine?['uuid'] as String? ?? json['machine_uuid'] as String? ?? '',
|
||||
machineName: machine?['name'] as String? ?? 'Machine',
|
||||
status: json['status'] as String? ?? 'pending_start',
|
||||
cost: (json['cost'] as num?)?.toDouble() ?? 0,
|
||||
startedAt: json['started_at'] != null
|
||||
? DateTime.tryParse(json['started_at'] as String)
|
||||
: null,
|
||||
endedAt: json['ended_at'] != null
|
||||
? DateTime.tryParse(json['ended_at'] as String)
|
||||
: null,
|
||||
durationMinutes: json['duration_minutes'] as int?,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_endpoints.dart';
|
||||
import '../domain/wash.dart';
|
||||
|
||||
/// Fournisseur de l'historique des lavages.
|
||||
final washesProvider = FutureProvider<List<Wash>>((ref) async {
|
||||
final response = await ref.watch(apiClientProvider).get(ApiEndpoints.washes);
|
||||
final data = response.data;
|
||||
|
||||
List<dynamic> list;
|
||||
if (data is List<dynamic>) {
|
||||
list = data;
|
||||
} else if (data is Map<String, dynamic> && data['data'] is List<dynamic>) {
|
||||
list = data['data'] as List<dynamic>;
|
||||
} else {
|
||||
list = [];
|
||||
}
|
||||
|
||||
return list.map((json) => Wash.fromJson(json as Map<String, dynamic>)).toList();
|
||||
});
|
||||
|
||||
/// Écran historique et démarrage de lavage.
|
||||
class WashScreen extends ConsumerWidget {
|
||||
const WashScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
final washesAsync = ref.watch(washesProvider);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: const Text('Mes lavages')),
|
||||
body: washesAsync.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (error, _) => Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text('Erreur : $error'),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: () => ref.invalidate(washesProvider),
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
data: (washes) {
|
||||
if (washes.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Aucun lavage enregistré.\nScannez un QR code pour démarrer un cycle.',
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final currencyFormat = NumberFormat.currency(locale: 'fr_FR', symbol: '€');
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async => ref.invalidate(washesProvider),
|
||||
child: ListView.separated(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: washes.length,
|
||||
separatorBuilder: (_, __) => const SizedBox(height: 8),
|
||||
itemBuilder: (context, index) {
|
||||
final wash = washes[index];
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: const Icon(Icons.local_laundry_service),
|
||||
title: Text(wash.machineName),
|
||||
subtitle: wash.startedAt != null
|
||||
? Text(DateFormat('dd/MM/yyyy HH:mm').format(wash.startedAt!))
|
||||
: null,
|
||||
trailing: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(currencyFormat.format(wash.cost)),
|
||||
Text(wash.status, style: Theme.of(context).textTheme.bodySmall),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: () {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Scan QR — à connecter à l\'API /washes/start')),
|
||||
);
|
||||
},
|
||||
icon: const Icon(Icons.qr_code_scanner),
|
||||
label: const Text('Scanner'),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user