Search in sources :

Example 61 with FirebaseDatabase

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

the class AccountManagerTest method setUserLoggedInFalseTest.

/**
 * 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 setUserLoggedInFalseTest() throws Exception {
    // create a test user and set them as online
    User testUser = TestingHelper.createTestUser();
    // set the user as online then set them as offline
    AccountManager.setUserLoginState(testUser.getUsername(), false);
    // 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 and we should fail
            // the test, otherwise it should succeed
            assertFalse("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 62 with FirebaseDatabase

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

the class AccountManagerTest method toggleOnlineUserTest.

@Test
public void toggleOnlineUserTest() 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.setOnline(true);
    sleep(1000);
    // 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()) {
                assertEquals(1, dataSnapshot.getValue(User.class).getUserExercises().size());
            }
        }

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

use of com.google.firebase.database.FirebaseDatabase in project OnlineCanteen by josephgunawan97.

the class EditProductActivity method submitData.

// To submit data
private void submitData() {
    Log.i(TAG, "TEXT2 MASUK");
    if (validateRegisterInfo()) {
        Log.i(TAG, "TEXT2 MASUK LAGI");
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        final DatabaseReference reference = firebaseDatabase.getReference();
        Query query = reference.child("products").orderByChild("tokoId").equalTo(merchant.getUid());
        query.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot productSnapshot : dataSnapshot.getChildren()) {
                    // DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
                    Product product = productSnapshot.getValue(Product.class);
                    if (merchant.getUid().equals(product.getTokoId()) && product.getName().equals(choose.toString())) {
                        Log.i(TAG, "TEXT2 test " + productSnapshot.getValue().toString());
                        // Log.i(TAG, "TEXT2 test " + nodeDataSnapshot.getValue().toString());
                        // String key = nodeDataSnapshot.getKey();
                        // String path = "/" + dataSnapshot.getKey() + "/" + key;
                        HashMap<String, Object> result = new HashMap<>();
                        // result.put("imageUrl", );
                        result.put("name", productName.getText().toString());
                        // HashMap<Integer, Object> result2 = new HashMap<>();
                        result.put("price", Integer.parseInt(productPrice.getText().toString()));
                        result.put("stock", Integer.parseInt(productQty.getText().toString()));
                        result.put("imageUrl", profPicUrl);
                        if (product.getImageUrl() != null) {
                            StorageReference photoRef = FirebaseStorage.getInstance().getReferenceFromUrl(product.getImageUrl());
                            photoRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {

                                @Override
                                public void onSuccess(Void aVoid) {
                                    // File deleted successfully
                                    Log.d(TAG, "onSuccess: deleted file");
                                }
                            }).addOnFailureListener(new OnFailureListener() {

                                @Override
                                public void onFailure(@NonNull Exception exception) {
                                    // Uh-oh, an error occurred!
                                    Log.d(TAG, "onFailure: did not delete file");
                                }
                            });
                        }
                        // if(!product.getImageUrl().equals())
                        reference.child("products").child(productSnapshot.getKey()).updateChildren(result);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            // Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);
            }
        });
        backToScreen();
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) Query(com.google.firebase.database.Query) StorageReference(com.google.firebase.storage.StorageReference) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) Product(com.example.asus.onlinecanteen.model.Product) DataSnapshot(com.google.firebase.database.DataSnapshot) DatabaseError(com.google.firebase.database.DatabaseError) NonNull(android.support.annotation.NonNull) ValueEventListener(com.google.firebase.database.ValueEventListener) OnFailureListener(com.google.android.gms.tasks.OnFailureListener)

Example 64 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project OnlineCanteen by josephgunawan97.

the class EditProductActivity method submitDatawithoutImage.

private void submitDatawithoutImage() {
    Log.i(TAG, "TEXT2 MASUK");
    if (validateRegisterInfo()) {
        Log.i(TAG, "TEXT2 MASUK LAGI");
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        final DatabaseReference reference = firebaseDatabase.getReference();
        Query query = reference.child("products").orderByChild("tokoId").equalTo(merchant.getUid());
        query.addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot productSnapshot : dataSnapshot.getChildren()) {
                    // DataSnapshot nodeDataSnapshot = dataSnapshot.getChildren().iterator().next();
                    Product product = productSnapshot.getValue(Product.class);
                    if (merchant.getUid().equals(product.getTokoId()) && product.getName().equals(choose.toString())) {
                        Log.i(TAG, "TEXT2 test " + productSnapshot.getValue().toString());
                        // String key = nodeDataSnapshot.getKey();
                        // String path = "/" + dataSnapshot.getKey() + "/" + key;
                        HashMap<String, Object> result = new HashMap<>();
                        // result.put("imageUrl", );
                        result.put("name", productName.getText().toString());
                        // HashMap<Integer, Object> result2 = new HashMap<>();
                        result.put("price", Integer.parseInt(productPrice.getText().toString()));
                        result.put("stock", Integer.parseInt(productQty.getText().toString()));
                        // if(!product.getImageUrl().equals())
                        reference.child("products").child(productSnapshot.getKey()).updateChildren(result);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            // Logger.error(TAG, ">>> Error:" + "find onCancelled:" + databaseError);
            }
        });
        backToScreen();
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseError(com.google.firebase.database.DatabaseError) Query(com.google.firebase.database.Query) DatabaseReference(com.google.firebase.database.DatabaseReference) HashMap(java.util.HashMap) Product(com.example.asus.onlinecanteen.model.Product) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 65 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project OnlineCanteen by josephgunawan97.

the class AccountUtil method createRole.

private static Task<Void> createRole(String role) {
    FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
    FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
    DatabaseReference reference = firebaseDatabase.getReference("role");
    return reference.child(firebaseUser.getUid()).setValue(role);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference) FirebaseUser(com.google.firebase.auth.FirebaseUser)

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