-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettingsWindow.xaml.cs
More file actions
112 lines (98 loc) · 3.82 KB
/
SettingsWindow.xaml.cs
File metadata and controls
112 lines (98 loc) · 3.82 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using Newtonsoft.Json;
namespace Just_One_Click
{
public class Settings
{
public bool DarkModeEnabled { get; set; }
public string TextEditor { get; set; }
public bool DeleteConfirmation { get; set; }
}
public partial class SettingsWindow : Window
{
string SettingsFilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
private Settings _appSettings;
public SettingsWindow()
{
InitializeComponent();
SettingsFilePath = SettingsFilePath + "appsettings.json";
Trace.WriteLine(SettingsFilePath);
// Load settings when the window is initialized
_appSettings = LoadSettings();
UpdateUI();
}
private Settings LoadSettings()
{
try
{
if (File.Exists(SettingsFilePath))
{
string json = File.ReadAllText(SettingsFilePath);
return JsonConvert.DeserializeObject<Settings>(json);
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading settings: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
return new Settings { DarkModeEnabled = false, TextEditor = "Notepad" };
// Return default settings if the file doesn't exist or there's an error
}
private void SaveSettings()
{
try
{
string json = JsonConvert.SerializeObject(_appSettings, Formatting.Indented);
File.WriteAllText(SettingsFilePath, json);
}
catch (Exception ex)
{
MessageBox.Show($"Error saving settings: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void UpdateUI()
{
// Update your UI controls with the loaded settings
DarkModeCheckbox.IsChecked = _appSettings.DarkModeEnabled;
Path.Text = _appSettings.TextEditor;
ConfirmationCheckbox.IsChecked = _appSettings.DeleteConfirmation;
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
// Update settings with values from UI controls
_appSettings.DarkModeEnabled = DarkModeCheckbox.IsChecked ?? false;
_appSettings.TextEditor = Path.Text;
// Save settings to the JSON file
SaveSettings();
}
private void FileBrowserDialog(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog
{
Title = "Select JSON File",
Filter = "JSON Files|*.json",
InitialDirectory = AppDomain.CurrentDomain.BaseDirectory
};
if (openFileDialog.ShowDialog() == true)
{
// Load settings from the selected JSON file
string json = File.ReadAllText(openFileDialog.FileName);
_appSettings = JsonConvert.DeserializeObject<Settings>(json);
// Update UI with the loaded settings
UpdateUI();
}
}
private void ResetClick(object sender, RoutedEventArgs e)
{
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); // Checks Documents Folder for path
savePath = System.IO.Path.Combine(savePath + "/Just One Click/");
string saveFile = System.IO.Path.Combine(savePath + "savedata.json");
MainWindow main = new MainWindow();
main.WritePlaceholderJson(saveFile);
}
}
}