mirror of
https://github.com/RedWizardsLab/EchoHub.git
synced 2026-09-05 15:46:01 +02:00
feat: enhance chat functionality with auto-join, persistent read positions, and theme border customization
This commit is contained in:
@@ -9,6 +9,10 @@ public static class ConfigManager
|
||||
|
||||
private static readonly string ConfigPath = Path.Combine(ConfigDir, "config.json");
|
||||
|
||||
// Load-mutate-save cycles run from both the UI thread and background tasks
|
||||
// (token refresh, room keys, last-read checkpoints) — serialize file access.
|
||||
private static readonly Lock FileLock = new();
|
||||
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
WriteIndented = true,
|
||||
@@ -17,54 +21,66 @@ public static class ConfigManager
|
||||
|
||||
public static ClientConfig Load()
|
||||
{
|
||||
try
|
||||
lock (FileLock)
|
||||
{
|
||||
if (!File.Exists(ConfigPath))
|
||||
return new ClientConfig();
|
||||
try
|
||||
{
|
||||
if (!File.Exists(ConfigPath))
|
||||
return new ClientConfig();
|
||||
|
||||
var json = File.ReadAllText(ConfigPath);
|
||||
return JsonSerializer.Deserialize<ClientConfig>(json, JsonOptions) ?? new ClientConfig();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new ClientConfig();
|
||||
var json = File.ReadAllText(ConfigPath);
|
||||
return JsonSerializer.Deserialize<ClientConfig>(json, JsonOptions) ?? new ClientConfig();
|
||||
}
|
||||
catch
|
||||
{
|
||||
return new ClientConfig();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void Save(ClientConfig config)
|
||||
{
|
||||
try
|
||||
lock (FileLock)
|
||||
{
|
||||
Directory.CreateDirectory(ConfigDir);
|
||||
var json = JsonSerializer.Serialize(config, JsonOptions);
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Silently fail — config save is best-effort
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(ConfigDir);
|
||||
var json = JsonSerializer.Serialize(config, JsonOptions);
|
||||
File.WriteAllText(ConfigPath, json);
|
||||
}
|
||||
catch
|
||||
{
|
||||
// Silently fail — config save is best-effort
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void SaveServer(SavedServer server)
|
||||
{
|
||||
var config = Load();
|
||||
var existing = config.SavedServers.FindIndex(s =>
|
||||
string.Equals(s.Url, server.Url, StringComparison.OrdinalIgnoreCase));
|
||||
lock (FileLock)
|
||||
{
|
||||
var config = Load();
|
||||
var existing = config.SavedServers.FindIndex(s =>
|
||||
string.Equals(s.Url, server.Url, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
if (existing >= 0)
|
||||
config.SavedServers[existing] = server;
|
||||
else
|
||||
config.SavedServers.Add(server);
|
||||
if (existing >= 0)
|
||||
config.SavedServers[existing] = server;
|
||||
else
|
||||
config.SavedServers.Add(server);
|
||||
|
||||
Save(config);
|
||||
Save(config);
|
||||
}
|
||||
}
|
||||
|
||||
public static void RemoveServer(string url)
|
||||
{
|
||||
var config = Load();
|
||||
config.SavedServers.RemoveAll(s =>
|
||||
string.Equals(s.Url, url, StringComparison.OrdinalIgnoreCase));
|
||||
lock (FileLock)
|
||||
{
|
||||
var config = Load();
|
||||
config.SavedServers.RemoveAll(s =>
|
||||
string.Equals(s.Url, url, StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
Save(config);
|
||||
Save(config);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user