use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class TeamManager method getJoinRequests.
/**
* Retrieves a list of join requests for the specified team. Returns a list of usernames
* of users requesting to join the team through the callback. If no requests exist then the
* returned list is empty.
*
* @param team Team to lookup
* @param listener Callback to receive list of requests if they exist
*/
public static void getJoinRequests(Team team, final JoinRequestListener listener) {
final ArrayList<String> joinRequests = new ArrayList<>();
// retrieve database reference
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference teamsReference = database.getReference("join_requests/" + team.getId());
teamsReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// return an empty list if no records found
if (dataSnapshot.exists()) {
Iterable<DataSnapshot> invitations = dataSnapshot.getChildren();
for (DataSnapshot invitation : invitations) {
joinRequests.add(invitation.getKey());
}
}
listener.onRequestsPopulated(joinRequests);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class TeamManager method newTeam.
/**
* Submit team data to firebase
* @param team The team data to be submitted
*
* @return whether team creation is successful
*/
public static boolean newTeam(Team team) {
// retrieve database reference to the teams
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference teamsReference = database.getReference("teams");
// generate a new business id
team.setId(teamsReference.push().getKey());
// Submit the team to the database
teamsReference.child(team.getId()).setValue(team);
// TODO check whether the team was successfully created (for example, does not already exist)
return true;
}
use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class TeamManager method inviteUser.
/**
* This method invite a user to a team.
*
* It create an invitation on the Teaminvitation branch on firebase.
*/
public static void inviteUser(final String username, final Team team) {
// if (Username not in ) //The aim here is to test if the user is not already on the team.
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference usersReference = database.getReference("teams_invitations");
// attempt to write the data
usersReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (!dataSnapshot.hasChild(username)) {
usersReference.child(username).setValue(username);
}
DatabaseReference userReference = database.getReference("teams_invitations/" + username);
userReference.child(userReference.push().getKey()).setValue(team);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class TeamManager method updateTeam.
public static void updateTeam(Team team, final BooleanResultListener listener) {
if (AccountManager.isOnline()) {
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference usersReference = database.getReference("teams/" + team.getId());
usersReference.setValue(team, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
listener.onResult(databaseError == null);
}
});
} else {
listener.onResult(false);
}
}
use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.
the class TeamManager method handleRequestToJoin.
/**
* Updates team membership based on whether the owner accepted or declined a request to join
*
* @param team Team associated with the join request
* @param requester Username of the user requesting to join
* @param accepted Whether or not the user accepted or declined. True if accepted, otherwise false
*/
public static void handleRequestToJoin(final Team team, final String requester, final boolean accepted) {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
final DatabaseReference teamReference = database.getReference("teams/" + team.getId());
teamReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
database.getReference("users/" + requester).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
User requestingUser = dataSnapshot.getValue(User.class);
if (accepted) {
TeamManager.addMemberToTeam(team, requestingUser);
}
// retrieve database reference to the teams
final DatabaseReference joinRequests = database.getReference("join_requests/" + team.getId());
joinRequests.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Iterable<DataSnapshot> requests = dataSnapshot.getChildren();
for (DataSnapshot request : requests) {
if (request.getKey().equals(requester)) {
joinRequests.child(request.getKey()).removeValue();
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Aggregations