Search in sources :

Example 41 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.

the class AccountManager method createUser.

/**
 * Creates a new user account
 *
 * @param newUser User details
 */
public static void createUser(final User newUser) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users");
    usersReference.child(newUser.getUsername()).setValue(newUser, null);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 42 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.

the class AccountManager method isLoggedIn.

/**
 * Check if a user is logged in
 *
 * @param user User to be removed
 *
 * @param booleanResultListener Callback to be called on completion
 */
public static void isLoggedIn(final User user, final BooleanResultListener booleanResultListener) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("online_users/");
    // attach a listener for data changes of the users reference.  this will occur when
    // the reference is populated
    usersReference.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // if the username is a child of the users node then fire the user exists event
            // indicating true, otherwise, fire event with false outcome
            booleanResultListener.onResult(dataSnapshot.hasChild(user.getUsername()));
        }

        @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 43 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.

the class TeamDetailActivity method populateMemberList.

/**
 * Populates the list of teams associated with the user
 */
private void populateMemberList() {
    final ListView teamMemberListView = findViewById(R.id.memberList);
    // retrieve database reference to the team members
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference teamsReference = database.getReference("teams/" + team.getId() + "/teamMembers");
    teamMemberAdapter = new TeamMembersAdapter(this, new ArrayList<String>());
    teamsReference.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            teamMemberAdapter.add(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            teamMemberAdapter.remove(dataSnapshot.getValue(String.class));
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    teamMemberListView.setAdapter(teamMemberAdapter);
    teamMemberListView.setOnItemClickListener(new TeamDetailActivity.TeamMemberListClickListener());
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) ListView(android.widget.ListView) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList) TeamMembersAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamMembersAdapter) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 44 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project SEProject by NicholasBarreyre.

the class TeamManager method removeMemberFromTeam.

/**
 * Removes the user from the specified team.
 *
 * @param team Team that user belongs to
 * @param user User to be removed
 */
public static void removeMemberFromTeam(Team team, final User user) {
    // retrieve database reference to the teams
    final FirebaseDatabase database = FirebaseDatabase.getInstance();
    final DatabaseReference teamsReference = database.getReference("teams/" + team.getId() + "/teamMembers");
    teamsReference.addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                Iterable<DataSnapshot> members = dataSnapshot.getChildren();
                for (DataSnapshot member : members) {
                    if (((String) member.getValue()).equals(user.getUsername())) {
                        teamsReference.child(member.getKey()).removeValue();
                        break;
                    }
                }
            }
        }

        @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 45 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)

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