use of garbagecollectors.com.unipool.models.TripEntry in project UniPool by divya21raj.
the class HomeActivityTEA method deleteEntry.
private void deleteEntry(View view, int position) {
final Integer[] i = { 0 };
TaskCompletionSource<DataSnapshot> userDBSource = new TaskCompletionSource<>();
Task userDBTask = userDBSource.getTask();
alertDialogBuilder.setMessage("Delete this entry?");
alertDialogBuilder.setPositiveButton("YES", (dialog, which) -> {
progressDialog = new ProgressDialog(view.getContext());
progressDialog.setMessage("Please wait...");
progressDialog.show();
TripEntry tripEntry = list.get(position);
Task<Void> task1 = Globals.entryDatabaseReference.child(tripEntry.getEntry_id()).removeValue();
Globals.userDatabaseReference.child("userTripEntries").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
userDBSource.setResult(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
userDBSource.setException(databaseError.toException());
Toast.makeText(context, "Network error! Please try again...", Toast.LENGTH_SHORT).show();
}
});
Task<Void> allTasks = Tasks.whenAll(task1, userDBTask);
allTasks.addOnSuccessListener(aVoid -> {
DataSnapshot userTripEntryData = (DataSnapshot) userDBTask.getResult();
for (DataSnapshot dataSnapshot : userTripEntryData.getChildren()) {
TripEntry userTripEntry = dataSnapshot.getValue(TripEntry.class);
if (userTripEntry != null && userTripEntry.getEntry_id().equals(tripEntry.getEntry_id()))
break;
i[0]++;
}
Task<Void> task3 = Globals.userDatabaseReference.child("userTripEntries").child(tripEntry.getEntry_id()).removeValue();
task3.addOnSuccessListener(aVoid1 -> {
UtilityMethods.removeFromList(BaseActivity.getTripEntryList(), tripEntry.getEntry_id());
BaseActivity.getFinalCurrentUser().getUserTripEntries().remove(tripEntry.getEntry_id());
progressDialog.dismiss();
});
task3.addOnFailureListener(e -> {
progressDialog.dismiss();
Toast.makeText(view.getContext(), "Network error! Please try again...", Toast.LENGTH_LONG).show();
});
});
allTasks.addOnFailureListener(e -> {
progressDialog.dismiss();
Toast.makeText(view.getContext(), "Network error! Please try again...", Toast.LENGTH_LONG).show();
});
});
alertDialogBuilder.setNegativeButton("NO", (dialog, which) -> dialog.dismiss());
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
Aggregations