public async Task <AddToFavouritesResponse> AddToFavourites(AddToFavouritesRequest item) { string url = $"/item/add-fav"; var requestBody = await Task.Run(() => JsonConvert.SerializeObject(item)); using (HttpClient httpClient = new HttpClient()) { AddToFavouritesResponse data = new AddToFavouritesResponse(); try { var authHeader = new AuthenticationHeaderValue("Bearer", await SecureStorage.GetAsync("auth_token")); httpClient.DefaultRequestHeaders.Authorization = authHeader; httpClient.BaseAddress = new Uri(Constants.BaseUrl); StringContent content = new StringContent(requestBody, Encoding.UTF8, "application/json"); HttpResponseMessage result = await httpClient.PostAsync(url, content); string response = await result.Content.ReadAsStringAsync(); data = JsonConvert.DeserializeObject <AddToFavouritesResponse>(response); if (result.IsSuccessStatusCode && result.StatusCode == HttpStatusCode.OK) { return(data); } return(null); } catch (Exception exp) { return(null); } } }
public async Task <AddToFavouritesResponse> AddToFavourites(AddToFavouritesRequest order) { IsBusy = true; var res = await favouritesAPI.AddToFavourites(order); IsBusy = false; return(res); }
private async void BtnAddToFavourites_Tapped(object sender, EventArgs e) { Image button = (Image)sender; button.IsEnabled = false; try { var item = (Data)button.BindingContext; var fontImageSource = (FontImageSource)button.Source; if (fontImageSource.Glyph == "" && fontImageSource.Color == Color.FromHex("#C3C3C3")) { await AnimateHeart(button); fontImageSource.Glyph = ""; fontImageSource.Color = Color.FromHex("#F25278"); //add to favourites here AddToFavouritesRequest addToFavouritesRequest = new AddToFavouritesRequest() { userId = OperationData.userId.ToString(), itemId = item.id.ToString() }; var res = await favouritesViewModel.AddToFavourites(addToFavouritesRequest); if (res != null) { if (res.Code == 0) { FavouriteItems favItem = new FavouriteItems() { id = item.id, userId = item.userId, foodType = item.foodType, foodName = item.foodName, quantity = item.quantity, description = item.description, unitPrice = item.unitPrice, itemImage = item.itemImage, preparedOn = item.preparedOn, expiryDate = item.expiryDate, isActive = item.isActive, isDeleted = item.isDeleted, createdAt = item.createdAt, updatedAt = item.updatedAt }; OperationData.FavouriteItemList.Add(favItem); await DisplayAlert("Message", item.foodName + " added to favourites!", null, "OK"); } else { await DisplayAlert("Message", "Couldn't add " + item.foodName + " to favourites. Please try again", null, "OK"); await AnimateHeart(button); fontImageSource.Glyph = ""; fontImageSource.Color = Color.FromHex("#C3C3C3"); } } else { await DisplayAlert("Message", "Couldn't add " + item.foodName + " to favourites. Please try again", null, "OK"); await AnimateHeart(button); fontImageSource.Glyph = ""; fontImageSource.Color = Color.FromHex("#C3C3C3"); } } else { await AnimateHeart(button); fontImageSource.Glyph = ""; fontImageSource.Color = Color.FromHex("#C3C3C3"); //remove from favourites here RemoveFromFavouriteRequest removeFromFavouriteRequest = new RemoveFromFavouriteRequest() { userId = OperationData.userId.ToString(), itemId = item.id.ToString() }; var res = await favouritesViewModel.RemoveFromFavourites(removeFromFavouriteRequest); if (res != null) { if (res.Code == 0) { foreach (var favItem in OperationData.FavouriteItemList) { if (item.id == favItem.id) { OperationData.FavouriteItemList.Remove(favItem); } } await DisplayAlert("Message", item.foodName + " removed from favourites", null, "OK"); } else { await DisplayAlert("Message", "Couldn't remove " + item.foodName + " from favourites. Please try again", null, "OK"); await AnimateHeart(button); fontImageSource.Glyph = ""; fontImageSource.Color = Color.FromHex("#F25278"); } } else { await DisplayAlert("Message", "Couldn't remove " + item.foodName + " from favourites. Please try again", null, "OK"); await AnimateHeart(button); fontImageSource.Glyph = ""; fontImageSource.Color = Color.FromHex("#F25278"); } } } catch (Exception ex) { } finally { button.IsEnabled = true; } }