diff --git a/EscribePassword.csproj b/EscribePassword.csproj index b81735f..9a73dee 100644 --- a/EscribePassword.csproj +++ b/EscribePassword.csproj @@ -17,4 +17,10 @@ + + + ..\Libraries\libObsidean\bin\Debug\net8.0-windows\libObsidean.dll + + + diff --git a/EstadoPersistente.cs b/EstadoPersistente.cs new file mode 100644 index 0000000..baa3b11 --- /dev/null +++ b/EstadoPersistente.cs @@ -0,0 +1,107 @@ +using System.Text.Json; +using System.IO; +using libObsidean; + +namespace EscribePassword +{ + internal class EstadoPersistente + { + // Ruta donde se guardará el estado + private static readonly string _filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "estado.json"); + + // Instancia privada estática, parte del patrón Singleton + private static EstadoPersistente _instance; + + private List passwords; + + public List Passwords + { + get + { + if (passwords == null) passwords = new List(); + return passwords; + } + set + { + passwords = value; + } + } + + // Constructor público sin parámetros requerido para la deserialización + public EstadoPersistente() + { + passwords = new List(); + } + + // Propiedad pública estática para acceder a la instancia + public static EstadoPersistente Instance + { + get + { + if (_instance == null) + { + _instance = CargarEstado(TiposEstadosPersistentes.Obsidean); + } + return _instance; + } + } + + // Método para guardar el estado en un archivo JSON + public void GuardarEstado(TiposEstadosPersistentes tipo) + { + if (tipo == TiposEstadosPersistentes.Json) + { + var options = new JsonSerializerOptions + { + WriteIndented = true // para una salida JSON formateada legiblemente + }; + string json = JsonSerializer.Serialize(this, options); + File.WriteAllText(_filePath, json); + } + else if (tipo == TiposEstadosPersistentes.Obsidean) + { + Obsidean obs = new Obsidean(); + var tabla = EscribePassword.Passwords.ConvertPasswordsListToArray(Passwords); + obs.EscribirPasswords(tabla); + } + } + // Método estático para cargar el estado desde un archivo JSON + private static EstadoPersistente CargarEstado(TiposEstadosPersistentes tipo) + { + try + { + if (tipo == TiposEstadosPersistentes.Json) + { + if (File.Exists(_filePath)) + { + string json = File.ReadAllText(_filePath); + return JsonSerializer.Deserialize(json); + } + return new EstadoPersistente(); // Devuelve una nueva instancia si no existe el archivo + } + else if (tipo == TiposEstadosPersistentes.Obsidean) + { + Obsidean obs = new Obsidean(); + var tabla = obs.LeerPasswords(); + if (tabla != null) + { + var newEP = new EstadoPersistente(); + newEP.Passwords = EscribePassword.Passwords.ConvertArrayToPasswordsList(tabla); + return newEP; + } + } + return new EstadoPersistente(); + } + catch + { + return new EstadoPersistente(); // Devuelve una nueva instancia si hay un error durante la carga + } + } + } + + enum TiposEstadosPersistentes + { + Obsidean, + Json + } +} diff --git a/KeyboardHelper.cs b/KeyboardHelper.cs index 2f66879..ec2c4a0 100644 --- a/KeyboardHelper.cs +++ b/KeyboardHelper.cs @@ -18,6 +18,51 @@ namespace EscribePassword [ObservableProperty] private string categoria; + public static List ConvertArrayToPasswordsList(string[,] tableArray) + { + var passwordsList = new List(); + + for (int i = 0; i < tableArray.GetLength(0); i++) + { + var passwords = new Passwords(); + + if (tableArray.GetLength(1) >= 1) + passwords.Categoria = tableArray[i, 0]; + if (tableArray.GetLength(1) >= 2) + passwords.Usuario = tableArray[i, 1]; + if (tableArray.GetLength(1) >= 3) + passwords.Password = tableArray[i, 2]; + passwordsList.Add(passwords); + } + + return passwordsList; + } + + public static string[,] ConvertPasswordsListToArray(List passwordsList) + { + if (passwordsList == null || passwordsList.Count == 0) + return new string[0, 0]; + + int rows = passwordsList.Count; + int columns = 3; // Asumimos que siempre hay tres columnas: Usuario, Password, Categoria + + string[,] tableArray = new string[rows + 1, columns]; + + // Fill header + tableArray[0, 0] = "Class"; + tableArray[0, 1] = "User"; + tableArray[0, 2] = "Password"; + + // Fill data + for (int i = 0; i < rows; i++) + { + tableArray[i + 1, 0] = passwordsList[i].Usuario; + tableArray[i + 1, 1] = passwordsList[i].Password; + tableArray[i + 1, 2] = passwordsList[i].Categoria; + } + + return tableArray; + } } public class KeyboardHelper diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index e004ff1..b009289 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -142,7 +142,7 @@ namespace EscribePassword private void OnApplicationExit(object sender, ExitEventArgs e) { EstadoPersistente.Instance.Passwords = new List(passwords); - EstadoPersistente.Instance.GuardarEstado(); + EstadoPersistente.Instance.GuardarEstado(TiposEstadosPersistentes.Obsidean); } [RelayCommand] @@ -184,75 +184,4 @@ namespace EscribePassword } - internal class EstadoPersistente - { - // Ruta donde se guardará el estado - private static readonly string _filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "estado.json"); - - // Instancia privada estática, parte del patrón Singleton - private static EstadoPersistente _instance; - - private List passwords; - - public List Passwords - { - get - { - if (passwords == null) passwords = new List(); - return passwords; - } - set - { - passwords = value; - } - } - - // Constructor público sin parámetros requerido para la deserialización - public EstadoPersistente() - { - passwords = new List(); - } - - // Propiedad pública estática para acceder a la instancia - public static EstadoPersistente Instance - { - get - { - if (_instance == null) - { - _instance = CargarEstado(); - } - return _instance; - } - } - - // Método para guardar el estado en un archivo JSON - public void GuardarEstado() - { - var options = new JsonSerializerOptions - { - WriteIndented = true // para una salida JSON formateada legiblemente - }; - string json = JsonSerializer.Serialize(this, options); - File.WriteAllText(_filePath, json); - } - - // Método estático para cargar el estado desde un archivo JSON - private static EstadoPersistente CargarEstado() - { - try - { - if (File.Exists(_filePath)) - { - string json = File.ReadAllText(_filePath); - return JsonSerializer.Deserialize(json); - } - return new EstadoPersistente(); // Devuelve una nueva instancia si no existe el archivo - } - catch - { - return new EstadoPersistente(); // Devuelve una nueva instancia si hay un error durante la carga - } - } - } }