-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingControl.xaml.cs
90 lines (76 loc) · 2.71 KB
/
SettingControl.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
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
namespace Flow.Launcher.Plugin.OneTimePassword
{
public class SettingControlViewModel : BaseModel
{
private readonly Setting Settings;
private ObservableCollection<Otp> otps = new();
public ObservableCollection<Otp> Otps
{
get => otps;
set
{
otps = value;
OnPropertyChanged(nameof(otps));
}
}
public SettingControlViewModel(Setting settings)
{
Settings = settings;
Otps = new(settings.Otps);
}
public Setting ToSettings()
{
Settings.Otps = Otps.ToList();
return Settings;
}
}
public partial class SettingControl : UserControl
{
private readonly PluginInitContext Context;
private readonly SettingControlViewModel ViewModel;
public event EventHandler<Setting>? OnUpdate;
public SettingControl(PluginInitContext context, Setting settings)
{
InitializeComponent();
Context = context;
DataContext = ViewModel = new(settings);
}
private void OnAdd(object sender, RoutedEventArgs e)
{
var window = new EditorWindow(Context);
window.OnConfirmed += (object? sender, Otp otp) =>
{
ViewModel.Otps.Add(otp);
OnUpdate?.Invoke(this, ViewModel.ToSettings());
};
window.ShowDialog();
}
private void OnDelete(object sender, RoutedEventArgs e)
{
var index = ListView.SelectedIndex;
var selected = ViewModel.Otps.ElementAtOrDefault(index);
if (selected == null) return;
var warning = Context.API.GetTranslation("flowlauncher_plugin_otp_setting_delete_warning");
var result = MessageBox.Show(string.Format(warning, selected.Label), "", MessageBoxButton.YesNo);
if (result != MessageBoxResult.Yes) return;
ViewModel.Otps.RemoveAt(index);
OnUpdate?.Invoke(this, ViewModel.ToSettings());
}
private void OnEdit(object sender, RoutedEventArgs e)
{
var index = ListView.SelectedIndex;
var selected = ViewModel.Otps.ElementAtOrDefault(index);
if (selected == null) return;
var window = new EditorWindow(Context, selected);
window.OnConfirmed += (object? sender, Otp otp) =>
{
ViewModel.Otps[index] = otp;
OnUpdate?.Invoke(this, ViewModel.ToSettings());
};
window.ShowDialog();
}
}
}