root / GES_PAC / ViewModel / ExportDataViewModel.cs @ 3fef487c
Historique | Voir | Annoter | Télécharger (3,525 ko)
1 | 957bebf1 | lbihannic | using System.Windows.Input; |
---|---|---|---|
2 | using GES_PAC.Model; |
||
3 | using GES_PAC.Helpers; |
||
4 | using System.Text.Json; |
||
5 | using System.Collections.ObjectModel; |
||
6 | 6f451cc1 | lbihannic | using System.Text.Json.Serialization; |
7 | 957bebf1 | lbihannic | |
8 | |||
9 | namespace GES_PAC.ViewModel |
||
10 | { |
||
11 | public class ExportDataViewModel : BaseViewModel |
||
12 | { |
||
13 | #region Attributs |
||
14 | private bool _hasData = false; |
||
15 | public string _dataFoundText; |
||
16 | #endregion |
||
17 | |||
18 | #region Propriétés |
||
19 | public bool HasData |
||
20 | { |
||
21 | get => _hasData; |
||
22 | set |
||
23 | { |
||
24 | SetProperty(ref _hasData, value); |
||
25 | OnPropertyChanged(); |
||
26 | } |
||
27 | } |
||
28 | public string DataFoundText |
||
29 | { |
||
30 | get => _dataFoundText; |
||
31 | set |
||
32 | { |
||
33 | SetProperty(ref _dataFoundText, value); |
||
34 | OnPropertyChanged(); |
||
35 | } |
||
36 | } |
||
37 | public ObservableCollection<Journee>? DayList { get; set; } |
||
38 | #endregion |
||
39 | |||
40 | #region Commandes |
||
41 | public ICommand ExportJSONCommand { get; } |
||
42 | public ICommand ExportCSVCommand { get; } |
||
43 | 3fef487c | lbihannic | public ICommand ClearDataCommand { get; } |
44 | 957bebf1 | lbihannic | #endregion |
45 | |||
46 | #region Constructeur |
||
47 | public ExportDataViewModel() |
||
48 | { |
||
49 | ExportJSONCommand = new Command(ExportJSON); |
||
50 | ExportCSVCommand = new Command(ExportCSV); |
||
51 | 3fef487c | lbihannic | ClearDataCommand = new Command(ClearData); |
52 | 957bebf1 | lbihannic | |
53 | DayList = JourneeViewModel.Instance?.Journees ?? null; |
||
54 | HasData = DayList != null && DayList.Count > 0; |
||
55 | DataFoundText = HasData ? "Données trouvées" : "Aucune donnée trouvée"; |
||
56 | } |
||
57 | #endregion |
||
58 | |||
59 | #region Méthodes |
||
60 | private void ExportJSON() |
||
61 | { |
||
62 | #if ANDROID |
||
63 | if(IsBusy) return; |
||
64 | IsBusy = true; |
||
65 | var lastDay = DayList.Last(); |
||
66 | string fileName = lastDay.Date.ToString(); |
||
67 | 6f451cc1 | lbihannic | string json = JsonSerializer.Serialize(lastDay, new JsonSerializerOptions |
68 | { |
||
69 | ReferenceHandler = ReferenceHandler.Preserve, |
||
70 | WriteIndented = true |
||
71 | }); |
||
72 | 957bebf1 | lbihannic | AndroidFileHelper.SaveJsonToDownloads(fileName, json); |
73 | IsBusy = false; |
||
74 | #endif |
||
75 | } |
||
76 | |||
77 | private void ExportCSV() |
||
78 | { |
||
79 | #if ANDROID |
||
80 | if(IsBusy) return; |
||
81 | IsBusy = true; |
||
82 | var lastDay = DayList.Last(); |
||
83 | string folderName = lastDay.Date.ToString(); |
||
84 | string[] csv = JourneeToCSV.ConvertToCSV(lastDay); |
||
85 | Console.WriteLine(csv[0]); |
||
86 | AndroidFileHelper.SaveCSVToDownloads(folderName, csv); |
||
87 | IsBusy = false; |
||
88 | #endif |
||
89 | } |
||
90 | |||
91 | 3fef487c | lbihannic | private async void ClearData() |
92 | { |
||
93 | if (IsBusy) return; |
||
94 | IsBusy = true; |
||
95 | App.Db.MesureCalibration.RemoveRange(App.Db.MesureCalibration); |
||
96 | App.Db.Calibration.RemoveRange(App.Db.Calibration); |
||
97 | App.Db.Mesure.RemoveRange(App.Db.Mesure); |
||
98 | App.Db.Comportement.RemoveRange(App.Db.Comportement); |
||
99 | App.Db.SerieAnimal.RemoveRange(App.Db.SerieAnimal); |
||
100 | App.Db.Serie.RemoveRange(App.Db.Serie); |
||
101 | App.Db.Journee.RemoveRange(App.Db.Journee); |
||
102 | App.Db.Personne.RemoveRange(App.Db.Personne); |
||
103 | App.Db.Lieu.RemoveRange(App.Db.Lieu); |
||
104 | |||
105 | var place = new Lieu("Elevage Langlade", "INRAE", "Mouton"); |
||
106 | var person = new Personne("Bond", "James", "jamesbond@inrae.fr"); |
||
107 | App.Db.Personne.Add(person); |
||
108 | App.Db.Lieu.Add(place); |
||
109 | |||
110 | await App.Db.SaveChangesAsync(); |
||
111 | IsBusy = false; |
||
112 | } |
||
113 | |||
114 | 957bebf1 | lbihannic | #endregion |
115 | } |
||
116 | } |