add stats page
All checks were successful
All checks were successful
This commit is contained in:
@@ -128,7 +128,12 @@ class ApiService {
|
||||
await _onUnauthorized!();
|
||||
}
|
||||
|
||||
throw Exception('API error ${res.statusCode}: $body');
|
||||
final message = _extractErrorMessage(body);
|
||||
throw ApiException(
|
||||
statusCode: res.statusCode,
|
||||
message: message,
|
||||
body: body,
|
||||
);
|
||||
}
|
||||
|
||||
dynamic _decodeBody(http.Response res) {
|
||||
@@ -149,4 +154,41 @@ class ApiService {
|
||||
return res.body;
|
||||
}
|
||||
}
|
||||
|
||||
String _extractErrorMessage(dynamic body) {
|
||||
if (body == null) return 'No response body';
|
||||
if (body is String) return body;
|
||||
if (body is Map<String, dynamic>) {
|
||||
for (final key in ['message', 'error', 'detail', 'msg']) {
|
||||
final val = body[key];
|
||||
if (val is String && val.trim().isNotEmpty) return val;
|
||||
}
|
||||
return body.toString();
|
||||
}
|
||||
if (body is List) {
|
||||
final parts = body
|
||||
.map((e) => e is Map
|
||||
? _extractErrorMessage(Map<String, dynamic>.from(e))
|
||||
: e.toString())
|
||||
.where((e) => e.trim().isNotEmpty)
|
||||
.toList();
|
||||
if (parts.isNotEmpty) return parts.join('; ');
|
||||
}
|
||||
return body.toString();
|
||||
}
|
||||
}
|
||||
|
||||
class ApiException implements Exception {
|
||||
final int statusCode;
|
||||
final String message;
|
||||
final dynamic body;
|
||||
|
||||
ApiException({
|
||||
required this.statusCode,
|
||||
required this.message,
|
||||
this.body,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'API error $statusCode: $message';
|
||||
}
|
||||
|
||||
@@ -11,3 +11,4 @@ part 'data_service_traction.dart';
|
||||
part 'data_service_trips.dart';
|
||||
part 'data_service_notifications.dart';
|
||||
part 'data_service_badges.dart';
|
||||
part 'data_service_stats.dart';
|
||||
|
||||
@@ -28,6 +28,10 @@ class DataService extends ChangeNotifier {
|
||||
// Homepage Data
|
||||
HomepageStats? _homepageStats;
|
||||
HomepageStats? get homepageStats => _homepageStats;
|
||||
StatsAbout? _aboutStats;
|
||||
StatsAbout? get aboutStats => _aboutStats;
|
||||
bool _isAboutStatsLoading = false;
|
||||
bool get isAboutStatsLoading => _isAboutStatsLoading;
|
||||
|
||||
// Legs Data
|
||||
List<Leg> _legs = [];
|
||||
|
||||
28
lib/services/data_service/data_service_stats.dart
Normal file
28
lib/services/data_service/data_service_stats.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
part of 'data_service.dart';
|
||||
|
||||
extension DataServiceStats on DataService {
|
||||
Future<void> fetchAboutStats({bool force = false}) async {
|
||||
if (_isAboutStatsLoading) return;
|
||||
if (!force && _aboutStats != null) return;
|
||||
_isAboutStatsLoading = true;
|
||||
_notifyAsync();
|
||||
try {
|
||||
final json = await api.get('/stats/about');
|
||||
if (json is Map<String, dynamic>) {
|
||||
_aboutStats = StatsAbout.fromJson(json);
|
||||
} else if (json is Map) {
|
||||
_aboutStats = StatsAbout.fromJson(
|
||||
json.map((key, value) => MapEntry(key.toString(), value)),
|
||||
);
|
||||
} else {
|
||||
throw Exception('Unexpected stats response: $json');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('Failed to fetch about stats: $e');
|
||||
_aboutStats = null;
|
||||
} finally {
|
||||
_isAboutStatsLoading = false;
|
||||
_notifyAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user