コード例 #1
0
        public async Task SelectedUser(object sender, SelectionChangedEventArgs e)
        {
            var user = (UserQViewModel)((ListView)sender).SelectedItem;

            Views.Busy.SetBusy(true, _loader.GetString("HttpLoading"));
            var fetchedUser = await _remoteApi.GetUser(user.Username);

            Views.Busy.SetBusy(false, null);
            PivotNavIndex = 1;
            NavigationService.Navigate(typeof(Views.UserPage), fetchedUser);
        }
コード例 #2
0
        public async void ClickAuthor(object sender, RoutedEventArgs e)
        {
            try
            {
                Views.Busy.SetBusy(true, _loader.GetString("HttpLoading"));
                var user = await _quizApi.GetUser(CreatedBy);

                Views.Busy.SetBusy(false, null);
                NavigationService.Navigate(typeof(Views.UserPage), user);
            }
            catch (Exception k)
            {
                var dialog = new MessageDialog($"Error: {k.Message}");
                await dialog.ShowAsync();

                return;
            }
        }
コード例 #3
0
ファイル: LoginPageViewModel.cs プロジェクト: cjesp/29Quizlet
        public override async Task OnNavigatedToAsync(object parameter, NavigationMode mode, IDictionary <string, object> suspensionState)
        {
            if (suspensionState.ContainsKey(nameof(NavigationParameter)))
            {
                var navParameter = suspensionState[nameof(NavigationParameter)] as LoginPageNavigationModel;
                NavigationParameter = navParameter;
            }
            if (parameter != null)
            {
                var zero = parameter as string;
                if (zero == null)
                {
                    var navParameter = parameter as LoginPageNavigationModel;
                    NavigationParameter = navParameter;
                }
            }

            if (NavigationParameter != null && (!string.IsNullOrEmpty(NavigationParameter.Error) || !string.IsNullOrEmpty(NavigationParameter.ErrorDescription)))
            {
                // this means that login created an error
            }
            if (NavigationParameter != null && !string.IsNullOrEmpty(NavigationParameter.Code))
            {
                _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "XXX");
                var request = new HttpRequestMessage(HttpMethod.Post, "https://api.quizlet.com/oauth/token");

                var formData = new List <KeyValuePair <string, string> >();
                formData.Add(new KeyValuePair <string, string>("grant_type", "authorization_code"));
                formData.Add(new KeyValuePair <string, string>("code", NavigationParameter.Code));

                request.Content = new FormUrlEncodedContent(formData);

                var response = await _client.SendAsync(request);

                var content = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var json         = JsonConvert.DeserializeObject <Authentication.Success>(content);
                    var userSettings = new UserSettings()
                    {
                        UserId     = json.user_id,
                        Token      = json.access_token,
                        Scope      = json.scope,
                        TokenType  = json.token_type,
                        Expiration = json.expires_in,
                        Created    = DateTime.UtcNow
                    };

                    _SettingsService.UserSettings = userSettings;
                    LogInOutText = "Log out";
                    await FetchAndSaveUser(userSettings.UserId);
                }
                else
                {
                    //todo: there was an error
                }
            }

            // load the page with data...
            if (_SettingsService.UserSettings != null)
            {
                UserId       = _SettingsService.UserSettings.UserId;
                LogInOutText = "Log out";
                try
                {
                    var user = await _quizletAPI.GetUser(UserId);

                    if (user.Profile_Image != null)
                    {
                        ProfilePictureSrc = new Uri(user.Profile_Image);
                    }

                    if (user.AccountType != null)
                    {
                        AccountType = user.AccountType;
                    }

                    if (user.Statistics != null)
                    {
                        TermsCreated = user.Statistics.PublicTermsEntered.ToString();
                    }

                    if (user.Statistics != null)
                    {
                        SetsCreated = user.Statistics.PublicSetsCreated.ToString();
                    }

                    // update the shell to reflect the new authenticated user
                    Views.Shell.Instance.UpdateUserImage();
                }
                catch (Exception e)
                {
                    var dialog = new MessageDialog($"Error: {e.Message}");
                    await dialog.ShowAsync();

                    return;
                }
            }

            if (_SettingsService.AuthenticatedUser != null)
            {
                LogInOutText = "Log out";
            }

            await Task.CompletedTask;
        }