Why does Tasks whenAllSuccess not working?

Viewed 12

I have a recyclerview for a list of members. When I long click 1 item in the recyclerview, ActionMode is enabled and I can select multiple members who wants to pay their annual dues. After selecting members, I will touch on pay button to submit payments. But it just say Added but not inserted inside the database.

Please look at my code

private void pay(ActionMode mode) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Confirm Annual Due")
            .setMessage("Please confirm to add annual due")
            .setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    progressDialog.setTitle("Adding New Annual Due");
                    progressDialog.setMessage("Please wait...");
                    progressDialog.setCancelable(false);
                    progressDialog.create();
                    progressDialog.show();

                    List<WriteBatch> writeBatches = new ArrayList<>();
                    List<Task<Void>> tasks = new ArrayList<>();
                    writeBatches.add(db.batch());
                    int counter = 0, batchIndex = 0;

                    for(Faculty faculty : selectList){
                        Map<String, Object> map = new HashMap<>();
                        map.put("facultyId", faculty.getId());
                        map.put("photoUrl", faculty.getPhotoUrl());
                        map.put("fullName", faculty.getFullName());
                        map.put("campus", faculty.getCampus());
                        map.put("acadRank", faculty.getAcadRank());
                        map.put("position", faculty.getPosition());
                        map.put("memberId", faculty.getMemberId());
                        map.put("acctType", faculty.getAcctType());
                        map.put("acctStatus", faculty.getAcctStatus());
                        map.put("amtPaid", String.valueOf(ANN_DUE_AMT));
                        map.put("datePaid", LocalDate.now().toString());
                        map.put("ye`ar", String.valueOf(LocalDate.now().getYear()));
                        map.put("status", "Pending");

                        map.values().removeAll(Collections.singleton(null));
                        writeBatches.get(batchIndex).set(annDueRef.document(), map);
                        counter++;

                        if(counter == 499){
                            tasks.add(writeBatches.get(batchIndex).commit());
                            writeBatches.add(db.batch());
                            counter = 0;
                            batchIndex++;
                        }
                    }

                    Tasks.whenAllSuccess(tasks)
                            .addOnCompleteListener(new OnCompleteListener<List<Object>>() {
                                @Override
                                public void onComplete(@NonNull @NotNull Task<List<Object>> task) {
                                    progressDialog.dismiss();

                                    if(task.isSuccessful()){
                                        Toast.makeText(context, "Added", Toast.LENGTH_SHORT).show();
                                        notifyDataSetChanged();
                                        selectList.clear();
                                        mode.finish();
                                    }else{
                                        Toast.makeText(context, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                }
            })
            .setNegativeButton("Cancel", null)
            .create().show();
}
0 Answers
Related