refactor: modernize code, improve performance, and clean up various components.
This commit is contained in:
@@ -1,8 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Threading.Tasks;
|
||||
using Toak.Serialization;
|
||||
using Toak.Core.Interfaces;
|
||||
|
||||
@@ -10,20 +6,16 @@ namespace Toak.Core;
|
||||
|
||||
public class HistoryManager : IHistoryManager
|
||||
{
|
||||
private readonly string HistoryDir = Constants.Paths.AppDataDir;
|
||||
private readonly string HistoryFile = Constants.Paths.HistoryFile;
|
||||
|
||||
public HistoryManager()
|
||||
{
|
||||
}
|
||||
private readonly string _historyDir = Constants.Paths.AppDataDir;
|
||||
private readonly string _historyFile = Constants.Paths.HistoryFile;
|
||||
|
||||
public void SaveEntry(string rawTranscript, string refinedText, string? skillName, long durationMs)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(HistoryDir))
|
||||
if (!Directory.Exists(_historyDir))
|
||||
{
|
||||
Directory.CreateDirectory(HistoryDir);
|
||||
Directory.CreateDirectory(_historyDir);
|
||||
}
|
||||
|
||||
var entry = new HistoryEntry
|
||||
@@ -38,9 +30,9 @@ public class HistoryManager : IHistoryManager
|
||||
var json = JsonSerializer.Serialize(entry, CompactJsonSerializerContext.Default.HistoryEntry);
|
||||
|
||||
// Thread-safe append
|
||||
lock (HistoryFile)
|
||||
lock (_historyFile)
|
||||
{
|
||||
File.AppendAllLines(HistoryFile, new[] { json });
|
||||
File.AppendAllLines(_historyFile, [json]);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -52,14 +44,14 @@ public class HistoryManager : IHistoryManager
|
||||
public List<HistoryEntry> LoadHistory()
|
||||
{
|
||||
var entries = new List<HistoryEntry>();
|
||||
if (!File.Exists(HistoryFile)) return entries;
|
||||
if (!File.Exists(_historyFile)) return entries;
|
||||
|
||||
try
|
||||
{
|
||||
string[] lines;
|
||||
lock (HistoryFile)
|
||||
lock (_historyFile)
|
||||
{
|
||||
lines = File.ReadAllLines(HistoryFile);
|
||||
lines = File.ReadAllLines(_historyFile);
|
||||
}
|
||||
|
||||
foreach (var line in lines)
|
||||
@@ -92,20 +84,20 @@ public class HistoryManager : IHistoryManager
|
||||
|
||||
public void ClearHistory()
|
||||
{
|
||||
if (File.Exists(HistoryFile))
|
||||
if (File.Exists(_historyFile))
|
||||
{
|
||||
try
|
||||
{
|
||||
lock (HistoryFile)
|
||||
lock (_historyFile)
|
||||
{
|
||||
// Securely delete
|
||||
var len = new FileInfo(HistoryFile).Length;
|
||||
using (var fs = new FileStream(HistoryFile, FileMode.Open, FileAccess.Write))
|
||||
var len = new FileInfo(_historyFile).Length;
|
||||
using (var fs = new FileStream(_historyFile, FileMode.Open, FileAccess.Write))
|
||||
{
|
||||
var blank = new byte[len];
|
||||
fs.Write(blank, 0, blank.Length);
|
||||
}
|
||||
File.Delete(HistoryFile);
|
||||
File.Delete(_historyFile);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user