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());
}
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) {
}
});
}
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) {
}
});
}
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) {
}
});
}
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) {
}
});
}
Aggregations