Search in sources :

Example 51 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 52 with FirebaseDatabase

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

the class AccountManager method setUserLoginState.

/**
 * Sets whether a user is marked as being online or not.  Users are tagged as online will
 * have a node in the online_users branch of the database
 *
 * @param username Name of user to tag online/offline
 * @param online Online state of user.  True if online, False if offline
 */
public static void setUserLoginState(String username, boolean online) {
    // retrieve a reference to the online users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("online_users/");
    // otherwise, delete any existing entry
    if (online) {
        usersReference.child(username).setValue(true);
    } else {
        usersReference.child(username).removeValue();
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 53 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 54 with FirebaseDatabase

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

the class AccountManager method userExists.

/**
 * Determines whether or not the specified user exists in the database
 *
 * @param username Name of the user to look up
 */
public static void userExists(final String username, @NonNull final UserExistsListener listener) {
    Objects.requireNonNull(listener, "Null value for UserExistsListener is not valid.");
    // retrieve a reference to the users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("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
            listener.onUserExists(dataSnapshot.hasChild(username));
        }

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

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

the class AccountManager method deleteUser.

/**
 * Delete a user account from the database
 *
 * @param user User to be removed
 *
 * @param booleanResultListener Callback to be called on completion
 */
public static void deleteUser(User user, final BooleanResultListener booleanResultListener) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/" + user.getUsername());
    usersReference.removeValue(new DatabaseReference.CompletionListener() {

        @Override
        public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
            // early exit condition, no one is listening for the result
            if (booleanResultListener == null)
                return;
            // if there are no reported errors, delete succeeded
            if (databaseError == null) {
                booleanResultListener.onResult(true);
            } else {
                booleanResultListener.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)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