Beispiel #1
0
        private async void postButton_Clicked(object sender, EventArgs e)
        {
            bool formValid       = true;
            var  postTitle       = title.Text;
            var  postDescription = description.Text;

            imageUrl = "";
            var postId = $@"{Guid.NewGuid()}";

            // Form validation

            // validate post title
            if (string.IsNullOrEmpty(postTitle))
            {
                titleErrorLabel.Text      = "Title cannot be empty";
                titleErrorLabel.IsVisible = true;
                formValid = false;
            }
            else
            {
                titleErrorLabel.IsVisible = false;
            }

            // validate post description
            if (string.IsNullOrEmpty(postDescription))
            {
                descriptionErrorLabel.Text      = "Description cannot be empty";
                descriptionErrorLabel.IsVisible = true;
                formValid = false;
            }
            else
            {
                descriptionErrorLabel.IsVisible = false;
            }

            if (categoryPicker.SelectedItem == null)
            {
                categoryErrorLabel.Text      = "Please select a category";
                categoryErrorLabel.IsVisible = true;
                formValid = false;
            }
            else
            {
                categoryErrorLabel.IsVisible = false;
            }

            if (formValid)
            {
                loader.IsVisible     = true;
                loader.IsRunning     = true;
                postButton.IsEnabled = false;
                // only upload file to Firebase Storage if the form is valid
                if (file != null)
                {
                    // upload image to Firebase Storage
                    var uniqueFileName = postId;

                    try
                    {
                        imageUrl = await firebaseHelper.UploadImage(file.GetStream(), uniqueFileName);
                    }
                    catch (Exception ex)
                    {
                        postButton.IsEnabled = true;
                        await DisplayAlert("Alert", "Fail to Upload Image: " + ex.Message, "OK");
                    }
                }

                // Add post to Realtime Database
                try
                {
                    await firebaseHelper.AddPost(postId, postTitle, postDescription, CategorySelectedItem.categoryName, imageUrl);

                    loader.IsVisible     = false;
                    loader.IsRunning     = false;
                    postButton.IsEnabled = true;
                    await DisplayAlert("Alert", "Post Added", "OK");

                    Categories.Clear();
                    BindingContext       = this;
                    App.Current.MainPage = new MasterDetailPageKitaBantu();
                }
                catch (Exception ex)
                {
                    loader.IsVisible     = false;
                    loader.IsRunning     = false;
                    postButton.IsEnabled = true;
                    await DisplayAlert("Alert", "Fail to Add Post: " + ex.Message, "OK");
                }
            }
        }