Example #1
0
        // Creating JSON object to represent new item, and then sending it to backend to be inserted into database table
        public async void OnAdd(object sender, EventArgs e)
        {
            string sid = await App.Authenticator.GetUserId();

            // Checking that user has filled in all required fields
            if (string.IsNullOrEmpty(newItemName.Text) || newItemCategory.SelectedIndex.Equals(-1) || string.IsNullOrEmpty(newItemDescription.Text) || string.IsNullOrEmpty(newItemLendDuration.Text) || string.IsNullOrEmpty(newItemLocation.Text))
            {
                await DisplayAlert("Error", "All fields must be completed", "Ok");
            }
            else
            {
                var location = newItemLocation.Text;
                var latitude = await getLatitudeFromLocation(location);

                var longitude = await getLongitudeFromLocation(location);

                int duration;
                if (int.TryParse(newItemLendDuration.Text, out duration) && duration > 0)
                {
                    buttonsPanel.IsVisible = false;
                    using (var scope = new ActivityIndicatorScope(syncIndicator, true))
                    {
                        var boardgames = new Boardgames {
                            Name = newItemName.Text, Description = newItemDescription.Text, Lend_duration = Int32.Parse(newItemLendDuration.Text), Location = location, Latitude = latitude, Longitude = longitude, Owner = sid, Borrowed = false, Category = newItemCategory.Items[newItemCategory.SelectedIndex]
                        };
                        await AddItem(boardgames);

                        //Add item photo to storage account
                        if (itemImageFile != null)
                        {
                            var boardGamesTable = await manager.GetBoardgamesAsync();

                            var itemId = boardGamesTable.Where(b => (String.Equals(b.Owner, sid)))
                                         .OrderByDescending(b => b.CreatedAt)
                                         .Select(b => b.Id)
                                         .ElementAt(0);
                            var itemName = await ImageManager.GenerateItemPhotoName(itemId);

                            await ImageManager.UploadImage(itemImageFile.GetStream(), itemName);

                            itemImageFile.Dispose();
                            itemImageFile = null;
                        }
                    }
                    buttonsPanel.IsVisible = true;
                    await DisplayAlert("Success", "Your item has been added", "Ok");

                    // Empty all fields after item has been successfully added
                    newItemName.Text              = string.Empty;
                    newItemDescription.Text       = descriptionPlaceholder;
                    newItemDescription.TextColor  = Color.Gray;
                    newItemLendDuration.Text      = string.Empty;
                    newItemLocation.Text          = string.Empty;
                    newItemCategory.SelectedIndex = -1;
                    newItemName.Unfocus();
                    itemImage.Source = null;
                }
                else
                {
                    await DisplayAlert("Error", "Number of days must be a positive whole number", "Ok");
                }
            }
        }