using System.Collections.ObjectModel; using System.Net.Http.Json; using System.Text.Json.Serialization; using Throw; using WebUI.Common.Models; namespace WebUI.Services; public class DataService : IDataService { private Dictionary avatars = new(); private Dictionary navigators = new(); private Dictionary titles = new(); private readonly HttpClient client; public DataService(HttpClient client) { this.client = client; } public async Task InitializeAsync() { var avatarList = await client.GetFromJsonAsync("data/Avatars.json", SourceGenerationContext.Default.ListAvatar); avatarList.ThrowIfNull(); avatars = avatarList.ToDictionary(avatar => avatar.AvatarId); var navigatorList = await client.GetFromJsonAsync("data/Navigators.json", SourceGenerationContext.Default.ListNavigator); navigatorList.ThrowIfNull(); navigators = navigatorList.ToDictionary(navigator => navigator.Id); var titleList = await client.GetFromJsonAsync("data/Titles.json", SourceGenerationContext.Default.ListTitle); titleList.ThrowIfNull(); titles = titleList.ToDictionary(title => title.Id); } public IReadOnlyDictionary GetAvatars() { return new ReadOnlyDictionary(avatars); } public IReadOnlyDictionary GetNavigators() { return new ReadOnlyDictionary(navigators); } public IReadOnlyDictionary GetTitles() { return new ReadOnlyDictionary(titles); } } [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] internal partial class SourceGenerationContext : JsonSerializerContext { }