Próbuję stworzyć panel logowania ale mam problem ze ściąganiem bazy danych rekordu z danymi użytkownika
w RegisterActivity sprawdzam czy użytkownik widnieje w bazie danych
if (!mUserViewModel.checkUser(textInputEditTextEmail.getText().toString().trim())) {
user.setName(textInputEditTextName.getText().toString().trim());
user.setEmail(textInputEditTextEmail.getText().toString().trim());
user.setPassword(textInputEditTextPassword.getText().toString().trim());
mUserViewModel.addUser(user);
// Snack Bar to show success message that record saved successfully
Snackbar.make(nestedScrollView, getString(R.string.success_message), Snackbar.LENGTH_LONG).show();
emptyInputEditText();
} else {
// Snack Bar to show error message that record already exists
Snackbar.make(nestedScrollView, getString(R.string.error_email_exists), Snackbar.LENGTH_LONG).show();
}
ale tu jest problem bo robię tą apkę z użyciem room i nie wiem czy mam użyć tu livedata bo nie obserwuje cały czas, a chcę mieć tylko odpowiedź czy użytkownik już istnieje jak próbowałem zrobić to bez livedata to miałem komunikat że zapytanie próbuje wykonać się na głównym wątku próbowałem to zrobić asynchronicznie bez livedata ale miałem błędy
link do pełnej apki: https://github.com/CharlesCZ/logowanie
ViewModel
public class UserViewModel extends AndroidViewModel {
private UserRepository mRepository;
private LiveData<List<User>> mAllUsers;
private LiveData<User> user;
public UserViewModel (Application application) {
super(application);
mRepository = new UserRepository(application);
mAllUsers = mRepository.getAllUsers();
}
public boolean checkUser(String wpisanyemail){
if(mRepository.checkUser(wpisanyemail).getValue()!=null)
{ System.out.println(mRepository.checkUser(wpisanyemail).getValue().getEmail()+" "+ true);
return true;}
else return false;
}
public boolean checkUser(String wpisanyemail,String wpisanepassword){
if(mRepository.checkUser(wpisanyemail,wpisanepassword).getValue()!=null)
{ System.out.println(mRepository.checkUser(wpisanyemail,wpisanepassword).getValue().getEmail()+" "+ true);
return true;}
else return false;
}
public LiveData<List<User>> getAllUsers() { return mAllUsers; }
public void addUser(User user) { mRepository.insert(user); }
}
public class UserRepository {
private UserDao mUserDao;
private LiveData<List<User>> mAllUsers;
UserRepository(Application application) {
UserRoomDatabase db=UserRoomDatabase.getDatabase(application);
mUserDao= db.userDao();
mAllUsers = mUserDao.getAllUsers();
}
public LiveData<List<User>> getAllUsers() {
return mAllUsers;
}
public LiveData<User> checkUser(String wpisanyemail){
return mUserDao.checkUser(wpisanyemail);
}
public LiveData<User> checkUser(String wpisanyemail,String wpisanepassword){
return mUserDao.checkUser(wpisanyemail,wpisanepassword);
}
public void insert (User user) {
new insertAsyncTask(mUserDao).execute(user);
}
private static class insertAsyncTask extends AsyncTask<User, Void, Void> {
private UserDao mAsyncTaskDao;
insertAsyncTask(UserDao dao) {
mAsyncTaskDao = dao;
}
@Override
protected Void doInBackground(final User... params) {
mAsyncTaskDao.insert(params[0]);
return null;
}
}
}
@Dao
public interface UserDao {
@Insert
void insert(User user);
@Delete
void delete(User user);
@Update
void update(User user);
@Query("DELETE FROM user_table")
void deleteAll();
@Query("SELECT * from user_table ORDER BY id ASC")
LiveData<List<User>> getAllUsers();
@Query("Select * from user_table WHERE user_table.email =:wpisanyemail")
LiveData<User> checkUser(String wpisanyemail); //ale chodzi ci o boolean
@Query("Select * from user_table WHERE user_table.email =:wpisanyemail AND user_table.password =:wpisanepassword ")
LiveData<User> checkUser(String wpisanyemail,String wpisanepassword); //ale chodzi ci o boolean
}
apka nie rejestruje użytkownika tylko pojawia się komunikat o sukcesie w rejestracji
całe registreactivity
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
private final AppCompatActivity activity = RegisterActivity.this;
private NestedScrollView nestedScrollView;
private TextInputLayout textInputLayoutName;
private TextInputLayout textInputLayoutEmail;
private TextInputLayout textInputLayoutPassword;
private TextInputLayout textInputLayoutConfirmPassword;
private TextInputEditText textInputEditTextName;
private TextInputEditText textInputEditTextEmail;
private TextInputEditText textInputEditTextPassword;
private TextInputEditText textInputEditTextConfirmPassword;
private AppCompatButton appCompatButtonRegister;
private AppCompatTextView appCompatTextViewLoginLink;
private InputValidation inputValidation;
private UserViewModel mUserViewModel;
private User user;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
getSupportActionBar().hide();
initViews();
initListeners();
initObjects();
}
/**
* This method is to initialize views
*/
private void initViews() {
nestedScrollView = (NestedScrollView) findViewById(R.id.nestedScrollView);
textInputLayoutName = (TextInputLayout) findViewById(R.id.textInputLayoutName);
textInputLayoutEmail = (TextInputLayout) findViewById(R.id.textInputLayoutEmail);
textInputLayoutPassword = (TextInputLayout) findViewById(R.id.textInputLayoutPassword);
textInputLayoutConfirmPassword = (TextInputLayout) findViewById(R.id.textInputLayoutConfirmPassword);
textInputEditTextName = (TextInputEditText) findViewById(R.id.textInputEditTextName);
textInputEditTextEmail = (TextInputEditText) findViewById(R.id.textInputEditTextEmail);
textInputEditTextPassword = (TextInputEditText) findViewById(R.id.textInputEditTextPassword);
textInputEditTextConfirmPassword = (TextInputEditText) findViewById(R.id.textInputEditTextConfirmPassword);
appCompatButtonRegister = (AppCompatButton) findViewById(R.id.appCompatButtonRegister);
appCompatTextViewLoginLink = (AppCompatTextView) findViewById(R.id.appCompatTextViewLoginLink);
}
/**
* This method is to initialize listeners
*/
private void initListeners() {
appCompatButtonRegister.setOnClickListener(this);
appCompatTextViewLoginLink.setOnClickListener(this);
}
/**
* This method is to initialize objects to be used
*/
private void initObjects() {
inputValidation = new InputValidation(activity);
//databaseHelper = new DatabaseHelper(activity);
mUserViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
user = new User();
}
/**
* This implemented method is to listen the click on view
*
* @param v
*/
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.appCompatButtonRegister:
postDataToSQLite();
break;
case R.id.appCompatTextViewLoginLink:
finish();
break;
}
}
/**
* This method is to validate the input text fields and post data to SQLite
*/
private void postDataToSQLite() {
if (!inputValidation.isInputEditTextFilled(textInputEditTextName, textInputLayoutName, getString(R.string.error_message_name))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
return;
}
if (!inputValidation.isInputEditTextEmail(textInputEditTextEmail, textInputLayoutEmail, getString(R.string.error_message_email))) {
return;
}
if (!inputValidation.isInputEditTextFilled(textInputEditTextPassword, textInputLayoutPassword, getString(R.string.error_message_password))) {
return;
}
if (!inputValidation.isInputEditTextMatches(textInputEditTextPassword, textInputEditTextConfirmPassword,
textInputLayoutConfirmPassword, getString(R.string.error_password_match))) {
return;
}
if (!mUserViewModel.checkUser(textInputEditTextEmail.getText().toString().trim())) {
user.setName(textInputEditTextName.getText().toString().trim());
user.setEmail(textInputEditTextEmail.getText().toString().trim());
user.setPassword(textInputEditTextPassword.getText().toString().trim());
mUserViewModel.addUser(user);
// Snack Bar to show success message that record saved successfully
Snackbar.make(nestedScrollView, getString(R.string.success_message), Snackbar.LENGTH_LONG).show();
emptyInputEditText();
} else {
// Snack Bar to show error message that record already exists
Snackbar.make(nestedScrollView, getString(R.string.error_email_exists), Snackbar.LENGTH_LONG).show();
}
}
/**
* This method is to empty all input edit text
*/
private void emptyInputEditText() {
textInputEditTextName.setText(null);
textInputEditTextEmail.setText(null);
textInputEditTextPassword.setText(null);
textInputEditTextConfirmPassword.setText(null);
}
}