fetchNewsForBounds function

Future<void> fetchNewsForBounds({
  1. required BuildContext context,
  2. required MapController mapController,
  3. required GdeltNewsService newsService,
  4. required void updateState(
    1. List<NewsMarker> markers,
    2. bool loading
    ),
})

Fetches news for current map bounds.

Implementation

Future<void> fetchNewsForBounds({
  required BuildContext context,
  required MapController mapController,
  required GdeltNewsService newsService,
  required void Function(List<NewsMarker> markers, bool loading) updateState,
}) async {
  // Don't clear existing markers, just set loading state
  // Get current cached markers first.
  final bounds = mapController.camera.visibleBounds;
  final currentMarkers = newsService.getMarkersInBounds(bounds);
  updateState(currentMarkers, true);

  try {
    final nm = await newsService.fetchNews(
      bounds: bounds,
      query: 'news',
      maxResults: 50,
      timeSpan: '24h',
    );
    updateState(nm, false);
  } catch (e) {
    updateState([], false);
    if (!context.mounted) return;
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text('Failed to fetch news: $e'),
        backgroundColor: Colors.red.shade700,
      ),
    );
  }
}