private static async void WebView_ScriptNotify(object sender, NotifyEventArgs e) { JObject message = JObject.Parse(e.Value); if (message["windowReady"] != null) { windowReady = (bool)message["windowReady"]; if (windowReady) { string deviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily; string deviceName = "Windows 10"; if (deviceFamily.Contains("Mobile")) { deviceName = "Windows Phone"; } else if (deviceFamily.Contains("Xbox")) { deviceName = "Xbox"; } else if (deviceFamily.Contains("Holographic")) { deviceName = "HoloLens"; } else if (deviceFamily.Contains("IoT")) { deviceName = "Windows IoT"; } else if (deviceFamily.Contains("Desktop")) { deviceName = "Windows 10"; } else { deviceName = "Windows 10"; } EasClientDeviceInformation info = new EasClientDeviceInformation(); await SetPlayerName($"Reverberate on {deviceName}: {info.FriendlyName}"); await SetAccessToken(AppConstants.SpotifyClient.AccessToken); await CreatePlayer(); await ConnectPlayer(); } } else if (message["request"] != null) { string request = (string)message["request"]; if (request == "accessToken") { if (AppConstants.SpotifyClient.AccessTokenExpiresAt > DateTimeOffset.UtcNow) { await SetAccessToken(AppConstants.SpotifyClient.AccessToken); } else { await SetAccessToken(await AppConstants.SpotifyClient.RefreshAccessToken()); } } } else if (message["player"] != null) { if (message["player"]["ready"] != null) { playerReady = (bool)message["player"]["ready"]; DeviceId = (string)message["player"]["deviceId"]; if (playerReady) { if (!PlayerConnected) { PlayerConnected = true; if (checkActivityTask != null) { checkActivityCancellationTokenSource.Cancel(); checkActivityTask = null; checkActivityCancellationTokenSource.Dispose(); } checkActivityCancellationTokenSource = new CancellationTokenSource(); checkActivityTask = CheckActivity(checkActivityCancellationTokenSource.Token); HelperMethods.GetViewModelLocator().MediaControlBarInstance.SetConnected(); } } else { PlayerConnected = false; if (checkActivityTask != null) { checkActivityCancellationTokenSource.Cancel(); checkActivityTask = null; checkActivityCancellationTokenSource.Dispose(); } HelperMethods.GetViewModelLocator().MediaControlBarInstance.SetDisconnected(); } if (MediaControlBarViewModel.ActiveDeviceId == null) { MediaControlBarViewModel.ActiveDeviceId = DeviceId; } } if (message["player"]["state"] != null) { SpotifyWebPlaybackState playbackState = JsonConvert.DeserializeObject <SpotifyWebPlaybackState>(message["player"]["state"].ToString()); WebPlaybackStateChanged?.Invoke(null, new SpotifyWebPlaybackStateEventArgs(playbackState)); } if (message["player"]["connected"] != null) { if (!(bool)message["player"]["connected"]) { PlayerConnected = false; HelperMethods.GetViewModelLocator().MediaControlBarInstance.SetDisconnected(); } } } }
public SpotifyWebPlaybackStateEventArgs(SpotifyWebPlaybackState state) { State = state; }