commit 3fef487c8a7064dd0fa42f820d2ecedc670f0197
Author: lbihannic <lucas.bihannic@inrae.fr>
Date:   Fri Apr 25 11:19:51 2025 +0200

    add warning when time to measure

diff --git a/GES_PAC/App.xaml.cs b/GES_PAC/App.xaml.cs
index 6e9594d..0e4bc4b 100644
--- a/GES_PAC/App.xaml.cs
+++ b/GES_PAC/App.xaml.cs
@@ -10,17 +10,10 @@ namespace GES_PAC
         public App()
         {
             InitializeComponent();
-
-            //var dbPath = Path.Combine(FileSystem.AppDataDirectory, "GES_PAC.db3");
-            //if (File.Exists(dbPath))
-            //    File.Delete(dbPath);
-
-
             Db = new AppDbContext();
             Db.Database.EnsureCreated();
 
             _ = InitSmartEnumsAsync();
-            //_ = InitDb();
         }
 
         protected override Window CreateWindow(IActivationState? activationState)
@@ -47,21 +40,7 @@ namespace GES_PAC
                 if (!await Db.TypeComportement.AnyAsync(c => c.Value == comportement.Value))
                     Db.TypeComportement.Add(comportement);
             }
-
-            await Db.SaveChangesAsync();
-        }
-
-
-
-        private async Task InitDb()
-        {
-            //ONLY FOR TESTS
-            var place = new Lieu("Elevage Langlade", "INRAE", "Mouton");
-            var person = new Personne("Bond", "James", "jamesbond@inrae.fr");
-            await Db.Personne.AddAsync(person);
-            await Db.Lieu.AddAsync(place);
             await Db.SaveChangesAsync();
-            //ONLY FOR TESTS
         }
     }
 }
\ No newline at end of file
diff --git a/GES_PAC/Controls/DoubleEntry.cs b/GES_PAC/Controls/DoubleEntry.cs
index 416468a..8489440 100644
--- a/GES_PAC/Controls/DoubleEntry.cs
+++ b/GES_PAC/Controls/DoubleEntry.cs
@@ -4,6 +4,21 @@ namespace GES_PAC.Controls
 {
     public class DoubleEntry : Entry
     {
+        public static readonly BindableProperty DoubleValueProperty =
+            BindableProperty.Create(
+                nameof(DoubleValue),
+                typeof(double?),
+                typeof(DoubleEntry),
+                default(double?),
+                BindingMode.TwoWay,
+                propertyChanged: OnDoubleValueChanged);
+
+        public double? DoubleValue
+        {
+            get => (double?)GetValue(DoubleValueProperty);
+            set => SetValue(DoubleValueProperty, value);
+        }
+
         public DoubleEntry()
         {
             Keyboard = Keyboard.Numeric;
@@ -11,6 +26,45 @@ namespace GES_PAC.Controls
             TextColor = Colors.Black;
             Placeholder = "0,0";
             FontSize = 16;
+
+            TextChanged += OnTextChanged;
+        }
+
+        private static void OnDoubleValueChanged(BindableObject bindable, object oldValue, object newValue)
+        {
+            if (bindable is DoubleEntry entry && newValue is double val)
+            {
+                var str = val.ToString(CultureInfo.CurrentCulture);
+                if (entry.Text != str)
+                    entry.Text = str;
+            }
+        }
+
+        private void OnTextChanged(object sender, TextChangedEventArgs e)
+        {
+            var input = e.NewTextValue;
+
+            if (string.IsNullOrWhiteSpace(input))
+                return;
+
+            if (input.StartsWith(","))
+            {
+                MainThread.BeginInvokeOnMainThread(() =>
+                {
+                    TextChanged -= OnTextChanged;
+                    Text = "0" + input;
+                    TextChanged += OnTextChanged;
+                });
+                return;
+            }
+            if (double.TryParse(input, NumberStyles.Float, CultureInfo.CurrentCulture, out double value))
+            {
+                SetValue(DoubleValueProperty, value);
+            }
+            else
+            {
+                SetValue(DoubleValueProperty, null);
+            }
         }
     }
 }
diff --git a/GES_PAC/Model/SerieAnimal.cs b/GES_PAC/Model/SerieAnimal.cs
index 4b55c87..dc3ce18 100644
--- a/GES_PAC/Model/SerieAnimal.cs
+++ b/GES_PAC/Model/SerieAnimal.cs
@@ -83,6 +83,12 @@ namespace GES_PAC.Model
 
             return $"{timeInChamber.Hours:00}:{timeInChamber.Minutes:00}" + (IsTimeWarning() ? "⚠" : "");
         }
+        public int GetMinInChamber()
+        {
+            if (Mesures.Count == 0)
+                return 0;
+            return (DateTime.Now - GetFirstMeasure().Time).Minutes;
+        }
         
     }
 }
diff --git a/GES_PAC/View/CreateMeasureView.xaml b/GES_PAC/View/CreateMeasureView.xaml
index af6474f..9f4c32a 100644
--- a/GES_PAC/View/CreateMeasureView.xaml
+++ b/GES_PAC/View/CreateMeasureView.xaml
@@ -55,7 +55,7 @@
                         <VerticalStackLayout>
                             <Label Text="CH4 (ppm)" FontSize="14" TextColor="Gray" FontAttributes="Bold"/>
                             <Grid ColumnDefinitions="*,Auto" ColumnSpacing="5">
-								<controls:DoubleEntry  Grid.Column="0" Text="{Binding ConcCH4, Mode=OneWayToSource, TargetNullValue=''}"/>
+                                <controls:DoubleEntry DoubleValue="{Binding ConcCH4}"/>
                                 <Label Grid.Column="1"
                                    Text="{Binding LastCH4, StringFormat='[{0} ppm]'}"
                                    FontSize="16"
@@ -76,7 +76,7 @@
                         <VerticalStackLayout>
                             <Label Text="O2 (%)" FontSize="14" TextColor="Gray" FontAttributes="Bold"/>
                             <Grid ColumnDefinitions="*,Auto" ColumnSpacing="5">
-								<controls:DoubleEntry Grid.Column="0" Text="{Binding ConcO2, Mode=OneWayToSource, TargetNullValue=''}"/>
+                                <controls:DoubleEntry DoubleValue="{Binding ConcO2}"/>
                                 <Label Grid.Column="1"
                                    Text="{Binding LastO2, StringFormat='[{0} %]'}"
                                    FontSize="16"
@@ -97,7 +97,7 @@
                         <VerticalStackLayout>
                             <Label Text="CO2 (%)" FontSize="14" TextColor="Gray" FontAttributes="Bold"/>
                             <Grid ColumnDefinitions="*,Auto" ColumnSpacing="5">
-								<controls:DoubleEntry Grid.Column="0" Text="{Binding ConcCO2, Mode=OneWayToSource, TargetNullValue=''}"/>
+                                <controls:DoubleEntry DoubleValue="{Binding ConcCO2}" />
                                 <Label Grid.Column="1"
                                    Text="{Binding LastCO2, StringFormat='[{0} %]'}"
                                    FontSize="16"
diff --git a/GES_PAC/View/ExportDataView.xaml b/GES_PAC/View/ExportDataView.xaml
index 6e32dd3..59977e5 100644
--- a/GES_PAC/View/ExportDataView.xaml
+++ b/GES_PAC/View/ExportDataView.xaml
@@ -13,12 +13,24 @@
             <ScrollView>
                 <StackLayout Spacing="100">
 
-                    <Label Text="Exporter les données"   
-                        FontSize="22"
-                        FontAttributes="Bold"
-                        HorizontalOptions="Center"/>
+					<Grid ColumnDefinitions="*,Auto,*" Padding="10">
 
-                    <Label Text="{Binding DataFoundText}"  
+						<Label Text="Exporter"
+                            FontSize="22"
+						    Grid.Column="1"
+                            FontAttributes="Bold"
+                            HorizontalOptions="Center"
+						    VerticalOptions="Center"/>
+						
+						<Button Text="Vider DB"
+	                        Command="{Binding ClearDataCommand}"
+	                        Grid.Column="2"
+	                        HorizontalOptions="End"
+	                        VerticalOptions="Center" />
+
+					</Grid>
+					
+					<Label Text="{Binding DataFoundText}"  
                        FontSize="18"  
                        HorizontalOptions="Center"/>
 
diff --git a/GES_PAC/ViewModel/Controls/ChamberButtonViewModel.cs b/GES_PAC/ViewModel/Controls/ChamberButtonViewModel.cs
index 199f885..91ccd88 100644
--- a/GES_PAC/ViewModel/Controls/ChamberButtonViewModel.cs
+++ b/GES_PAC/ViewModel/Controls/ChamberButtonViewModel.cs
@@ -1,5 +1,6 @@
 ﻿using GES_PAC.Model;
 using GES_PAC.View;
+using System.ComponentModel.Design;
 using System.Windows.Input;
 
 namespace GES_PAC.ViewModel.Controls
@@ -139,6 +140,7 @@ namespace GES_PAC.ViewModel.Controls
             ButtonIsEnabled = true;
             ButtonLabel = "0";
             TimeIn = "";
+            TimeColor = Colors.Black;
         }
         public void UpdateProperties()
         {
@@ -164,11 +166,11 @@ namespace GES_PAC.ViewModel.Controls
                 _isSubscribed = false;
             }
 
+            UpdateTimerColor();
             ButtonIsEnabled = !AnimalSet.IsOut;
             ButtonBackgroundColor = AnimalSet.HasBehaviour() ? Colors.Yellow : Colors.LightGreen;
             ButtonLabel = measureCount.ToString();
             TimeIn = AnimalSet.GetFormatedTimeInChamber();
-            TimeColor = AnimalSet.IsTimeWarning() ? Colors.Red : Colors.Black;
         }
 
 
@@ -186,7 +188,17 @@ namespace GES_PAC.ViewModel.Controls
         public void OnTimerTick(object? sender, EventArgs e)
         {
             TimeIn = AnimalSet?.GetFormatedTimeInChamber() ?? "";
-            TimeColor = AnimalSet?.IsTimeWarning() ?? false ? Colors.Red : Colors.Black;
+            UpdateTimerColor();
+        }
+        public void UpdateTimerColor()
+        {
+            var nbMesure = AnimalSet?.GetMeasureCount() ?? 0;
+            var min = AnimalSet?.GetMinInChamber() ?? 0;
+
+            if (nbMesure == 1 && min >= 25 ||
+                nbMesure == 2 && (min >= 50 || (AnimalSet.GetLastMeasure().Conc_CO2 > 2 && min >= 40)) ||
+                AnimalSet?.IsTimeWarning() == true)
+                    TimeColor = Colors.Red;
         }
 
         #endregion
diff --git a/GES_PAC/ViewModel/CreateMeasureViewModel.cs b/GES_PAC/ViewModel/CreateMeasureViewModel.cs
index 204bde2..a881d85 100644
--- a/GES_PAC/ViewModel/CreateMeasureViewModel.cs
+++ b/GES_PAC/ViewModel/CreateMeasureViewModel.cs
@@ -241,6 +241,12 @@ namespace GES_PAC.ViewModel
                 LastTime = "Dernière mesure : " + (int)(DateTime.Now - LastMeasure.Time).TotalMinutes + " min";
                 UpdateColors();
             }
+            else
+            {
+                ConcCH4 = 0.0;
+                ConcO2 = 20.9;
+                ConcCO2 = 0.0;
+            }
         }
         private void UpdateColors()
         {
diff --git a/GES_PAC/ViewModel/CreateSetViewModel.cs b/GES_PAC/ViewModel/CreateSetViewModel.cs
index a42b059..8209438 100644
--- a/GES_PAC/ViewModel/CreateSetViewModel.cs
+++ b/GES_PAC/ViewModel/CreateSetViewModel.cs
@@ -30,12 +30,21 @@ namespace GES_PAC.ViewModel
             get => _dateTimeMAJ.TimeOfDay;
             set
             {
-                _dateTimeMAJ = _dateTimeMAJ.Date + value;
+                var today = DateTime.Today;
+                var now = DateTime.Now;
+                var proposedTime = today + value;
+
+                if (proposedTime > now.AddHours(6))
+                    _dateTimeMAJ = today.AddDays(-1) + value;
+                else
+                    _dateTimeMAJ = today + value;
+
                 OnPropertyChanged();
                 ValidateForm();
             }
         }
 
+
         public double? Temperature
         {
             get => _temperature;
diff --git a/GES_PAC/ViewModel/ExportDataViewModel.cs b/GES_PAC/ViewModel/ExportDataViewModel.cs
index 0fe544f..202e990 100644
--- a/GES_PAC/ViewModel/ExportDataViewModel.cs
+++ b/GES_PAC/ViewModel/ExportDataViewModel.cs
@@ -40,6 +40,7 @@ namespace GES_PAC.ViewModel
         #region Commandes
         public ICommand ExportJSONCommand { get; }
         public ICommand ExportCSVCommand { get; }
+        public ICommand ClearDataCommand { get; }
         #endregion
 
         #region Constructeur
@@ -47,6 +48,7 @@ namespace GES_PAC.ViewModel
         {
             ExportJSONCommand = new Command(ExportJSON);
             ExportCSVCommand = new Command(ExportCSV);
+            ClearDataCommand = new Command(ClearData);
 
             DayList = JourneeViewModel.Instance?.Journees ?? null;
             HasData = DayList != null && DayList.Count > 0;
@@ -86,6 +88,29 @@ namespace GES_PAC.ViewModel
 #endif
         }
 
+        private async void ClearData()
+        {
+            if (IsBusy) return;
+            IsBusy = true;
+            App.Db.MesureCalibration.RemoveRange(App.Db.MesureCalibration);
+            App.Db.Calibration.RemoveRange(App.Db.Calibration);
+            App.Db.Mesure.RemoveRange(App.Db.Mesure);
+            App.Db.Comportement.RemoveRange(App.Db.Comportement);
+            App.Db.SerieAnimal.RemoveRange(App.Db.SerieAnimal);
+            App.Db.Serie.RemoveRange(App.Db.Serie);
+            App.Db.Journee.RemoveRange(App.Db.Journee);
+            App.Db.Personne.RemoveRange(App.Db.Personne);
+            App.Db.Lieu.RemoveRange(App.Db.Lieu);
+
+            var place = new Lieu("Elevage Langlade", "INRAE", "Mouton");
+            var person = new Personne("Bond", "James", "jamesbond@inrae.fr");
+            App.Db.Personne.Add(person);
+            App.Db.Lieu.Add(place);
+
+            await App.Db.SaveChangesAsync();
+            IsBusy = false;
+        }
+
         #endregion
     }
 }
diff --git a/GES_PAC/ViewModel/JourneeViewModel.cs b/GES_PAC/ViewModel/JourneeViewModel.cs
index bf282b4..6e63e2b 100644
--- a/GES_PAC/ViewModel/JourneeViewModel.cs
+++ b/GES_PAC/ViewModel/JourneeViewModel.cs
@@ -25,6 +25,8 @@ namespace GES_PAC.ViewModel
         public async Task InitAsync()
         {
             var journees = await App.Db.Journee
+                .Include(j => j.Responsable)
+                .Include(j => j.Lieu)
                 .Include(j => j.Calibrations)
                     .ThenInclude(c => c.Mesures)
                         .ThenInclude(m => m.Type)
@@ -38,7 +40,6 @@ namespace GES_PAC.ViewModel
                         .ThenInclude(sa => sa.Comportements)
                 .ToListAsync();
 
-
             foreach (var j in journees)
             {
                 foreach (var c in j.Calibrations)
