Search in sources :

Example 6 with FirebaseDatabase

use of com.google.firebase.database.FirebaseDatabase 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)

Example 7 with FirebaseDatabase

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

the class AccountManager method updateUser.

/**
 * Update a user account
 *
 * @param updatedUser User details
 */
public static void updateUser(final User updatedUser) {
    AccountManager.user = updatedUser;
    if (online) {
        // Ensure that the user is the currently authenticated user
        if (lastAuth.equals(updatedUser.getUsername())) {
            FirebaseDatabase database = FirebaseDatabase.getInstance();
            DatabaseReference usersReference = database.getReference("users/" + updatedUser.getUsername());
            usersReference.setValue(updatedUser, null);
        }
    }
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 8 with FirebaseDatabase

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

the class AccountManager method getUser.

/**
 * Retrieves the user account associated with the specified username.  User will be available
 * to the provided callback.
 *
 * @param username Username of the user to be loaded
 * @param listener Callback to handle response
 */
public static void getUser(String username, @NonNull final UserObjectListener listener) {
    Objects.requireNonNull(listener, "Null value for UserObjectListener is not valid.");
    Objects.requireNonNull(user, "Get user called in offline mode before logging in");
    if (!online) {
        if (username.equals(user.getUsername())) {
            listener.onUserPopulated(user);
        } else {
            listener.onUserPopulated(null);
        }
        return;
    }
    // retrieve a reference to the users node
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference usersReference = database.getReference("users/" + username);
    // 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) {
            // otherwise return null
            if (dataSnapshot.exists()) {
                listener.onUserPopulated(dataSnapshot.getValue(User.class));
            } else {
                listener.onUserPopulated(null);
            }
        }

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

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

the class RecordActivity method saveToFirebase.

private static void saveToFirebase(String dbRef, long time, List<Location> locationList) {
    FirebaseDatabase db = FirebaseDatabase.getInstance();
    DatabaseReference myRef = db.getReference(dbRef);
    RecordedWorkout workout = new RecordedWorkout(locationList, time);
    String key = myRef.push().getKey();
    myRef.child(key).setValue(workout);
}
Also used : FirebaseDatabase(com.google.firebase.database.FirebaseDatabase) DatabaseReference(com.google.firebase.database.DatabaseReference)

Example 10 with FirebaseDatabase

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

the class TeamActivity method populateTeamList.

/**
 * Populates the list of teams associated with the user
 */
private void populateTeamList() {
    // Get the reference to the UI contents
    final ListView teamListView = findViewById(R.id.teamList);
    // retrieve database reference to the teams
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference teamsReference = database.getReference("teams");
    teamAdapter = new TeamsAdapter(this, new ArrayList<Team>());
    teamsReference.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
                if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) == -1)
                    teamAdapter.add(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
                if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) == -1) {
                    teamAdapter.add(dataSnapshot.getValue(Team.class));
                } else {
                    teamAdapter.remove(teamAdapter.getItem(teamAdapter.getPosition(dataSnapshot.getValue(Team.class))));
                    teamAdapter.add(dataSnapshot.getValue(Team.class));
                }
            } else if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1) {
                teamAdapter.remove(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue(Team.class).getTeamMembers().contains(user.getUsername())) {
                if (teamAdapter.getPosition(dataSnapshot.getValue(Team.class)) != -1)
                    teamAdapter.remove(dataSnapshot.getValue(Team.class));
            }
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            Log.d("TeamList", dataSnapshot.getValue(Team.class).getName() + "has moved locations...");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
    teamListView.setAdapter(teamAdapter);
    teamListView.setOnItemClickListener(new TeamListClickListener());
}
Also used : TeamsAdapter(ca.dal.cs.athletemonitor.athletemonitor.adapters.TeamsAdapter) 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) DataSnapshot(com.google.firebase.database.DataSnapshot) ChildEventListener(com.google.firebase.database.ChildEventListener)

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