Search in sources :

Example 31 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase in project priend by TakoJ.

the class disease_detail method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_disease_detail);
    TextView name = (TextView) findViewById(R.id.disease_name);
    final TextView define = (TextView) findViewById(R.id.define);
    final TextView condition = (TextView) findViewById(R.id.condition);
    final TextView cause = (TextView) findViewById(R.id.cause);
    Intent intent = getIntent();
    name.setText(intent.getStringExtra("name"));
    final String names = name.getText().toString();
    for (int i = 1; i < 40; i++) {
        final String num = Integer.toString(i);
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        final DatabaseReference myRef = database.getReference().child("diseases").child(num);
        myRef.child("diease_name").addListenerForSingleValueEvent(new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    String disease_name = dataSnapshot.getValue().toString();
                    if (disease_name.equals(names)) {
                        myRef.child("define").addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String disease_define = dataSnapshot.getValue().toString();
                                define.setText(disease_define);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                        myRef.child("condition").addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String disease_condition1 = dataSnapshot.getValue().toString();
                                String disease_condition2 = disease_condition1.replace("[", "");
                                String disease_condition3 = disease_condition2.replace("],", "\n");
                                String disease_condition = disease_condition3.replace("]]", "");
                                condition.setText(disease_condition);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                        myRef.child("cause").addListenerForSingleValueEvent(new ValueEventListener() {

                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                String disease_cause = dataSnapshot.getValue().toString();
                                cause.setText(disease_cause);
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {
                            }
                        });
                        return;
                    }
                    ;
                }
            }

            @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) TextView(android.widget.TextView) Intent(android.content.Intent) ValueEventListener(com.google.firebase.database.ValueEventListener) DataSnapshot(com.google.firebase.database.DataSnapshot)

Example 32 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 33 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 34 with FirebaseDatabase

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

the class TestingHelper method addTestUserExercises.

/**
 * Test helper to add test exercises to testuser
 */
public static void addTestUserExercises() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/testuser");
    ArrayList<Exercise> testExercises = new ArrayList<>();
    testExercises.add(testExercise1);
    testExercises.add(testExercise2);
    testExercises.add(testExercise3);
    usersReference.child("userExercises").setValue(testExercises);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) Exercise(ca.dal.cs.athletemonitor.athletemonitor.Exercise) DatabaseReference(com.google.firebase.database.DatabaseReference) ArrayList(java.util.ArrayList)

Example 35 with FirebaseDatabase

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

the class TestingHelper method resetTestUserWorkouts.

/**
 * Test helper to reset the testuser's workout list
 */
public static void resetTestUserWorkouts() {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/testuser");
    usersReference.child("userWorkouts").setValue(null);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) 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