Files
mileograph_flutter/lib/services/data_service/data_service_badges.dart
2025-12-26 22:49:43 +00:00

131 lines
3.8 KiB
Dart

part of 'data_service.dart';
extension DataServiceBadges on DataService {
Future<void> 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<dynamic>? 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<String, dynamic>>()
.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<void> 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<dynamic>? 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<String, dynamic>>()
.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<void> 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<dynamic>? 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<String, dynamic>>()
.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();
}
}
}