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