Guilhermes
This commit is contained in:
56
GuilhermesApp/Pages/NewFormPage.xaml.cs
Normal file
56
GuilhermesApp/Pages/NewFormPage.xaml.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using GuilhermesApp.Helpers;
|
||||
using Firebase.Database.Query;
|
||||
|
||||
namespace GuilhermesApp.Pages;
|
||||
|
||||
public partial class NewFormPage : ContentPage
|
||||
{
|
||||
private FirebaseService _firebaseService = new FirebaseService();
|
||||
|
||||
public NewFormPage()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private async void OnSubmitClicked(object sender, EventArgs e)
|
||||
{
|
||||
// Obriga a responder a algumas
|
||||
if (HumorPicker.SelectedIndex == -1 || SonoPicker.SelectedIndex == -1)
|
||||
{
|
||||
await DisplayAlert("Erro", "Responde pelo menos às perguntas de humor e sono.", "OK");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var user = _firebaseService.AuthClient.User;
|
||||
if (user == null) return;
|
||||
|
||||
// Prepara os dados do formulário
|
||||
var novoFormulario = new
|
||||
{
|
||||
Data = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
|
||||
Humor = HumorPicker.SelectedItem.ToString(),
|
||||
Stress = Math.Round(StressSlider.Value),
|
||||
Sono = SonoPicker.SelectedItem.ToString(),
|
||||
FezExercicio = ExercicioSwitch.IsToggled,
|
||||
Notas = string.IsNullOrWhiteSpace(NotasEntry.Text) ? "Sem notas" : NotasEntry.Text
|
||||
};
|
||||
|
||||
// Guarda na Realtime Database na pasta "Forms" -> "ID do Utilizador"
|
||||
await _firebaseService.DbClient
|
||||
.Child("Forms")
|
||||
.Child(user.Uid)
|
||||
.PostAsync(novoFormulario); // PostAsync cria um novo registo na lista
|
||||
|
||||
await DisplayAlert("Sucesso", "Formulário submetido e guardado!", "OK");
|
||||
|
||||
// Volta à página anterior
|
||||
await Shell.Current.GoToAsync("..");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await DisplayAlert("Erro", $"Não foi possível guardar. Erro: {ex.Message}", "OK");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user