loadEpisodeChapters static method

Future<Episode> loadEpisodeChapters({
  1. required Episode episode,
  2. bool forceReload = false,
  3. dynamic timeout = const Duration(seconds: 20),
})

Podcasts that support the newer podcast namespace can include chapter markers. Typically this is in the form of a Url in the RSS feed pointing to a JSON file. This method takes the Url and loads the chapters, storing the results in the Episode itself. Repeatedly calling this method for the same episode will, by default, not reload the chapters data and will simply return the Episode back. If you need to reload the chapters set the forceReload parameter to true.

Implementation

static Future<Episode> loadEpisodeChapters({
  required Episode episode,
  bool forceReload = false,
  final timeout = const Duration(seconds: 20),
}) async {
  final client = Dio(
    BaseOptions(
      connectTimeout: timeout,
      receiveTimeout: timeout,
    ),
  );

  if (episode.chapters!.chapters.isNotEmpty &&
      !episode.chapters!.loaded &&
      !forceReload) {
    try {
      final response = await client.get(episode.chapters!.url);

      if (response.statusCode == 200 &&
          response.data is Map<String, dynamic>) {
        _loadChapters(response, episode.chapters!);
      }
    } on DioException catch (e) {
      switch (e.type) {
        case DioExceptionType.connectionTimeout:
        case DioExceptionType.sendTimeout:
        case DioExceptionType.receiveTimeout:
          throw PodcastTimeoutException(e.message ?? '');
        case DioExceptionType.connectionError:
        case DioExceptionType.badResponse:
          throw PodcastFailedException(e.message ?? '');
        case DioExceptionType.badCertificate:
          throw PodcastCertificateException(e.message ?? '');
        case DioExceptionType.cancel:
          throw PodcastCancelledException(e.message ?? '');
        case DioExceptionType.unknown:
          throw PodcastUnknownException(e.message ?? '');
      }
    }
  }

  return episode;
}