root / GES_PAC / ViewModel / ExportDataViewModel.cs @ 9601eaf0
Historique | Voir | Annoter | Télécharger (2,296 ko)
1 |
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 |
|
7 |
|
8 |
namespace GES_PAC.ViewModel |
9 |
{ |
10 |
public class ExportDataViewModel : BaseViewModel |
11 |
{ |
12 |
#region Attributs |
13 |
private bool _hasData = false; |
14 |
public string _dataFoundText; |
15 |
#endregion |
16 |
|
17 |
#region Propriétés |
18 |
public bool HasData |
19 |
{ |
20 |
get => _hasData; |
21 |
set |
22 |
{ |
23 |
SetProperty(ref _hasData, value); |
24 |
OnPropertyChanged(); |
25 |
} |
26 |
} |
27 |
public string DataFoundText |
28 |
{ |
29 |
get => _dataFoundText; |
30 |
set |
31 |
{ |
32 |
SetProperty(ref _dataFoundText, value); |
33 |
OnPropertyChanged(); |
34 |
} |
35 |
} |
36 |
public ObservableCollection<Journee>? DayList { get; set; } |
37 |
#endregion |
38 |
|
39 |
#region Commandes |
40 |
public ICommand ExportJSONCommand { get; } |
41 |
public ICommand ExportCSVCommand { get; } |
42 |
#endregion |
43 |
|
44 |
#region Constructeur |
45 |
public ExportDataViewModel() |
46 |
{ |
47 |
ExportJSONCommand = new Command(ExportJSON); |
48 |
ExportCSVCommand = new Command(ExportCSV); |
49 |
|
50 |
DayList = JourneeViewModel.Instance?.Journees ?? null; |
51 |
HasData = DayList != null && DayList.Count > 0; |
52 |
DataFoundText = HasData ? "Données trouvées" : "Aucune donnée trouvée"; |
53 |
} |
54 |
#endregion |
55 |
|
56 |
#region Méthodes |
57 |
private void ExportJSON() |
58 |
{ |
59 |
#if ANDROID |
60 |
if(IsBusy) return; |
61 |
IsBusy = true; |
62 |
var lastDay = DayList.Last(); |
63 |
string fileName = lastDay.Date.ToString(); |
64 |
string json = JsonSerializer.Serialize(lastDay); |
65 |
AndroidFileHelper.SaveJsonToDownloads(fileName, json); |
66 |
IsBusy = false; |
67 |
#endif |
68 |
} |
69 |
|
70 |
private void ExportCSV() |
71 |
{ |
72 |
#if ANDROID |
73 |
if(IsBusy) return; |
74 |
IsBusy = true; |
75 |
var lastDay = DayList.Last(); |
76 |
string folderName = lastDay.Date.ToString(); |
77 |
string[] csv = JourneeToCSV.ConvertToCSV(lastDay); |
78 |
Console.WriteLine(csv[0]); |
79 |
AndroidFileHelper.SaveCSVToDownloads(folderName, csv); |
80 |
IsBusy = false; |
81 |
#endif |
82 |
} |
83 |
|
84 |
#endregion |
85 |
} |
86 |
} |
87 |
|