corrigir os novos erros amnhã e adicionar qual tipo de sexo a pessoa é no register
This commit is contained in:
@@ -5,7 +5,6 @@ import android.content.SharedPreferences;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.RadioButton;
|
||||
import android.widget.RadioGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
@@ -27,7 +26,6 @@ public class RegisterActivity extends AppCompatActivity {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_register);
|
||||
|
||||
// Ligar o código aos IDs do ecrã
|
||||
etRegNome = findViewById(R.id.etRegNome);
|
||||
etRegEmail = findViewById(R.id.etRegEmail);
|
||||
etRegPassword = findViewById(R.id.etRegPassword);
|
||||
@@ -38,10 +36,8 @@ public class RegisterActivity extends AppCompatActivity {
|
||||
btnRegister = findViewById(R.id.btnRegister);
|
||||
tvGoToLogin = findViewById(R.id.tvGoToLogin);
|
||||
|
||||
// Se o utilizador já tem conta, volta para o Login
|
||||
tvGoToLogin.setOnClickListener(v -> finish());
|
||||
|
||||
// Quando clica no botão de Registar
|
||||
btnRegister.setOnClickListener(v -> {
|
||||
String nome = etRegNome.getText().toString().trim();
|
||||
String email = etRegEmail.getText().toString().trim();
|
||||
@@ -50,65 +46,54 @@ public class RegisterActivity extends AppCompatActivity {
|
||||
String alturaStr = etRegAltura.getText().toString().trim();
|
||||
String pesoStr = etRegPeso.getText().toString().trim();
|
||||
|
||||
// 1. Verificar se não há campos vazios
|
||||
if (nome.isEmpty() || email.isEmpty() || password.isEmpty() || idadeStr.isEmpty() || alturaStr.isEmpty() || pesoStr.isEmpty()) {
|
||||
Toast.makeText(this, "Por favor, preenche todos os campos!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
// 1.1 Verificar se o utilizador escolheu o sexo
|
||||
int selectedSexoId = radioGroupSexo.getCheckedRadioButtonId();
|
||||
if (selectedSexoId == -1) {
|
||||
Toast.makeText(this, "Por favor, escolhe o teu sexo!", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
String sexoSelecionado = "";
|
||||
if (selectedSexoId == R.id.radioMasculino) {
|
||||
sexoSelecionado = "Masculino";
|
||||
} else if (selectedSexoId == R.id.radioFeminino) {
|
||||
sexoSelecionado = "Feminino";
|
||||
}
|
||||
String sexoSelecionado = (selectedSexoId == R.id.radioMasculino) ? "Masculino" : "Feminino";
|
||||
int idade = Integer.parseInt(idadeStr);
|
||||
float altura = Float.parseFloat(alturaStr);
|
||||
float peso = Float.parseFloat(pesoStr);
|
||||
|
||||
// 2. GUARDAR TODOS OS DADOS NA MEMÓRIA (SharedPreferences)
|
||||
// Grava no telemóvel para acesso rápido
|
||||
SharedPreferences prefs = getSharedPreferences("MeusDadosApp", MODE_PRIVATE);
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putString("nome", nome);
|
||||
editor.putString("email", email);
|
||||
editor.putString("sexo", sexoSelecionado);
|
||||
editor.putInt("idade", Integer.parseInt(idadeStr));
|
||||
editor.putFloat("altura", Float.parseFloat(alturaStr));
|
||||
editor.putFloat("peso", Float.parseFloat(pesoStr));
|
||||
editor.putInt("idade", idade);
|
||||
editor.putFloat("altura", altura);
|
||||
editor.putFloat("peso", peso);
|
||||
editor.apply();
|
||||
|
||||
// 3. Preparar os dados para enviar para o Supabase
|
||||
UserCredentials credentials = new UserCredentials(email, password);
|
||||
|
||||
// Vai buscar o Retrofit ao ficheiro SupabaseConfig
|
||||
// Empacota tudo para enviar para o Supabase
|
||||
UserCredentials credentials = new UserCredentials(email, password, nome, idade, altura, peso, sexoSelecionado);
|
||||
SupabaseApi api = SupabaseConfig.getRetrofit().create(SupabaseApi.class);
|
||||
|
||||
// 4. Fazer o Registo na Internet usando a chave configurada
|
||||
api.signUp(SupabaseConfig.SUPABASE_KEY, credentials).enqueue(new Callback<SupabaseResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<SupabaseResponse> call, Response<SupabaseResponse> response) {
|
||||
if (response.isSuccessful()) {
|
||||
Toast.makeText(RegisterActivity.this, "Conta criada! Verifica o teu email.", Toast.LENGTH_LONG).show();
|
||||
|
||||
// Mandar para o ecrã de espera
|
||||
Toast.makeText(RegisterActivity.this, "Conta criada no Supabase! Verifica o teu email.", Toast.LENGTH_LONG).show();
|
||||
Intent intent = new Intent(RegisterActivity.this, VerificacaoActivity.class);
|
||||
intent.putExtra("email_registo", email);
|
||||
intent.putExtra("password_registo", password);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
|
||||
} else {
|
||||
Toast.makeText(RegisterActivity.this, "Erro ao criar conta. O email já existe?", Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(RegisterActivity.this, "Erro ao criar conta online.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Call<SupabaseResponse> call, Throwable t) {
|
||||
Toast.makeText(RegisterActivity.this, "Falha na ligação! Verifica a tua internet.", Toast.LENGTH_LONG).show();
|
||||
Toast.makeText(RegisterActivity.this, "Falha na ligação à internet.", Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user