Atualizar LoginActivity e ContaActivity
parent
49ba834e96
commit
81eb71bd46
|
|
@ -1,8 +1,10 @@
|
||||||
package com.example.vdcscore;
|
package com.example.vdcscore;
|
||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.CheckBox;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
|
@ -15,11 +17,17 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
|
|
||||||
TextInputEditText editEmail, editPassword;
|
TextInputEditText editEmail, editPassword;
|
||||||
Button btnLogin;
|
Button btnLogin;
|
||||||
|
CheckBox checkRememberMe;
|
||||||
FirebaseAuth mAuth;
|
FirebaseAuth mAuth;
|
||||||
|
|
||||||
private TextView criarContaTextView;
|
private TextView criarContaTextView;
|
||||||
private TextView txtForgotPassword;
|
private TextView txtForgotPassword;
|
||||||
|
|
||||||
|
private static final String PREFS_NAME = "LoginPrefs";
|
||||||
|
private static final String KEY_EMAIL = "email";
|
||||||
|
private static final String KEY_PASSWORD = "password";
|
||||||
|
private static final String KEY_REMEMBER = "remember";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
@ -28,6 +36,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
editEmail = findViewById(R.id.editEmail);
|
editEmail = findViewById(R.id.editEmail);
|
||||||
editPassword = findViewById(R.id.editPassword2);
|
editPassword = findViewById(R.id.editPassword2);
|
||||||
btnLogin = findViewById(R.id.btnLogin);
|
btnLogin = findViewById(R.id.btnLogin);
|
||||||
|
checkRememberMe = findViewById(R.id.checkRememberMe);
|
||||||
criarContaTextView = findViewById(R.id.txtRegister);
|
criarContaTextView = findViewById(R.id.txtRegister);
|
||||||
txtForgotPassword = findViewById(R.id.txtForgotPassword);
|
txtForgotPassword = findViewById(R.id.txtForgotPassword);
|
||||||
|
|
||||||
|
|
@ -36,6 +45,45 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
btnLogin.setOnClickListener(v -> loginUser());
|
btnLogin.setOnClickListener(v -> loginUser());
|
||||||
criarContaTextView.setOnClickListener(view -> criarConta());
|
criarContaTextView.setOnClickListener(view -> criarConta());
|
||||||
txtForgotPassword.setOnClickListener(view -> recuperarPassword());
|
txtForgotPassword.setOnClickListener(view -> recuperarPassword());
|
||||||
|
|
||||||
|
// Carregar credenciais guardadas
|
||||||
|
loadSavedCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadSavedCredentials() {
|
||||||
|
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
|
||||||
|
boolean remember = prefs.getBoolean(KEY_REMEMBER, false);
|
||||||
|
|
||||||
|
if (remember) {
|
||||||
|
String savedEmail = prefs.getString(KEY_EMAIL, "");
|
||||||
|
String savedPassword = prefs.getString(KEY_PASSWORD, "");
|
||||||
|
|
||||||
|
if (!savedEmail.isEmpty() && !savedPassword.isEmpty()) {
|
||||||
|
editEmail.setText(savedEmail);
|
||||||
|
editPassword.setText(savedPassword);
|
||||||
|
checkRememberMe.setChecked(true);
|
||||||
|
|
||||||
|
// Tentar login automático
|
||||||
|
autoLogin(savedEmail, savedPassword);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void autoLogin(String email, String password) {
|
||||||
|
mAuth.signInWithEmailAndPassword(email, password)
|
||||||
|
.addOnCompleteListener(task -> {
|
||||||
|
if (task.isSuccessful()) {
|
||||||
|
// Login automático bem-sucedido
|
||||||
|
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
|
startActivity(intent);
|
||||||
|
finish();
|
||||||
|
} else {
|
||||||
|
// Se o login automático falhar, apenas limpar a password do campo
|
||||||
|
// O utilizador pode tentar fazer login manualmente
|
||||||
|
editPassword.setText("");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void criarConta() {
|
private void criarConta() {
|
||||||
|
|
@ -60,11 +108,19 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
mAuth.signInWithEmailAndPassword(email, password)
|
mAuth.signInWithEmailAndPassword(email, password)
|
||||||
.addOnCompleteListener(task -> {
|
.addOnCompleteListener(task -> {
|
||||||
if (task.isSuccessful()) {
|
if (task.isSuccessful()) {
|
||||||
|
// Guardar credenciais se "Lembrar-me" estiver marcado
|
||||||
|
if (checkRememberMe.isChecked()) {
|
||||||
|
saveCredentials(email, password);
|
||||||
|
} else {
|
||||||
|
clearSavedCredentials();
|
||||||
|
}
|
||||||
|
|
||||||
Toast.makeText(this, "Login efetuado!", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "Login efetuado!", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
// Abre a tua página principal
|
// Abre a tua página principal
|
||||||
startActivity(new Intent(LoginActivity.this, MainActivity.class));
|
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
|
startActivity(intent);
|
||||||
finish();
|
finish();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -77,12 +133,31 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void saveCredentials(String email, String password) {
|
||||||
|
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = prefs.edit();
|
||||||
|
editor.putString(KEY_EMAIL, email);
|
||||||
|
editor.putString(KEY_PASSWORD, password);
|
||||||
|
editor.putBoolean(KEY_REMEMBER, true);
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void clearSavedCredentials() {
|
||||||
|
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = prefs.edit();
|
||||||
|
editor.clear();
|
||||||
|
editor.apply();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onStart() {
|
protected void onStart() {
|
||||||
super.onStart();
|
super.onStart();
|
||||||
|
// Verificar se já está autenticado (caso o login automático não tenha funcionado)
|
||||||
if (FirebaseAuth.getInstance().getCurrentUser() != null){
|
if (FirebaseAuth.getInstance().getCurrentUser() != null){
|
||||||
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
startActivity(intent);
|
startActivity(intent);
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package com.example.vdcscore.ui.definicoes;
|
||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.app.ProgressDialog;
|
import android.app.ProgressDialog;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.content.SharedPreferences;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
|
@ -89,6 +90,7 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
editName = findViewById(R.id.editName);
|
editName = findViewById(R.id.editName);
|
||||||
textEmail = findViewById(R.id.textEmail);
|
textEmail = findViewById(R.id.textEmail);
|
||||||
btnSaveName = findViewById(R.id.btnSaveName);
|
btnSaveName = findViewById(R.id.btnSaveName);
|
||||||
|
btnLogout = findViewById(R.id.btnLogout);
|
||||||
btnChangePhoto = findViewById(R.id.btnChangePhoto);
|
btnChangePhoto = findViewById(R.id.btnChangePhoto);
|
||||||
cardImageContainer = findViewById(R.id.cardImageContainer);
|
cardImageContainer = findViewById(R.id.cardImageContainer);
|
||||||
|
|
||||||
|
|
@ -224,6 +226,28 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
if (btnSaveName != null) {
|
if (btnSaveName != null) {
|
||||||
btnSaveName.setOnClickListener(v -> saveUserName());
|
btnSaveName.setOnClickListener(v -> saveUserName());
|
||||||
}
|
}
|
||||||
|
if (btnLogout != null) {
|
||||||
|
btnLogout.setOnClickListener(v -> logoutUser());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void logoutUser() {
|
||||||
|
// Limpar credenciais guardadas
|
||||||
|
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
|
||||||
|
SharedPreferences.Editor editor = prefs.edit();
|
||||||
|
editor.clear();
|
||||||
|
editor.apply();
|
||||||
|
|
||||||
|
// Fazer logout do Firebase
|
||||||
|
mAuth.signOut();
|
||||||
|
|
||||||
|
Toast.makeText(this, "Sessão terminada", Toast.LENGTH_SHORT).show();
|
||||||
|
|
||||||
|
// Voltar para o login
|
||||||
|
Intent intent = new Intent(ContaActivity.this, LoginActivity.class);
|
||||||
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
|
||||||
|
startActivity(intent);
|
||||||
|
finish();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openImagePicker() {
|
private void openImagePicker() {
|
||||||
|
|
|
||||||
|
|
@ -233,6 +233,39 @@
|
||||||
|
|
||||||
</com.google.android.material.card.MaterialCardView>
|
</com.google.android.material.card.MaterialCardView>
|
||||||
|
|
||||||
|
<!-- Logout Section -->
|
||||||
|
<com.google.android.material.card.MaterialCardView
|
||||||
|
android:id="@+id/cardLogout"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="24dp"
|
||||||
|
app:cardCornerRadius="20dp"
|
||||||
|
app:cardElevation="4dp"
|
||||||
|
app:cardBackgroundColor="#FFFFFF"
|
||||||
|
app:strokeWidth="0dp">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:padding="24dp">
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnLogout"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="56dp"
|
||||||
|
android:backgroundTint="#D32F2F"
|
||||||
|
android:text="Terminar Sessão"
|
||||||
|
android:textColor="#FFFFFF"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:elevation="2dp"
|
||||||
|
android:stateListAnimator="@null" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
</com.google.android.material.card.MaterialCardView>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
</androidx.core.widget.NestedScrollView>
|
</androidx.core.widget.NestedScrollView>
|
||||||
|
|
|
||||||
|
|
@ -146,6 +146,16 @@
|
||||||
|
|
||||||
</com.google.android.material.textfield.TextInputLayout>
|
</com.google.android.material.textfield.TextInputLayout>
|
||||||
|
|
||||||
|
<!-- Remember Me Checkbox -->
|
||||||
|
<CheckBox
|
||||||
|
android:id="@+id/checkRememberMe"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="12dp"
|
||||||
|
android:text="Lembrar-me"
|
||||||
|
android:textColor="#616161"
|
||||||
|
android:textSize="14sp" />
|
||||||
|
|
||||||
<!-- Forgot Password Link -->
|
<!-- Forgot Password Link -->
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/txtForgotPassword"
|
android:id="@+id/txtForgotPassword"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue