Search in sources :

Example 1 with ValueEventListener

use of com.google.firebase.database.ValueEventListener in project quickstart-android by firebase.

the class PostDetailActivity method postComment.

private void postComment() {
    final String uid = getUid();
    FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Get user information
            User user = dataSnapshot.getValue(User.class);
            String authorName = user.username;
            // Create new comment object
            String commentText = mCommentField.getText().toString();
            Comment comment = new Comment(uid, authorName, commentText);
            // Push the comment, it will appear in the list
            mCommentsReference.push().setValue(comment);
            // Clear the field
            mCommentField.setText(null);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : Comment(com.google.firebase.quickstart.database.models.Comment) User(com.google.firebase.quickstart.database.models.User) DatabaseError(com.google.firebase.database.DatabaseError) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 2 with ValueEventListener

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

the class AccountManagerTest method offlineUserTest.

@Test
public void offlineUserTest() throws Exception {
    final User testUser = TestingHelper.createTestUser();
    AccountManager.createUser(testUser);
    sleep(1000);
    AccountManager.authenticate(testUser, TestingHelper.assertTrueBooleanResult());
    sleep(1000);
    AccountManager.setOnline(false);
    testUser.addUserExercise(TestingHelper.testExercise1);
    AccountManager.updateUser(testUser);
    AccountManager.getUser(testUser.getUsername(), new AccountManager.UserObjectListener() {

        @Override
        public void onUserPopulated(User user) {
            assertEquals(1, user.getUserExercises().size());
        }
    });
    // retrieve a reference to the users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/" + testUser.getUsername());
    // 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) {
            assertTrue(dataSnapshot.exists());
            if (dataSnapshot.exists()) {
                assertTrue(dataSnapshot.getValue(User.class).getUserExercises().isEmpty());
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    sleep(1000);
    AccountManager.deleteUser(testUser, TestingHelper.assertTrueBooleanResult());
}
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) Test(org.junit.Test)

Example 3 with ValueEventListener

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

the class AccountManagerTest method setUserLoggedInTrueTest.

/**
 * Tests that a user that has been authenticated is tagged as logged in
 *
 * Will pass if there exists a child node in the online_users reference equal to the users
 * username.
 *
 * @throws Exception
 */
@Test
public void setUserLoggedInTrueTest() throws Exception {
    // create a test user and set them as online
    User testUser = TestingHelper.createTestUser();
    sleep(3000);
    AccountManager.setUserLoginState(testUser.getUsername(), true);
    sleep(3000);
    // attempt to retrieve a reference to the logged in user
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("online_users/" + testUser.getUsername());
    // 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 reference exists, then the user is tagged as logged in
            assertTrue("Expecting non-empty result from database, but no data returned...", dataSnapshot.exists());
        }

        @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) Test(org.junit.Test)

Example 4 with ValueEventListener

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

the class AccountManagerTest method getUserSuccessTest.

/**
 * Tests the successful generation of a user object instance from user account information
 * in the database.
 *
 * Will pass if user object is populated with the user information from the database
 *
 * @throws Exception
 */
@Test
public void getUserSuccessTest() throws Exception {
    // construct a test user and add them to the accounts list for testing
    final User testUser = TestingHelper.createTestUser();
    AccountManager.createUser(testUser);
    // retrieve a reference to the users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/" + testUser.getUsername());
    // 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 reference exists, convert it to a user instance and pass to listener
            // otherwise return null
            assertTrue(dataSnapshot.exists());
            if (dataSnapshot.exists()) {
                User user = dataSnapshot.getValue(User.class);
                assertNotNull("Expecting username " + testUser.getUsername() + ", but seen null", user);
                assertEquals("Expecting username " + testUser.getUsername() + ", but seen " + user.getUsername(), user.getUsername(), testUser.getUsername());
                // delete test user from database
                AccountManager.deleteUser(testUser, TestingHelper.assertTrueBooleanResult());
            }
        }

        @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) Test(org.junit.Test)

Example 5 with ValueEventListener

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

the class AccountManager method authenticate.

/**
 * Determines whether the specified users username and password match those stored in the
 * database.
 *
 * @param user User to be authenticated
 * @param listener Callback on authentication result
 */
public static void authenticate(final User user, final BooleanResultListener listener) {
    // retrieve a reference to the users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/" + user.getUsername());
    // 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) {
            // always assume authentication fails until proven otherwise
            boolean authResult = false;
            // if the reference exists, convert it to a user instance
            if (dataSnapshot.exists()) {
                User userLoggingIn = dataSnapshot.getValue(User.class);
                // and return a successful login attempt, otherwise, report a failed login
                if (userLoggingIn.getPassword().equals(user.getPassword())) {
                    AccountManager.setUserLoginState(user.getUsername(), true);
                    lastAuth = user.getUsername();
                    online = true;
                    AccountManager.user = userLoggingIn;
                    authResult = true;
                }
            }
            // call the listener if there is one with the result of authentication
            if (listener != null)
                listener.onResult(authResult);
        }

        @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

DataSnapshot (com.google.firebase.database.DataSnapshot)211 ValueEventListener (com.google.firebase.database.ValueEventListener)211 DatabaseError (com.google.firebase.database.DatabaseError)210 DatabaseReference (com.google.firebase.database.DatabaseReference)62 View (android.view.View)47 Intent (android.content.Intent)43 TextView (android.widget.TextView)30 FirebaseDatabase (com.google.firebase.database.FirebaseDatabase)24 RecyclerView (android.support.v7.widget.RecyclerView)20 FirebaseUser (com.google.firebase.auth.FirebaseUser)20 HashMap (java.util.HashMap)20 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)19 Bundle (android.os.Bundle)16 ImageView (android.widget.ImageView)15 ArrayList (java.util.ArrayList)15 User (com.jexapps.bloodhub.m_Model.User)11 Map (java.util.Map)11 Date (java.util.Date)10 Query (com.google.firebase.database.Query)9 User (com.polito.mad17.madmax.entities.User)9