Skip to content

Commit

Permalink
1. Updated login module in M-V-P style
Browse files Browse the repository at this point in the history
2. Refactored LoginPage back to before the branch, updated Model's userIsFound's return value to int so Mockito test works. Updated Presenter's login method.
3. After this it works for me. The only weird thing is that for invaild user and vaild user case, need to click twice.
  • Loading branch information
xxlalfredo99 committed Aug 9, 2021
1 parent 133852a commit b230c81
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 183 deletions.
238 changes: 73 additions & 165 deletions app/src/main/java/com/example/b07project/LoginPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
public class LoginPage extends AppCompatActivity {

private Presenter presenter;
private Model model;

private TextInputEditText loginEmail;
private TextInputEditText loginPassword;
Expand Down Expand Up @@ -73,9 +72,7 @@ protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setTitle("B07 Hospital App");
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2E5DA3")));

model = new Model();

presenter = new Presenter(model, this);
presenter = new Presenter(new Model(), this);

loginEmail = (TextInputEditText) findViewById(R.id.email_text);
loginPassword = (TextInputEditText) findViewById(R.id.password_text);
Expand Down Expand Up @@ -114,6 +111,14 @@ public void onClick(View v) {

}


public void navigateToPatientActivity(){
Intent navigateToPatientIntent = new Intent(LoginPage.this, PatientActivity.class);
//navigateToPatientIntent.putExtra("patient", patient);

startActivity(navigateToPatientIntent);
}

public void navigateToPatientSignup(){
Intent navigateToPatientSignup = new Intent(this, PatientSignup.class);
startActivity(navigateToPatientSignup);
Expand All @@ -124,7 +129,6 @@ public void login(){
}

public void userLogin(String email, String password) {

// String[] printmsg;
// printmsg = Presenter.checkLoginDetails(email, password);
//
Expand All @@ -139,142 +143,57 @@ public void userLogin(String email, String password) {
// return;
// }


progressBar.setVisibility(View.VISIBLE);

Context pageContext = this;
model.userIsFound(email, password);


auth = FirebaseAuth.getInstance();

if (model.loginSuccess() == 1){

// Get current UID
String currentLoggedInUID = auth.getUid();
Log.i("UID OBTAINED", currentLoggedInUID);
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Get current UID
String currentLoggedInUID = auth.getUid();
Log.i("UID OBTAINED", currentLoggedInUID);

final Boolean[] checkIfDoc = {false};
final Boolean[] checkIfDoc = {false};

// final ConfirmCallback cb = null;

//Match UID to see if user is a doctor... run through doctor UID list
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("doctors");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()){
String curDocUID = child.getKey();
if(curDocUID.equals(currentLoggedInUID)){
Log.i("UID MATCHED", curDocUID);
checkIfDoc[0] = true;
startActivity(new Intent(LoginPage.this, DoctorActivity.class));
//Match UID to see if user is a doctor... run through doctor UID list
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("doctors");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()){
String curDocUID = child.getKey();
if(curDocUID.equals(currentLoggedInUID)){
Log.i("UID MATCHED", curDocUID);
checkIfDoc[0] = true;
startActivity(new Intent(LoginPage.this, DoctorActivity.class));
// confirmUserPageNavigationCallback(true);
}
}
checkIfDoc[0] = false;
}
}
// checkIfDoc[0] = false;

if(checkIfDoc[0])
startActivity(new Intent(LoginPage.this, DoctorActivity.class));
else
startActivity(new Intent(LoginPage.this, PatientActivity.class));
if(checkIfDoc[0])
startActivity(new Intent(LoginPage.this, DoctorActivity.class));
else
startActivity(new Intent(LoginPage.this, PatientActivity.class));
// if(checkIfDoc[0] != null && checkIfDoc[0] == false)
// confirmUserPageNavigationCallback(false);
// cb.confirmCallback(checkIfDoc[0]);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
new AlertDialog.Builder(LoginPage.this)
.setTitle("Something went wrong, please restart the application")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
});
}
});//.addOnCompleteListener(LoginPage.this,new OnCompleteListener<AuthResult>() {
// @Override
// public void onComplete(@NonNull Task<AuthResult> task) {
;

//test code
// checkIfDoc[0] = true;

if (checkIfDoc[0]) {
startActivity(new Intent(LoginPage.this, DoctorActivity.class));
} else {
//Else not a doctor and login via patientlogin
startActivity(new Intent(LoginPage.this, PatientActivity.class));
}
} else if (model.loginSuccess() == 0){
progressBar.setVisibility(View.INVISIBLE);
new AlertDialog.Builder(pageContext)
.setTitle("Login information was incorrect, please try again.")
//.setMessage("Are you sure you want to delete this entry?")

// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
});

// A null listener allows the button to dismiss the dialog and take no further action.
// .setNegativeButton(android.R.string.no, null)
// .setIcon(android.R.drawable.ic_dialog_alert)
// .show();
// }
}
}

// auth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
// @Override
// public void onComplete(@NonNull Task<AuthResult> task) {
// if (task.isSuccessful()) {
// // Get current UID
// String currentLoggedInUID = auth.getUid();
// Log.i("UID OBTAINED", currentLoggedInUID);
//
// final Boolean[] checkIfDoc = {false};
//
//// final ConfirmCallback cb = null;
//
// //Match UID to see if user is a doctor... run through doctor UID list
// DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("doctors");
// ref.addListenerForSingleValueEvent(new ValueEventListener() {
// @Override
// public void onDataChange(@NonNull DataSnapshot snapshot) {
// for (DataSnapshot child: snapshot.getChildren()){
// String curDocUID = child.getKey();
// if(curDocUID.equals(currentLoggedInUID)){
// Log.i("UID MATCHED", curDocUID);
// checkIfDoc[0] = true;
// startActivity(new Intent(LoginPage.this, DoctorActivity.class));
//// confirmUserPageNavigationCallback(true);
// }
// }
//// checkIfDoc[0] = false;
//
// if(checkIfDoc[0])
// startActivity(new Intent(LoginPage.this, DoctorActivity.class));
// else
// startActivity(new Intent(LoginPage.this, PatientActivity.class));
//// if(checkIfDoc[0] != null && checkIfDoc[0] == false)
//// confirmUserPageNavigationCallback(false);
//// cb.confirmCallback(checkIfDoc[0]);
// }
// @Override
// public void onCancelled(@NonNull DatabaseError error) {
// new AlertDialog.Builder(LoginPage.this)
// .setTitle("Something went wrong, please restart the application")
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // Continue with delete operation
// }
// });
// }
// });//.addOnCompleteListener(LoginPage.this,new OnCompleteListener<AuthResult>() {
@Override
public void onCancelled(@NonNull DatabaseError error) {
new AlertDialog.Builder(LoginPage.this)
.setTitle("Something went wrong, please restart the application")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
});
}
});//.addOnCompleteListener(LoginPage.this,new OnCompleteListener<AuthResult>() {
// @Override
// public void onComplete(@NonNull Task<AuthResult> task) {
// ;
Expand All @@ -285,46 +204,37 @@ public void onClick(DialogInterface dialog, int which) {
// if (checkIfDoc[0]) {
// startActivity(new Intent(LoginPage.this, DoctorActivity.class));
// } else {
//Else not a doctor and login via patientlogin
//Else not a doctor and login via patientlogin
// startActivity(new Intent(LoginPage.this, PatientActivity.class));
// }
// }
// });
// @Override
// public void onCancelled(@NonNull DatabaseError error) {
// new AlertDialog.Builder(LoginPage.this)
// .setTitle("Something went wrong, please restart the application")
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // Continue with delete operation
// }
// });
// }
// });

//If authentication fails
// } else {
// progressBar.setVisibility(View.INVISIBLE);
// new AlertDialog.Builder(pageContext)
// .setTitle("Login information was incorrect, please try again.")
// //.setMessage("Are you sure you want to delete this entry?")
//
// // Specifying a listener allows you to take an action before dismissing the dialog.
// // The dialog is automatically dismissed when a dialog button is clicked.
// .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog, int which) {
// // Continue with delete operation
// }
// });
//
// // A null listener allows the button to dismiss the dialog and take no further action.
//// .setNegativeButton(android.R.string.no, null)
//// .setIcon(android.R.drawable.ic_dialog_alert)
//// .show();
//// }
//
// }
// });

//If authentication fails
} else {
progressBar.setVisibility(View.INVISIBLE);
new AlertDialog.Builder(pageContext)
.setTitle("Login information was incorrect, please try again.")
//.setMessage("Are you sure you want to delete this entry?")

// Specifying a listener allows you to take an action before dismissing the dialog.
// The dialog is automatically dismissed when a dialog button is clicked.
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Continue with delete operation
}
});

// A null listener allows the button to dismiss the dialog and take no further action.
// .setNegativeButton(android.R.string.no, null)
// .setIcon(android.R.drawable.ic_dialog_alert)
// .show();
}

}
});

}

public void confirmUserPageNavigationCallback(boolean isDoc) {
if (isDoc) {
Expand All @@ -333,8 +243,6 @@ public void confirmUserPageNavigationCallback(boolean isDoc) {
//Else not a doctor and login via patientlogin
startActivity(new Intent(LoginPage.this, PatientActivity.class));
}
// }
// });
}
}

4 changes: 2 additions & 2 deletions app/src/main/java/com/example/b07project/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Model(){
public int loginSuccess(){return loginSuccess;}


public void userIsFound(String email, String password) {
public int userIsFound(String email, String password) {
//int k = 0;
// if(emails.contains(email)){
//search in database
Expand Down Expand Up @@ -64,7 +64,7 @@ public void onComplete(@NonNull Task<AuthResult> task) {
}
}
});

return this.loginSuccess;
}

}
14 changes: 10 additions & 4 deletions app/src/main/java/com/example/b07project/Presenter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ else if(password.length() < 6)
// }
// else if(!Patterns.EMAIL_ADDRESS.matcher(email).matches())
// view.displayMessage("Please enter a valid email!");
else
view.userLogin(email, password);
if(model.loginSuccess()==0)
else{
int checker = model.userIsFound(email, password);
if(checker==0)
view.displayMessage("invalid login");
view.displayMessage("trying to login");
else{
view.displayMessage("trying to login");
view.userLogin(email, password);
}

}

}

public static String[] checkLoginDetails(String email, String password){
Expand Down
15 changes: 5 additions & 10 deletions app/src/main/res/layout/activity_login_page.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,12 @@
android:visibility="gone"
android:textColor="@android:color/holo_red_dark"/>

</LinearLayout>
<TextView
android:id="@+id/existedUser"
android:layout_width="307dp"
android:layout_height="36dp" />

</LinearLayout>

<TextView
android:id="@+id/existedUser"
android:layout_width="307dp"
android:layout_height="36dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.699" />

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public class MockitoPresenterTests {
public void presenterTest(){
when(view.getEmail()).thenReturn("[email protected]");
when(view.getPassword()).thenReturn("abcdef");
// when(model.userIsFound("[email protected]", "abcdef")).thenCallRealMethod(model.loginSuccess());
// when(model.loginSuccess()).thenReturn(1);
when(model.userIsFound("[email protected]", "abcdef")).thenReturn(1);

Presenter presenter = new Presenter(model, view);
presenter.login();
Expand Down

0 comments on commit b230c81

Please sign in to comment.