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) {
}
});
}
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);
}
}
}
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) {
}
});
}
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);
}
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());
}
Aggregations