public void Loaded() { try { Preferences currentPrefs = _preferencesService.CurrentPreferences.Clone(); bool updateAvailable = false; if (currentPrefs.AppCheckForUpdates) { UpdateInfo updateInfo = _updateService.CheckForUpdate(); if (updateInfo != null) { updateAvailable = true; _navigationService.ShowUpdateInfo(updateInfo); } } bool searchOnStartup = false; if (!updateAvailable && currentPrefs.SearchOnStartup) { currentPrefs.Validate(); if (!currentPrefs.HasErrors) { searchOnStartup = true; SearchParameters searchParams = new SearchParameters(SearchType.Channel) { Channel = currentPrefs.SearchChannelName, VideoType = currentPrefs.SearchVideoType, LoadLimitType = currentPrefs.SearchLoadLimitType, LoadFrom = DateTime.Now.Date.AddDays(-currentPrefs.SearchLoadLastDays), LoadFromDefault = DateTime.Now.Date.AddDays(-currentPrefs.SearchLoadLastDays), LoadTo = DateTime.Now.Date, LoadToDefault = DateTime.Now.Date, LoadLastVods = currentPrefs.SearchLoadLastVods }; _searchService.PerformSearch(searchParams); } } if (!updateAvailable && !searchOnStartup) { _navigationService.ShowWelcome(); } _twitchService.Authorize(_runtimeDataService.RuntimeData.AccessToken); } catch (Exception ex) { _dialogService.ShowAndLogException(ex); } }
private void Navigating(Uri url) { try { lock (_commandLockObject) { string urlStr = url?.OriginalString; if (!string.IsNullOrWhiteSpace(urlStr) && urlStr.StartsWith("http://www.tl.com", StringComparison.OrdinalIgnoreCase)) { NameValueCollection urlParams = HttpUtility.ParseQueryString(url.Query); int tokenIndex = urlStr.IndexOf("#access_token="); if (tokenIndex >= 0) { tokenIndex = tokenIndex + 14; // #access_token= -> 14 chars int paramIndex = urlStr.IndexOf("&"); string accessToken = null; if (paramIndex >= 0) { accessToken = urlStr.Substring(tokenIndex, paramIndex - tokenIndex); } else { accessToken = urlStr.Substring(tokenIndex); } if (string.IsNullOrWhiteSpace(accessToken)) { _dialogService.ShowMessageBox("Twitch did not respond with an access token! Authorization aborted!", "Error", MessageBoxButton.OK); _navigationService.NavigateBack(); } else { if (_twitchService.Authorize(accessToken)) { _navigationService.ShowRevokeAuthorization(); _notificationService.ShowNotification("Twitch authorization successful!"); } else { _dialogService.ShowMessageBox("Access Token '" + accessToken + "' could not be verified! Authorization aborted!", "Error", MessageBoxButton.OK); _navigationService.NavigateBack(); } } } else if (urlParams.ContainsKey("error")) { string error = urlParams.Get("error"); if (!string.IsNullOrWhiteSpace(error) && error.Equals("access_denied", StringComparison.OrdinalIgnoreCase)) { _navigationService.NavigateBack(); _notificationService.ShowNotification("Twitch authorization has been canceled!"); } else { Action unspecifiedError = () => { _dialogService.ShowMessageBox("Twitch responded with an unspecified error! Authorization aborted!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); _navigationService.NavigateBack(); }; if (urlParams.ContainsKey("error_description")) { string errorDesc = urlParams.Get("error_description"); if (string.IsNullOrWhiteSpace(errorDesc)) { unspecifiedError(); } else { _dialogService.ShowMessageBox( "Twitch responded with an error:" + Environment.NewLine + Environment.NewLine + "\"" + errorDesc + "\"" + Environment.NewLine + Environment.NewLine + "Authorization aborted!", "Error", MessageBoxButton.OK, MessageBoxImage.Error); _navigationService.NavigateBack(); } } else { unspecifiedError(); } } } else { _dialogService.ShowMessageBox("Twitch responded neither with an access token nor an error! Authorization aborted!", "Error", MessageBoxButton.OK); _navigationService.NavigateBack(); } } } } catch (Exception ex) { _dialogService.ShowAndLogException(ex); } }