part of 'data_service.dart'; extension DataServiceBadges on DataService { Future fetchBadgeAwards({ int offset = 0, int limit = 50, bool append = false, String badgeCode = 'class_clearance', }) async { _isBadgeAwardsLoading = true; if (!append) _badgeAwards = []; try { final json = await api.get( '/badge/awards/me?limit=$limit&offset=$offset&badge_code=$badgeCode', ); List? list; if (json is List) { list = json; } else if (json is Map) { for (final key in ['awards', 'badge_awards', 'data']) { final value = json[key]; if (value is List) { list = value; break; } } } final parsed = list ?.whereType>() .map(BadgeAward.fromJson) .toList(); final items = parsed ?? []; _badgeAwards = append ? [..._badgeAwards, ...items] : items; _badgeAwards.sort((a, b) { final aTs = a.awardedAt?.millisecondsSinceEpoch ?? 0; final bTs = b.awardedAt?.millisecondsSinceEpoch ?? 0; return bTs.compareTo(aTs); }); _badgeAwardsHasMore = items.length >= limit; } catch (e) { debugPrint('Failed to fetch badge awards: $e'); if (!append) _badgeAwards = []; _badgeAwardsHasMore = false; } finally { _isBadgeAwardsLoading = false; _notifyAsync(); } } Future fetchClassClearanceProgress({ int offset = 0, int limit = 20, bool append = false, }) async { _isClassClearanceProgressLoading = true; if (!append) _classClearanceProgress = []; try { final json = await api.get('/badge/completion/class?limit=$limit&offset=$offset'); List? list; if (json is List) { list = json; } else if (json is Map) { for (final key in ['progress', 'data', 'items', 'classes']) { final value = json[key]; if (value is List) { list = value; break; } } } final parsed = list ?.whereType>() .map(ClassClearanceProgress.fromJson) .toList(); final items = parsed ?? []; _classClearanceProgress = append ? [..._classClearanceProgress, ...items] : items; _classClearanceHasMore = items.length >= limit; } catch (e) { debugPrint('Failed to fetch class clearance progress: $e'); if (!append) _classClearanceProgress = []; _classClearanceHasMore = false; } finally { _isClassClearanceProgressLoading = false; _notifyAsync(); } } Future fetchLocoClearanceProgress({ int offset = 0, int limit = 20, bool append = false, }) async { _isLocoClearanceProgressLoading = true; if (!append) _locoClearanceProgress = []; try { final json = await api.get('/badge/completion/loco?limit=$limit&offset=$offset'); List? list; if (json is List) { list = json; } else if (json is Map) { for (final key in ['progress', 'data', 'items', 'locos']) { final value = json[key]; if (value is List) { list = value; break; } } } final parsed = list ?.whereType>() .map(LocoClearanceProgress.fromJson) .toList(); final items = parsed ?? []; _locoClearanceProgress = append ? [..._locoClearanceProgress, ...items] : items; _locoClearanceHasMore = items.length >= limit; } catch (e) { debugPrint('Failed to fetch loco clearance progress: $e'); if (!append) _locoClearanceProgress = []; _locoClearanceHasMore = false; } finally { _isLocoClearanceProgressLoading = false; _notifyAsync(); } } }