async void SendNotification() { try { HttpResponseMessage response = await NotificationRequest.GetNotPulledNotifications(); if (response.IsSuccessStatusCode) { string response_content = await response.Content.ReadAsStringAsync(); List <AppUserNotification> user_notifications = JsonConvert.DeserializeObject <List <AppUserNotification> >(response_content); if (user_notifications.Any()) { foreach (AppUserNotification user_notification in user_notifications) { HttpResponseMessage responseOb = await ObservationRequest.GetObservationById(user_notification.Observation); if (responseOb.IsSuccessStatusCode) { string response_contentOb = await responseOb.Content.ReadAsStringAsync(); RequestObservation observation = JsonConvert.DeserializeObject <RequestObservation>(response_contentOb); string title = "🚨 Good news! 🚨"; string message = "💰 Your observed product: " + observation.Product.Title + " is now available for: €" + user_notification.Notified_price + " 💰"; notificationManager.SendNotification(title, message); } else { if (responseOb.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Try Again!", "No connection with the server", "OK"); } if (responseOb.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } } } } else { if (response.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Try Again!", "No connection with the server", "OK"); } if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } } catch (Exception ex) { Console.WriteLine("No notification"); } }
private async void FillPage() { List <OutputNotification> output_list = new List <OutputNotification>(); HttpResponseMessage response = await NotificationRequest.GetAllNotifications(); if (response.IsSuccessStatusCode) { string response_content = await response.Content.ReadAsStringAsync(); List <AppUserNotification> notifications_list = JsonConvert.DeserializeObject <List <AppUserNotification> >(response_content); foreach (AppUserNotification notification in notifications_list) { HttpResponseMessage responseOb = await ObservationRequest.GetObservationById(notification.Observation); if (responseOb.IsSuccessStatusCode) { string response_contentOb = await responseOb.Content.ReadAsStringAsync(); RequestObservation observation = JsonConvert.DeserializeObject <RequestObservation>(response_contentOb); if (observation != null) { OutputNotification output_value = new OutputNotification(observation, notification); output_list.Add(output_value); } } else { if (response.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Attention!!!", "No connection with the server", "OK"); } if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } } Notifications = output_list; MyCollectionView.ItemsSource = Notifications; } else { if (response.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Attention!!!", "No connection with the server", "OK"); } if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } }
async private void InsertObservation(object sender, EventArgs e) { string result = await DisplayPromptAsync("What's your desired price?", "Insert a threshold price", keyboard : Keyboard.Numeric); string email; if (result != null) { try { email = await SecureStorage.GetAsync("email"); RequestObservation observation = new RequestObservation(Page_product, result, email); string json = JsonConvert.SerializeObject(observation); HttpResponseMessage response = await ObservationRequest.InsertObservation(json); if (response.IsSuccessStatusCode) { string response_content = await response.Content.ReadAsStringAsync(); await DisplayAlert("Success!", "Observation successful", "OK"); } else { if (response.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Try Again!", "No connection with the server", "OK"); } if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } } catch (Exception ex) { await DisplayAlert("Error!", "Something went wrong", "OK"); } } }
private async void UpdateOrDelete(object sender, EventArgs e) { Button button = (Button)sender; string observation_product = (string)button.CommandParameter; string action = await DisplayActionSheet("What would you like to do?", "Cancel", null, "Update Observation", "Delete Observation"); if (action == "Delete Observation") { HttpResponseMessage response = await ObservationRequest.DeleteObservation(observation_product); if (response.IsSuccessStatusCode) { try { RequestObservation obs_to_remove = CompleteObservations.Find(RequestObservation => RequestObservation.Product.Id == observation_product); CompleteObservations.Remove(obs_to_remove); Console.WriteLine(CompleteObservations); MyCollectionView.ItemsSource = null; MyCollectionView.ItemsSource = CompleteObservations; await DisplayAlert("Success!", "Observation removed", "OK"); } catch (Exception ex) { await DisplayAlert("Error!", "Something went wrong", "OK"); } } else { if (response.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Attention!!!", "No connection with the server", "OK"); } if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } } else if (action == "Update Observation") { RequestObservation obs_to_update = CompleteObservations.Find(RequestObservation => RequestObservation.Product.Id == observation_product); string threshold_price = await DisplayPromptAsync("What's your desired price?", "Insert a threshold price", keyboard : Keyboard.Numeric); ModifyObservation update_observation = new ModifyObservation(obs_to_update.Email, obs_to_update.Product.Id, threshold_price); string json = JsonConvert.SerializeObject(update_observation); HttpResponseMessage response = await ObservationRequest.UpdateObservation(json, observation_product); if (response.IsSuccessStatusCode) { int index = CompleteObservations.FindIndex(RequestObservation => RequestObservation.Product.Id == observation_product); CompleteObservations[index].Threshold_price = string.Format("{0:f2}", threshold_price); MyCollectionView.ItemsSource = null; MyCollectionView.ItemsSource = CompleteObservations; await DisplayAlert("Success!", "Observation updated", "OK"); } else { if (response.StatusCode == System.Net.HttpStatusCode.BadGateway) { await DisplayAlert("Attention!!!", "No connection with the server", "OK"); } if (response.StatusCode == System.Net.HttpStatusCode.BadRequest) { await DisplayAlert("Try Again!", "Invalid request", "OK"); } } } }