mirror of
https://github.com/Nezumi-2711/PRM392.git
synced 2026-09-22 13:48:29 +00:00
Add Remember Login Function
This commit is contained in:
@@ -55,6 +55,8 @@ dependencies {
|
||||
|
||||
implementation 'com.squareup.picasso:picasso:2.5.2'
|
||||
implementation 'com.firebaseui:firebase-ui-database:1.2.0'
|
||||
implementation 'io.paperdb:paperdb:2.1'
|
||||
implementation 'com.github.rey5137:material:1.2.4'
|
||||
}
|
||||
|
||||
apply plugin: 'com.google.gms.google-services'
|
||||
@@ -10,6 +10,8 @@ public class Common {
|
||||
public static User currentUser;
|
||||
|
||||
public static final String DELETE = "Xoá";
|
||||
public static final String USER_KEY = "User";
|
||||
public static final String PWD_KEY = "Password";
|
||||
|
||||
public static String convertCodeToStatus(String status) {
|
||||
if(status.equals("0"))
|
||||
|
||||
@@ -29,6 +29,8 @@ import com.waterbase.foodify.Model.Category;
|
||||
import com.waterbase.foodify.Service.ListenOrder;
|
||||
import com.waterbase.foodify.ViewHolder.MenuViewHolder;
|
||||
|
||||
import io.paperdb.Paper;
|
||||
|
||||
|
||||
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
|
||||
|
||||
@@ -63,6 +65,9 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
database = FirebaseDatabase.getInstance();
|
||||
category = database.getReference("Category");
|
||||
|
||||
//Init paper
|
||||
Paper.init(this);
|
||||
|
||||
//Load menu
|
||||
recyler_menu = (RecyclerView) findViewById(R.id.recyler_menu);
|
||||
recyler_menu.setHasFixedSize(true);
|
||||
@@ -135,6 +140,11 @@ public class Home extends AppCompatActivity implements NavigationView.OnNavigati
|
||||
Intent orderIntent = new Intent(Home.this, OrderStatus.class);
|
||||
startActivity(orderIntent);
|
||||
} else if (id == R.id.nav_log_out) {
|
||||
|
||||
//Delete Remember user & password
|
||||
Paper.book().destroy();
|
||||
|
||||
//Logout
|
||||
Intent signIn = new Intent(Home.this, SignIn.class);
|
||||
signIn.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||
startActivity(signIn);
|
||||
|
||||
@@ -17,14 +17,18 @@ import com.google.firebase.database.DatabaseError;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.rey.material.widget.CheckBox;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Model.User;
|
||||
|
||||
import io.paperdb.Paper;
|
||||
|
||||
public class SignIn extends AppCompatActivity {
|
||||
|
||||
EditText edtPhone, edtPassword;
|
||||
Button btnSignIn;
|
||||
TextView txtAppName;
|
||||
CheckBox ckbRemember;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -34,6 +38,10 @@ public class SignIn extends AppCompatActivity {
|
||||
edtPassword = (EditText) findViewById(R.id.edtPassword);
|
||||
edtPhone = (EditText) findViewById(R.id.edtPhone);
|
||||
btnSignIn = (Button) findViewById(R.id.btnSignIn);
|
||||
ckbRemember = (CheckBox)findViewById(R.id.ckbRemember);
|
||||
|
||||
//Init Paper
|
||||
Paper.init(this);
|
||||
|
||||
txtAppName = (TextView) findViewById(R.id.txtAppName);
|
||||
|
||||
@@ -49,6 +57,12 @@ public class SignIn extends AppCompatActivity {
|
||||
public void onClick(View v) {
|
||||
|
||||
if (Common.isConnectedToInternet(getBaseContext())) {
|
||||
|
||||
//Save user& password
|
||||
if(ckbRemember.isChecked()) {
|
||||
Paper.book().write(Common.USER_KEY, edtPhone.getText().toString());
|
||||
Paper.book().write(Common.PWD_KEY, edtPassword.getText().toString());
|
||||
}
|
||||
final ProgressDialog mDialog = new ProgressDialog(SignIn.this);
|
||||
mDialog.setMessage("Please waiting...");
|
||||
mDialog.show();
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
package com.waterbase.foodify;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Typeface;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.google.firebase.database.DataSnapshot;
|
||||
import com.google.firebase.database.DatabaseError;
|
||||
import com.google.firebase.database.DatabaseReference;
|
||||
import com.google.firebase.database.FirebaseDatabase;
|
||||
import com.google.firebase.database.ValueEventListener;
|
||||
import com.waterbase.foodify.Common.Common;
|
||||
import com.waterbase.foodify.Model.User;
|
||||
|
||||
import io.paperdb.Paper;
|
||||
|
||||
public class Welcome extends AppCompatActivity {
|
||||
|
||||
Button btnSignIn, btnSignUp;
|
||||
@@ -23,11 +35,14 @@ public class Welcome extends AppCompatActivity {
|
||||
btnSignUp = (Button) findViewById(R.id.btnSignUp);
|
||||
|
||||
txtSlogan = (TextView) findViewById(R.id.txtSlogan);
|
||||
txtAppName = (TextView)findViewById(R.id.txtAppName);
|
||||
txtAppName = (TextView) findViewById(R.id.txtAppName);
|
||||
Typeface face = Typeface.createFromAsset(getAssets(), "fonts/NABILA.TTF");
|
||||
txtSlogan.setTypeface(face);
|
||||
txtAppName.setTypeface(face);
|
||||
|
||||
//Init paper
|
||||
Paper.init(this);
|
||||
|
||||
|
||||
btnSignIn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@@ -44,5 +59,61 @@ public class Welcome extends AppCompatActivity {
|
||||
startActivity(signUp);
|
||||
}
|
||||
});
|
||||
|
||||
//Check remember
|
||||
String user = Paper.book().read(Common.USER_KEY);
|
||||
String pwd = Paper.book().read(Common.PWD_KEY);
|
||||
if (user != null && pwd != null) {
|
||||
if (!user.isEmpty() && !pwd.isEmpty()) {
|
||||
login(user, pwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void login(String phone, String pwd) {
|
||||
if (Common.isConnectedToInternet(getBaseContext())) {
|
||||
|
||||
//Init Firebase
|
||||
FirebaseDatabase database = FirebaseDatabase.getInstance();
|
||||
final DatabaseReference table_user = database.getReference("User");
|
||||
|
||||
final ProgressDialog mDialog = new ProgressDialog(Welcome.this);
|
||||
mDialog.setMessage("Please waiting...");
|
||||
mDialog.show();
|
||||
|
||||
table_user.addListenerForSingleValueEvent(new ValueEventListener() {
|
||||
|
||||
|
||||
@Override
|
||||
public void onDataChange(DataSnapshot dataSnapshot) {
|
||||
//Check if user not in database
|
||||
if (dataSnapshot.child(phone).exists()) {
|
||||
//Get User information
|
||||
mDialog.dismiss();
|
||||
User user = dataSnapshot.child(phone).getValue(User.class);
|
||||
user.setPhone(phone);
|
||||
if (user.getPassword().equals(pwd)) {
|
||||
Intent homeIntent = new Intent(Welcome.this, Home.class);
|
||||
Common.currentUser = user;
|
||||
startActivity(homeIntent);
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(Welcome.this, "Số điện thoại hoặc mật khẩu không đúng. Xin vui lòng thử lại!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
} else {
|
||||
mDialog.dismiss();
|
||||
Toast.makeText(Welcome.this, "Số điện thoại chưa được đăng ký. Vui lòng đăng ký để sử dụng!", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCancelled(DatabaseError databaseError) {
|
||||
|
||||
}
|
||||
});
|
||||
} else {
|
||||
Toast.makeText(Welcome.this, "Vui lòng kiểm tra kết nối mạng!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -78,6 +78,22 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/edtPhone"
|
||||
app:layout_constraintWidth_percent=".8" />
|
||||
|
||||
<com.rey.material.widget.CheckBox
|
||||
android:id="@+id/ckbRemember"
|
||||
style="@style/Material.Drawable.CheckBox"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:gravity="center"
|
||||
android:text="Remember me"
|
||||
android:textColor="@color/white"
|
||||
app:cbd_strokeColor="@color/white"
|
||||
app:cbd_tickColor="@color/purple_500"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.142"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@+id/edtPassword" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnSignIn"
|
||||
android:layout_width="0dp"
|
||||
|
||||
Reference in New Issue
Block a user