Renato Lulic
Renato Lulic

Reputation: 270

Kotlin Upload Image to Firebase Animation or setContentLayout to another activity while uploading

I want to show an animation (like loading bar) while the photo is being uploaded to the firebase or set the content layout to the user's profile and show an animation there while the photo is being uploaded.

Right now, when I click "Share" button it just acts like the "Share" isn't clicked so user's click it multiple times without knowing that there will be as many photos as many times they clicked "Share"

here is my share function:

fun share(user: User, imageUri: Uri?, caption: String) {

    if (imageUri != null) {
        usersRepo.uploadUserImage(user.uid, imageUri).onSuccessTask { downloadUrl ->
            Tasks.whenAll(
                    feedPostsRepo.createFeedPost(user.uid, mkFeedPost(user, caption,
                            downloadUrl.toString()))
            )
        }.addOnCompleteListener{
            _shareCompletedEvent.call()
        }.addOnFailureListener(onFailureListener)
    }
}

Upvotes: 1

Views: 92

Answers (1)

mightyWOZ
mightyWOZ

Reputation: 8345

You can add a ProgressBar to your activity/fragment content xml

     <!-- Set colors etc as per your choice -->
     <ProgressBar
            android:id="@+id/progressBar_myActivity"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:backgroundTint="#FFFFFF"
            android:foregroundTint="#FFFFFF"
            android:indeterminate="true"
            android:indeterminateTint="#FFFFFF"
            android:progressTint="#FFFFFF"
            android:visibility="invisible" />

And when share button is clicked you show the ProgressBar and disable the screen interaction using

progressBar_myActivity.visibility = View.VISIBLE  //show progress bar
window.setFlags(                                  //disable view for user interaction
      WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
      WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
)

And once you get the api response you can show the appropriate message and hide the ProgressBar and enable user interaction using

window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
progressBar_myActivity.visibility = View.GONE
// show appropriate message

Please note that if your api response takes considerable amount of time, then try to move them to some other UI element as halting interaction for long times is not very good UI/UX feature.

Upvotes: 2

Related Questions