quero fazer o fire base agora
This commit is contained in:
@@ -10,15 +10,23 @@ import com.example.cuida.data.AppDatabase;
|
||||
import com.example.cuida.data.dao.UserDao;
|
||||
import com.example.cuida.data.model.User;
|
||||
import com.example.cuida.databinding.ActivityLoginBinding;
|
||||
import com.example.cuida.R;
|
||||
|
||||
import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
// gvjhbk
|
||||
private ActivityLoginBinding binding;
|
||||
private FirebaseAuth mAuth;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Initialize Firebase Auth
|
||||
mAuth = FirebaseAuth.getInstance();
|
||||
|
||||
// Check if user is already logged in
|
||||
// Check if user is already logged in and wants to be remembered
|
||||
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
||||
@@ -54,31 +62,41 @@ public class LoginActivity extends AppCompatActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
AppDatabase db = AppDatabase.getDatabase(getApplicationContext());
|
||||
UserDao userDao = db.userDao();
|
||||
binding.loginButton.setEnabled(false);
|
||||
binding.loginButton.setText("A entrar...");
|
||||
|
||||
AppDatabase.databaseWriteExecutor.execute(() -> {
|
||||
User user = userDao.login(email, password);
|
||||
runOnUiThread(() -> {
|
||||
if (user != null) {
|
||||
// Login Success
|
||||
// Login Success
|
||||
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
||||
boolean rememberMe = binding.checkboxRememberMe.isChecked();
|
||||
mAuth.signInWithEmailAndPassword(email, password)
|
||||
.addOnCompleteListener(this, task -> {
|
||||
binding.loginButton.setEnabled(true);
|
||||
binding.loginButton.setText(R.string.login_button);
|
||||
|
||||
prefs.edit().putBoolean("is_logged_in", true).apply();
|
||||
prefs.edit().putBoolean("remember_me", rememberMe).apply();
|
||||
if (task.isSuccessful()) {
|
||||
// Sign in success
|
||||
FirebaseUser user = mAuth.getCurrentUser();
|
||||
|
||||
prefs.edit().putString("user_name", user.name).apply();
|
||||
prefs.edit().putString("user_email", user.email).apply();
|
||||
SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE);
|
||||
boolean rememberMe = binding.checkboxRememberMe.isChecked();
|
||||
|
||||
Toast.makeText(this, "Bem-vindo " + user.name, Toast.LENGTH_SHORT).show();
|
||||
startActivity(new Intent(this, MainActivity.class));
|
||||
finish();
|
||||
} else {
|
||||
Toast.makeText(this, "Credenciais inválidas", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
});
|
||||
prefs.edit().putBoolean("is_logged_in", true).apply();
|
||||
prefs.edit().putBoolean("remember_me", rememberMe).apply();
|
||||
|
||||
// We can save user email/name from FirebaseUser if needed
|
||||
if (user != null) {
|
||||
prefs.edit().putString("user_email", user.getEmail()).apply();
|
||||
String displayName = user.getDisplayName();
|
||||
if (displayName != null) {
|
||||
prefs.edit().putString("user_name", displayName).apply();
|
||||
}
|
||||
}
|
||||
|
||||
Toast.makeText(LoginActivity.this, "Bem-vindo!", Toast.LENGTH_SHORT).show();
|
||||
startActivity(new Intent(LoginActivity.this, MainActivity.class));
|
||||
finish();
|
||||
} else {
|
||||
// If sign in fails, display a message to the user.
|
||||
Toast.makeText(LoginActivity.this, "Falha na autenticação: " + task.getException().getMessage(),
|
||||
Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,11 +16,23 @@ import androidx.fragment.app.Fragment;
|
||||
import com.example.cuida.databinding.FragmentSns24Binding;
|
||||
import com.google.android.gms.location.FusedLocationProviderClient;
|
||||
import com.google.android.gms.location.LocationServices;
|
||||
import com.google.ai.client.generativeai.GenerativeModel;
|
||||
import com.google.ai.client.generativeai.java.GenerativeModelFutures;
|
||||
import com.google.ai.client.generativeai.type.Content;
|
||||
import com.google.ai.client.generativeai.type.GenerateContentResponse;
|
||||
import com.google.common.util.concurrent.FutureCallback;
|
||||
import com.google.common.util.concurrent.Futures;
|
||||
import com.google.common.util.concurrent.ListenableFuture;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
public class Sns24Fragment extends Fragment {
|
||||
|
||||
private FragmentSns24Binding binding;
|
||||
private FusedLocationProviderClient fusedLocationClient;
|
||||
private GenerativeModelFutures ai; // Equivalent to 'const ai = genkit(...)'
|
||||
// API Key configurada
|
||||
private static final String API_KEY = "AIzaSyBmLgn-SHaTDvAeDWsw2iTZRR9gahhOu7k";
|
||||
|
||||
public View onCreateView(@NonNull LayoutInflater inflater,
|
||||
ViewGroup container, Bundle savedInstanceState) {
|
||||
@@ -28,9 +40,14 @@ public class Sns24Fragment extends Fragment {
|
||||
|
||||
fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireActivity());
|
||||
|
||||
// Initialize Gemini
|
||||
// generativeModel = new GenerativeModel("gemini-pro", GEMINI_API_KEY); //
|
||||
// Removed
|
||||
// Initialize Gemini (Android equivalent of Genkit gemini15Flash configuration)
|
||||
try {
|
||||
GenerativeModel gm = new GenerativeModel("gemini-1.5-flash", API_KEY);
|
||||
ai = GenerativeModelFutures.from(gm);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Toast.makeText(getContext(), "Erro ao iniciar IA: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
binding.buttonCallSns.setOnClickListener(v -> {
|
||||
try {
|
||||
@@ -43,11 +60,69 @@ public class Sns24Fragment extends Fragment {
|
||||
}
|
||||
});
|
||||
|
||||
// binding.buttonTriage.setOnClickListener(v -> performTriage()); // Removed
|
||||
binding.buttonAiTriage.setOnClickListener(v -> performTriage());
|
||||
|
||||
return binding.getRoot();
|
||||
}
|
||||
|
||||
private void performTriage() {
|
||||
if (API_KEY.equals("YOUR_API_KEY")) {
|
||||
Toast.makeText(getContext(), "Configure a API Key no ficheiro Sns24Fragment.java!", Toast.LENGTH_LONG)
|
||||
.show();
|
||||
return;
|
||||
}
|
||||
|
||||
if (ai == null) {
|
||||
Toast.makeText(getContext(), "IA não inicializada. Verifique s Logs.", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
String symptoms = binding.inputSymptoms.getText().toString();
|
||||
if (symptoms.isEmpty()) {
|
||||
binding.inputSymptoms.setError("Por favor descreva o que sente");
|
||||
return;
|
||||
}
|
||||
|
||||
binding.buttonAiTriage.setEnabled(false);
|
||||
binding.buttonAiTriage.setText("A analisar...");
|
||||
binding.textAiResult.setVisibility(View.GONE);
|
||||
|
||||
// This prompt structure is equivalent to the flow in Genkit
|
||||
String prompt = "O utilizador está a sentir o seguinte: " + symptoms + ". " +
|
||||
"Analisa a gravidade como 'gemini-1.5-flash'. Deves recomendar uma de duas opções: 'Ir ao Hospital' ou 'Ir ao Posto Médico'. "
|
||||
+
|
||||
"Explica brevemente o porquê. Responde em Português de Portugal.";
|
||||
|
||||
Content content = new Content.Builder().addText(prompt).build();
|
||||
Executor executor = Executors.newSingleThreadExecutor();
|
||||
|
||||
// Equivalent to: const { text } = await ai.generate(...)
|
||||
ListenableFuture<GenerateContentResponse> response = ai.generateContent(content);
|
||||
|
||||
Futures.addCallback(response, new FutureCallback<GenerateContentResponse>() {
|
||||
@Override
|
||||
public void onSuccess(GenerateContentResponse result) {
|
||||
String resultText = result.getText();
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
binding.buttonAiTriage.setEnabled(true);
|
||||
binding.buttonAiTriage.setText("Analisar Sintomas");
|
||||
binding.textAiResult.setText(resultText);
|
||||
binding.textAiResult.setVisibility(View.VISIBLE);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(Throwable t) {
|
||||
t.printStackTrace();
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
binding.buttonAiTriage.setEnabled(true);
|
||||
binding.buttonAiTriage.setText("Analisar Sintomas");
|
||||
Toast.makeText(getContext(), "Erro na análise: " + t.getMessage(), Toast.LENGTH_LONG).show();
|
||||
});
|
||||
}
|
||||
}, executor);
|
||||
}
|
||||
|
||||
// AI Triage methods removed
|
||||
|
||||
// Nearest Hospital feature removed with AI Triage
|
||||
|
||||
@@ -27,5 +27,54 @@
|
||||
app:iconGravity="textStart"
|
||||
android:layout_marginBottom="32dp"/>
|
||||
|
||||
<!-- Triage AI removed -->
|
||||
<View
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="1dp"
|
||||
android:background="#CCCCCC"
|
||||
android:layout_marginBottom="24dp"/>
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Triagem com Inteligência Artificial"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/primary_color"
|
||||
android:layout_marginBottom="16dp"/>
|
||||
|
||||
<com.google.android.material.textfield.TextInputLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="Descreva o que está a sentir..."
|
||||
android:layout_marginBottom="16dp"
|
||||
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
|
||||
|
||||
<com.google.android.material.textfield.TextInputEditText
|
||||
android:id="@+id/input_symptoms"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:inputType="textCapSentences|textMultiLine"
|
||||
android:minLines="3"
|
||||
android:gravity="top|start"/>
|
||||
</com.google.android.material.textfield.TextInputLayout>
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/button_ai_triage"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="60dp"
|
||||
android:text="Analisar Sintomas"
|
||||
android:textSize="16sp"
|
||||
app:cornerRadius="8dp"
|
||||
android:layout_marginBottom="24dp"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_ai_result"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@android:color/black"
|
||||
android:padding="16dp"
|
||||
android:background="#F5F5F5"
|
||||
android:visibility="gone"/>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user