Search in sources :

Example 1 with PairUp

use of garbagecollectors.com.unipool.models.PairUp in project UniPool by divya21raj.

the class UtilityMethods method getMessageList.

public static List<Message> getMessageList(HashMap<String, ArrayList<Message>> messages, String userId) {
    ArrayList<Message> personalMessageList;
    HashMap<String, PairUp> pairUpList = BaseActivity.getFinalCurrentUser().getPairUps();
    String pairUpId = null;
    for (Map.Entry<String, PairUp> entry : pairUpList.entrySet()) {
        PairUp pairUp = entry.getValue();
        if (pairUp.getCreatorId().equals(userId) || pairUp.getRequesterId().equals(userId)) {
            pairUpId = pairUp.getPairUpId();
            break;
        }
    }
    List messageList = messages.get(pairUpId);
    if (messageList != null)
        personalMessageList = new ArrayList<>(messages.get(pairUpId));
    else {
        personalMessageList = new ArrayList<>();
        messages.put(pairUpId, personalMessageList);
    }
    return personalMessageList;
}
Also used : Message(garbagecollectors.com.unipool.models.Message) ArrayList(java.util.ArrayList) PairUp(garbagecollectors.com.unipool.models.PairUp) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with PairUp

use of garbagecollectors.com.unipool.models.PairUp in project UniPool by divya21raj.

the class UtilityMethods method populateChatMap.

public static Task populateChatMap(DataSnapshot pairUpSnapshot) {
    final String[] userId = new String[1];
    ArrayList<PairUp> pairUps = new ArrayList<>();
    for (DataSnapshot dataSnapshot : pairUpSnapshot.getChildren()) pairUps.add(dataSnapshot.getValue(PairUp.class));
    Task task = accessUserDatabase("users");
    HashMap<String, User> finalChatMap = new HashMap<>();
    task.addOnCompleteListener(aTask -> {
        DataSnapshot snapshot = (DataSnapshot) task.getResult();
        for (PairUp pairUp : pairUps) {
            if (!(pairUp.getCreatorId().equals("dummy"))) {
                if (pairUp.getCreatorId().equals(BaseActivity.getFinalCurrentUser().getUserId()))
                    userId[0] = pairUp.getRequesterId();
                else
                    userId[0] = pairUp.getCreatorId();
                User user = snapshot.child(userId[0]).getValue(User.class);
                if (user != null) {
                    finalChatMap.put(user.getUserId(), user);
                }
            }
        }
        BaseActivity.setChatMap(finalChatMap);
    });
    return task;
}
Also used : Task(com.google.android.gms.tasks.Task) User(garbagecollectors.com.unipool.models.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PairUp(garbagecollectors.com.unipool.models.PairUp) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 3 with PairUp

use of garbagecollectors.com.unipool.models.PairUp in project UniPool by divya21raj.

the class ReceivedRequestsTEA method onBindViewHolder.

// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
    requestsProgressDialog = new ProgressDialog(context);
    requestsProgressDialog.setMessage("Please wait...");
    requestsProgressDialog.show();
    holder.cardArrow.setVisibility(View.GONE);
    holder.messageCard.setVisibility(View.GONE);
    MessageDBTask.addOnCompleteListener(o -> requestsProgressDialog.dismiss());
    MessageDBTask.addOnFailureListener(o -> requestsProgressDialog.dismiss());
    holder.requestButton.setOnClickListener(view -> {
        DatabaseReference userDatabaseReference = FirebaseDatabase.getInstance().getReference(Globals.UNI + "users");
        DatabaseReference pairUpDatabaseReference = Globals.pairUpDatabaseReference;
        DatabaseReference notificationDatabaseReference = Globals.notificationDatabaseReference;
        TripEntry tripEntry = list.get(position);
        User finalCurrentUser = BaseActivity.getFinalCurrentUser();
        HashMap<String, PairUp> currentUserPairUps = finalCurrentUser.getPairUps();
        HashMap<String, ArrayList<String>> finalCurrentUserReceivedRequests = finalCurrentUser.getRequestsReceived();
        ReceivedRequestsFragment.alertDialogBuilder.setPositiveButton("YES", (dialog, which) -> {
            requestsProgressDialog.show();
            final User[] tripEntryUser = new User[1];
            // the user that created the clicked tripEntry
            Task userTask = accessUserDatabase("users/" + tripEntry.getUser_id());
            userTask.addOnSuccessListener(aVoid -> {
                DataSnapshot snapshot = (DataSnapshot) userTask.getResult();
                tripEntryUser[0] = snapshot.getValue(User.class);
                HashMap<String, PairUp> tripEntryUserPairUps = null;
                if (tripEntryUser[0] != null)
                    tripEntryUserPairUps = tripEntryUser[0].getPairUps();
                else
                    Toast.makeText(context, "Problems! Please try again...", Toast.LENGTH_LONG);
                HashMap<String, TripEntry> tripEntryUserSentRequests = tripEntryUser[0].getRequestSent();
                String pairUpId = finalCurrentUser.getUserId() + tripEntryUser[0].getUserId();
                String[] timeParts = tripEntry.getTime().split(":");
                // give them 4 hours
                timeParts[0] = String.valueOf((Integer.parseInt(timeParts[0]) + 4) % 24);
                String expiryDate = tripEntry.getDate() + "/" + timeParts[0] + "/" + timeParts[1];
                PairUp pairUp = new PairUp(pairUpId, finalCurrentUser.getUserId(), tripEntryUser[0].getUserId(), expiryDate, new ArrayList<>());
                pairUp.getMessages().add("def@ult");
                isAlreadyInList = UtilityMethods.addPairUpInMap(currentUserPairUps, pairUp);
                if (!isAlreadyInList) {
                    UtilityMethods.addPairUpInMap(tripEntryUserPairUps, pairUp);
                    UtilityMethods.removeFromMap(finalCurrentUserReceivedRequests, tripEntry.getEntry_id(), tripEntryUser[0].getUserId());
                    tripEntryUserSentRequests.remove(tripEntry.getEntry_id());
                    finalCurrentUser.setRequestsReceived(finalCurrentUserReceivedRequests);
                    finalCurrentUser.setPairUps(currentUserPairUps);
                    BaseActivity.setFinalCurrentUser(finalCurrentUser);
                    HashMap<String, String> notificationObject = new HashMap<>();
                    notificationObject.put("from", finalCurrentUser.getUserId());
                    notificationObject.put("type", "requestAccepted");
                    Task<Void> task1 = userDatabaseReference.child(finalCurrentUser.getUserId()).child("pairUps").setValue(currentUserPairUps);
                    Task<Void> task2 = userDatabaseReference.child(tripEntryUser[0].getUserId()).child("pairUps").setValue(tripEntryUserPairUps);
                    Task<Void> task3 = userDatabaseReference.child(finalCurrentUser.getUserId()).child("requestsReceived").setValue(finalCurrentUserReceivedRequests);
                    Task<Void> task4 = userDatabaseReference.child(tripEntryUser[0].getUserId()).child("requestSent").setValue(tripEntryUserSentRequests);
                    Task<Void> task5 = pairUpDatabaseReference.child(pairUpId).setValue(pairUp);
                    Task<Void> task6 = notificationDatabaseReference.child(tripEntryUser[0].getUserId()).push().setValue(notificationObject);
                    Task<Void> allTask = Tasks.whenAll(task1, task2, task3, task4, task5, task6);
                    allTask.addOnSuccessListener(bVoid -> {
                        tripEntry.setEntry_id("dummyId");
                        userDatabaseReference.child((tripEntryUser[0]).getUserId()).child("requestSent").child("dummyId").setValue(tripEntry);
                        BaseActivity.getChatMap().put(tripEntryUser[0].getUserId(), tripEntryUser[0]);
                        requestsProgressDialog.dismiss();
                        // RequestActivity.refreshRequests(context);
                        Intent intent = new Intent(context, RequestActivity.class);
                        intent.putExtra("openingTab", 2);
                        ((Activity) context).finish();
                        context.startActivity(intent);
                        ((Activity) context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    });
                    allTask.addOnFailureListener(e -> {
                        requestsProgressDialog.dismiss();
                        // apologize profusely to the user!
                        Toast.makeText(view.getContext(), "FAIL", Toast.LENGTH_LONG).show();
                    });
                }
                dialog.dismiss();
            });
        });
        ReceivedRequestsFragment.alertDialogBuilder.setNegativeButton("NO", (dialog, which) -> dialog.dismiss());
        AlertDialog alert = ReceivedRequestsFragment.alertDialogBuilder.create();
        alert.show();
    });
    TripEntry tripEntry = list.get(position);
    UtilityMethods.fillTripEntryHolder(holder, tripEntry);
}
Also used : AlertDialog(android.support.v7.app.AlertDialog) Task(com.google.android.gms.tasks.Task) MessageDBTask(garbagecollectors.com.unipool.activities.SplashActivity.MessageDBTask) User(garbagecollectors.com.unipool.models.User) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) RequestActivity(garbagecollectors.com.unipool.activities.RequestActivity.RequestActivity) BaseActivity(garbagecollectors.com.unipool.activities.BaseActivity) Activity(android.app.Activity) TripEntry(garbagecollectors.com.unipool.models.TripEntry) Intent(android.content.Intent) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot) PairUp(garbagecollectors.com.unipool.models.PairUp)

Example 4 with PairUp

use of garbagecollectors.com.unipool.models.PairUp in project UniPool by divya21raj.

the class LoginActivity method dummyInitFinalCurrentUser.

private void dummyInitFinalCurrentUser(FirebaseUser user) {
    GenLocation dummyGenLocation = new GenLocation("dummy", "dummy", 0d, 0d);
    TripEntry dummyTripEntry = new TripEntry("dummy", "dummyId", "DummyUser", "12:00", "1/11/12", dummyGenLocation, dummyGenLocation, "dummyMessage", "", "", true);
    HashMap<String, TripEntry> dummyUserEntries = new HashMap<>();
    dummyUserEntries.put("dummy", dummyTripEntry);
    HashMap<String, TripEntry> dummyRequestSent = new HashMap<>();
    dummyRequestSent.put(dummyTripEntry.getEntry_id(), dummyTripEntry);
    ArrayList<String> dummyUserIdList = new ArrayList<>();
    dummyUserIdList.add("dummy");
    HashMap<String, ArrayList<String>> dummyRequestReceived = new HashMap<>();
    dummyRequestReceived.put("dummy", dummyUserIdList);
    PairUp dummyPairUp = new PairUp("dummydummy", "dummy", "dummy", "dummy", dummyUserIdList);
    HashMap<String, PairUp> dummyPairUps = new HashMap<>();
    dummyPairUps.put("dummy", dummyPairUp);
    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(instanceIdResult -> {
        String deviceToken = instanceIdResult.getToken();
        String url = "";
        Uri photoUrl = user.getPhotoUrl();
        if (photoUrl != null)
            url = photoUrl.toString();
        finalCurrentUser = new User(user.getUid(), user.getDisplayName(), url, user.getEmail(), dummyUserEntries, dummyRequestSent, dummyRequestReceived, deviceToken, true, dummyPairUps);
    });
}
Also used : User(garbagecollectors.com.unipool.models.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) BaseActivity.finalCurrentUser(garbagecollectors.com.unipool.activities.BaseActivity.finalCurrentUser) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PairUp(garbagecollectors.com.unipool.models.PairUp) TripEntry(garbagecollectors.com.unipool.models.TripEntry) GenLocation(garbagecollectors.com.unipool.models.GenLocation) Uri(android.net.Uri)

Aggregations

PairUp (garbagecollectors.com.unipool.models.PairUp)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 User (garbagecollectors.com.unipool.models.User)3 Task (com.google.android.gms.tasks.Task)2 FirebaseUser (com.google.firebase.auth.FirebaseUser)2 DataSnapshot (com.google.firebase.database.DataSnapshot)2 TripEntry (garbagecollectors.com.unipool.models.TripEntry)2 Activity (android.app.Activity)1 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 AlertDialog (android.support.v7.app.AlertDialog)1 DatabaseReference (com.google.firebase.database.DatabaseReference)1 BaseActivity (garbagecollectors.com.unipool.activities.BaseActivity)1 BaseActivity.finalCurrentUser (garbagecollectors.com.unipool.activities.BaseActivity.finalCurrentUser)1 RequestActivity (garbagecollectors.com.unipool.activities.RequestActivity.RequestActivity)1 MessageDBTask (garbagecollectors.com.unipool.activities.SplashActivity.MessageDBTask)1 GenLocation (garbagecollectors.com.unipool.models.GenLocation)1 Message (garbagecollectors.com.unipool.models.Message)1