Search in sources :

Example 46 with FirebaseDatabase

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) {
        }
    });
}
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 47 with FirebaseDatabase

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;
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 48 with FirebaseDatabase

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) {
        }
    });
}
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 49 with FirebaseDatabase

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);
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 50 with FirebaseDatabase

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

FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)66 DatabaseReference (com.google.firebase.database.DatabaseReference)62 DatabaseError (com.google.firebase.database.DatabaseError)36 DataSnapshot (com.google.firebase.database.DataSnapshot)34 ValueEventListener (com.google.firebase.database.ValueEventListener)30 ArrayList (java.util.ArrayList)10 ListView (android.widget.ListView)7 Intent (android.content.Intent)6 FirebaseUser (com.google.firebase.auth.FirebaseUser)6 Test (org.junit.Test)5 View (android.view.View)4 TextView (android.widget.TextView)4 ChildEventListener (com.google.firebase.database.ChildEventListener)4 AdapterView (android.widget.AdapterView)3 Button (android.widget.Button)3 Query (com.google.firebase.database.Query)3 Provides (dagger.Provides)3 SharedPreferences (android.content.SharedPreferences)2 TeamsAdapter (ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter)2 Product (com.example.asus.onlinecanteen.model.Product)2