diff --git a/lib/screens/garage_screen.dart b/lib/screens/garage_screen.dart index 6089289..cfd9674 100644 --- a/lib/screens/garage_screen.dart +++ b/lib/screens/garage_screen.dart @@ -33,6 +33,8 @@ class GarageScreen extends StatefulWidget { class GarageScreenState extends State { List> _cars = []; + int _totalCarsCount = 0; + int _recentCarsCount = 0; bool _isLoading = true; bool _isLoadingMore = false; bool _hasMore = true; @@ -79,6 +81,7 @@ class GarageScreenState extends State { _hasMore = true; _selectedIds.clear(); _selectionMode = false; + _loadCollectionStats(); } if (!_hasMore && !reset) return; @@ -134,6 +137,43 @@ class GarageScreenState extends State { } } + Future _loadCollectionStats() async { + try { + final weekAgoIso = DateTime.now() + .subtract(const Duration(days: 7)) + .toUtc() + .toIso8601String(); + + final totalResult = await supabase + .from('hotwheels') + .select('id') + .eq('collection_id', widget.collectionId); + + final recentResult = await supabase + .from('hotwheels') + .select('id') + .eq('collection_id', widget.collectionId) + .gte('created_at', weekAgoIso); + + if (!mounted) return; + setState(() { + _totalCarsCount = totalResult.length; + _recentCarsCount = recentResult.length; + }); + } catch (_) { + if (!mounted) return; + setState(() { + _totalCarsCount = _cars.length; + _recentCarsCount = _cars.where((c) { + final ts = c['created_at']; + final d = ts == null ? null : DateTime.tryParse(ts.toString()); + final weekAgo = DateTime.now().subtract(const Duration(days: 7)); + return d != null && d.isAfter(weekAgo); + }).length; + }); + } + } + List> get _filteredCars { if (_searchQuery.isEmpty) return _cars; final q = _searchQuery.toLowerCase(); @@ -230,13 +270,13 @@ class GarageScreenState extends State { children: [ _StatChip( icon: Icons.directions_car, - label: '${_cars.length}', + label: '$_totalCarsCount', subtitle: 'Total Cars', ), const SizedBox(width: 12), _StatChip( icon: Icons.new_releases, - label: _recentCount(), + label: '$_recentCarsCount', subtitle: 'This Week', ), ], @@ -365,17 +405,6 @@ class GarageScreenState extends State { ); } - String _recentCount() { - final weekAgo = DateTime.now().subtract(const Duration(days: 7)); - final count = _cars.where((c) { - final ts = c['created_at']; - if (ts == null) return false; - final d = DateTime.tryParse(ts.toString()); - return d != null && d.isAfter(weekAgo); - }).length; - return '$count'; - } - void _toggleSelectionMode(bool enabled) { if (widget.isViewer && enabled) return; setState(() {