Falta mejorar Eliminar

This commit is contained in:
Miguel 2024-06-16 17:39:45 +02:00
parent 13f78d7723
commit 59b5300356
4 changed files with 159 additions and 72 deletions

View File

@ -17,4 +17,10 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Reference Include="libObsidean">
<HintPath>..\Libraries\libObsidean\bin\Debug\net8.0-windows\libObsidean.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

107
EstadoPersistente.cs Normal file
View File

@ -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> passwords;
public List<Passwords> Passwords
{
get
{
if (passwords == null) passwords = new List<Passwords>();
return passwords;
}
set
{
passwords = value;
}
}
// Constructor público sin parámetros requerido para la deserialización
public EstadoPersistente()
{
passwords = new List<Passwords>();
}
// 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<EstadoPersistente>(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
}
}

View File

@ -18,6 +18,51 @@ namespace EscribePassword
[ObservableProperty]
private string categoria;
public static List<Passwords> ConvertArrayToPasswordsList(string[,] tableArray)
{
var passwordsList = new List<Passwords>();
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<Passwords> 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

View File

@ -142,7 +142,7 @@ namespace EscribePassword
private void OnApplicationExit(object sender, ExitEventArgs e)
{
EstadoPersistente.Instance.Passwords = new List<Passwords>(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> passwords;
public List<Passwords> Passwords
{
get
{
if (passwords == null) passwords = new List<Passwords>();
return passwords;
}
set
{
passwords = value;
}
}
// Constructor público sin parámetros requerido para la deserialización
public EstadoPersistente()
{
passwords = new List<Passwords>();
}
// 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<EstadoPersistente>(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
}
}
}
}