/
SaveVehicleCommand.cs
107 lines (83 loc) · 2.86 KB
/
SaveVehicleCommand.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
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Navigation;
using Crystalbyte.Asphalt.Pages;
using Microsoft.Phone.Shell;
using System.Composition;
using Crystalbyte.Asphalt.Contexts;
using Crystalbyte.Asphalt.Resources;
using Crystalbyte.Asphalt.Data;
namespace Crystalbyte.Asphalt.Commands {
[Export, Shared]
[Export(typeof(IAppBarButtonCommand))]
public sealed class SaveVehicleCommand : IAppBarButtonCommand {
public SaveVehicleCommand() {
Button = new ApplicationBarIconButton(new Uri("/Assets/ApplicationBar/Save.png", UriKind.Relative)) {
Text = AppResources.SaveVehicleButtonText
};
Button.Click += (sender, e) => Execute(null);
}
[Import]
public Navigator Navigator { get; set; }
[Import]
public AppContext AppContext { get; set; }
[Import]
public VehicleSelectionSource VehicleSelectionSource { get; set; }
[Import]
public LocalStorage LocalStorage { get; set; }
[Import]
public Channels Channels { get; set; }
public event EventHandler VehicleSaved;
public void OnVehicleSaved(EventArgs e) {
var handler = VehicleSaved;
if (handler != null)
handler(this, e);
}
#region Implementation of ICommand
public bool CanExecute(object parameter) {
return IsApplicable;
}
public async void Execute(object parameter) {
var vehicle = VehicleSelectionSource.Selection;
var id = vehicle.Id;
await Channels.Database.Enqueue(() => {
if (id == 0) {
LocalStorage.DataContext.Vehicles.InsertOnSubmit(vehicle);
}
LocalStorage.DataContext.SubmitChanges(ConflictMode.FailOnFirstConflict);
});
OnVehicleSaved(EventArgs.Empty);
if (Navigator.Service.CanGoBack) {
Navigator.Service.GoBack();
} else {
Navigator.Navigate<LandingPage>();
}
}
public event EventHandler CanExecuteChanged;
public void OnCanExecuteChanged(EventArgs e) {
var handler = CanExecuteChanged;
if (handler != null)
handler(this, e);
}
#endregion
#region Implementation of IAppBarButtonCommand
public ApplicationBarIconButton Button {
get;
private set;
}
public bool IsApplicable {
get {
var page = Navigator.GetCurrentPage<VehicleCompositionPage>();
return page != null;
}
}
public int Position {
get { return 0; }
}
#endregion
}
}