/// <summary>
            /// Removes the current favorite from the favorites list.
            /// </summary>
            private void RemoveFavorite()
            {
                // If the current view model is busy do nothing
                if (this.viewModel.IsBusy)
                {
                    return;
                }

                // Prepare the data to be send to the server
                var request = new Json.JsonObject {
                    { "montonera", this.favorite.Id }
                };

                // Send request to the server
                this.viewModel.IsBusy = true;
                WebHelper.SendAsync(
                    Uris.GetRemoveFromFavoritesUri(),
                    request.AsHttpContent(),
                    _ =>
                {
                    this.OnFavoriteRemoved();
                    this.viewModel.IsBusy = false;
                },
                    () => this.viewModel.IsBusy = false);
            }
            /// <summary>
            /// Sets the current favorite as primary.
            /// </summary>
            private void SetAsPrimary()
            {
                // If the current view model is busy do nothing
                if (this.viewModel.IsBusy)
                {
                    return;
                }

                // Prepare the data to be send to the server
                var request = new Json.JsonObject {
                    { "montonera", this.favorite.Id }, { "principal", true }
                };

                // Send request to the server
                this.viewModel.IsBusy = true;
                WebHelper.SendAsync(
                    Uris.GetAddToFavoritesUri(),
                    request.AsHttpContent(),
                    _ =>
                {
                    this.IsPrimary        = true;
                    this.viewModel.IsBusy = false;
                },
                    () => this.viewModel.IsBusy = false);
            }
示例#3
0
 /// <summary>
 /// Updates the submitted reports from the server.
 /// </summary>
 private void UpdateReports()
 {
     this.IsBusy = true;
     WebHelper.SendAsync(
         Uris.GetGetReportsUri(),
         null,
         this.ProcessUpdateReportsResult,
         () => this.IsBusy = false);
 }
        /// <summary>
        /// Reports the incident.
        /// </summary>
        private void ReportIncident()
        {
            // If the current view model is busy do nothing
            if (this.IsBusy)
            {
                return;
            }

            // Validate that the notification type is valid
            if (this.IncidentTypeIndex < 0)
            {
                App.DisplayAlert(
                    Localization.ErrorDialogTitle,
                    Localization.ErrorInvalidIncidentType,
                    Localization.DialogDismiss);
                return;
            }

            // Save the report date
            if (!this.reportDate.HasValue)
            {
                this.reportDate = DateTime.UtcNow;
            }

            // Prepare report data
            // ReSharper disable once UseObjectOrCollectionInitializer
            var report = new MultipartFormDataContent();

            report.Add(new StringContent(this.pin.Id), "montonera");
            if (this.incidentTypeIndex != -1)
            {
                report.Add(new StringContent(this.IncidentTypes[this.incidentTypeIndex]), "incidencia");
            }

            // Is report photo is specified
            if (this.ReportPhotoData != null)
            {
                // Add photo to the report
                var imageContent = new ByteArrayContent(this.ReportPhotoData);
                imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/jpeg");
                report.Add(imageContent, "imagen", "imagen.jpg");
            }

            // Send the report to the server
            this.IsBusy = true;
            WebHelper.SendAsync(
                Uris.GetSubmitReportUri(),
                report,
                this.ProcessReportIncidentResult,
                () => this.IsBusy = false,
                timeout: TimeSpan.FromMinutes(5));
        }
        /// <summary>
        /// Gets the incident types from the server.
        /// </summary>
        private void GetIncidentTypes()
        {
            // If the current view model is busy do nothing
            if (this.IsBusy)
            {
                return;
            }

            // Get the incident types from the server
            this.IsBusy = true;
            WebHelper.SendAsync(
                Uris.GetGetIncidentTypesUri(),
                null,
                this.ProcessGetIncidentTypesResult,
                () => this.IsBusy = false);
        }
示例#6
0
        /// <summary>
        /// Refreshes the expired access token.
        /// </summary>
        /// <param name="httpClient">The <see cref="HttpClient"/> to perform the send operation.</param>
        /// <returns>A task that represents the asynchronous refresh operation.</returns>
        private static async Task <string> InternalRefreshToken(HttpClient httpClient)
        {
            // Prepare the refresh request
            var endpoint       = Uris.GetRefreshTokenUri();
            var refreshRequest = new JsonObject
            {
                { "grant_type", "refresh_token" },
                { "refresh_token", Settings.Instance.GetValue(Settings.RefreshToken, string.Empty) }
            };

            using (var request = new HttpRequestMessage(endpoint.Method, WebHelper.AppendNonce(endpoint.Uri)))
            {
                // Setup the request
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                request.Content = refreshRequest.AsHttpContent();

                // Send the request to the server
                var socketTimeout = httpClient.Timeout.Add(TimeSpan.FromSeconds(5));
                var response      = await WebHelper.SendRequest(httpClient, request, socketTimeout);

                using (response)
                {
                    // Parse the server response
                    var responseContent = await WebHelper.ParseResponse(response);

                    // Parse the new access token data
                    var accessToken = responseContent.GetItemOrDefault("access_token").GetStringValueOrDefault(string.Empty);
                    var expiresIn   = responseContent.GetItemOrDefault("expires_in").GetIntValueOrDefault(24 * 60 * 60);

                    // If all of the fields are returned
                    if (!string.IsNullOrEmpty(accessToken))
                    {
                        // Update access token
                        Settings.Instance.SetValue(Settings.AccessToken, accessToken);
                        Settings.Instance.SetValue(Settings.AccessTokenExpires, DateTime.UtcNow.AddSeconds(expiresIn));
                        return(accessToken);
                    }

                    // Report error
                    throw new RemoteServerException(
                              HttpStatusCode.Unauthorized,
                              Localization.ErrorDialogTitle,
                              Localization.ErrorUnauthorized,
                              null);
                }
            }
        }
示例#7
0
        /// <summary>
        /// Sets the push notification token used for the current application.
        /// </summary>
        /// <param name="pushToken">The device push notification token.</param>
        internal static void SetPushToken(string pushToken)
        {
            // If push token have not been changed
            Debug.WriteLine("New push token available: " + pushToken);
            var currentPushToken = Settings.Instance.GetValue(Settings.PushToken, string.Empty);

            if (string.Compare(currentPushToken, pushToken, StringComparison.CurrentCultureIgnoreCase) == 0)
            {
                // Do nothing
                return;
            }

            // Get the current application instance
            var instance = Application.Current as App;

            if (instance != null)
            {
                // Set the push token
                instance.PushToken = pushToken;
            }

            // If user is logged in
            if (Settings.Instance.Contains(Settings.AccessToken))
            {
                // Prepare the data to be send to the server
                var deviceId = instance?.DeviceId ?? string.Empty;
                var request  = new Json.JsonObject
                {
                    { "device", deviceId },
                    { "push_token", pushToken }
                };

                // Send request to the server
                Debug.WriteLine("Update push token on server");
                WebHelper.SendAsync(
                    Uris.GetChangePushTokenUri(),
                    request.AsHttpContent(),
                    _ => Settings.Instance.SetValue(Settings.PushToken, pushToken));
            }
        }