326 lines
10 KiB
Dart
326 lines
10 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:intl/intl.dart';
|
|
import '../../core/constants/app_constants.dart';
|
|
import '../../core/theme/app_theme.dart';
|
|
import '../../data/models/session.dart';
|
|
import '../../data/models/target_type.dart';
|
|
import '../../data/repositories/session_repository.dart';
|
|
import 'session_detail_screen.dart';
|
|
import 'widgets/session_list_item.dart';
|
|
import 'widgets/history_chart.dart';
|
|
|
|
class HistoryScreen extends StatefulWidget {
|
|
/// Incrémenté par la navigation à chaque ouverture de l'onglet Historique,
|
|
/// pour forcer un rechargement des sessions (l'écran est gardé vivant par un
|
|
/// IndexedStack, sinon une session tout juste clôturée n'apparaîtrait pas
|
|
/// tant qu'on ne tire pas manuellement pour rafraîchir).
|
|
final int refreshTick;
|
|
|
|
const HistoryScreen({super.key, this.refreshTick = 0});
|
|
|
|
@override
|
|
State<HistoryScreen> createState() => _HistoryScreenState();
|
|
}
|
|
|
|
class _HistoryScreenState extends State<HistoryScreen> {
|
|
List<Session> _sessions = [];
|
|
bool _isLoading = true;
|
|
TargetType? _filterType;
|
|
|
|
// --- MODIFICATION : Remplacement de DateTime par DateTimeRange ---
|
|
DateTimeRange? _selectedDateRange;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadSessions();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(HistoryScreen oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
// L'onglet vient d'être ré-ouvert : on recharge pour afficher les sessions
|
|
// récemment clôturées sans avoir à tirer manuellement pour rafraîchir.
|
|
if (oldWidget.refreshTick != widget.refreshTick) {
|
|
_loadSessions();
|
|
}
|
|
}
|
|
|
|
Future<void> _loadSessions() async {
|
|
if (!mounted) return;
|
|
setState(() => _isLoading = true);
|
|
|
|
try {
|
|
final repository = context.read<SessionRepository>();
|
|
final sessions = await repository.getAllSessions();
|
|
|
|
if (mounted) {
|
|
setState(() {
|
|
_sessions = sessions;
|
|
|
|
// --- FILTRAGE PAR TYPE DE CIBLE ---
|
|
// Une session est retenue si au moins une de ses cibles est du type choisi.
|
|
if (_filterType != null) {
|
|
_sessions = _sessions
|
|
.where((s) => s.analyses.any((a) => a.targetType == _filterType))
|
|
.toList();
|
|
}
|
|
|
|
// --- LOGIQUE DE FILTRAGE PAR PÉRIODE ---
|
|
if (_selectedDateRange != null) {
|
|
_sessions = _sessions.where((s) {
|
|
// On compare uniquement les dates (sans les heures) pour éviter les bugs
|
|
final sessionDate = DateTime(
|
|
s.createdAt.year,
|
|
s.createdAt.month,
|
|
s.createdAt.day,
|
|
);
|
|
final startDate = DateTime(
|
|
_selectedDateRange!.start.year,
|
|
_selectedDateRange!.start.month,
|
|
_selectedDateRange!.start.day,
|
|
);
|
|
final endDate = DateTime(
|
|
_selectedDateRange!.end.year,
|
|
_selectedDateRange!.end.month,
|
|
_selectedDateRange!.end.day,
|
|
);
|
|
|
|
return sessionDate.isAtSameMomentAs(startDate) ||
|
|
sessionDate.isAtSameMomentAs(endDate) ||
|
|
(sessionDate.isAfter(startDate) &&
|
|
sessionDate.isBefore(endDate));
|
|
}).toList();
|
|
}
|
|
_isLoading = false;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
if (mounted) {
|
|
setState(() => _isLoading = false);
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(
|
|
content: Text('Erreur: $e'),
|
|
backgroundColor: AppTheme.errorColor,
|
|
),
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
// --- MODIFICATION : Fonction DateRangePicker ---
|
|
Future<void> _pickDateRange() async {
|
|
final DateTimeRange? picked = await showDateRangePicker(
|
|
context: context,
|
|
initialDateRange: _selectedDateRange,
|
|
firstDate: DateTime(2020),
|
|
lastDate: DateTime.now().add(const Duration(days: 1)),
|
|
locale: const Locale('fr', 'FR'),
|
|
builder: (context, child) {
|
|
return Theme(
|
|
data: ThemeData.light().copyWith(
|
|
colorScheme: ColorScheme.light(
|
|
primary: AppTheme.primaryColor, // En-tête et sélection
|
|
onPrimary: Colors.white, // Texte sur en-tête/sélection
|
|
surface: Colors.white, // Fond du calendrier
|
|
onSurface: Colors.black87, // Texte des dates (Noir sur Blanc)
|
|
secondary: AppTheme.primaryColor,
|
|
),
|
|
dialogTheme: const DialogThemeData(backgroundColor: Colors.white),
|
|
textButtonTheme: TextButtonThemeData(
|
|
style: TextButton.styleFrom(foregroundColor: AppTheme.primaryColor),
|
|
),
|
|
),
|
|
child: child!,
|
|
);
|
|
},
|
|
);
|
|
|
|
if (picked != null) {
|
|
setState(() => _selectedDateRange = picked);
|
|
_loadSessions();
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Historique'),
|
|
actions: [
|
|
// NOTE : on passe par un String sentinelle ('all') car un
|
|
// PopupMenuItem avec value null ne déclenche jamais onSelected
|
|
// (Flutter l'interprète comme une annulation du menu).
|
|
PopupMenuButton<String>(
|
|
icon: Icon(
|
|
Icons.filter_list,
|
|
// Icône colorée quand un filtre est actif, pour le rendre visible.
|
|
color: _filterType != null ? AppTheme.primaryColor : null,
|
|
),
|
|
onSelected: (value) {
|
|
setState(() {
|
|
_filterType =
|
|
value == 'all' ? null : TargetType.fromString(value);
|
|
});
|
|
_loadSessions();
|
|
},
|
|
itemBuilder: (context) => [
|
|
const PopupMenuItem(value: 'all', child: Text('Tous')),
|
|
...TargetType.values.map(
|
|
(type) => PopupMenuItem(
|
|
value: type.name,
|
|
child: Text(type.displayName),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
body: Column(
|
|
children: [
|
|
Expanded(
|
|
child: _isLoading
|
|
? const Center(child: CircularProgressIndicator())
|
|
: _sessions.isEmpty
|
|
? _buildEmptyState()
|
|
: _buildContent(),
|
|
),
|
|
_buildBottomFilterBar(),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildBottomFilterBar() {
|
|
return Container(
|
|
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
|
decoration: BoxDecoration(
|
|
color: Theme.of(context).cardColor,
|
|
boxShadow: const [
|
|
BoxShadow(
|
|
color: Colors.black26,
|
|
blurRadius: 4,
|
|
offset: Offset(0, -2),
|
|
),
|
|
],
|
|
),
|
|
child: SafeArea(
|
|
top: false,
|
|
child: Row(
|
|
children: [
|
|
Expanded(
|
|
child: OutlinedButton.icon(
|
|
onPressed: _pickDateRange,
|
|
icon: const Icon(Icons.date_range, size: 18),
|
|
label: Text(
|
|
_selectedDateRange == null
|
|
? 'Choisir une période'
|
|
: '${DateFormat('dd/MM/yy').format(_selectedDateRange!.start)} - ${DateFormat('dd/MM/yy').format(_selectedDateRange!.end)}',
|
|
),
|
|
),
|
|
),
|
|
if (_selectedDateRange != null)
|
|
IconButton(
|
|
icon: const Icon(Icons.close, color: AppTheme.errorColor),
|
|
onPressed: () {
|
|
setState(() => _selectedDateRange = null);
|
|
_loadSessions();
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildEmptyState() {
|
|
return Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
Icon(Icons.history, size: 64, color: Colors.grey[400]),
|
|
const SizedBox(height: 16),
|
|
const Text('Aucune session sur cette période'),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildContent() {
|
|
return RefreshIndicator(
|
|
onRefresh: _loadSessions,
|
|
child: CustomScrollView(
|
|
slivers: [
|
|
if (_sessions.length >= 2 && _selectedDateRange == null)
|
|
SliverToBoxAdapter(
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(AppConstants.defaultPadding),
|
|
child: HistoryChart(sessions: _sessions),
|
|
),
|
|
),
|
|
SliverPadding(
|
|
padding: const EdgeInsets.all(AppConstants.defaultPadding),
|
|
sliver: SliverList(
|
|
delegate: SliverChildBuilderDelegate((context, index) {
|
|
final session = _sessions[index];
|
|
return Padding(
|
|
padding: const EdgeInsets.only(bottom: 12),
|
|
child: SessionListItem(
|
|
session: session,
|
|
onTap: () => _openSessionDetail(session),
|
|
onDelete: () => _deleteSession(session),
|
|
),
|
|
);
|
|
}, childCount: _sessions.length),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _openSessionDetail(Session session) async {
|
|
await Navigator.push(
|
|
context,
|
|
MaterialPageRoute(builder: (_) => SessionDetailScreen(session: session)),
|
|
);
|
|
_loadSessions();
|
|
}
|
|
|
|
Future<void> _deleteSession(Session session) async {
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Supprimer'),
|
|
content: Text(
|
|
'Supprimer la session du ${DateFormat('dd/MM/yyyy').format(session.createdAt)}?',
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: const Text('Annuler'),
|
|
),
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: const Text(
|
|
'Supprimer',
|
|
style: TextStyle(color: AppTheme.errorColor),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
|
|
if (confirmed == true && mounted) {
|
|
try {
|
|
final repository = context.read<SessionRepository>();
|
|
await repository.deleteSession(session.id);
|
|
_loadSessions();
|
|
} catch (e) {
|
|
debugPrint('Erreur suppression: $e');
|
|
}
|
|
}
|
|
}
|
|
}
|