-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.xaml.cs
111 lines (88 loc) · 3.58 KB
/
Settings.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
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using System;
using System.Collections.ObjectModel;
namespace homarr {
public sealed partial class Settings : Page {
public record class Setting(
string key = null,
string description = null
);
public static ReadOnlyCollection<Setting> mandatorySettings = new(
new Setting[] {
new Setting {
key = "SonarrUrl",
description = "Sonarr URL"
},
new Setting {
key = "SonarrApiKey",
description = "Sonarr API Key"
},
new Setting {
key = "RadarrUrl",
description = "Radarr URL"
},
new Setting {
key = "RadarrApiKey",
description = "Radarr API Key"
}
}
);
public Settings() {
this.InitializeComponent();
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
for (var i = 0; i < mandatorySettings.Count; i++) {
var setting = mandatorySettings[i];
var textBlock = new TextBlock {
Text = setting.description,
VerticalAlignment = VerticalAlignment.Center,
Margin = new Thickness(10),
FontFamily = new FontFamily("/Assets/fonts/cerebrisans-regular.ttf#Cerebri Sans"),
};
var textBox = new TextBox {
Margin = new Thickness(10),
Width = 350,
FontFamily = new FontFamily("/Assets/fonts/cerebrisans-regular.ttf#Cerebri Sans"),
};
if (localSettings.Values[setting.key] != null) {
textBox.Text = localSettings.Values[setting.key] as string;
}
textBox.SelectionChanged += (object sender, RoutedEventArgs e) => {
localSettings.Values[setting.key] = textBox.Text;
};
var border = new Border {
Background = (Brush)Application.Current.Resources["CardBackgroundFillColorDefaultBrush"],
Padding = new Thickness(10),
CornerRadius = new CornerRadius(5),
};
Grid.SetRow(border, i);
Grid.SetColumnSpan(border, 2);
this.SettingsGrid.Children.Add(border);
Grid.SetRow(textBlock, i);
Grid.SetColumn(textBlock, 0);
this.SettingsGrid.Children.Add(textBlock);
Grid.SetRow(textBox, i);
Grid.SetColumn(textBox, 1);
this.SettingsGrid.Children.Add(textBox);
}
}
public static bool IsMissingSettings() {
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
foreach (var setting in mandatorySettings) {
if (localSettings.Values[setting.key] == null) {
return true;
}
}
return false;
}
public static string GetSetting(string key) {
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
var value = localSettings.Values[key];
if (value != null) {
return value.ToString();
}
throw new Exception($"Setting '{key}' not found");
}
}
}