-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsPage.xaml.cs
184 lines (166 loc) · 7.01 KB
/
SettingsPage.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Search;
namespace proshandadmin
{
public sealed partial class SettingsPage : Page
{
Dictionary<string, string> settings = new();
public SettingsPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFolder settingsFolder = ApplicationData.Current.LocalFolder;
Debug.WriteLine(ApplicationData.Current.LocalFolder.Path);
StorageFileQueryResult results = settingsFolder.CreateFileQuery();
IReadOnlyList<StorageFile> filesInFolder = await results.GetFilesAsync();
if (filesInFolder.Count > 0)
{
foreach (StorageFile item in filesInFolder)
{
Debug.WriteLine(item.Name);
if (item.Name == "settings.json")
{
Debug.WriteLine("Writing to file");
// Store the settings to the JSON
await FileIO.WriteTextAsync(item, $"{{Thirty: '{ThirtyDir.Text.Replace('\\', '>')}', ThirtyFive: '{ThirtyFiveDir.Text.Replace('\\', '>')}', Forty: '{FortyDir.Text.Replace('\\', '>')}', FortyFive: '{FortyFiveDir.Text.Replace('\\', '>')}', OutputDir: '{OutputDir.Text.Replace('\\', '>')}'}}");
}
}
}
SaveButtonIcon.Glyph = "\uE73E";
SaveButtonText.Text = "Saved";
await Task.Delay(500);
SaveButtonIcon.Glyph = "\uE74E";
SaveButtonText.Text = "Save";
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
// Read the settings JSON and populate the settings page options
StorageFolder settingsFolder = ApplicationData.Current.LocalFolder;
Debug.WriteLine(ApplicationData.Current.LocalFolder.Path);
StorageFileQueryResult results = settingsFolder.CreateFileQuery();
IReadOnlyList<StorageFile> filesInFolder = await results.GetFilesAsync();
if (filesInFolder.Count > 0)
{
foreach (StorageFile item in filesInFolder)
{
Debug.WriteLine(item.Name);
if (item.Name == "settings.json")
{
// Export the options to a dictionary
Debug.WriteLine("Found the settings file!");
string json = File.ReadAllText(item.Path);
settings = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
var thirtyPath = settings["Thirty"].Replace('>', '\\');
var thirtyFivePath = settings["ThirtyFive"].Replace('>', '\\');
var fortyPath = settings["Forty"].Replace('>', '\\');
var fortyFivePath = settings["FortyFive"].Replace('>', '\\');
var outputDirPath = settings["OutputDir"].Replace('>', '\\');
if (thirtyPath.Length > 0)
{
ThirtyDir.Text = thirtyPath;
}
if (thirtyFivePath.Length > 0)
{
ThirtyFiveDir.Text = thirtyFivePath;
}
if (fortyPath.Length > 0)
{
FortyDir.Text = fortyPath;
}
if (fortyFivePath.Length > 0)
{
FortyFiveDir.Text = fortyFivePath;
}
if (fortyFivePath.Length > 0)
{
FortyFiveDir.Text = fortyFivePath;
}
if (outputDirPath.Length > 0)
{
OutputDir.Text = outputDirPath;
}
}
}
}
else
{
// Create the file
StorageFile settingsFile = await settingsFolder.CreateFileAsync("settings.json", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(settingsFile, "{Thirty: '', ThirtyFive: '', Forty: '', FortyFive: '', OutputDir: ''}");
}
}
private static async void SetFilePath(TextBox textBox)
{
var openPicker = new FileOpenPicker();
var window = App.m_window;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hWnd);
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.FileTypeFilter.Add(".gltf");
var file = await openPicker.PickSingleFileAsync();
if (file != null)
{
textBox.Text = file.Path;
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
SetFilePath(ThirtyDir);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
SetFilePath(ThirtyFiveDir);
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
SetFilePath(FortyDir);
}
private void Button_Click_4(object sender, RoutedEventArgs e)
{
SetFilePath(FortyFiveDir);
}
private async void Button_Click_5(object sender, RoutedEventArgs e)
{
var openPicker = new FolderPicker();
var window = App.m_window;
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
WinRT.Interop.InitializeWithWindow.Initialize(openPicker, hWnd);
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.Desktop;
openPicker.FileTypeFilter.Add("*");
var folder = await openPicker.PickSingleFolderAsync();
if (folder != null)
{
OutputDir.Text = folder.Path;
}
}
private void Button_Click_6(object sender, RoutedEventArgs e)
{
foreach(var textBox in new List<TextBox>{ThirtyDir, ThirtyFiveDir, FortyDir, FortyFiveDir, OutputDir})
{
textBox.Text = "";
}
}
}
}