Search in sources :

Example 11 with FirebaseDatabase

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

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

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

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

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.");
        }
    });
}
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)53 DatabaseReference (com.google.firebase.database.DatabaseReference)49 DatabaseError (com.google.firebase.database.DatabaseError)28 DataSnapshot (com.google.firebase.database.DataSnapshot)26 ValueEventListener (com.google.firebase.database.ValueEventListener)24 ArrayList (java.util.ArrayList)7 FirebaseUser (com.google.firebase.auth.FirebaseUser)6 ListView (android.widget.ListView)5 Test (org.junit.Test)5 TextView (android.widget.TextView)4 Intent (android.content.Intent)3 View (android.view.View)3 Query (com.google.firebase.database.Query)3 Provides (dagger.Provides)3 SharedPreferences (android.content.SharedPreferences)2 AdapterView (android.widget.AdapterView)2 Product (com.example.asus.onlinecanteen.model.Product)2 ChildEventListener (com.google.firebase.database.ChildEventListener)2 HashMap (java.util.HashMap)2 Before (org.junit.Before)2