Search in sources :

Example 11 with TripEntry

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();
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Task(com.google.android.gms.tasks.Task) TripEntry(garbagecollectors.com.unipool.models.TripEntry) DataSnapshot(com.google.firebase.database.DataSnapshot) ProgressDialog(android.app.ProgressDialog) TaskCompletionSource(com.google.android.gms.tasks.TaskCompletionSource) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener)

Aggregations

TripEntry (garbagecollectors.com.unipool.models.TripEntry)11 Task (com.google.android.gms.tasks.Task)5 DataSnapshot (com.google.firebase.database.DataSnapshot)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 User (garbagecollectors.com.unipool.models.User)4 ProgressDialog (android.app.ProgressDialog)3 AlertDialog (android.support.v7.app.AlertDialog)3 Intent (android.content.Intent)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 DatabaseError (com.google.firebase.database.DatabaseError)2 DatabaseReference (com.google.firebase.database.DatabaseReference)2 ValueEventListener (com.google.firebase.database.ValueEventListener)2 PairUp (garbagecollectors.com.unipool.models.PairUp)2 Map (java.util.Map)2 Activity (android.app.Activity)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 RequiresApi (android.support.annotation.RequiresApi)1 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1