parseAlbum function

JsonMap parseAlbum(
  1. JsonMap result
)

Parses an album from result.

Implementation

JsonMap parseAlbum(JsonMap result) {
  final JsonMap realResult;
  if (result.containsKey(MTRIR)) {
    realResult = nav(result, [MTRIR]) as JsonMap;
  } else {
    realResult = result;
  }
  final artists =
      List<JsonMap>.from(
            (nav(realResult, ['subtitle', 'runs'], nullIfAbsent: true) ?? [])
                as List,
          ) // TODO should nullIfAbsent be true?
          .where((x) => x.containsKey('navigationEndpoint'))
          .map((x) => parseIdName(x as JsonMap?))
          .toList();

  final album = <String, dynamic>{
    'title': nav(realResult, TITLE_TEXT),
    'type': 'Album',
    'artists': artists,
    'browseId': nav(realResult, [...TITLE, ...NAVIGATION_BROWSE_ID]),
    'audioPlaylistId': parseAlbumPlaylistIdIfExists(
      nav(realResult, THUMBNAIL_OVERLAY_NAVIGATION, nullIfAbsent: true)
          as JsonMap?,
    ),
    'thumbnails': nav(realResult, THUMBNAIL_RENDERER),
    'isExplicit':
        nav(realResult, SUBTITLE_BADGE_LABEL, nullIfAbsent: true) != null,
  };

  const validTypes = {'Album', 'Single', 'EP'};
  final yearRegex = RegExp(r'^\d+$');

  final subtitle = nav(realResult, SUBTITLE, nullIfAbsent: true) as String?;
  final subtitle2 = nav(realResult, SUBTITLE2, nullIfAbsent: true) as String?;

  final type = [
    subtitle,
    subtitle2,
  ].firstWhere((s) => s != null && validTypes.contains(s), orElse: () => null);
  if (type != null) album['type'] = type;

  final year = [
    subtitle,
    subtitle2,
  ].firstWhere((s) => s != null && yearRegex.hasMatch(s), orElse: () => null);
  if (year != null) album['year'] = year;

  return album;
}