Files
velocity-pay-flutter/lib/providers/uptime_provider.dart
2026-06-24 00:31:01 +02:00

55 lines
1.6 KiB
Dart

import 'package:flutter/foundation.dart';
import 'package:qpay/http/http.dart';
import 'package:qpay/models/uptime_item_model.dart';
enum UptimeStatus { idle, loading, success, error }
class UptimeProvider extends ChangeNotifier {
final Http _http = Http();
UptimeStatus _status = UptimeStatus.idle;
List<UptimeItemModel> _items = [];
String? _error;
UptimeStatus get status => _status;
List<UptimeItemModel> get items => _items;
String? get error => _error;
bool get isLoading => _status == UptimeStatus.loading;
bool get hasError => _status == UptimeStatus.error;
/// Count of services that are currently up (status == true).
int get upCount => _items.where((i) => i.status).length;
/// Count of services that are currently down (status == false).
int get downCount => _items.where((i) => !i.status).length;
Future<void> fetchUptimes() async {
_status = UptimeStatus.loading;
_error = null;
notifyListeners();
try {
final response = await _http.getRaw(
'/public/uptimes?sort=createdAt%2Cdesc',
);
if (response.statusCode == 200) {
final body = response.data as Map<String, dynamic>;
final content = body['content'] as List<dynamic>;
_items = content
.map((e) => UptimeItemModel.fromJson(e as Map<String, dynamic>))
.toList();
_status = UptimeStatus.success;
} else {
_error = 'Failed to load uptime data (status ${response.statusCode})';
_status = UptimeStatus.error;
}
} catch (e) {
_error = 'Network error: ${e.toString()}';
_status = UptimeStatus.error;
}
notifyListeners();
}
}