This repository has been archived by the owner on Jun 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SettingsView.axaml.cs
100 lines (86 loc) · 3.5 KB
/
SettingsView.axaml.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
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.SettingsFactory.Demo.Models;
using Avalonia.SettingsFactory.Demo.ViewModels;
using Avalonia.SettingsFactory.ViewModels;
using Avalonia.Styling;
using Avalonia.Themes.Fluent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Threading.Tasks;
namespace Avalonia.SettingsFactory.Demo.Views
{
public partial class SettingsView : SettingsFactory, ISettingsValidator
{
public SettingsView()
{
AvaloniaXamlLoader.Load(this);
// Very much unnecessary, but not having this bothers me.
// Allows you to focus seemingly nothing.
Grid focusDelegate = this.FindControl<Grid>("FocusDelegate")!;
focusDelegate.PointerPressed += (_, _) => focusDelegate.Focus();
Grid focusDelegate2 = this.FindControl<Grid>("FocusDelegate2")!;
focusDelegate2.PointerPressed += (_, _) => focusDelegate.Focus();
SettingsFactoryOptions options = new()
{
// Application implementation of a message prompt
AlertAction = (msg) => Debug.WriteLine(msg),
// Folder browse dialog or custom input system.
BrowseAction = async (title) =>
{
OpenFolderDialog dialog = new() { Title = title };
var result = await dialog.ShowAsync(App.StaticView);
return result;
},
// Custom resource loader
FetchResource = (res) => JsonSerializer.Deserialize<Dictionary<string, string>>(File.ReadAllText(res))
};
// Initialize the settings layout
InitializeSettingsFactory(new SettingsFactoryViewModel(true), this, Settings.Config, options);
AfterSaveEvent += () => {
// Dispose view
(App.StaticView.DataContext as AppViewModel)!.Content = null;
};
AfterCancelEvent += () => {
// Dispose view
(App.StaticView.DataContext as AppViewModel)!.Content = null;
};
}
public bool? ValidateBool(string key, bool value)
{
return key switch
{
_ => null
};
}
public bool? ValidateString(string key, string value)
{
return key switch
{
"UserName" => value.All(c => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')),
"FullName" => value.Contains((string)this["FirstName"]!) && value.Contains((string)this["LastName"]!),
"Theme" => ValidateTheme(value),
_ => null
};
}
public static bool? ValidateTheme(string value)
{
Application.Current!.RequestedThemeVariant = value == "Dark" ? ThemeVariant.Dark : ThemeVariant.Light;
return null;
}
public string? ValidateSave(Dictionary<string, bool?> validated)
{
if (validated["UserName"] == null)
{
return "Please enter a username before saving!";
}
// By default return a standard error message,
// or null where all validations passed
return validated.Where(x => x.Value == false).Any() ? "One or more settings could not be verified. Please review your settings." : null;
}
}
}