Integration fonctionnalités V1 ( resas + gestion machines
This commit is contained in:
@@ -1,103 +1,152 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_endpoints.dart';
|
||||
import '../../../core/api/api_response.dart';
|
||||
import '../../../core/router/app_router.dart';
|
||||
import '../../../core/theme/app_colors.dart';
|
||||
import '../../../core/widgets/empty_state.dart';
|
||||
import '../../../core/widgets/screen_header.dart';
|
||||
import '../data/wash_repository.dart';
|
||||
import '../domain/wash.dart';
|
||||
import '../domain/wash_progress.dart';
|
||||
import 'widgets/active_wash_progress_card.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;
|
||||
bool _isActiveWash(String status) =>
|
||||
status == 'running' || status == 'pending_start' || status == 'active';
|
||||
|
||||
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 {
|
||||
/// Écran historique et lavages en cours avec progression.
|
||||
class WashScreen extends ConsumerStatefulWidget {
|
||||
const WashScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
ConsumerState<WashScreen> createState() => _WashScreenState();
|
||||
}
|
||||
|
||||
class _WashScreenState extends ConsumerState<WashScreen> {
|
||||
Timer? _ticker;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_ticker = Timer.periodic(const Duration(seconds: 1), (_) {
|
||||
if (mounted) setState(() {});
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ticker?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
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,
|
||||
return washesAsync.when(
|
||||
loading: () => const Center(child: CircularProgressIndicator()),
|
||||
error: (error, _) => EmptyState(
|
||||
icon: Icons.error_outline,
|
||||
title: 'Erreur de chargement',
|
||||
subtitle: error is DioException
|
||||
? ApiResponse.errorMessage(error, fallback: 'Impossible de charger les lavages.')
|
||||
: 'Impossible de charger les lavages.',
|
||||
actionLabel: 'Réessayer',
|
||||
onAction: () => ref.invalidate(washesProvider),
|
||||
),
|
||||
data: (washes) {
|
||||
if (washes.isEmpty) {
|
||||
return EmptyState(
|
||||
icon: Icons.qr_code_scanner_outlined,
|
||||
title: 'Aucun lavage',
|
||||
subtitle: 'Scannez une machine pour voir\nses infos et démarrer un cycle.',
|
||||
actionLabel: 'Scanner',
|
||||
onAction: () => context.push(AppRoutes.washScan),
|
||||
);
|
||||
}
|
||||
|
||||
final active = washes.where((w) => _isActiveWash(w.status)).toList();
|
||||
final history = washes.where((w) => !_isActiveWash(w.status)).toList();
|
||||
final currencyFormat = NumberFormat.currency(locale: 'fr_FR', symbol: '€');
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async => ref.invalidate(washesProvider),
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 8, 16, 88),
|
||||
children: [
|
||||
Text('Erreur : $error'),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton(
|
||||
onPressed: () => ref.invalidate(washesProvider),
|
||||
child: const Text('Réessayer'),
|
||||
),
|
||||
if (active.isNotEmpty) ...[
|
||||
const ScreenHeader(
|
||||
title: 'En cours',
|
||||
subtitle: 'Progression en temps réel',
|
||||
),
|
||||
...active.map((wash) {
|
||||
final progress = _liveProgress(wash);
|
||||
return ActiveWashProgressCard(
|
||||
wash: wash,
|
||||
format: currencyFormat,
|
||||
progress: progress,
|
||||
);
|
||||
}),
|
||||
const SizedBox(height: 16),
|
||||
],
|
||||
if (history.isNotEmpty) ...[
|
||||
Text('Historique', style: Theme.of(context).textTheme.titleMedium),
|
||||
const SizedBox(height: 10),
|
||||
...history.map(
|
||||
(wash) => Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: _HistoryWashTile(wash: wash, format: currencyFormat),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
WashProgress _liveProgress(Wash wash) {
|
||||
if (wash.progress != null && wash.progress!.percent > 0) {
|
||||
return WashProgress.compute(
|
||||
startedAt: wash.startedAt,
|
||||
estimatedEndAt: wash.progress!.estimatedEndAt ?? wash.cycleEndsAt,
|
||||
durationMinutes: wash.durationMinutes,
|
||||
machineType: wash.machineType,
|
||||
status: wash.status,
|
||||
);
|
||||
}
|
||||
return wash.liveProgress;
|
||||
}
|
||||
}
|
||||
|
||||
class _HistoryWashTile extends StatelessWidget {
|
||||
const _HistoryWashTile({required this.wash, required this.format});
|
||||
|
||||
final Wash wash;
|
||||
final NumberFormat format;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Card(
|
||||
child: ListTile(
|
||||
leading: const CircleAvatar(
|
||||
backgroundColor: Color(0xFFE2E8F0),
|
||||
child: Icon(Icons.check_circle_outline, color: AppColors.success, size: 20),
|
||||
),
|
||||
title: Text(wash.machineName),
|
||||
subtitle: wash.startedAt != null
|
||||
? Text(DateFormat('dd/MM/yyyy · HH:mm').format(wash.startedAt!))
|
||||
: null,
|
||||
trailing: Text(
|
||||
format.format(wash.cost),
|
||||
style: const TextStyle(fontWeight: FontWeight.w600),
|
||||
),
|
||||
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