-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdentifiantViewModel.cs
More file actions
77 lines (66 loc) · 2.49 KB
/
Copy pathIdentifiantViewModel.cs
File metadata and controls
77 lines (66 loc) · 2.49 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
using System.ComponentModel;
using System.Text.RegularExpressions;
using System.Windows.Media;
using Upsilon.Apps.Passkey.GUI.WPF.Themes;
using Upsilon.Apps.Passkey.Interfaces.Models;
using Upsilon.Apps.Passkey.Interfaces.Utils;
namespace Upsilon.Apps.Passkey.GUI.WPF.ViewModels.Controls
{
public partial class IdentifierViewModel : INotifyPropertyChanged
{
private readonly IAccount _account;
public static readonly Dictionary<string, string> IdentifiersTypes = new()
{
{ "[Username]", "👤" },
{ "[Email]", "📧" },
{ "[Phone Number]", "🖁" },
{ "[Passkey]", "🗝" },
{ "[Authentificator App]", "📲" },
};
public Brush IdentifierBackground => _account.HasChanged("Identifiers") ? DarkMode.ChangedBrush : DarkMode.UnchangedBrush2;
public string Identifier
{
get;
set
{
if (field != value)
{
if (IdentifiersTypes.Keys.Union(IdentifiersTypes.Values).All(x => !value.StartsWith(x, StringComparison.CurrentCultureIgnoreCase)))
{
value = _getIdentifierType(value);
}
foreach (KeyValuePair<string, string> idType in IdentifiersTypes)
{
field = value.Replace(idType.Key, idType.Value);
}
OnPropertyChanged(nameof(Identifier));
}
}
} = string.Empty;
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs($"{propertyName}Background"));
}
public IdentifierViewModel(IAccount account, string identifier)
{
_account = account;
Identifier = identifier;
}
public void Refresh()
{
OnPropertyChanged(nameof(IdentifierBackground));
}
[GeneratedRegex(@"^\+\d{1,3}[\d\s\-\.]{6,20}$")]
private static partial Regex _phoneRegex();
[GeneratedRegex(@"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$")]
private static partial Regex _mailRegex();
private static string _getIdentifierType(string identifier)
{
return _phoneRegex().IsMatch(identifier) ? "🖁" + identifier
: _mailRegex().IsMatch(identifier) ? "📧" + identifier
: "👤" + identifier;
}
}
}