handleMethodCall method

Future handleMethodCall(
  1. MethodCall call
)

handles method coming through the method channel

Implementation

Future<dynamic> handleMethodCall(MethodCall call) async {
  // check if spotify is loaded
  if (_sdkLoaded == false) {
    _sdkLoadFuture ??= _initializeSpotify();
    await _sdkLoadFuture;
  }

  switch (call.method) {
    case MethodNames.connectToSpotify:
      if (_currentPlayer != null) {
        return true;
      }
      log('Connecting to Spotify...');
      var clientId = call.arguments[ParamNames.clientId] as String?;
      var redirectUrl = call.arguments[ParamNames.redirectUrl] as String?;
      var playerName = call.arguments[ParamNames.playerName] as String?;
      var scopes =
          call.arguments[ParamNames.scope] as String? ?? defaultScopes;
      var accessToken = call.arguments[ParamNames.accessToken] as String?;

      // ensure that required arguments are present
      if (clientId == null ||
          clientId.isEmpty ||
          redirectUrl == null ||
          redirectUrl.isEmpty) {
        throw PlatformException(
            message:
                'Client id or redirectUrl are not set or have invalid format',
            code: 'Authentication Error');
      }

      // get initial token if not supplied
      if (accessToken == null || accessToken.isEmpty) {
        await _authorizeSpotify(
            clientId: clientId, redirectUrl: redirectUrl, scopes: scopes);
      }

      // create player
      _currentPlayer = Player(PlayerOptions(
          name: playerName,
          getOAuthToken: allowInterop((Function callback, t) {
            _getSpotifyAuthToken().then((value) {
              callback(value);
            });
          })));

      _registerPlayerEvents(_currentPlayer!);
      var result = await promiseToFuture(_currentPlayer!.connect());
      if (result == true) {
        // wait for the confirmation
        num time = 0;
        while (_currentPlayer!.deviceID == null) {
          await Future.delayed(const Duration(milliseconds: 200));
          time += 200;
          if (time > 10000) {
            return false;
          }
        }
        return true;
      } else {
        // disconnected
        _onSpotifyDisconnected(
            errorCode: 'Initialization Error',
            errorDetails: 'Attempt to connect to the Spotify SDK failed');
        return false;
      }
    case MethodNames.getAccessToken:
      var clientId = call.arguments[ParamNames.clientId] as String?;
      var redirectUrl = call.arguments[ParamNames.redirectUrl] as String?;

      // ensure that required arguments are present
      if (clientId == null ||
          clientId.isEmpty ||
          redirectUrl == null ||
          redirectUrl.isEmpty) {
        throw PlatformException(
            message:
                'Client id or redirectUrl are not set or have invalid format',
            code: 'Authentication Error');
      }

      return await _authorizeSpotify(
          clientId: clientId,
          redirectUrl: redirectUrl,
          scopes:
              call.arguments[ParamNames.scope] as String? ?? defaultScopes);
    case MethodNames.disconnectFromSpotify:
      log('Disconnecting from Spotify...');
      _spotifyToken = null;
      if (_currentPlayer == null) {
        return true;
      } else {
        _currentPlayer!.disconnect();
        _onSpotifyDisconnected();
        return true;
      }
    case MethodNames.play:
      await _play(call.arguments[ParamNames.spotifyUri] as String?);
      break;
    case MethodNames.queueTrack:
      await _queue(call.arguments[ParamNames.spotifyUri] as String?);
      break;
    case MethodNames.setShuffle:
      await _setShuffle(call.arguments[ParamNames.shuffle] as bool?);
      break;
    case MethodNames.setRepeatMode:
      await _setRepeatMode(
          call.arguments[ParamNames.repeatMode] as RepeatMode?);
      break;
    case MethodNames.resume:
      await promiseToFuture(_currentPlayer?.resume());
      break;
    case MethodNames.pause:
      await promiseToFuture(_currentPlayer?.pause());
      break;
    case MethodNames.skipNext:
      await promiseToFuture(_currentPlayer?.nextTrack());
      break;
    case MethodNames.skipPrevious:
      await promiseToFuture(_currentPlayer?.previousTrack());
      break;
    case MethodNames.getPlayerState:
      var stateRaw = await promiseToFuture(_currentPlayer?.getCurrentState())
          as WebPlaybackState?;
      if (stateRaw == null) return null;
      return jsonEncode(toPlayerState(stateRaw)!.toJson());
    default:
      throw PlatformException(
          code: 'Unimplemented',
          details:
              "Method '${call.method}' not implemented in web spotify_sdk");
  }
}