52 lines
1.4 KiB
Dart
52 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../models/rotation_config.dart';
|
|
|
|
/// Provides holiday lookup via Nager.Date.
|
|
class HolidaysService {
|
|
/// Fetches Philippine public holidays for [year].
|
|
///
|
|
/// Returns a list of [Holiday] instances.
|
|
static Future<List<Holiday>> fetchPhilippinesHolidays(int year) async {
|
|
final uri = Uri.https('date.nager.at', '/api/v3/PublicHolidays/$year/PH');
|
|
final response = await http.get(uri);
|
|
if (response.statusCode != 200) {
|
|
throw Exception('Failed to fetch holidays: ${response.statusCode}');
|
|
}
|
|
|
|
final decoded = jsonDecode(response.body);
|
|
if (decoded is! List) {
|
|
throw Exception('Unexpected holiday response format');
|
|
}
|
|
|
|
return decoded
|
|
.whereType<Map<String, dynamic>>()
|
|
.map((item) {
|
|
final dateString = item['date'] as String?;
|
|
final name =
|
|
item['localName'] as String? ??
|
|
item['name'] as String? ??
|
|
'Holiday';
|
|
|
|
if (dateString == null) {
|
|
return null;
|
|
}
|
|
|
|
final parsed = DateTime.tryParse(dateString);
|
|
if (parsed == null) {
|
|
return null;
|
|
}
|
|
|
|
return Holiday(
|
|
date: DateTime(parsed.year, parsed.month, parsed.day),
|
|
name: name,
|
|
source: 'nager',
|
|
);
|
|
})
|
|
.whereType<Holiday>()
|
|
.toList();
|
|
}
|
|
}
|