/
DriverCompositionPage.xaml.cs
150 lines (119 loc) · 4.41 KB
/
DriverCompositionPage.xaml.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.IO;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Navigation;
using Crystalbyte.Asphalt.Contexts;
using Crystalbyte.Asphalt.Data;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using GestureEventArgs = System.Windows.Input.GestureEventArgs;
namespace Crystalbyte.Asphalt.Pages {
public partial class DriverCompositionPage {
private const string DriverStateKey = "driver";
private bool _isNewPageInstance;
private readonly PhotoChooserTask _photoChooser;
private string _chosenPhotoName;
public DriverCompositionPage() {
InitializeComponent();
BindingValidationError += OnBindingValidationError;
_photoChooser = new PhotoChooserTask { ShowCamera = true, PixelHeight = 200, PixelWidth = 200 };
_photoChooser.Completed += OnPhotoChooserTaskCompleted;
_isNewPageInstance = true;
}
public DriverSelectionSource DriverSelectionSource {
get {
return App.Composition.GetExport<DriverSelectionSource>();
}
}
protected override void OnKeyUp(KeyEventArgs e) {
if (e.Key == Key.Enter) {
HandleEnterKey();
e.Handled = true;
}
base.OnKeyUp(e);
}
private void HandleEnterKey() {
Focus();
}
private void OnBindingValidationError(object sender, ValidationErrorEventArgs e) {
var button = ApplicationBar.Buttons.OfType<ApplicationBarIconButton>().First();
button.IsEnabled = !Driver.HasErrors;
}
public LocalStorage LocalStorage {
get {
return App.Composition.GetExport<LocalStorage>();
}
}
/// <summary>
/// Gets or sets the current datacontext as a vehicle.
/// </summary>
public Driver Driver {
get { return (Driver)DataContext; }
set { DataContext = value; }
}
protected override void OnNavigatedFrom(NavigationEventArgs e) {
base.OnNavigatedFrom(e);
if (e.NavigationMode == NavigationMode.New) {
State[DriverStateKey] = Driver;
}
}
protected override void OnNavigatedTo(NavigationEventArgs e) {
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.New) {
InitializeDriver();
}
if (_isNewPageInstance && Driver == null) {
Driver = (Driver)State[DriverStateKey];
}
if (_chosenPhotoName != null) {
Driver.ImagePath = _chosenPhotoName;
}
if (Driver.Image == null && Driver.ImagePath != null) {
Driver.RestoreImageFromPath();
}
this.UpdateApplicationBar();
Driver.ValidateAll();
_isNewPageInstance = false;
}
private void InitializeDriver() {
Driver = DriverSelectionSource.Selection;
Driver.ValidateAll();
}
private void OnStackPanelTap(object sender, GestureEventArgs e) {
SelectImage();
}
private void SelectImage() {
_photoChooser.Show();
}
private void OnPhotoChooserTaskCompleted(object sender, PhotoResult e) {
// User canceled photo selection
if (e.ChosenPhoto == null) {
return;
}
SmartDispatcher.InvokeAsync(() =>
HandleChosenPhoto(e.OriginalFileName, e.ChosenPhoto));
}
private async void HandleChosenPhoto(string name, Stream data) {
if (string.IsNullOrWhiteSpace(name)) {
return;
}
_chosenPhotoName = Guid.NewGuid().ToString();
await LocalStorage.StoreImageAsync(_chosenPhotoName, data);
}
private void OnForenameChanged(object sender, TextChangedEventArgs e) {
var textbox = (TextBox)sender;
Driver.Forename = textbox.Text;
}
private void OnSurnameChanged(object sender, TextChangedEventArgs e) {
var textbox = (TextBox)sender;
Driver.Surname = textbox.Text;
}
}
}