Search in sources :

Example 1 with User

use of garbagecollectors.com.unipool.models.User 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 2 with User

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

the class UtilityMethods method populateReceivedRequestsList.

public static Task populateReceivedRequestsList(ArrayList<TripEntry> receivedRequestsList, HashMap<String, ArrayList<String>> receivedRequestsMap, ArrayList<TripEntry> tripEntries) {
    final TripEntry[] temp = new TripEntry[1];
    // all users
    Task userTask = accessUserDatabase("users");
    userTask.addOnSuccessListener(aVoid -> {
        DataSnapshot snapshot = (DataSnapshot) userTask.getResult();
        for (Map.Entry<String, ArrayList<String>> entry : receivedRequestsMap.entrySet()) {
            if (!(entry.getKey().equals("dummy"))) {
                TripEntry tripEntry = getTripEntryFromList(entry.getKey(), tripEntries);
                if (tripEntry != null) {
                    for (String userId : entry.getValue()) {
                        final User[] user = new User[1];
                        user[0] = snapshot.child(userId).getValue(User.class);
                        if (user[0] != null) {
                            temp[0] = new TripEntry(tripEntry);
                            temp[0].setName(user[0].getName());
                            temp[0].setUser_id(user[0].getUserId());
                            receivedRequestsList.add(temp[0]);
                        }
                    }
                }
            }
        }
    });
    return userTask;
}
Also used : Task(com.google.android.gms.tasks.Task) User(garbagecollectors.com.unipool.models.User) FirebaseUser(com.google.firebase.auth.FirebaseUser) ArrayList(java.util.ArrayList) TripEntry(garbagecollectors.com.unipool.models.TripEntry) DataSnapshot(com.google.firebase.database.DataSnapshot) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with User

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

the class HomeActivityTEA method sendRequest.

private void sendRequest(View view, int position) {
    alertDialogBuilder.setMessage("Send request to " + list.get(position).getName() + " ?");
    alertDialogBuilder.setPositiveButton("YES", (dialog, which) -> {
        progressDialog = new ProgressDialog(view.getContext());
        progressDialog.setMessage("Please wait...");
        User user = BaseActivity.getFinalCurrentUser();
        TripEntry tripEntry = list.get(position);
        if (tripEntry.getUser_id().equals(user.getUserId())) {
            Toast.makeText(view.getContext(), "Can't pool with yourself, that feature isn't ready yet...", Toast.LENGTH_SHORT).show();
            return;
        }
        progressDialog.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);
            DatabaseReference userDatabaseReference = FirebaseDatabase.getInstance().getReference(Globals.UNI + "users");
            HashMap<String, TripEntry> requestSent = user.getRequestSent();
            HashMap<String, ArrayList<String>> requestsReceived = tripEntryUser[0].getRequestsReceived();
            isAlreadyRequested = addRequestInList(requestSent, user.getPairUps(), tripEntry);
            if (!isAlreadyRequested) {
                isRequestAlreadyInMap = putInMap(requestsReceived, tripEntry.getEntry_id(), user.getUserId());
            }
            user.setRequestSent(requestSent);
            tripEntryUser[0].setRequestsReceived(requestsReceived);
            if (!isAlreadyRequested && !isRequestAlreadyInMap) {
                // update firebase database to include arrayList that contains name of the item_trip_entry clicked in requests sent...
                Task<Void> task1 = userDatabaseReference.child(user.getUserId()).child("requestSent").setValue(requestSent);
                Task<Void> task2 = userDatabaseReference.child(tripEntryUser[0].getUserId()).child("requestsReceived").setValue(requestsReceived);
                HashMap<String, String> notificationObject = new HashMap<>();
                notificationObject.put("from", user.getUserId());
                notificationObject.put("type", "requestCreated");
                Task<Void> task3 = Globals.notificationDatabaseReference.child(tripEntryUser[0].getUserId()).push().setValue(notificationObject);
                Task<Void> allTask = Tasks.whenAll(task1, task2, task3);
                allTask.addOnSuccessListener(bVoid -> {
                    progressDialog.dismiss();
                    BaseActivity.setFinalCurrentUser(user);
                    Toast.makeText(view.getContext(), "Request Sent!", Toast.LENGTH_LONG).show();
                });
                allTask.addOnFailureListener(e -> {
                    progressDialog.dismiss();
                    // apologize profusely to the user!
                    Toast.makeText(view.getContext(), "FAIL", Toast.LENGTH_LONG).show();
                });
            } else {
                progressDialog.dismiss();
                Toast.makeText(view.getContext(), "You two are already paired up!", 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) User(garbagecollectors.com.unipool.models.User) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TripEntry(garbagecollectors.com.unipool.models.TripEntry) ProgressDialog(android.app.ProgressDialog) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 4 with User

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

the class UserAdapter method onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull MyHolder holder, int position) {
    User user = userList.get(position);
    UtilityMethods.fillUserHolder(holder, user);
    holder.itemView.setOnClickListener(view -> {
        // TreeMap<Long, Message> messageMap = UtilityMethods.getPersonalMessageMap(BaseActivity.getMessages(), user.getUserId());
        MessageListActivity.setChatUser(user);
        MessageListActivity.setName(UtilityMethods.sanitizeName(user.getName()));
        // MessageListActivity.setPersonalMessageMap(messageMap);
        MessageListActivity.setPairUp(UtilityMethods.getPairUp(user, BaseActivity.getFinalCurrentUser().getPairUps()));
        context.startActivity(new Intent(context, MessageListActivity.class));
    });
}
Also used : MessageListActivity(garbagecollectors.com.unipool.activities.MessageListActivity) User(garbagecollectors.com.unipool.models.User) Intent(android.content.Intent)

Example 5 with User

use of garbagecollectors.com.unipool.models.User 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)

Aggregations

User (garbagecollectors.com.unipool.models.User)7 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 Task (com.google.android.gms.tasks.Task)4 DataSnapshot (com.google.firebase.database.DataSnapshot)4 TripEntry (garbagecollectors.com.unipool.models.TripEntry)4 FirebaseUser (com.google.firebase.auth.FirebaseUser)3 PairUp (garbagecollectors.com.unipool.models.PairUp)3 ProgressDialog (android.app.ProgressDialog)2 Intent (android.content.Intent)2 AlertDialog (android.support.v7.app.AlertDialog)2 DatabaseReference (com.google.firebase.database.DatabaseReference)2 Map (java.util.Map)2 Activity (android.app.Activity)1 Uri (android.net.Uri)1 DefaultItemAnimator (android.support.v7.widget.DefaultItemAnimator)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 BaseActivity (garbagecollectors.com.unipool.activities.BaseActivity)1