Skip to content

Instantly share code, notes, and snippets.

@seanyc4
Created July 3, 2020 15:20
Show Gist options
  • Save seanyc4/c09294fa51ee1243a281ae3e7fc076f0 to your computer and use it in GitHub Desktop.
Save seanyc4/c09294fa51ee1243a281ae3e7fc076f0 to your computer and use it in GitHub Desktop.
VIEWMODEL
public class ViewModelUsers extends AndroidViewModel {
public boolean
UNAUTHENTICATED, // Initial state, the user needs to authenticate
AUTHENTICATED, // The user has authenticated successfully
INVALID_AUTHENTICATION; // Authentication failed
private RepoUsers repoUsers;
private LiveData<List<TableUsers>> verifyUserLogin;
private String email, password;
private boolean validated;
private SharedPreferences mLoginPrefs;
private SharedPreferences.Editor mEditor;
public ViewModelUsers(@NonNull Application application) {
super(application);
repoUsers = new RepoUsers(application);
mLoginPrefs = getApplication().getSharedPreferences(LOGIN_PREF, Context.MODE_PRIVATE);
mEditor = mLoginPrefs.edit();
verifyUserLogin = repoUsers.verifyUserLogin(email, password);
}
public LiveData<List<TableUsers>> verifyUserLogin(String email, String password) {
MediatorLiveData mediator = new MediatorLiveData();
mediator.addSource(repoUsers.verifyUserLogin(email, password), new Observer<List<TableUsers>>() {
@Override
public void onChanged(List<TableUsers> tableUsers) {
for (TableUsers ut : tableUsers) {
// If the users details match whats in the Room Database the login success boolean is set to true and the user can progress.
if (ut.getEmail().equals(email) && ut.getPassword().equals(password)) {
mEditor.putString("fName", ut.getFirstName());
mEditor.putString("lName", ut.getLastName());
mEditor.putString("email", ut.getEmail());
mEditor.putLong("userId", ut.getPkUserId());
mEditor.apply();
mEditor.commit();
AUTHENTICATED = true;
authenticate();
}
}
mediator.removeSource(repoUsers.verifyUserLogin(email, password));
}
});
return mediator;
}
public boolean authenticate() {
return AUTHENTICATED;
}
public class LoginFragment extends BaseFragment implements TextWatcher, CompoundButton.OnCheckedChangeListener, View.OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view =inflater.inflate(R.layout.fragment_login, container, false);
// Initialises the View model.
mViewModelUsers = new ViewModelProvider(requireActivity()).get(ViewModelUsers.class);
return view;
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnLogin:
// Checks if the username and password is valid.
if (!validateEmail() | !validatePassword()) {
return;
}
strEmailInput = etEmail.getText().toString();
strPasswordInput = etPassword.getText().toString();
// Checks the users inputted username & password against the information stored in the Room Database
mViewModelUsers.verifyUserLogin(strEmailInput, strPasswordInput).observe(getViewLifecycleOwner(), new Observer<List<TableUsers>>() {
@Override
public void onChanged(List<TableUsers> tableUsers) {
AUTHENTICATED = mViewModelUsers.authenticate();
if (AUTHENTICATED) {
loginSettings();
} else{
Snackbar.make(view,
"Invalid login",
Snackbar.LENGTH_SHORT
).show();
}
}
});
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment