Statistiques
| Branche: | Révision:

root / GES_PAC / ViewModel / ExportDataViewModel.cs @ 6f451cc1

Historique | Voir | Annoter | Télécharger (2,483 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
        #endregion
44
45
        #region Constructeur
46
        public ExportDataViewModel()
47
        {
48
            ExportJSONCommand = new Command(ExportJSON);
49
            ExportCSVCommand = new Command(ExportCSV);
50
51
            DayList = JourneeViewModel.Instance?.Journees ?? null;
52
            HasData = DayList != null && DayList.Count > 0;
53
            DataFoundText = HasData ? "Données trouvées" : "Aucune donnée trouvée";
54
        }
55
        #endregion
56
57
        #region Méthodes
58
        private void ExportJSON()
59
        {
60
#if ANDROID
61
            if(IsBusy) return;
62
            IsBusy = true;
63
            var lastDay = DayList.Last();
64
            string fileName = lastDay.Date.ToString();
65 6f451cc1 lbihannic
            string json = JsonSerializer.Serialize(lastDay, new JsonSerializerOptions
66
            {
67
                ReferenceHandler = ReferenceHandler.Preserve,
68
                WriteIndented = true
69
            });
70 957bebf1 lbihannic
            AndroidFileHelper.SaveJsonToDownloads(fileName, json);
71
            IsBusy = false;
72
#endif
73
        }
74
75
        private void ExportCSV()
76
        {
77
#if ANDROID
78
            if(IsBusy) return;
79
            IsBusy = true;
80
            var lastDay = DayList.Last();
81
            string folderName = lastDay.Date.ToString();
82
            string[] csv = JourneeToCSV.ConvertToCSV(lastDay);
83
            Console.WriteLine(csv[0]);
84
            AndroidFileHelper.SaveCSVToDownloads(folderName, csv);
85
            IsBusy = false;
86
#endif
87
        }
88
89
        #endregion
90
    }
91
}