badge percentage support
This commit is contained in:
@@ -1,10 +1,18 @@
|
||||
part of 'data_service.dart';
|
||||
|
||||
extension DataServiceBadges on DataService {
|
||||
Future<void> fetchBadgeAwards() async {
|
||||
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');
|
||||
final json = await api.get(
|
||||
'/badge/awards/me?limit=$limit&offset=$offset&badge_code=$badgeCode',
|
||||
);
|
||||
List<dynamic>? list;
|
||||
if (json is List) {
|
||||
list = json;
|
||||
@@ -21,22 +29,102 @@ extension DataServiceBadges on DataService {
|
||||
?.whereType<Map<String, dynamic>>()
|
||||
.map(BadgeAward.fromJson)
|
||||
.toList();
|
||||
if (parsed != null) {
|
||||
parsed.sort((a, b) {
|
||||
final aTs = a.awardedAt?.millisecondsSinceEpoch ?? 0;
|
||||
final bTs = b.awardedAt?.millisecondsSinceEpoch ?? 0;
|
||||
return bTs.compareTo(aTs);
|
||||
});
|
||||
_badgeAwards = parsed;
|
||||
} else {
|
||||
_badgeAwards = [];
|
||||
}
|
||||
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');
|
||||
_badgeAwards = [];
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,8 @@ class DataService extends ChangeNotifier {
|
||||
|
||||
DataService({required this.api});
|
||||
|
||||
String? _currentUserId;
|
||||
|
||||
_LegFetchOptions _lastLegsFetch = const _LegFetchOptions();
|
||||
|
||||
// Homepage Data
|
||||
@@ -102,6 +104,24 @@ class DataService extends ChangeNotifier {
|
||||
List<BadgeAward> get badgeAwards => _badgeAwards;
|
||||
bool _isBadgeAwardsLoading = false;
|
||||
bool get isBadgeAwardsLoading => _isBadgeAwardsLoading;
|
||||
bool _badgeAwardsHasMore = false;
|
||||
bool get badgeAwardsHasMore => _badgeAwardsHasMore;
|
||||
List<ClassClearanceProgress> _classClearanceProgress = [];
|
||||
List<ClassClearanceProgress> get classClearanceProgress =>
|
||||
_classClearanceProgress;
|
||||
bool _isClassClearanceProgressLoading = false;
|
||||
bool get isClassClearanceProgressLoading =>
|
||||
_isClassClearanceProgressLoading;
|
||||
bool _classClearanceHasMore = false;
|
||||
bool get classClearanceHasMore => _classClearanceHasMore;
|
||||
List<LocoClearanceProgress> _locoClearanceProgress = [];
|
||||
List<LocoClearanceProgress> get locoClearanceProgress =>
|
||||
_locoClearanceProgress;
|
||||
bool _isLocoClearanceProgressLoading = false;
|
||||
bool get isLocoClearanceProgressLoading =>
|
||||
_isLocoClearanceProgressLoading;
|
||||
bool _locoClearanceHasMore = false;
|
||||
bool get locoClearanceHasMore => _locoClearanceHasMore;
|
||||
|
||||
static const List<EventField> _fallbackEventFields = [
|
||||
EventField(name: 'operator', display: 'Operator'),
|
||||
@@ -357,6 +377,8 @@ class DataService extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void clear() {
|
||||
_currentUserId = null;
|
||||
_lastLegsFetch = const _LegFetchOptions();
|
||||
_homepageStats = null;
|
||||
_legs = [];
|
||||
_onThisDay = [];
|
||||
@@ -367,9 +389,44 @@ class DataService extends ChangeNotifier {
|
||||
_isLocoTimelineLoading.clear();
|
||||
_latestLocoChanges = [];
|
||||
_isLatestLocoChangesLoading = false;
|
||||
_isHomepageLoading = false;
|
||||
_isOnThisDayLoading = false;
|
||||
_legsHasMore = false;
|
||||
_isLegsLoading = false;
|
||||
_traction = [];
|
||||
_isTractionLoading = false;
|
||||
_tractionHasMore = false;
|
||||
_latestLocoChangesHasMore = false;
|
||||
_latestLocoChangesFetched = 0;
|
||||
_isTripDetailsLoading = false;
|
||||
_locoClasses = [];
|
||||
_tripList = [];
|
||||
_stationCache.clear();
|
||||
_stationInFlightByKey.clear();
|
||||
_stationNetworks = [];
|
||||
_stationCountryNetworks = {};
|
||||
_stationFiltersFetchedAt = null;
|
||||
_notifications = [];
|
||||
_isNotificationsLoading = false;
|
||||
_badgeAwards = [];
|
||||
_badgeAwardsHasMore = false;
|
||||
_isBadgeAwardsLoading = false;
|
||||
_classClearanceProgress = [];
|
||||
_isClassClearanceProgressLoading = false;
|
||||
_classClearanceHasMore = false;
|
||||
_locoClearanceProgress = [];
|
||||
_isLocoClearanceProgressLoading = false;
|
||||
_locoClearanceHasMore = false;
|
||||
_notifyAsync();
|
||||
}
|
||||
|
||||
void handleAuthChanged(String? userId) {
|
||||
if (_currentUserId == userId) return;
|
||||
_currentUserId = userId;
|
||||
clear();
|
||||
_currentUserId = userId;
|
||||
}
|
||||
|
||||
double getMileageForCurrentYear() {
|
||||
final currentYear = DateTime.now().year;
|
||||
return getMileageForYear(currentYear) ?? 0;
|
||||
|
||||
Reference in New Issue
Block a user