use of com.google.firebase.database.FirebaseDatabase 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.FirebaseDatabase 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) {
}
});
}
use of com.google.firebase.database.FirebaseDatabase 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 TeamInvitationListener 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.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class TeamManager method getTeamMembers.
/**
* Retrieves a team from firebase for accessing the list of team members
*
* @param team Team to access
* @param listener Callback to pass information to
*/
public static void getTeamMembers(final Team team, final TeamPopulatedListener listener) {
// retrieve database reference to the teams
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference teamsReference = database.getReference("teams/" + team.getId());
teamsReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
listener.onTeamPopulated(dataSnapshot.getValue(Team.class));
return;
} else {
listener.onTeamPopulated(null);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class UserInformationActivity method retrieveInfo.
/**
* This method connects to Firebase, retrieving the user information
* stored for a given userId.
*
* @param userId the ID for the user to display
* @return the user info corresponding with the ID if successful, null otherwise
*/
private void retrieveInfo(final String userId) {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference myRef = db.getReference(getString(R.string.activity_user_information_firebase, userId));
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserInformation information = dataSnapshot.getValue(UserInformation.class);
Log.d(TAG, "Retrieved " + userId + " from Firebase.");
if (information != null) {
info = information;
changeDisplayedInfo(info);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "Failed to retrieve " + userId + " from Firebase.");
}
});
}
Aggregations