From e0e273d1a4ed0e0380269bcc773096c05a6bb976 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cezary=20Pi=C4=85tek?= Date: Tue, 19 Jul 2022 22:29:29 +0200 Subject: [PATCH] Add Vault window --- .../Settings/AppSettingsService.cs | 8 ++- .../ViewModels/MainWindowViewModel.cs | 6 ++ .../ViewModels/VaultViewModel.cs | 63 +++++++++++++++++++ .../ScriptRunner.GUI/Views/MainWindow.axaml | 3 +- .../ScriptRunner.GUI/Views/Vault.axaml | 51 +++++++++++++++ .../ScriptRunner.GUI/Views/Vault.axaml.cs | 33 ++++++++++ 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 src/ScriptRunner/ScriptRunner.GUI/ViewModels/VaultViewModel.cs create mode 100644 src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml create mode 100644 src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml.cs diff --git a/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs b/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs index 1896db9..adf0f57 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/Settings/AppSettingsService.cs @@ -80,12 +80,18 @@ public static void UpdateScriptConfigs(IEnumerable configScri } private static string GetSettingsPath() + { + return GetSettingsPathFor("settings.json"); + } + + public static string GetSettingsPathFor(string settingsFile) { var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ScriptRunner"); if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } - return Path.Combine(path, "settings.json"); + + return Path.Combine(path, settingsFile); } } \ No newline at end of file diff --git a/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs index f206e15..e6e6a3f 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs +++ b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/MainWindowViewModel.cs @@ -288,6 +288,12 @@ public void RefreshSettings() BuildUi(); } + public void OpenVaultWindow() + { + var window = new Vault(); + window.Show(); + } + public bool SelectedActionInstalled { diff --git a/src/ScriptRunner/ScriptRunner.GUI/ViewModels/VaultViewModel.cs b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/VaultViewModel.cs new file mode 100644 index 0000000..64f5b80 --- /dev/null +++ b/src/ScriptRunner/ScriptRunner.GUI/ViewModels/VaultViewModel.cs @@ -0,0 +1,63 @@ +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.Json; +using ReactiveUI; +using ScriptRunner.GUI.Settings; + +namespace ScriptRunner.GUI.ViewModels +{ + public class VaultViewModel : ViewModelBase + { + public ObservableCollection Entries + { + get => _entries; + set => this.RaiseAndSetIfChanged(ref _entries, value); + } + + private ObservableCollection _entries; + + + public VaultViewModel() + { + var vaultPath = AppSettingsService.GetSettingsPathFor("Vault.dat"); + if (File.Exists(vaultPath)) + { + File.Decrypt(vaultPath); + var content = File.ReadAllText(vaultPath); + File.Encrypt(vaultPath); + var data = JsonSerializer.Deserialize>(content); + Entries = new ObservableCollection(data?? new List()); + } + else + { + Entries = new ObservableCollection() + { + + }; + } + + } + + public void AddNewVaultEntry() + { + Entries.Add(new VaultEntry()); + } + + public void SaveVault() + { + var date = Entries.ToList(); + var vaultPath = AppSettingsService.GetSettingsPathFor("Vault.dat"); + File.WriteAllText(vaultPath, JsonSerializer.Serialize(date), Encoding.UTF8); + File.Encrypt(vaultPath); + } + } + + public class VaultEntry + { + public string Name { get; set; } + public string Secret { get; set; } + } +} diff --git a/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml b/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml index bbbb769..8b305c4 100644 --- a/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml +++ b/src/ScriptRunner/ScriptRunner.GUI/Views/MainWindow.axaml @@ -48,7 +48,8 @@ + + + + + + diff --git a/src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml.cs b/src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml.cs new file mode 100644 index 0000000..25d88b8 --- /dev/null +++ b/src/ScriptRunner/ScriptRunner.GUI/Views/Vault.axaml.cs @@ -0,0 +1,33 @@ +using Avalonia; +using Avalonia.Controls; +using Avalonia.Interactivity; +using Avalonia.Markup.Xaml; +using ScriptRunner.GUI.ViewModels; + +namespace ScriptRunner.GUI.Views +{ + public partial class Vault : Window + { + public Vault() + { + InitializeComponent(); + DataContext = this.ViewModel = new VaultViewModel(); +#if DEBUG + this.AttachDevTools(); +#endif + } + + public VaultViewModel ViewModel { get; set; } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + private void CloseVaultDialog(object? sender, RoutedEventArgs e) + { + ViewModel.SaveVault(); + Close(); + } + } +}