private void updateUserName() {
String userName = mUserNameInput.getText().toString();
// Disable the update button until the user name update has been done
mUpdateButton.setEnabled(false);
// Subscribe to updating the user name.
// Re-enable the button once the user name has been updated
mDisposable.add(mViewModel.updateUserName(userName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(() -> mUpdateButton.setEnabled(true),
throwable -> Log.e(TAG, "Unable to update username", throwable)));
}
/**
* Data Access Object for the users table.
*/
@Dao
public interface UserDao {
/**
* Get the user from the table. Since for simplicity we only have one user in the database,
* this query gets all users from the table, but limits the result to just the 1st user.
*
* @return the user from the table
*/
@Query("SELECT * FROM Users LIMIT 1")
Flowable<User> getUser();
/**
* Insert a user in the database. If the user already exists, replace it.
*
* @param user the user to be inserted.
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
Completable insertUser(User user);
/**
* Delete all users.
*/
@Query("DELETE FROM Users")
void deleteAllUsers();
}
Get the user name of the user.
/**
* Get the user name of the user.
*
* @return a {@link Flowable} that will emit every time the user name has been updated.
*/
public Flowable<String> getUserName() {
return mDataSource.getUser()
// for every emission of the user, get the user name
.map(user -> {
mUser = user;
return user.getUserName();
});
}