Vorschläge added
parent
0923b85a20
commit
e6293adbc7
|
|
@ -77,12 +77,37 @@ class _FavoritesTabState extends State<FavoritesTab> {
|
||||||
return const Center(child: Text('Keine Favoriten gefunden'));
|
return const Center(child: Text('Keine Favoriten gefunden'));
|
||||||
}
|
}
|
||||||
final data = snapshot.data!.data() as Map<String, dynamic>;
|
final data = snapshot.data!.data() as Map<String, dynamic>;
|
||||||
final favorites = List<String>.from(data['favorites'] ?? []);
|
final allFavorites = List<String>.from(data['favorites'] ?? []);
|
||||||
|
|
||||||
if (favorites.isEmpty) {
|
if (allFavorites.isEmpty) {
|
||||||
return const Center(child: Text('Keine Favoriten gefunden'));
|
return const Center(child: Text('Keine Favoriten gefunden'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Lade alle Favoriten-Dokumente auf einmal
|
||||||
|
return FutureBuilder<List<DocumentSnapshot>>(
|
||||||
|
future: Future.wait(allFavorites.map((id) =>
|
||||||
|
FirebaseFirestore.instance.collection('Training').doc(id).get()
|
||||||
|
)),
|
||||||
|
builder: (context, multiSnapshot) {
|
||||||
|
if (multiSnapshot.connectionState == ConnectionState.waiting) {
|
||||||
|
return const Center(child: CircularProgressIndicator());
|
||||||
|
}
|
||||||
|
if (!multiSnapshot.hasData) {
|
||||||
|
return const Center(child: Text('Favoriten konnten nicht geladen werden'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Filtere die Favoriten basierend auf der Kategorie
|
||||||
|
final filteredFavorites = multiSnapshot.data!.where((doc) {
|
||||||
|
if (!doc.exists) return false;
|
||||||
|
if (_selectedCategory == null) return true;
|
||||||
|
final trainingData = doc.data() as Map<String, dynamic>;
|
||||||
|
return trainingData['category'] == _selectedCategory;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
if (filteredFavorites.isEmpty) {
|
||||||
|
return const Center(child: Text('Keine Favoriten in dieser Kategorie gefunden'));
|
||||||
|
}
|
||||||
|
|
||||||
return GridView.builder(
|
return GridView.builder(
|
||||||
padding: const EdgeInsets.all(8),
|
padding: const EdgeInsets.all(8),
|
||||||
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
gridDelegate: const SliverGridDelegateWithMaxCrossAxisExtent(
|
||||||
|
|
@ -91,19 +116,11 @@ class _FavoritesTabState extends State<FavoritesTab> {
|
||||||
crossAxisSpacing: 10,
|
crossAxisSpacing: 10,
|
||||||
mainAxisSpacing: 10,
|
mainAxisSpacing: 10,
|
||||||
),
|
),
|
||||||
itemCount: favorites.length,
|
itemCount: filteredFavorites.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
return FutureBuilder<DocumentSnapshot>(
|
final doc = filteredFavorites[index];
|
||||||
future: FirebaseFirestore.instance.collection('Training').doc(favorites[index]).get(),
|
final trainingData = doc.data() as Map<String, dynamic>;
|
||||||
builder: (context, trainingSnapshot) {
|
|
||||||
if (!trainingSnapshot.hasData || !trainingSnapshot.data!.exists) {
|
|
||||||
return const SizedBox.shrink();
|
|
||||||
}
|
|
||||||
final trainingData = trainingSnapshot.data!.data() as Map<String, dynamic>;
|
|
||||||
// Filter nach Kategorie, falls gesetzt
|
|
||||||
if (_selectedCategory != null && trainingData['category'] != _selectedCategory) {
|
|
||||||
return const SizedBox.shrink();
|
|
||||||
}
|
|
||||||
return Card(
|
return Card(
|
||||||
child: Stack(
|
child: Stack(
|
||||||
children: [
|
children: [
|
||||||
|
|
@ -112,7 +129,7 @@ class _FavoritesTabState extends State<FavoritesTab> {
|
||||||
Navigator.push(
|
Navigator.push(
|
||||||
context,
|
context,
|
||||||
MaterialPageRoute(
|
MaterialPageRoute(
|
||||||
builder: (context) => TrainingDetailScreen(trainingId: favorites[index]),
|
builder: (context) => TrainingDetailScreen(trainingId: doc.id),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
@ -170,7 +187,7 @@ class _FavoritesTabState extends State<FavoritesTab> {
|
||||||
icon: const Icon(Icons.favorite, color: Colors.red),
|
icon: const Icon(Icons.favorite, color: Colors.red),
|
||||||
onPressed: () async {
|
onPressed: () async {
|
||||||
await FirebaseFirestore.instance.collection('User').doc(user.uid).update({
|
await FirebaseFirestore.instance.collection('User').doc(user.uid).update({
|
||||||
'favorites': FieldValue.arrayRemove([favorites[index]]),
|
'favorites': FieldValue.arrayRemove([doc.id]),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import 'search_tab.dart';
|
||||||
import 'favorites_tab.dart';
|
import 'favorites_tab.dart';
|
||||||
import 'calendar_tab.dart';
|
import 'calendar_tab.dart';
|
||||||
import 'profile_tab.dart';
|
import 'profile_tab.dart';
|
||||||
|
import 'training_detail_screen.dart';
|
||||||
|
|
||||||
class HomeScreen extends StatefulWidget {
|
class HomeScreen extends StatefulWidget {
|
||||||
final VoidCallback? onLogoutSuccess;
|
final VoidCallback? onLogoutSuccess;
|
||||||
|
|
@ -23,11 +24,76 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
bool _isLoading = true;
|
bool _isLoading = true;
|
||||||
DateTime? _calendarInitialDate;
|
DateTime? _calendarInitialDate;
|
||||||
String? _favoriteCategoryFilter;
|
String? _favoriteCategoryFilter;
|
||||||
|
List<Map<String, dynamic>> _suggestions = [];
|
||||||
|
bool _isLoadingSuggestions = true;
|
||||||
|
String? _suggestionsError;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_loadNextTraining();
|
_loadNextTraining();
|
||||||
|
_loadSuggestions();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _loadSuggestions() async {
|
||||||
|
setState(() {
|
||||||
|
_isLoadingSuggestions = true;
|
||||||
|
_suggestionsError = null;
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
final user = FirebaseAuth.instance.currentUser;
|
||||||
|
if (user == null) {
|
||||||
|
setState(() => _isLoadingSuggestions = false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. Hole alle vom User genutzten Übungs-IDs
|
||||||
|
final userDoc = await FirebaseFirestore.instance.collection('User').doc(user.uid).get();
|
||||||
|
final Set<String> usedExerciseIds = {};
|
||||||
|
if (userDoc.exists) {
|
||||||
|
final userData = userDoc.data() as Map<String, dynamic>;
|
||||||
|
final trainings = Map<String, dynamic>.from(userData['trainings'] ?? {});
|
||||||
|
trainings.forEach((date, trainingsList) {
|
||||||
|
final list = List<Map<String, dynamic>>.from(trainingsList);
|
||||||
|
for (final training in list) {
|
||||||
|
final exercises = List<Map<String, dynamic>>.from(training['exercises'] ?? []);
|
||||||
|
for (final exercise in exercises) {
|
||||||
|
if (exercise.containsKey('id')) {
|
||||||
|
usedExerciseIds.add(exercise['id']);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Hole alle Übungen
|
||||||
|
final exercisesSnapshot = await FirebaseFirestore.instance.collection('Training').get();
|
||||||
|
|
||||||
|
final allExercises = exercisesSnapshot.docs.map((doc) {
|
||||||
|
return {'id': doc.id, ...doc.data() as Map<String, dynamic>};
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
// 3. Filtere nach "nicht genutzt" und "gut bewertet"
|
||||||
|
final suggestions = allExercises.where((exercise) {
|
||||||
|
final isUsed = usedExerciseIds.contains(exercise['id']);
|
||||||
|
final rating = (exercise['rating overall'] as num?)?.toDouble() ?? 0.0;
|
||||||
|
return !isUsed && rating >= 4.0;
|
||||||
|
}).toList();
|
||||||
|
|
||||||
|
suggestions.shuffle();
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_suggestions = suggestions.take(5).toList();
|
||||||
|
_isLoadingSuggestions = false;
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (e) {
|
||||||
|
setState(() {
|
||||||
|
_isLoadingSuggestions = false;
|
||||||
|
_suggestionsError = 'Fehler beim Laden.';
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _loadNextTraining() async {
|
Future<void> _loadNextTraining() async {
|
||||||
|
|
@ -158,9 +224,15 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _onItemTapped(int index) {
|
void _onItemTapped(int index) {
|
||||||
|
if (_selectedIndex != index) {
|
||||||
setState(() {
|
setState(() {
|
||||||
_selectedIndex = index;
|
_selectedIndex = index;
|
||||||
});
|
});
|
||||||
|
// Wenn zum Home-Tab gewechselt wird, lade die Vorschläge neu
|
||||||
|
if (index == 0) {
|
||||||
|
_loadSuggestions();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Widget> get _screens => [
|
List<Widget> get _screens => [
|
||||||
|
|
@ -326,19 +398,36 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
icon: const Icon(Icons.chevron_right),
|
icon: const Icon(Icons.chevron_right),
|
||||||
onPressed: () {},
|
onPressed: () {
|
||||||
|
setState(() => _selectedIndex = 1);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
if (_isLoadingSuggestions)
|
||||||
|
const SizedBox(
|
||||||
|
height: 170,
|
||||||
|
child: Center(child: CircularProgressIndicator()),
|
||||||
|
)
|
||||||
|
else if (_suggestionsError != null)
|
||||||
SizedBox(
|
SizedBox(
|
||||||
height: 170,
|
height: 170,
|
||||||
child: ListView(
|
child: Center(child: Text(_suggestionsError!)),
|
||||||
|
)
|
||||||
|
else if (_suggestions.isEmpty)
|
||||||
|
const SizedBox(
|
||||||
|
height: 170,
|
||||||
|
child: Center(child: Text('Keine neuen Vorschläge gefunden.')),
|
||||||
|
)
|
||||||
|
else
|
||||||
|
SizedBox(
|
||||||
|
height: 170,
|
||||||
|
child: ListView.builder(
|
||||||
scrollDirection: Axis.horizontal,
|
scrollDirection: Axis.horizontal,
|
||||||
children: [
|
itemCount: _suggestions.length,
|
||||||
_buildSuggestionCard('Wurf- & Torabschluss', 'Sprungwurf', Icons.sports_handball, categoryColors['Wurf- & Torabschluss']!),
|
itemBuilder: (context, index) {
|
||||||
_buildSuggestionCard('Pass', 'Doppelpass', Icons.compare_arrows, categoryColors['Pass']!),
|
return _buildSuggestionCard(_suggestions[index]);
|
||||||
_buildSuggestionCard('Koordination', 'Leiterlauf', Icons.directions_walk, categoryColors['Koordination']!),
|
},
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
@ -381,48 +470,82 @@ class _HomeScreenState extends State<HomeScreen> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildSuggestionCard(String category, String title, IconData icon, Color color) {
|
Widget _buildSuggestionCard(Map<String, dynamic> exercise) {
|
||||||
return Container(
|
final category = exercise['category'] as String? ?? 'Sonstiges';
|
||||||
|
final title = exercise['title'] as String? ?? 'Unbekannte Übung';
|
||||||
|
final id = exercise['id'] as String;
|
||||||
|
|
||||||
|
final color = categoryColors[category] ?? Colors.grey;
|
||||||
|
final icon = categoryIcons[category] ?? Icons.help_outline;
|
||||||
|
|
||||||
|
return GestureDetector(
|
||||||
|
onTap: () {
|
||||||
|
Navigator.push(
|
||||||
|
context,
|
||||||
|
MaterialPageRoute(
|
||||||
|
builder: (context) => TrainingDetailScreen(trainingId: id),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: Container(
|
||||||
width: 130,
|
width: 130,
|
||||||
margin: const EdgeInsets.only(right: 16),
|
margin: const EdgeInsets.only(right: 16),
|
||||||
child: Card(
|
child: Card(
|
||||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
|
||||||
elevation: 3,
|
elevation: 3,
|
||||||
|
clipBehavior: Clip.antiAlias,
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Container(
|
Container(
|
||||||
height: 80,
|
height: 80,
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: color.withOpacity(0.15),
|
color: color.withOpacity(0.15),
|
||||||
borderRadius: const BorderRadius.only(
|
|
||||||
topLeft: Radius.circular(16),
|
|
||||||
topRight: Radius.circular(16),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
child: Center(
|
child: Center(
|
||||||
child: Icon(icon, color: color, size: 40),
|
child: Icon(icon, color: color, size: 40),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Padding(
|
Expanded(
|
||||||
|
child: Padding(
|
||||||
padding: const EdgeInsets.all(8.0),
|
padding: const EdgeInsets.all(8.0),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
Text(category, style: const TextStyle(fontSize: 12, color: Colors.grey)),
|
Text(
|
||||||
|
category,
|
||||||
|
style: const TextStyle(fontSize: 12, color: Colors.grey),
|
||||||
|
maxLines: 1,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
const SizedBox(height: 4),
|
const SizedBox(height: 4),
|
||||||
Text(title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 14)),
|
Text(
|
||||||
|
title,
|
||||||
|
style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 13),
|
||||||
|
maxLines: 2,
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Map<String, IconData> categoryIcons = {
|
||||||
|
'Aufwärmen & Mobilisation': Icons.directions_run,
|
||||||
|
'Wurf- & Torabschluss': Icons.sports_handball,
|
||||||
|
'Torwarttraining': Icons.sports,
|
||||||
|
'Athletik': Icons.fitness_center,
|
||||||
|
'Pass': Icons.compare_arrows,
|
||||||
|
'Koordination': Icons.directions_walk,
|
||||||
|
};
|
||||||
|
|
||||||
const Map<String, Color> categoryColors = {
|
const Map<String, Color> categoryColors = {
|
||||||
'Aufwärmen & Mobilisation': Colors.deepOrange,
|
'Aufwärmen & Mobilisation': Colors.deepOrange,
|
||||||
'Wurf- & Torabschluss': Colors.orange,
|
'Wurf- & Torabschluss': Colors.orange,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue