-
Notifications
You must be signed in to change notification settings - Fork 15
/
AddOrChangeDevicePage.xaml.cs
119 lines (104 loc) · 5.11 KB
/
AddOrChangeDevicePage.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Storage;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
// Pour plus d'informations sur le modèle d'élément Page vierge, consultez la page https://go.microsoft.com/fwlink/?LinkId=234238
namespace ScreenlyManager
{
/// <summary>
/// Une page vide peut être utilisée seule ou constituer une page de destination au sein d'un frame.
/// Back button : http://www.wintellect.com/devcenter/jprosise/handling-the-back-button-in-windows-10-uwp-apps
/// </summary>
public sealed partial class AddOrChangeDevicePage : Page
{
private List<Device> Devices;
private const string DB_FILE = "db.json";
private string PathDbFile;
private Device ExistingDevice;
private Windows.ApplicationModel.Resources.ResourceLoader Loader;
public AddOrChangeDevicePage()
{
this.Devices = new List<Device>();
// AppData folder access
var localFolder = ApplicationData.Current.LocalFolder;
this.PathDbFile = localFolder.Path + Path.DirectorySeparatorChar + DB_FILE;
this.Loader = new Windows.ApplicationModel.Resources.ResourceLoader();
this.InitializeComponent();
}
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
StorageFile file = await StorageFile.GetFileFromPathAsync(this.PathDbFile);
string json = await FileIO.ReadTextAsync(file);
this.Devices = JsonConvert.DeserializeObject<List<Device>>(json) ?? new List<Device>();
// Load device information into fields if Edit Mode
if(e.Parameter != null && e.Parameter is Device)
{
this.ExistingDevice = e.Parameter as Device;
this.TextBoxName.Text = this.ExistingDevice.Name;
this.TextBoxLocation.Text = this.ExistingDevice.Location;
this.TextBoxIp.Text = this.ExistingDevice.IpAddress;
this.TextBoxPort.Text = this.ExistingDevice.Port;
this.TextBoxApi.Text = this.ExistingDevice.ApiVersion;
this.TextBlockTitle.Text = $"{ this.Loader.GetString("EditDevice") } \"{ this.ExistingDevice.Name }\"";
}
}
private async void ButtonSubmit_Click(object sender, RoutedEventArgs e)
{
if(!this.TextBoxName.Text.Equals(string.Empty) && !this.TextBoxIp.Text.Equals(string.Empty) && !this.TextBoxPort.Equals(string.Empty))
{
// To catch Edit Mode
if (this.ExistingDevice != null)
{
Device deviceToDelete = this.Devices.Where(x => x.Name.Equals(this.ExistingDevice.Name) && x.IpAddress.Equals(this.ExistingDevice.IpAddress)).FirstOrDefault();
this.Devices.Remove(deviceToDelete);
}
Device newDevice = new Device
{
Name = this.TextBoxName.Text,
Location = this.TextBoxLocation.Text,
IpAddress = this.TextBoxIp.Text,
Port = this.TextBoxPort.Text,
ApiVersion = this.TextBoxApi.Text
};
this.Devices.Add(newDevice);
var dbContent = JsonConvert.SerializeObject(this.Devices);
StorageFile file = await StorageFile.GetFileFromPathAsync(this.PathDbFile);
if (file != null)
{
CachedFileManager.DeferUpdates(file);
await FileIO.WriteTextAsync(file, dbContent);
Windows.Storage.Provider.FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
var dialog = new MessageDialog(this.ExistingDevice != null ? this.Loader.GetString("ConfirmationEditDevice") : this.Loader.GetString("ConfirmationAddDevice"));
if (status != Windows.Storage.Provider.FileUpdateStatus.Complete)
{
dialog.Content = this.Loader.GetString("ErrorCannotSave");
dialog.Title = this.Loader.GetString("Error");
}
dialog.Commands.Add(new UICommand("Ok") { Id = 0 });
dialog.DefaultCommandIndex = 0;
var result = await dialog.ShowAsync();
this.Frame.Navigate(typeof(MainPage), null);
}
}
else
{
var dialogError = new MessageDialog(this.Loader.GetString("RequiredFileds"));
dialogError.Commands.Add(new UICommand("Ok") { Id = 0 });
dialogError.DefaultCommandIndex = 0;
await dialogError.ShowAsync();
this.TextBoxName.Focus(FocusState.Programmatic);
this.TextBoxName.SelectAll();
}
}
private void ButtonCancel_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage), null);
}
}
}