corrigir os novos erros amnhã e adicionar qual tipo de sexo a pessoa é no register
This commit is contained in:
@@ -2,6 +2,7 @@ package com.example.pap;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.text.method.ScrollingMovementMethod;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
@@ -18,7 +19,7 @@ public class ChatActivity extends AppCompatActivity {
|
||||
private Button btnEnviar;
|
||||
private TextView btnVoltarChat;
|
||||
|
||||
// NÃO TE ESQUEÇAS DE COLAR A TUA CHAVE AQUI!
|
||||
// A TUA CHAVE (cuidado na escola com ela)
|
||||
private final String MINHA_API_KEY = "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";
|
||||
|
||||
@Override
|
||||
@@ -31,17 +32,17 @@ public class ChatActivity extends AppCompatActivity {
|
||||
btnEnviar = findViewById(R.id.btnEnviarChat);
|
||||
btnVoltarChat = findViewById(R.id.btnVoltarChat);
|
||||
|
||||
// --- LÓGICA DO BOTÃO VOLTAR PARA O HOME ---
|
||||
// Faz com que o texto do chat consiga rolar se for muito grande
|
||||
tvChatLog.setMovementMethod(new ScrollingMovementMethod());
|
||||
|
||||
btnVoltarChat.setOnClickListener(v -> {
|
||||
// Cria a intenção de ir para a MainActivity (Home)
|
||||
Intent intent = new Intent(ChatActivity.this, HomeActivity.class);
|
||||
// Esta linha garante que não ficas com mil ecrãs abertos uns por cima dos outros
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
startActivity(intent);
|
||||
finish(); // Fecha o Chat
|
||||
finish();
|
||||
});
|
||||
|
||||
// Receber a análise da foto se existir
|
||||
// Receber a análise da foto (se veio de lá)
|
||||
String analiseComida = getIntent().getStringExtra("analise_comida");
|
||||
if (analiseComida != null && !analiseComida.isEmpty()) {
|
||||
tvChatLog.setText("IA: Analisei o teu prato.\n" + analiseComida + "\n\nO que queres saber mais?");
|
||||
@@ -59,9 +60,10 @@ public class ChatActivity extends AppCompatActivity {
|
||||
|
||||
private void perguntarIA(String texto) {
|
||||
tvChatLog.append("\n\nIA: A pensar... ⏳");
|
||||
btnEnviar.setEnabled(false); // Bloqueia o botão para não haver spam
|
||||
|
||||
AiRequest request = new AiRequest(java.util.Arrays.asList(
|
||||
new Message("system", Collections.singletonList(new ContentPart("text", "És um nutricionista de Portugal. Responde SEMPRE de forma muito curta (máximo 2 frases). Nunca uses asteriscos."))),
|
||||
new Message("system", Collections.singletonList(new ContentPart("text", "És um nutricionista de Portugal. Responde SEMPRE de forma muito curta (máximo 3 frases). Nunca uses asteriscos."))),
|
||||
new Message("user", Collections.singletonList(new ContentPart("text", texto)))
|
||||
));
|
||||
|
||||
@@ -70,17 +72,23 @@ public class ChatActivity extends AppCompatActivity {
|
||||
.enqueue(new Callback<AiResponse>() {
|
||||
@Override
|
||||
public void onResponse(Call<AiResponse> call, Response<AiResponse> response) {
|
||||
btnEnviar.setEnabled(true);
|
||||
if (response.isSuccessful() && response.body() != null) {
|
||||
String resposta = response.body().choices.get(0).message.content;
|
||||
String limpa = resposta.replace("**", "").replace("*", "");
|
||||
String atual = tvChatLog.getText().toString();
|
||||
tvChatLog.setText(atual.replace("IA: A pensar... ⏳", "IA: " + limpa));
|
||||
} else {
|
||||
// Se a API chumbou mas houve resposta (Erro 400, 429...)
|
||||
String atual = tvChatLog.getText().toString();
|
||||
tvChatLog.setText(atual.replace("IA: A pensar... ⏳", "IA: Tive um pequeno bloqueio (Erro " + response.code() + "). Tenta outra vez!"));
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void onFailure(Call<AiResponse> call, Throwable t) {
|
||||
btnEnviar.setEnabled(true);
|
||||
String atual = tvChatLog.getText().toString();
|
||||
tvChatLog.setText(atual.replace("IA: A pensar... ⏳", "IA: Erro de rede."));
|
||||
tvChatLog.setText(atual.replace("IA: A pensar... ⏳", "IA: Erro de comunicação (O servidor demorou muito a responder). Tenta novamente."));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user