-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathExportViewModel.cs
128 lines (103 loc) · 3.29 KB
/
ExportViewModel.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Input;
using GridExport.Models;
using DevExpress.Maui.Controls;
using DevExpress.Maui.DataGrid;
using Microsoft.Maui.ApplicationModel.DataTransfer;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Storage;
namespace GridExport.ViewModels;
public enum ExportFormat {
Pdf, Xlsx, Docx
}
public enum Orientation {
Portrait, Landscape
}
public enum PaperSize {
A4, A3, A5
}
public class ExportViewModel : BindableObject {
Exporter exporter;
bool isLandscape;
BottomSheetState bottomSheetState;
string fileName;
public ExportViewModel() {
this.fileName = "SampleExport";
this.bottomSheetState = BottomSheetState.Hidden;
this.isLandscape = true;
this.exporter = new Exporter(OnStartExport, OnEndExport, OnCancelExport);
SelectedFormat = ExportFormat.Pdf;
SelectedPaperSize = PaperSize.A4;
}
public IList<Employee> Employees { get; } = new EmployeesRepository().Employees;
public ExportFormat SelectedFormat { get; set; }
public PaperSize SelectedPaperSize { get; set; }
public bool PortraitSelected {
get => !this.isLandscape;
set {
if (value) {
this.isLandscape = false;
OnPropertyChanged(nameof(LandscapeSelected));
} else {
OnPropertyChanged(nameof(PortraitSelected));
}
}
}
public bool LandscapeSelected {
get => this.isLandscape;
set {
if (value) {
this.isLandscape = true;
OnPropertyChanged(nameof(PortraitSelected));
} else {
OnPropertyChanged(nameof(LandscapeSelected));
}
}
}
public BottomSheetState BottomSheetState {
get => this.bottomSheetState;
set {
this.bottomSheetState = value;
OnPropertyChanged();
}
}
public bool IsInExport => this.exporter.IsInExport;
public ICommand ExportCommand => new Command<DataGridView>(PerformExport);
public ICommand CancelExportCommand => new Command(CancelExport);
string ExportFilePath => Path.Combine(FileSystem.CacheDirectory, this.fileName + $".{SelectedFormat}".ToLower());
void OnStartExport() {
OnPropertyChanged(nameof(IsInExport));
}
void OnEndExport(Exception ex) {
//You can properly handle the errors here
if (ex is null) {
Dispatcher.Dispatch(() => {
CloseBottomSheet();
OpenShareDialog(ExportFilePath);
});
}
OnPropertyChanged(nameof(IsInExport));
}
void OnCancelExport() {
OnPropertyChanged(nameof(IsInExport));
}
void PerformExport(DataGridView grid) {
this.exporter.Export(grid, ExportFilePath, SelectedFormat, SelectedPaperSize, LandscapeSelected);
}
void CancelExport() {
this.exporter.Cancel();
}
void CloseBottomSheet() {
BottomSheetState = BottomSheetState.Hidden;
}
async void OpenShareDialog(string path) {
await Task.Delay(1000);
await Share.Default.RequestAsync(new ShareFileRequest {
Title = "Share exported file",
File = new ShareFile(path)
});
}
}