Example #1
0
        protected override async void OnAppearing()
        {
            base.OnAppearing();

            if (IsBusy)
            {
                return;
            }


            if (Settings.GetSetting(CommonConstants.TokenKey) != string.Empty)
            {
                IProgressDialog progress = null;
                try
                {
                    IsBusy   = true;
                    progress = UserDialogs.Instance.Loading(TranslateServices.GetResourceString(CommonConstants.RefreshTokenKey));
                    await LiveIdLogOnViewModel.GetNewAccessToken();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format(TranslateServices.GetResourceString(CommonConstants.RefreshTokenExceptionTip), ex.Message));
                }
                finally
                {
                    progress?.Hide();
                    IsBusy = false;
                }
            }

            if (LogOnViewModel.Instance.IsLoggedIn)
            {
                App.GoHome();
            }
            else
            {
                await logo.FadeTo(1, 500, Easing.CubicOut);

                await title.FadeTo(1, 1000, Easing.CubicOut);
            }
        }
Example #2
0
        protected override async void OnAppearing()
        {
            base.OnAppearing();

            if (IsBusy)
            {
                return;
            }

            if (Settings.GetSetting(CommonConstants.TokenKey) != string.Empty)
            {
                IProgressDialog progress = null;
                try
                {
                    IsBusy   = true;
                    progress = UserDialogs.Instance.Loading("Refreshing tokens...");
                    await LiveIdLogOnViewModel.GetNewAccessToken();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Unable to refresh token: {ex}");
                }
                finally
                {
                    progress?.Hide();
                    IsBusy = false;
                }
            }

            if (LogOnViewModel.Instance.IsLoggedIn)
            {
                App.GoHome();
            }
            else
            {
                await logo.FadeTo(1, 500, Easing.CubicOut);

                await title.FadeTo(1, 1000, Easing.CubicOut);
            }
        }
Example #3
0
        /// <summary>
        /// Handle Add/Del/Modify of Contributions.
        /// </summary>
        /// <param name="model">Instance of ContributionModel</param>
        /// <param name="httpMethod">Put/Post/Delete</param>
        /// <returns></returns>
        async Task <string> DoWork(string url, Object model, HttpMethod httpMethod, string token, bool isImage, bool isRefreshedToken, bool isAddOrUpdateContribution = false)
        {
            MyProfileViewModel.Instance.ErrorMessage = string.Empty;
            string errorMsg = "";

            try
            {
                var handler = new HttpClientHandler
                {
                    UseDefaultCredentials = false,
                };

                using (var client = new HttpClient(handler)
                {
                    Timeout = new System.TimeSpan(0, 0, 15)
                })
                {
                    client.DefaultRequestHeaders.Add(CommonConstants.OcpApimSubscriptionKey, CommonConstants.OcpApimSubscriptionValue);
                    client.DefaultRequestHeaders.Add(CommonConstants.AuthorizationKey, "Bearer " + token);
                    if (!isImage)
                    {
                        client.DefaultRequestHeaders.Add(CommonConstants.AcceptsKey, CommonConstants.MediaTypeForJson);
                    }

                    string requestJsonString = string.Empty;
                    if (model != null)
                    {
                        requestJsonString = JsonConvert.SerializeObject(model);
                    }

                    HttpResponseMessage response = null;
                    using (StringContent theContent = new StringContent(requestJsonString, System.Text.Encoding.UTF8, CommonConstants.MediaTypeForJson))
                    {
                        switch (httpMethod)
                        {
                        case HttpMethod.Put:
                            response = await client.PutAsync(url, theContent);

                            break;

                        case HttpMethod.Post:
                            response = await client.PostAsync(url, theContent);

                            break;

                        case HttpMethod.Delete:
                            response = await client.DeleteAsync(url);

                            break;

                        case HttpMethod.Get:
                            response = await client.GetAsync(url);

                            break;

                        default:
                            break;
                        }

                        // Parse response
                        if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.NoContent || response.StatusCode == HttpStatusCode.Created)
                        {
                            var responseString = await response.Content.ReadAsStringAsync();

                            if (string.IsNullOrEmpty(responseString))
                            {
                                return(CommonConstants.OkResult);
                            }
                            return(responseString);
                        }
                        else if (response.StatusCode == HttpStatusCode.BadRequest)
                        {
                            if (isAddOrUpdateContribution)
                            {
                                var responseString = await response.Content.ReadAsStringAsync();

                                if (!string.IsNullOrEmpty(responseString))
                                {
                                    errorMsg = responseString;
                                }
                            }
                            else
                            {
                                throw new WebException(string.Format(System.Globalization.CultureInfo.InvariantCulture, TranslateServices.GetResourceString(CommonConstants.NetworkErrorFormatString), response.StatusCode.ToString()));
                            }
                        }
                        else if (response.StatusCode == HttpStatusCode.Forbidden)
                        {
                            if (isRefreshedToken)
                            {
                                throw new WebException(string.Format(System.Globalization.CultureInfo.InvariantCulture, TranslateServices.GetResourceString(CommonConstants.NetworkErrorFormatString), response.StatusCode.ToString()));
                            }
                            else
                            {
                                string newAccessToken = await LiveIdLogOnViewModel.GetNewAccessToken();

                                string result = await DoWork(url, model, httpMethod, newAccessToken, isImage, true, isAddOrUpdateContribution);

                                return(result);
                            }
                        }
                        else
                        {
                            if (CheckInternetConnection())
                            {
                                string newAccessToken = await LiveIdLogOnViewModel.GetNewAccessToken();

                                string result = await DoWork(url, model, httpMethod, newAccessToken, isImage, true, isAddOrUpdateContribution);

                                return(result);
                            }
                            else
                            {
                                throw new WebException(string.Format(System.Globalization.CultureInfo.InvariantCulture, TranslateServices.GetResourceString(CommonConstants.NetworkErrorFormatString), response.StatusCode.ToString()));
                            }
                        }
                    }
                }
            }
            catch (WebException ex)
            {
                errorMsg = TranslateServices.GetResourceString(CommonConstants.DefaultNetworkErrorString);
            }
            catch (HttpRequestException ex)
            {
                errorMsg = TranslateServices.GetResourceString(CommonConstants.DefaultNetworkErrorString);
            }
            catch (Exception ex) {
                errorMsg = TranslateServices.GetResourceString(CommonConstants.DefaultNetworkErrorString);
            }

            if (!string.IsNullOrEmpty(errorMsg))
            {
                MyProfileViewModel.Instance.ErrorMessage = errorMsg;
                throw new Exception(errorMsg);
            }
            return(null);
        }