fix: show full garage stats independent of pagination

This commit is contained in:
Lukas Müllner 2026-03-05 14:08:21 +01:00
parent f2c3e80f3f
commit 69362e7a96

View file

@ -33,6 +33,8 @@ class GarageScreen extends StatefulWidget {
class GarageScreenState extends State<GarageScreen> { class GarageScreenState extends State<GarageScreen> {
List<Map<String, dynamic>> _cars = []; List<Map<String, dynamic>> _cars = [];
int _totalCarsCount = 0;
int _recentCarsCount = 0;
bool _isLoading = true; bool _isLoading = true;
bool _isLoadingMore = false; bool _isLoadingMore = false;
bool _hasMore = true; bool _hasMore = true;
@ -79,6 +81,7 @@ class GarageScreenState extends State<GarageScreen> {
_hasMore = true; _hasMore = true;
_selectedIds.clear(); _selectedIds.clear();
_selectionMode = false; _selectionMode = false;
_loadCollectionStats();
} }
if (!_hasMore && !reset) return; if (!_hasMore && !reset) return;
@ -134,6 +137,43 @@ class GarageScreenState extends State<GarageScreen> {
} }
} }
Future<void> _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<Map<String, dynamic>> get _filteredCars { List<Map<String, dynamic>> get _filteredCars {
if (_searchQuery.isEmpty) return _cars; if (_searchQuery.isEmpty) return _cars;
final q = _searchQuery.toLowerCase(); final q = _searchQuery.toLowerCase();
@ -230,13 +270,13 @@ class GarageScreenState extends State<GarageScreen> {
children: [ children: [
_StatChip( _StatChip(
icon: Icons.directions_car, icon: Icons.directions_car,
label: '${_cars.length}', label: '$_totalCarsCount',
subtitle: 'Total Cars', subtitle: 'Total Cars',
), ),
const SizedBox(width: 12), const SizedBox(width: 12),
_StatChip( _StatChip(
icon: Icons.new_releases, icon: Icons.new_releases,
label: _recentCount(), label: '$_recentCarsCount',
subtitle: 'This Week', subtitle: 'This Week',
), ),
], ],
@ -365,17 +405,6 @@ class GarageScreenState extends State<GarageScreen> {
); );
} }
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) { void _toggleSelectionMode(bool enabled) {
if (widget.isViewer && enabled) return; if (widget.isViewer && enabled) return;
setState(() { setState(() {