Bug fixed

main
joschy2002 2025-07-02 10:31:35 +02:00
parent a235c2ee6d
commit d7808773e2
1 changed files with 75 additions and 34 deletions

View File

@ -31,6 +31,7 @@ class _HomeScreenState extends State<HomeScreen> {
List<Map<String, dynamic>> _suggestions = [];
bool _isLoadingSuggestions = true;
String? _suggestionsError;
String? _userRole;
@override
void initState() {
@ -116,11 +117,11 @@ class _HomeScreenState extends State<HomeScreen> {
if (!userDoc.exists) return;
final userData = userDoc.data() as Map<String, dynamic>;
final userRole = userData['role'] as String?;
_userRole = userData['role'] as String?;
final userClub = userData['club'] as String?;
QuerySnapshot trainersSnapshot;
if (userRole == 'trainer') {
if (_userRole == 'trainer') {
// Trainer sees only their own trainings
trainersSnapshot = await FirebaseFirestore.instance
.collection('User')
@ -153,45 +154,84 @@ class _HomeScreenState extends State<HomeScreen> {
final trainingTimes = trainerData['trainingTimes'] as Map<String, dynamic>? ?? {};
final trainingDurations = trainerData['trainingDurations'] as Map<String, dynamic>? ?? {};
final cancelledTrainings = trainerData['cancelledTrainings'] as List<dynamic>? ?? [];
final trainings = trainerData['trainings'] as Map<String, dynamic>? ?? {};
trainingTimes.forEach((day, timeStr) {
if (timeStr == null) return;
// First, check individual trainings (manually added)
trainings.forEach((dateString, trainingsList) {
final date = DateTime.tryParse(dateString);
if (date == null) return;
final timeParts = (timeStr as String).split(':');
if (timeParts.length != 2) return;
final hour = int.tryParse(timeParts[0]) ?? 0;
final minute = int.tryParse(timeParts[1]) ?? 0;
// Only consider future trainings
if (date.isBefore(now)) return;
final daysUntilNext = _getDaysUntilNext(day, now.weekday);
final eventDate = DateTime(now.year, now.month, now.day + daysUntilNext, hour, minute);
// Check the next 4 weeks
for (var i = 0; i < 4; i++) {
final date = eventDate.add(Duration(days: i * 7));
final normalizedDate = DateTime(date.year, date.month, date.day);
final dateString = normalizedDate.toIso8601String();
final list = List<Map<String, dynamic>>.from(trainingsList);
for (final training in list) {
final trainingTime = training['time'] as String? ?? '19:00';
final trainingDuration = training['duration'] as int? ?? 60;
final isCancelled = cancelledTrainings.any((cancelled) {
if (cancelled is Map<String, dynamic>) {
final cancelledDate = DateTime.parse(cancelled['date'] as String);
return isSameDay(cancelledDate, normalizedDate);
}
return false;
});
if (!isCancelled && (nextTrainingDate == null || date.isBefore(nextTrainingDate!))) {
nextTrainingDate = date;
final timeParts = trainingTime.split(':');
if (timeParts.length != 2) continue;
final hour = int.tryParse(timeParts[0]) ?? 0;
final minute = int.tryParse(timeParts[1]) ?? 0;
final trainingDateTime = DateTime(date.year, date.month, date.day, hour, minute);
if (trainingDateTime.isAfter(now) && (nextTrainingDate == null || trainingDateTime.isBefore(nextTrainingDate!))) {
nextTrainingDate = trainingDateTime;
nextTraining = {
'date': dateString,
'time': timeStr,
'duration': trainingDurations[day] ?? 60,
'time': trainingTime,
'duration': trainingDuration,
'trainerName': trainerData['name'] ?? 'Unknown Trainer',
'day': day,
'day': DateFormat('EEEE', 'de_DE').format(date),
};
}
}
});
// Then check regular training times (only if no individual training was found)
if (nextTrainingDate == null) {
trainingTimes.forEach((day, timeStr) {
if (timeStr == null) return;
final timeParts = (timeStr as String).split(':');
if (timeParts.length != 2) return;
final hour = int.tryParse(timeParts[0]) ?? 0;
final minute = int.tryParse(timeParts[1]) ?? 0;
final daysUntilNext = _getDaysUntilNext(day, now.weekday);
final eventDate = DateTime(now.year, now.month, now.day + daysUntilNext, hour, minute);
// Check the next 4 weeks
for (var i = 0; i < 4; i++) {
final date = eventDate.add(Duration(days: i * 7));
final normalizedDate = DateTime(date.year, date.month, date.day);
final dateString = normalizedDate.toIso8601String();
final isCancelled = cancelledTrainings.any((cancelled) {
if (cancelled is Map<String, dynamic>) {
final cancelledDate = DateTime.parse(cancelled['date'] as String);
return isSameDay(cancelledDate, normalizedDate);
}
return false;
});
if (!isCancelled) {
if (nextTrainingDate == null || date.isBefore(nextTrainingDate!)) {
nextTrainingDate = date;
nextTraining = {
'date': dateString,
'time': timeStr,
'duration': trainingDurations[day] ?? 60,
'trainerName': trainerData['name'] ?? 'Unknown Trainer',
'day': day,
};
}
}
}
});
}
}
setState(() {
@ -237,9 +277,10 @@ class _HomeScreenState extends State<HomeScreen> {
setState(() {
_selectedIndex = index;
});
// Reload suggestions when switching to the Home tab
// Reload suggestions and next training when switching to the Home tab
if (index == 0) {
_loadSuggestions();
_loadNextTraining();
}
}
}
@ -290,10 +331,10 @@ class _HomeScreenState extends State<HomeScreen> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const SizedBox(height: 16),
const Center(
Center(
child: Text(
'Hallo Trainer!',
style: TextStyle(
_userRole == 'trainer' ? 'Hallo Trainer!' : 'Hallo Spieler!',
style: const TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Color(0xFFE57399),