Search in sources :

Example 41 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class TeamActivity method populateTeamList.

/**
 * Populates the list of teams associated with the user
 */
private void populateTeamList() {
    // Get the reference to the UI contents
    final ListView teamListView = findViewById(R.id.teamList);
    // retrieve database reference to the teams
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference teamsReference = database.getReference("teams");
    teamAdapter = new TeamsAdapter(this, new ArrayList<Team>());
    teamsReference.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
                if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) == -1)
                    teamAdapter.add(dataSnapshot.getValue(Team.class));
            }
            if (dataSnapshot.getValue(Team.class).getOwner().equals(user.getUsername())) {
                TeamActivity.this.handleJoinRequests(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
                if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) == -1) {
                    teamAdapter.add(dataSnapshot.getValue(Team.class));
                } else {
                    teamAdapter.remove(teamAdapter.getItem(teamAdapter.getPosition(dataSnapshot.getValue(Team.class))));
                    teamAdapter.add(dataSnapshot.getValue(Team.class));
                }
            } else if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1) {
                teamAdapter.remove(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
                if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1)
                    teamAdapter.remove(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            Log.d("TeamList", dataSnapshot.getValue(Team.class).getName() + "has moved locations...");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    teamListView.setAdapter(teamAdapter);
    teamListView.setOnItemClickListener(new TeamListClickListener());
}
Also used : TeamsAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter) FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 42 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class TeamManager method getTeamInvites.

/**
 * Retrieves a list of team invitations for the specified user.  Returns a list of teams
 * where invitations are active through the callback.  If no invitations are active then the
 * returned list is empty.
 *
 * @param user User to check for team invitations
 * @param listener Callback to receive list of invitations
 */
public static void getTeamInvites(User user, final TeamListListener listener) {
    final ArrayList<Team> teamInvitations = new ArrayList<>();
    // retrieve database reference to the teams
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference teamsReference = database.getReference("teams_invitations/" + user.getUsername());
    teamsReference.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // return an empty list if no records found
            if (!dataSnapshot.exists()) {
                listener.onInvitationsPopulated(new ArrayList<Team>());
                return;
            }
            Iterable<DataSnapshot> invitations = dataSnapshot.getChildren();
            for (DataSnapshot invitation : invitations) {
                teamInvitations.add(invitation.getValue(Team.class));
            }
            listener.onInvitationsPopulated(teamInvitations);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 43 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class TeamManager method addMemberToTeam.

/**
 * Adds the user to the specified team.
 *
 * @param team Team that user belongs to
 * @param user User to be removed
 */
public static void addMemberToTeam(Team team, final User user) {
    // retrieve database reference to the teams
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference teamsReference = database.getReference("teams/" + team.getId());
    teamsReference.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Team team = dataSnapshot.getValue(Team.class);
                team.addTeamMember(user.getUsername());
                teamsReference.setValue(team);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 44 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class TeamManager method requestToJoinTeam.

/**
 * Creates a request to join a team
 *
 * @param team Team to be joined
 * @param user User requesting to join the team
 */
public static void requestToJoinTeam(final Team team, final User user) {
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference joinRequestsReference = database.getReference("join_requests/" + team.getId());
    // attempt to write the data
    joinRequestsReference.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            joinRequestsReference.child(user.getUsername()).setValue(System.currentTimeMillis());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 45 with DatabaseReference

use of com.google.firebase.database.DatabaseReference in project SEProject by NicholasBarreyre.

the class TeamManager method handleInvitation.

/**
 * Updates team membership based on whether the user accepted or declined the invitation
 *
 * @param user User responding to an invitation
 * @param team Team associated with the invitation
 * @param accepted Whether or not the user accepted or declined.  True if accepted, otherwise false
 */
public static void handleInvitation(final User user, final Team team, boolean accepted) {
    // retrieve database reference to the teams
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference teamOwnerReference = database.getReference("users/" + team.getOwner());
    if (accepted) {
        teamOwnerReference.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                TeamManager.addMemberToTeam(team, user);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
    // retrieve database reference to the teams
    final DatabaseReference userInvitations = database.getReference("teams_invitations/" + user.getUsername());
    userInvitations.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Iterable<DataSnapshot> invitations = dataSnapshot.getChildren();
            for (DataSnapshot invitation : invitations) {
                if (invitation.getValue(Team.class).getName().equals(team.getName())) {
                    userInvitations.child(invitation.getKey()).removeValue();
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Aggregations

DatabaseReference (com.google.firebase.database.DatabaseReference)388 DatabaseError (com.google.firebase.database.DatabaseError)219 DataSnapshot (com.google.firebase.database.DataSnapshot)218 ValueEventListener (com.google.firebase.database.ValueEventListener)172 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)100 Intent (android.content.Intent)66 HashMap (java.util.HashMap)59 View (android.view.View)58 ArrayList (java.util.ArrayList)36 TextView (android.widget.TextView)33 Test (org.junit.Test)29 NonNull (androidx.annotation.NonNull)27 FirebaseAuth (com.google.firebase.auth.FirebaseAuth)24 ChildEventListener (com.google.firebase.database.ChildEventListener)23 FirebaseUser (com.google.firebase.auth.FirebaseUser)21 MutableData (com.google.firebase.database.MutableData)21 ImageView (android.widget.ImageView)20 Transaction (com.google.firebase.database.Transaction)20 ProgressDialog (android.app.ProgressDialog)19 Query (com.google.firebase.database.Query)19