Commit 3a088d6e authored by iman Fauzi's avatar iman Fauzi

fix login

parent d7548879
...@@ -61,97 +61,23 @@ public class LoginActivity extends AppCompatActivity { ...@@ -61,97 +61,23 @@ public class LoginActivity extends AppCompatActivity {
// bindUI // bindUI
btnSignIn = findViewById(R.id.signInButtonDashboardLogin); btnSignIn = findViewById(R.id.signInButtonDashboardLogin);
registerSheetLayout = findViewById(R.id.ConstraintRegisterLayout);
// get window height // sheetBehavior2 = BottomSheetBehavior.from(registerSheetLayout);
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels;
width = displayMetrics.widthPixels;
// get bottom sheets
sheetBehavior = BottomSheetBehavior.from(loginSheetLayout);
sheetBehavior2 = BottomSheetBehavior.from(registerSheetLayout);
// button on click to form login
btnSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
sheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
loginSheetLayout.setMaxHeight(height - 100);
} else {
sheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
}
});
//button on click to form register //button on click to form register
btnSignup.setOnClickListener(new View.OnClickListener() { // btnSignup.setOnClickListener(new View.OnClickListener() {
@Override // @Override
public void onClick(View view) { // public void onClick(View view) {
if (sheetBehavior2.getState() != BottomSheetBehavior.STATE_EXPANDED) { // if (sheetBehavior2.getState() != BottomSheetBehavior.STATE_EXPANDED) {
sheetBehavior2.setState(BottomSheetBehavior.STATE_EXPANDED); // sheetBehavior2.setState(BottomSheetBehavior.STATE_EXPANDED);
registerSheetLayout.setMaxHeight(height - 100); // registerSheetLayout.setMaxHeight(height - 100);
} else { // } else {
sheetBehavior2.setState(BottomSheetBehavior.STATE_COLLAPSED); // sheetBehavior2.setState(BottomSheetBehavior.STATE_COLLAPSED);
} // }
} // }
}); // });
/*
* LOGIN LOGIKA DIBAWAH INI
* */
//<<<<<<< HEAD
// public void onClick(View v) {
// Snackbar snackbar = Snackbar.make(v, "Oppss! Wrong Username Or Password!", Snackbar.LENGTH_LONG);
//
// if (TextUtils.isEmpty(username.getText().toString()) &&
// TextUtils.isEmpty(password.getText().toString())){
// username.setError("This field can't empty");
// password.setError("This field can't empty");
// }else if (TextUtils.isEmpty(username.getText().toString())){
// username.setError("This field can't empty");
// }else if (TextUtils.isEmpty(password.getText().toString())){
// password.setError("This field can't empty");
// }else{
// progressBar.setVisibility(View.VISIBLE);
// username.setEnabled(false);
// password.setEnabled(false);
// btnLogin.setEnabled(false);
//
// mainViewModels.checkLogin(new UsersModels(username.getText().toString(),
// password.getText().toString())).observe(LoginActivity.this, new Observer<DataResponse<UsersModels>>() {
// @Override
// public void onChanged(DataResponse<UsersModels> usersModelsDataResponse) {
// Log.d("Status", usersModelsDataResponse.getMessageData());
//
// if (usersModelsDataResponse != null){
// if (usersModelsDataResponse.getStatusData()){
// SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putBoolean(SplashScreen.SESSION, true);
// editor.commit();
//
// startActivity(new Intent(LoginActivity.this, MainActivity.class));
//
// progressBar.setVisibility(View.GONE);
// username.setEnabled(true);
// password.setEnabled(true);
// btnLogin.setEnabled(true);
// }else {
// utils.snackBar(v,"Oppss! Wrong Username Or Password!").show();
// progressBar.setVisibility(View.GONE);
// username.setEnabled(true);
// password.setEnabled(true);
// btnLogin.setEnabled(true);
// }
// }else{
// utils.snackBar(v, "Oppss! ログイン中にエラーが発生しました。しばらくしてからもう一度お試しください").show();
// }
//
// }
// });
//=======
// session login // session login
sharedPreferences = getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE); sharedPreferences = getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
...@@ -160,7 +86,6 @@ public class LoginActivity extends AppCompatActivity { ...@@ -160,7 +86,6 @@ public class LoginActivity extends AppCompatActivity {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
loginState = new LoginState(LoginActivity.this, mainViewModels); loginState = new LoginState(LoginActivity.this, mainViewModels);
loginState.setCancelable(false);
loginState.show(getSupportFragmentManager(), loginState.getTag()); loginState.show(getSupportFragmentManager(), loginState.getTag());
} }
}); });
......
package com.yono.messeripos; package com.yono.messeripos;
import android.app.Dialog; import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
...@@ -11,10 +13,12 @@ import android.util.Log; ...@@ -11,10 +13,12 @@ import android.util.Log;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.Observer; import androidx.lifecycle.Observer;
...@@ -23,6 +27,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; ...@@ -23,6 +27,7 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog; import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.google.android.material.button.MaterialButton; import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.snackbar.Snackbar; import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputEditText;
import com.yono.messeripos.models.MainViewModels; import com.yono.messeripos.models.MainViewModels;
...@@ -33,6 +38,7 @@ import com.yono.messeripos.utils.Utils; ...@@ -33,6 +38,7 @@ import com.yono.messeripos.utils.Utils;
public class LoginState extends BottomSheetDialogFragment { public class LoginState extends BottomSheetDialogFragment {
private static final String TAG = "LoginState"; private static final String TAG = "LoginState";
private static String DEFAULT_LOGIN_NAME = "sign in";
BottomSheetBehavior sheetBehavior; BottomSheetBehavior sheetBehavior;
BottomSheetDialog sheetDialog; BottomSheetDialog sheetDialog;
RelativeLayout loginLayout; RelativeLayout loginLayout;
...@@ -43,6 +49,10 @@ public class LoginState extends BottomSheetDialogFragment { ...@@ -43,6 +49,10 @@ public class LoginState extends BottomSheetDialogFragment {
MainViewModels mainViewModels; MainViewModels mainViewModels;
SharedPreferences sharedPreferences; SharedPreferences sharedPreferences;
Utils utils; Utils utils;
ProgressBar bar;
AlertDialog alertDialog;
ProgressDialog dialog;
public LoginState(Context context, MainViewModels mainViewModels) { public LoginState(Context context, MainViewModels mainViewModels) {
this.context = context; this.context = context;
...@@ -56,6 +66,8 @@ public class LoginState extends BottomSheetDialogFragment { ...@@ -56,6 +66,8 @@ public class LoginState extends BottomSheetDialogFragment {
sheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState); sheetDialog = (BottomSheetDialog) super.onCreateDialog(savedInstanceState);
dialog = new ProgressDialog(context);
// inflating layout // inflating layout
View view = View.inflate(getContext(), R.layout.login_form, null); View view = View.inflate(getContext(), R.layout.login_form, null);
...@@ -65,6 +77,10 @@ public class LoginState extends BottomSheetDialogFragment { ...@@ -65,6 +77,10 @@ public class LoginState extends BottomSheetDialogFragment {
password = view.findViewById(R.id.ETPasswordLogin); password = view.findViewById(R.id.ETPasswordLogin);
loginButton = view.findViewById(R.id.SignInButtonInLogin); loginButton = view.findViewById(R.id.SignInButtonInLogin);
closeBtn = view.findViewById(R.id.closeBtnLogin); closeBtn = view.findViewById(R.id.closeBtnLogin);
bar = view.findViewById(R.id.progressLogin);
// set progress bar gone
bar.setVisibility(View.GONE);
// create session // create session
sharedPreferences = context.getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE); sharedPreferences = context.getSharedPreferences(SplashScreen.MY_SHARED_PREFERENCES, Context.MODE_PRIVATE);
...@@ -78,16 +94,28 @@ public class LoginState extends BottomSheetDialogFragment { ...@@ -78,16 +94,28 @@ public class LoginState extends BottomSheetDialogFragment {
// get Window size // get Window size
int getHeight = Resources.getSystem().getDisplayMetrics().heightPixels; int getHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
int setHeight = getHeight - (getHeight * 10) / 100; int setHeight = getHeight - (getHeight * 5) / 100;
// set max height // set max height
sheetBehavior.setPeekHeight(setHeight); sheetBehavior.setPeekHeight(setHeight);
loginLayout.setMinimumHeight(setHeight); loginLayout.setMinimumHeight(setHeight);
Log.d(TAG, "onCreateDialog: "+sheetBehavior.getState()); // alert dialog buat nampilin error
alertDialog = new AlertDialog.Builder(context)
.setTitle("Username dan Password Salah")
.setMessage("Oppss! Username dan Password yang kamu masukan salah!")
.setPositiveButton("Coba Lagi", null)
.setCancelable(false)
.create();
loginButton.setOnClickListener(view1 -> { loginButton.setOnClickListener(view1 -> {
if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) loginAuth(view1); if (sheetBehavior.getState() != BottomSheetBehavior.STATE_EXPANDED) {
bar.setVisibility(View.VISIBLE);
loginButton.setText("");
sheetDialog.setCancelable(false);
loginAuth(view1);
}
}); });
// close btn is clicked // close btn is clicked
...@@ -100,7 +128,6 @@ public class LoginState extends BottomSheetDialogFragment { ...@@ -100,7 +128,6 @@ public class LoginState extends BottomSheetDialogFragment {
} }
private void loginAuth(View v) { private void loginAuth(View v) {
Snackbar snackbar = Snackbar.make(v, "Oppss! Wrong Username Or Password!", Snackbar.LENGTH_LONG);
if (TextUtils.isEmpty(username.getText().toString()) && if (TextUtils.isEmpty(username.getText().toString()) &&
TextUtils.isEmpty(password.getText().toString())){ TextUtils.isEmpty(password.getText().toString())){
...@@ -111,38 +138,73 @@ public class LoginState extends BottomSheetDialogFragment { ...@@ -111,38 +138,73 @@ public class LoginState extends BottomSheetDialogFragment {
}else if (TextUtils.isEmpty(password.getText().toString())){ }else if (TextUtils.isEmpty(password.getText().toString())){
password.setError("This field can't empty"); password.setError("This field can't empty");
}else{ }else{
/**
* Jika tombol sign in ditekan
*/
// aktifkan progress bar
bar.setVisibility(View.VISIBLE);
// hilangkan tulisan di button
loginButton.setText("");
// nonaktifkan semua form
username.setEnabled(false); username.setEnabled(false);
password.setEnabled(false); password.setEnabled(false);
loginButton.setEnabled(false); loginButton.setEnabled(false);
closeBtn.setEnabled(false); closeBtn.setEnabled(false);
// buat checking login
mainViewModels.checkLogin(new UsersModels(username.getText().toString(), mainViewModels.checkLogin(new UsersModels(username.getText().toString(),
password.getText().toString())) password.getText().toString()))
.observe((LifecycleOwner) context, new Observer<DataResponse<UsersModels>>() { .observe((LifecycleOwner) context, new Observer<DataResponse<UsersModels>>() {
@Override @Override
public void onChanged(DataResponse<UsersModels> usersModelsDataResponse) { public void onChanged(DataResponse<UsersModels> usersModelsDataResponse) {
Log.d("Status", usersModelsDataResponse.getMessageData());
if (usersModelsDataResponse != null) { if (usersModelsDataResponse != null) {
/**
* Jika response atau data ada dan cocok
*/
if (usersModelsDataResponse.getStatusData()) { if (usersModelsDataResponse.getStatusData()) {
// SharedPreferences.Editor editor = sharedPreferences.edit(); // SharedPreferences.Editor editor = sharedPreferences.edit();
// editor.putBoolean(SplashScreen.SESSION, true); // editor.putBoolean(SplashScreen.SESSION, true);
// editor.commit(); // editor.commit();
// redirect ke home
startActivity(new Intent(context, MainActivity.class)); startActivity(new Intent(context, MainActivity.class));
// set progress bar jadi nonaktif
bar.setVisibility(View.GONE);
// munculkan tulisan default di button sign in
loginButton.setText(DEFAULT_LOGIN_NAME);
// ubah sheet dialog jadi tidak bisa di slide atau cancel
sheetDialog.setCancelable(true);
// set form jadi enable
username.setEnabled(true); username.setEnabled(true);
password.setEnabled(true); password.setEnabled(true);
loginButton.setEnabled(true); loginButton.setEnabled(true);
closeBtn.setEnabled(true); closeBtn.setEnabled(true);
} else { } else {
utils.snackBar(v, "Oppss! Wrong Username Or Password!").show(); /**
* Jika response atau data tidak cocok
*/
bar.setVisibility(View.GONE);
loginButton.setText(DEFAULT_LOGIN_NAME);
sheetDialog.setCancelable(true);
utils.snackBar(v, "Oppss! Wrong Username or Password!").show();
username.setEnabled(true); username.setEnabled(true);
password.setEnabled(true); password.setEnabled(true);
loginButton.setEnabled(true); loginButton.setEnabled(true);
closeBtn.setEnabled(true); closeBtn.setEnabled(true);
} }
} else { } else {
bar.setVisibility(View.VISIBLE);
utils.snackBar(v, "Oppss! ログイン中にエラーが発生しました。しばらくしてからもう一度お試しください").show(); utils.snackBar(v, "Oppss! ログイン中にエラーが発生しました。しばらくしてからもう一度お試しください").show();
} }
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" <layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
...@@ -14,94 +15,117 @@ ...@@ -14,94 +15,117 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
<ImageButton <ImageButton
android:id="@+id/closeBtnLogin" android:id="@+id/closeBtnLogin"
android:layout_width="20dp" android:layout_width="20dp"
android:layout_height="20dp" android:layout_height="20dp"
android:src="@drawable/ic_baseline_plus_24" android:src="@drawable/ic_baseline_plus_24"
android:scaleType="centerInside" android:scaleType="centerInside"
android:adjustViewBounds="true" android:adjustViewBounds="true"
android:padding="20dp" android:padding="20dp"
android:backgroundTint="@android:color/transparent" android:backgroundTint="@android:color/transparent"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
/> />
<ImageView <ImageView
android:id="@+id/messerLogoInLogin" android:id="@+id/messerLogoInLogin"
android:layout_width="150dp" android:layout_width="150dp"
android:layout_height="150dp" android:layout_height="150dp"
android:src="@drawable/ic_messer_primary" android:src="@drawable/ic_messer_primary"
android:layout_below="@id/closeBtnLogin" android:layout_below="@id/closeBtnLogin"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/> android:layout_marginTop="20dp"/>
<com.google.android.material.textfield.TextInputLayout <com.google.android.material.textfield.TextInputLayout
android:id="@+id/formUsernameLogin" android:id="@+id/formUsernameLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
android:layout_below="@id/messerLogoInLogin"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ETUsernameLogin"
android:layout_width="match_parent" android:layout_width="match_parent"
android:inputType="text"
android:imeOptions="actionNext"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/username" />
android:layout_below="@id/messerLogoInLogin"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"> </com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputEditText <com.google.android.material.textfield.TextInputLayout
android:id="@+id/ETUsernameLogin" android:id="@+id/FormPasswordLogin"
android:layout_width="match_parent" android:layout_width="match_parent"
android:inputType="text" android:layout_height="wrap_content"
android:imeOptions="actionNext" android:hint="@string/password"
android:layout_height="wrap_content" android:layout_below="@id/formUsernameLogin"
/> android:layout_marginTop="20dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputEditText
<com.google.android.material.textfield.TextInputLayout android:id="@+id/ETPasswordLogin"
android:id="@+id/FormPasswordLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password"
android:layout_below="@id/formUsernameLogin"
android:layout_marginTop="20dp"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ETPasswordLogin"
android:layout_width="match_parent"
android:inputType="textPassword"
android:imeOptions="actionDone|actionGo"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
android:id="@+id/CBRememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me"
android:textColor="@color/colorPrimary"
android:layout_below="@id/FormPasswordLogin"
android:layout_marginVertical="10dp"
app:useMaterialThemeColors="true"/>
<com.google.android.material.button.MaterialButton
android:id="@+id/SignInButtonInLogin"
android:layout_width="match_parent" android:layout_width="match_parent"
android:inputType="textPassword"
android:imeOptions="actionDone|actionGo"
android:layout_height="wrap_content" android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/CBRememberMe" />
android:text="Sign In"
android:layout_below="@id/CBRememberMe" </com.google.android.material.textfield.TextInputLayout>
android:padding="20dp"/>
<CheckBox
android:id="@+id/CBRememberMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me"
android:textColor="@color/colorPrimary"
android:layout_below="@id/FormPasswordLogin"
android:layout_marginVertical="10dp"
app:useMaterialThemeColors="true"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/CBRememberMe">
<com.google.android.material.button.MaterialButton
android:id="@+id/SignInButtonInLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign in"
android:padding="20dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<ProgressBar
android:id="@+id/progressLogin"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="2dp"
app:layout_constraintBottom_toBottomOf="@id/SignInButtonInLogin"
app:layout_constraintTop_toTopOf="@id/SignInButtonInLogin"
app:layout_constraintStart_toStartOf="@id/SignInButtonInLogin"
app:layout_constraintEnd_toEndOf="@id/SignInButtonInLogin"
/>
<com.google.android.material.textview.MaterialTextView </androidx.constraintlayout.widget.ConstraintLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content" <com.google.android.material.textview.MaterialTextView
android:text="Forgot Password?" android:id="@+id/forgotPwd"
android:layout_below="@id/SignInButtonInLogin" android:layout_width="wrap_content"
android:layout_marginTop="10dp" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:text="Forgot Password?"
android:textColor="@color/colorPrimary"/> android:layout_below="@id/constraintBtn"
android:layout_marginTop="10dp"
android:layout_alignParentEnd="true"
android:textColor="@color/colorPrimary"/>
</RelativeLayout> </RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.coordinatorlayout.widget.CoordinatorLayout>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment