initial commit
This commit is contained in:
38
Lab505.Client/Services/LangService.cs
Normal file
38
Lab505.Client/Services/LangService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace Lab505.Client.Services;
|
||||
|
||||
public class LangService
|
||||
{
|
||||
public string Current { get; private set; } = "vi";
|
||||
|
||||
public event Action? LangChanged;
|
||||
|
||||
public string T(string vi, string en) => Current == "en" ? en : vi;
|
||||
|
||||
public void Set(string lang)
|
||||
{
|
||||
if (Current == lang) return;
|
||||
Current = lang;
|
||||
LangChanged?.Invoke();
|
||||
}
|
||||
|
||||
public async Task LoadAsync(HttpClient http)
|
||||
{
|
||||
try
|
||||
{
|
||||
var prefs = await http.GetFromJsonAsync<PrefsDto>("/api/prefs");
|
||||
if (prefs?.Lang is { } l) Set(l);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public async Task ToggleAsync(HttpClient http)
|
||||
{
|
||||
Set(Current == "vi" ? "en" : "vi");
|
||||
try { await http.PutAsJsonAsync("/api/prefs", new { Lang = (string?)Current, NotifMuted = (bool?)null }); }
|
||||
catch { }
|
||||
}
|
||||
|
||||
private record PrefsDto(string? Lang);
|
||||
}
|
||||
Reference in New Issue
Block a user