corrigir os novos erros amnhã e adicionar qual tipo de sexo a pessoa é no register

This commit is contained in:
2026-05-12 17:19:13 +01:00
parent 1a8ddf4b79
commit 160ddcc95c
10 changed files with 444 additions and 322 deletions

View File

@@ -1,6 +1,7 @@
package com.example.pap;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.ImageDecoder;
import android.net.Uri;
@@ -32,7 +33,7 @@ public class FotoActivity extends AppCompatActivity {
private Bitmap imagemCapturada;
private String textoAnalise = "";
// COLA A TUA CHAVE AQUI
// MANTÉM A TUA CHAVE (cuidado para não partilhar no futuro)
private final String MINHA_API_KEY = "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";
@Override
@@ -142,6 +143,10 @@ public class FotoActivity extends AppCompatActivity {
textoAnalise = resposta.replace("**", "").replace("*", "");
tvResultadoIA.setText(textoAnalise);
btnIrParaChat.setVisibility(View.VISIBLE);
// A MAGIA ACONTECE AQUI!
extrairEGuardarDados(textoAnalise);
} catch (Exception e) { tvResultadoIA.setText("Erro na resposta."); }
} else { tvResultadoIA.setText("Erro: " + response.code()); }
}
@@ -152,4 +157,51 @@ public class FotoActivity extends AppCompatActivity {
}
});
}
// Função que "lê" a resposta da IA e guarda os números
private void extrairEGuardarDados(String texto) {
try {
// Apanhar o Nome do prato (tudo o que está a seguir a "Prato: " até à próxima linha)
int indexNomeStart = texto.indexOf("Prato: ") + 7;
int indexNomeEnd = texto.indexOf("\n", indexNomeStart);
String nomePrato = texto.substring(indexNomeStart, indexNomeEnd).trim();
// Apanhar os números
int calorias = extrairNumero(texto, "Calorias: ", " kcal");
int proteina = extrairNumero(texto, "Macros: ", "g Proteína");
int hidratos = extrairNumero(texto, "Proteína, ", "g Hidratos");
int gordura = extrairNumero(texto, "Hidratos, ", "g Gordura");
// Abrir a memória
SharedPreferences prefs = getSharedPreferences("DadosSaude", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
// Guardar nome do último prato
editor.putString("ultimo_prato", nomePrato);
// Somar aos valores do dia
editor.putInt("cal_hoje", prefs.getInt("cal_hoje", 0) + calorias);
editor.putInt("prot_hoje", prefs.getInt("prot_hoje", 0) + proteina);
editor.putInt("hidr_hoje", prefs.getInt("hidr_hoje", 0) + hidratos);
editor.putInt("gord_hoje", prefs.getInt("gord_hoje", 0) + gordura);
editor.apply();
} catch (Exception e) {
// Ignora se a IA responder noutro formato para não crashar
}
}
// Ferramenta que corta a fatia certa de texto para tirar os números
private int extrairNumero(String texto, String inicio, String fim) {
try {
int start = texto.indexOf(inicio) + inicio.length();
int end = texto.indexOf(fim, start);
String valorString = texto.substring(start, end).trim();
// Limpa tudo o que não for número (caso a IA escreva mal)
valorString = valorString.replaceAll("[^0-9]", "");
return Integer.parseInt(valorString);
} catch (Exception e) {
return 0; // Se não encontrar, assume 0
}
}
}