-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbeer_provider.dart
More file actions
550 lines (482 loc) · 17.2 KB
/
Copy pathbeer_provider.dart
File metadata and controls
550 lines (482 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';
import '../models/models.dart';
import '../services/services.dart';
import '../domain/services/services.dart';
import '../domain/repositories/repositories.dart';
import '../domain/models/models.dart';
/// Provider for managing beer festival data and state
class BeerProvider extends ChangeNotifier {
final AnalyticsService _analyticsService;
final DrinkFilterService _filterService;
final DrinkSortService _sortService;
DrinkRepository? _drinkRepository;
FestivalRepository? _festivalRepository;
List<Drink> _allDrinks = [];
List<Drink> _filteredDrinks = [];
List<Festival> _festivals = [];
Festival? _currentFestival;
bool _isLoading = false;
bool _isFestivalsLoading = false;
bool _isInitialized = false;
String? _error;
String? _festivalsError;
String? _selectedCategory;
Set<String> _selectedStyles = {};
DrinkSort _currentSort = DrinkSort.nameAsc;
String _searchQuery = '';
bool _showFavoritesOnly = false;
bool _hideUnavailable = false;
ThemeMode _themeMode = ThemeMode.system;
// Timestamp tracking for automatic refresh
DateTime? _lastDrinksRefresh;
DateTime? _lastFestivalsRefresh;
// Staleness thresholds
static const Duration _drinksStalenessThreshold = Duration(hours: 1);
static const Duration _festivalsStalenessThreshold = Duration(hours: 24);
BeerProvider({
AnalyticsService? analyticsService,
DrinkFilterService? filterService,
DrinkSortService? sortService,
DrinkRepository? drinkRepository,
FestivalRepository? festivalRepository,
}) : _analyticsService = analyticsService ?? AnalyticsService(),
_filterService = filterService ?? DrinkFilterService(),
_sortService = sortService ?? DrinkSortService(),
_drinkRepository = drinkRepository,
_festivalRepository = festivalRepository;
// Getters
List<Drink> get drinks => _filteredDrinks;
List<Drink> get allDrinks => _allDrinks;
List<Festival> get festivals => _festivals;
/// Get festivals sorted by date (live/upcoming first, then past in reverse chronological order)
List<Festival> get sortedFestivals => Festival.sortByDate(_festivals);
Festival get currentFestival => _currentFestival ?? DefaultFestivals.cambridge2025;
bool get isLoading => _isLoading;
bool get isFestivalsLoading => _isFestivalsLoading;
bool get isInitialized => _isInitialized;
String? get error => _error;
String? get festivalsError => _festivalsError;
String? get selectedCategory => _selectedCategory;
Set<String> get selectedStyles => _selectedStyles;
DrinkSort get currentSort => _currentSort;
String get searchQuery => _searchQuery;
bool get showFavoritesOnly => _showFavoritesOnly;
bool get hideUnavailable => _hideUnavailable;
bool get hasFestivals => _festivals.isNotEmpty;
ThemeMode get themeMode => _themeMode;
DateTime? get lastDrinksRefresh => _lastDrinksRefresh;
AnalyticsService get analyticsService => _analyticsService;
/// Get unique categories from loaded drinks
List<String> get availableCategories {
final categories = _allDrinks.map((d) => d.category).toSet().toList();
categories.sort();
return categories;
}
/// Get unique styles from loaded drinks (filtered by category if selected)
List<String> get availableStyles {
var drinks = _allDrinks;
// If a category is selected, only show styles from that category
if (_selectedCategory != null) {
drinks = drinks.where((d) => d.category == _selectedCategory).toList();
}
final styles = drinks
.where((d) => d.style != null && d.style!.isNotEmpty)
.map((d) => d.style!)
.toSet()
.toList();
styles.sort();
return styles;
}
/// Get drink count by category
Map<String, int> get categoryCountsMap {
final counts = <String, int>{};
for (final drink in _allDrinks) {
counts[drink.category] = (counts[drink.category] ?? 0) + 1;
}
return counts;
}
/// Get drink count by style
Map<String, int> get styleCountsMap {
var drinks = _allDrinks;
// If a category is selected, only count styles from that category
if (_selectedCategory != null) {
drinks = drinks.where((d) => d.category == _selectedCategory).toList();
}
final counts = <String, int>{};
for (final drink in drinks) {
if (drink.style != null && drink.style!.isNotEmpty) {
counts[drink.style!] = (counts[drink.style!] ?? 0) + 1;
}
}
return counts;
}
/// Get favorite drinks
List<Drink> get favoriteDrinks =>
_allDrinks.where((d) => d.isFavorite).toList();
/// Check if a festival ID is valid (exists in the registry)
bool isValidFestivalId(String? festivalId) {
if (festivalId == null || festivalId.isEmpty) return false;
return _festivals.any((f) => f.id == festivalId);
}
/// Get a festival by ID, or null if not found
Festival? getFestivalById(String festivalId) {
try {
return _festivals.firstWhere((f) => f.id == festivalId);
} catch (e) {
return null;
}
}
/// Check if drinks data is stale and should be refreshed
bool get isDrinksDataStale {
if (_lastDrinksRefresh == null) return true;
return DateTime.now().difference(_lastDrinksRefresh!) > _drinksStalenessThreshold;
}
/// Check if festivals data is stale and should be refreshed
bool get isFestivalsDataStale {
if (_lastFestivalsRefresh == null) return true;
return DateTime.now().difference(_lastFestivalsRefresh!) > _festivalsStalenessThreshold;
}
/// Initialize with SharedPreferences and load festivals
Future<void> initialize() async {
final prefs = await SharedPreferences.getInstance();
// Create repositories if not provided
if (_drinkRepository == null) {
final favoritesService = FavoritesService(prefs);
final ratingsService = RatingsService(prefs);
final tastingLogService = TastingLogService(prefs);
final apiService = BeerApiService();
_drinkRepository = ApiDrinkRepository(
apiService: apiService,
favoritesService: favoritesService,
ratingsService: ratingsService,
tastingLogService: tastingLogService,
);
}
if (_festivalRepository == null) {
final festivalService = FestivalService();
final festivalStorageService = FestivalStorageService(prefs);
_festivalRepository = ApiFestivalRepository(
festivalService: festivalService,
festivalStorageService: festivalStorageService,
);
}
// Load theme mode preference
final themeIndex = prefs.getInt('themeMode') ?? ThemeMode.system.index;
_themeMode = ThemeMode.values[themeIndex];
// Load hide unavailable preference
_hideUnavailable = prefs.getBool('hideUnavailable') ?? false;
// Load festivals dynamically
await loadFestivals();
// Restore previously selected festival if available
final savedFestivalId = await _festivalRepository!.getSelectedFestivalId();
if (savedFestivalId != null) {
final savedFestival = _festivals.where((f) => f.id == savedFestivalId).firstOrNull;
if (savedFestival != null) {
_currentFestival = savedFestival;
}
}
_isInitialized = true;
notifyListeners();
}
/// Load festivals from the API
Future<void> loadFestivals() async {
_isFestivalsLoading = true;
_festivalsError = null;
notifyListeners();
try {
final response = await _festivalRepository!.getFestivals();
_festivals = response.festivals;
// Set default festival if not already set
if (_currentFestival == null && response.defaultFestival != null) {
_currentFestival = response.defaultFestival;
}
_festivalsError = null;
_lastFestivalsRefresh = DateTime.now();
} catch (e) {
_festivalsError = _getUserFriendlyErrorMessage(e);
// Don't fall back to hardcoded festivals - show error to user
_festivals = [];
} finally {
_isFestivalsLoading = false;
notifyListeners();
}
}
/// Load drinks from the current festival
Future<void> loadDrinks() async {
if (_currentFestival == null) {
// Wait for festivals to be loaded first
if (_festivals.isEmpty) {
await loadFestivals();
}
_currentFestival ??= DefaultFestivals.cambridge2025;
}
_isLoading = true;
_error = null;
notifyListeners();
try {
// Repository returns drinks with favorites and ratings already populated
_allDrinks = await _drinkRepository!.getDrinks(currentFestival);
_applyFiltersAndSort();
_error = null;
_lastDrinksRefresh = DateTime.now();
} catch (e, stackTrace) {
_error = _getUserFriendlyErrorMessage(e);
_allDrinks = [];
_filteredDrinks = [];
// Log error to Crashlytics
await _analyticsService.logError(
e,
stackTrace,
reason: 'Failed to load drinks for festival: ${currentFestival.id}',
);
} finally {
_isLoading = false;
notifyListeners();
}
}
/// Change the current festival (drinks loaded lazily on demand)
///
/// If [persist] is true (default), saves the festival selection to local storage.
/// Set to false for temporary festival viewing (e.g., deep links to old festivals).
Future<void> setFestival(Festival festival, {bool persist = true}) async {
if (_currentFestival?.id == festival.id) return;
_currentFestival = festival;
_selectedCategory = null;
_selectedStyles = {};
_searchQuery = '';
// Clear existing drinks and show loading state immediately
_allDrinks = [];
_filteredDrinks = [];
_isLoading = true;
_error = null;
notifyListeners();
// Log analytics event
await _analyticsService.logFestivalSelected(festival);
// Persist festival selection only if requested
if (persist) {
await _festivalRepository?.setSelectedFestivalId(festival.id);
}
// Load drinks for the new festival (loadDrinks will call notifyListeners when done)
await _loadDrinksInternal();
}
/// Internal method to load drinks without setting initial loading state
Future<void> _loadDrinksInternal() async {
try {
// Repository returns drinks with favorites and ratings already populated
_allDrinks = await _drinkRepository!.getDrinks(currentFestival);
_applyFiltersAndSort();
_error = null;
_lastDrinksRefresh = DateTime.now();
} catch (e, stackTrace) {
_error = _getUserFriendlyErrorMessage(e);
_allDrinks = [];
_filteredDrinks = [];
// Log error to Crashlytics
await _analyticsService.logError(
e,
stackTrace,
reason: 'Failed to load drinks internally for festival: ${currentFestival.id}',
);
} finally {
_isLoading = false;
notifyListeners();
}
}
/// Refresh data if it's stale (called when app resumes from background)
Future<void> refreshIfStale() async {
// Don't refresh if already loading
if (_isLoading || _isFestivalsLoading) return;
// Refresh festivals if stale
if (isFestivalsDataStale) {
await loadFestivals();
}
// Refresh drinks if stale
if (isDrinksDataStale) {
await loadDrinks();
}
}
/// Convert exceptions to user-friendly error messages
String _getUserFriendlyErrorMessage(Object error) {
if (error is BeerApiException) {
if (error.statusCode == 404) {
return 'Festival data not found. Please try a different festival.';
} else if (error.statusCode != null && error.statusCode! >= 500) {
return 'Server error. Please try again later.';
} else if (error.statusCode != null && error.statusCode! >= 400) {
return 'Could not load drinks. Please try again.';
} else {
return 'Could not load drinks. Please check your connection.';
}
} else if (error is FestivalServiceException) {
if (error.statusCode == 404) {
return 'Festival list not found. Please try again later.';
} else if (error.statusCode != null && error.statusCode! >= 500) {
return 'Server error. Please try again later.';
} else {
return 'Could not load festivals. Please check your connection.';
}
} else if (error is http.ClientException) {
return 'No internet connection. Please check your network.';
} else if (error is TimeoutException) {
return 'Request timed out. Please check your connection and try again.';
} else {
return 'Something went wrong. Please try again.';
}
}
/// Set category filter
void setCategory(String? category) {
_selectedCategory = category;
// Clear style filter when changing category since styles are category-dependent
if (_selectedStyles.isNotEmpty) {
_selectedStyles = {};
}
_applyFiltersAndSort();
notifyListeners();
// Log analytics event (fire and forget)
unawaited(_analyticsService.logCategoryFilter(category));
}
/// Toggle a style filter (supports multiple style selection)
void toggleStyle(String style) {
if (_selectedStyles.contains(style)) {
_selectedStyles = Set.from(_selectedStyles)..remove(style);
} else {
_selectedStyles = Set.from(_selectedStyles)..add(style);
}
_applyFiltersAndSort();
notifyListeners();
// Log analytics event (fire and forget)
unawaited(_analyticsService.logStyleFilter(_selectedStyles));
}
/// Clear all style filters
void clearStyles() {
_selectedStyles = {};
_applyFiltersAndSort();
notifyListeners();
}
/// Set sort option
void setSort(DrinkSort sort) {
_currentSort = sort;
_applyFiltersAndSort();
notifyListeners();
// Log analytics event (fire and forget)
unawaited(_analyticsService.logSortChange(sort.name));
}
/// Set search query
void setSearchQuery(String query) {
_searchQuery = query.toLowerCase();
_applyFiltersAndSort();
notifyListeners();
// Log analytics event if query is not empty (fire and forget)
if (query.trim().isNotEmpty) {
unawaited(_analyticsService.logSearch(query));
}
}
/// Toggle showing favorites only
void setShowFavoritesOnly(bool value) {
_showFavoritesOnly = value;
_applyFiltersAndSort();
notifyListeners();
}
/// Toggle hiding unavailable drinks and persist preference
Future<void> setHideUnavailable(bool value) async {
_hideUnavailable = value;
_applyFiltersAndSort();
notifyListeners();
// Persist the preference
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('hideUnavailable', value);
}
/// Set theme mode and persist preference
Future<void> setThemeMode(ThemeMode mode) async {
_themeMode = mode;
notifyListeners();
// Persist the preference
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('themeMode', mode.index);
}
/// Toggle favorite status for a drink
Future<void> toggleFavorite(Drink drink) async {
if (_drinkRepository == null) return;
final newStatus = await _drinkRepository!.toggleFavorite(
currentFestival.id,
drink.id,
);
drink.isFavorite = newStatus;
if (_showFavoritesOnly) {
_applyFiltersAndSort();
}
notifyListeners();
// Log analytics event (fire and forget)
if (newStatus) {
unawaited(_analyticsService.logFavoriteAdded(drink));
} else {
unawaited(_analyticsService.logFavoriteRemoved(drink));
}
}
/// Set rating for a drink (1-5), or clear it with null
Future<void> setRating(Drink drink, int? rating) async {
if (_drinkRepository == null) return;
if (rating == null) {
await _drinkRepository!.removeRating(
currentFestival.id,
drink.id,
);
drink.rating = null;
} else {
await _drinkRepository!.setRating(
currentFestival.id,
drink.id,
rating,
);
drink.rating = rating;
// Log analytics event for rating (fire and forget)
unawaited(_analyticsService.logRatingGiven(drink, rating));
}
notifyListeners();
}
/// Toggle tasted status for a drink
Future<void> toggleTasted(Drink drink) async {
if (_drinkRepository == null) return;
final newStatus = await _drinkRepository!.toggleTasted(
currentFestival.id,
drink.id,
);
drink.isTasted = newStatus;
notifyListeners();
// Log analytics event
if (newStatus) {
unawaited(analyticsService.logTastedAdded(drink));
} else {
unawaited(analyticsService.logTastedRemoved(drink));
}
}
/// Get a drink by ID
Drink? getDrinkById(String id) {
try {
return _allDrinks.firstWhere((d) => d.id == id);
} catch (e) {
return null;
}
}
void _applyFiltersAndSort() {
// Apply all filters using domain service (returns new list)
final filtered = _filterService.filterDrinks(
_allDrinks,
category: _selectedCategory,
styles: _selectedStyles,
favoritesOnly: _showFavoritesOnly,
hideUnavailable: _hideUnavailable,
searchQuery: _searchQuery,
);
// Apply sort using domain service (returns new sorted list)
_filteredDrinks = _sortService.sortDrinks(filtered, _currentSort);
}
@override
void dispose() {
// Note: Repositories own their services and manage lifecycle
super.dispose();
}
}