The Lazy Programmer
The Lazy Programmer

Reputation: 55

Progress Dialog inside OnSuccess method, Firebase-Firestore?

I am uploading an ArrayList to firebase-storage and then retrieving download URL and then storing custom data object into firebase-firestore. I dont know when to stop the progress dialog.

public void uploadNotesToSTORAGE(final ArrayList<NoteItem> nItems, final Activity activity) {
    final ProgressDialog dialog = new ProgressDialog(activity);
    dialog.setMessage("Uploading picture...");
    dialog.show();

    for (int i = 0; i < nItems.size(); i++) {
        final String id = Calendar.getInstance().getTimeInMillis() + "";
        StorageReference reference = mStorageRef.child("notes/" + id);
        String filePath = nItems.get(i).getFilePath();
        final int finalI = i;
        final int finalI1 = i;
        reference.putFile(Uri.fromFile(new File(filePath))).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                StorageReference reference = mStorageRef.child("notes/" + id);
                reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri dUri) {
                        nItems.get(finalI).setId(id);
                        nItems.get(finalI).setDownloadLink(dUri + "");
                        db.collection("notes").document().set(nItems.get(finalI))
                                .addOnSuccessListener(new OnSuccessListener<Void>() {
                                    @Override
                                    public void onSuccess(Void aVoid) {

                                        Toast.makeText(activity, "Successfully uploaded : " + nItems.get(finalI1).getFileName(), Toast.LENGTH_SHORT).show();
                                    }
                                });
                    }
                });
            }
        }).addOnFailureListener(onFailureListener);
        dialog.dismiss();
        activity.finish();
        activity.startActivity(new Intent(activity, MainActivity.class));
    }
}

Upvotes: 0

Views: 341

Answers (1)

Naresh NK
Naresh NK

Reputation: 1066

reference.putFile(file) uploads file asynchronously so don't upload all files in a for loop.

Instead of that create a function which uploads a file, and when a file is uploaded successfully in onSuccess remove the uploaded file object from the list and send the next file object for upload.

And when there is no file left in the files list dismiss the dialog.

It's just like a queue.

private ProgressDialog dialog;
private ArrayList<NoteItem> nItems

public void uploadNotesToSTORAGE(final ArrayList<NoteItem> items, Activity activity) {
    nItems = items

    dialog = new ProgressDialog(activity);
    dialog.setMessage("Uploading picture...");
    dialog.show();


    if(nItems != null && !nItems.isEmpty()){
      uploadSingleNote(nItems.get(0));        //get first item
     }
}

private void uploadSingleNote(NoteItem item){
    final String id = Calendar.getInstance().getTimeInMillis() + "";
    StorageReference reference = mStorageRef.child("notes/" + id);
    String filePath = item.getFilePath();

    reference.putFile(Uri.fromFile(new File(filePath))).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            StorageReference reference = mStorageRef.child("notes/" + id);
            reference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri dUri) {
                    item.setId(id);
                    item.setDownloadLink(dUri + "");
                    db.collection("notes").document().set(nItems.get(finalI))
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(activity, "Successfully uploaded : " + item.getFileName(), Toast.LENGTH_SHORT).show();
                            nItems.remove(0);
                            if(nItems != null && !nItems.isEmpty()){
                               uploadSingleNote(nItems.get(0));        //get first item
                             }else{
                              dialog.dismiss();
                                activity.finish();
                              activity.startActivity(new Intent(activity, MainActivity.class));
                             }
                          }
                     });
                }
            });
        }
    }).addOnFailureListener(onFailureListener);
}

Upvotes: 2

Related Questions