corrige o erro da camera e da ia não funcionar e mudar o layout futuramente
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package com.example.pap;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.util.Log;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
@@ -12,82 +13,74 @@ import androidx.appcompat.app.AppCompatActivity;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.Callback;
|
||||
import retrofit2.Response;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.converter.gson.GsonConverterFactory;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
private EditText etLoginEmail, etLoginPassword;
|
||||
private Button btnLogin;
|
||||
private TextView tvGoToRegister;
|
||||
private SupabaseApi api;
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
|
||||
// 1. Inicializar os componentes
|
||||
// Ligar o código aos IDs do ecrã
|
||||
etLoginEmail = findViewById(R.id.etLoginEmail);
|
||||
etLoginPassword = findViewById(R.id.etLoginPassword);
|
||||
btnLogin = findViewById(R.id.btnLogin);
|
||||
tvGoToRegister = findViewById(R.id.tvGoToRegister);
|
||||
|
||||
// 2. Iniciar a comunicação com a API do Supabase
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
.baseUrl(SupabaseConfig.URL)
|
||||
.addConverterFactory(GsonConverterFactory.create())
|
||||
.build();
|
||||
api = retrofit.create(SupabaseApi.class);
|
||||
|
||||
// 3. Configurar os cliques
|
||||
btnLogin.setOnClickListener(v -> efetuarLogin());
|
||||
// Iniciar a memória do telemóvel
|
||||
sharedPreferences = getSharedPreferences("MeusDadosApp", MODE_PRIVATE);
|
||||
|
||||
// Enviar para o Registo
|
||||
tvGoToRegister.setOnClickListener(v -> {
|
||||
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
|
||||
startActivity(intent);
|
||||
});
|
||||
}
|
||||
|
||||
private void efetuarLogin() {
|
||||
String email = etLoginEmail.getText().toString().trim();
|
||||
String password = etLoginPassword.getText().toString().trim();
|
||||
// Botão de Entrar
|
||||
btnLogin.setOnClickListener(v -> {
|
||||
String email = etLoginEmail.getText().toString().trim();
|
||||
String password = etLoginPassword.getText().toString().trim();
|
||||
|
||||
// Validação básica
|
||||
if (email.isEmpty() || password.isEmpty()) {
|
||||
Toast.makeText(this, "Preencha o email e a password!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
if (email.isEmpty() || password.isEmpty()) {
|
||||
Toast.makeText(this, "Preenche o email e a password!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
UserCredentials credentials = new UserCredentials(email, password);
|
||||
UserCredentials credentials = new UserCredentials(email, password);
|
||||
SupabaseApi api = SupabaseConfig.getRetrofit().create(SupabaseApi.class);
|
||||
|
||||
// Faz o pedido de Login ao Supabase
|
||||
api.login(SupabaseConfig.API_KEY, credentials).enqueue(new Callback<SupabaseResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<SupabaseResponse> call, Response<SupabaseResponse> response) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
api.login(SupabaseConfig.SUPABASE_KEY, credentials).enqueue(new Callback<SupabaseResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<SupabaseResponse> call, Response<SupabaseResponse> response) {
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
|
||||
// Login com sucesso! Vai para a Home
|
||||
Toast.makeText(LoginActivity.this, "Login com sucesso!", Toast.LENGTH_SHORT).show();
|
||||
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
|
||||
startActivity(intent);
|
||||
finish(); // Fecha a tela de login
|
||||
// Guardar o token e o email para usar nas Definições
|
||||
String token = response.body().access_token;
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.putString("access_token", token);
|
||||
editor.putString("email", email);
|
||||
editor.apply();
|
||||
|
||||
} else {
|
||||
// SE DER ERRO (ex: 404, 400), MOSTRA O LINK EXATO E O CÓDIGO
|
||||
String urlQueFalhou = response.raw().request().url().toString();
|
||||
if (response.code() == 400) {
|
||||
Toast.makeText(LoginActivity.this, "Email ou password incorretos!", Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(LoginActivity.this, "Entraste com sucesso! 🚀", Toast.LENGTH_SHORT).show();
|
||||
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(LoginActivity.this, "Erro " + response.code() + "! Link: " + urlQueFalhou, Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(LoginActivity.this, "Dados errados! Verifica a tua password e email.", Toast.LENGTH_LONG).show();
|
||||
Log.e("SUPABASE", "Erro Login: " + response.code());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<SupabaseResponse> call, Throwable t) {
|
||||
Toast.makeText(LoginActivity.this, "Falha na ligação: " + t.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@Override
|
||||
public void onFailure(Call<SupabaseResponse> call, Throwable t) {
|
||||
Toast.makeText(LoginActivity.this, "Falha na ligação! Verifica a internet.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user