toPlayerState method

PlayerState? toPlayerState(
  1. WebPlaybackState? state
)

Converts a native WebPlaybackState to the library PlayerState

Implementation

PlayerState? toPlayerState(WebPlaybackState? state) {
  if (state == null) return null;
  var trackRaw = state.track_window.current_track;
  var albumRaw = trackRaw?.album;
  var restrictionsRaw = state.disallows;
  var artists = <Artist>[];

  if (trackRaw != null) {
    for (var artist in trackRaw.artists) {
      artists.add(Artist(artist.name, artist.uri));
    }
  }

  // getting repeat mode
  options.RepeatMode repeatMode;
  switch (state.repeat_mode) {
    case 1:
      repeatMode = options.RepeatMode.context;
      break;
    case 2:
      repeatMode = options.RepeatMode.track;
      break;
    default:
      repeatMode = options.RepeatMode.off;
      break;
  }

  return PlayerState(
    trackRaw != null
        ? Track(
            Album(albumRaw!.name, albumRaw.uri),
            artists[0],
            artists,
            -1,
            ImageUri(albumRaw.images[0].url),
            trackRaw.name,
            trackRaw.uri,
            trackRaw.linked_from.uri,
            isEpisode: trackRaw.type == 'episode',
            isPodcast: trackRaw.type == 'episode',
          )
        : null,
    1.0,
    state.position,
    options.PlayerOptions(repeatMode, isShuffling: state.shuffle),
    PlayerRestrictions(
        canSkipNext: restrictionsRaw.skipping_next || true,
        canSkipPrevious: restrictionsRaw.skipping_prev || true,
        canSeek: restrictionsRaw.seeking || true,
        canRepeatTrack: true,
        canRepeatContext: true,
        canToggleShuffle: true),
    isPaused: state.paused,
  );
}