diff --git a/QtCNETAPI/Services/ApiService/ApiService.cs b/QtCNETAPI/Services/ApiService/ApiService.cs index 12b9f4e..a40f871 100644 --- a/QtCNETAPI/Services/ApiService/ApiService.cs +++ b/QtCNETAPI/Services/ApiService/ApiService.cs @@ -210,7 +210,7 @@ namespace QtCNETAPI.Services.ApiService { var restRequest = new RestRequest($"users/profile-pic/{userId}") .AddHeader("Authorization", $"Bearer {SessionToken}"); - var response = await _client.GetAsync(restRequest); + var response = await _client.GetAsync(restRequest).ConfigureAwait(false); if (response != null) { diff --git a/qtcnet-client/Factories/ImageFactory.cs b/qtcnet-client/Factories/ImageFactory.cs index 22b29c8..8bae9d5 100644 --- a/qtcnet-client/Factories/ImageFactory.cs +++ b/qtcnet-client/Factories/ImageFactory.cs @@ -1,17 +1,15 @@ -using QtCNETAPI.Models; +using qtcnet_client.Properties; +using QtCNETAPI.Models; +using QtCNETAPI.Schema; using QtCNETAPI.Services.ApiService; -using System; -using System.Collections.Generic; -using System.Text; using System.Drawing.Drawing2D; -using Microsoft.Extensions.FileProviders; -using qtcnet_client.Properties; namespace qtcnet_client.Factories { public class ImageFactory { private readonly IApiService _apiService; + private readonly HttpClient _http = new(); public ImageFactory(IApiService apiService) { _apiService = apiService; @@ -19,76 +17,66 @@ namespace qtcnet_client.Factories public async Task GetAndCreateProfileImage(string userId, int status = -1, int cosmeticStoreId = 0) { - Bitmap combined = new(139, 138); + var pfpTask = _apiService.GetUserProfilePic(userId); + var cosmeticTask = cosmeticStoreId >= 0 ? _apiService.GetStoreItem(cosmeticStoreId) : Task.FromResult?>(null)!; - Bitmap img1 = Resources.DefaultPfp; - Bitmap? img2 = null; - Bitmap img3 = Resources.OfflineIcon; - - var _pfpRes = await _apiService.GetUserProfilePic(userId); - if (_pfpRes.Success && _pfpRes.Data != null) + return await Task.Run(async () => { - using var ms = new MemoryStream(_pfpRes.Data); - img1 = (Bitmap)Image.FromStream(ms); - } + Bitmap img1 = Resources.DefaultPfp; + Bitmap? img2 = null; + Bitmap img3 = Resources.OfflineIcon; - // get cosmetic image - if (cosmeticStoreId >= 0) - { - var _storeRes = await _apiService.GetStoreItem(cosmeticStoreId); - if (_storeRes.Success && _storeRes.Data != null) + var pfpRes = await pfpTask; + if (pfpRes.Success && pfpRes.Data != null) { - using var _http = new HttpClient(); - using var _httpRes = await _http.GetAsync(_storeRes.Data.AssetUrl); - if (_httpRes.IsSuccessStatusCode) + using var ms = new MemoryStream(pfpRes.Data); + img1 = (Bitmap)Image.FromStream(ms); + } + + if (cosmeticStoreId > 0) + { + var storeRes = await cosmeticTask; + if (storeRes?.Success == true) { - var _imgByes = _httpRes.Content.ReadAsByteArrayAsync().GetAwaiter().GetResult(); - using var ms = new MemoryStream(_imgByes); - img2 = (Bitmap?)Image.FromStream(ms); + using var httpRes = await _http.GetAsync(storeRes.Data?.AssetUrl); + if (httpRes.IsSuccessStatusCode) + { + var bytes = await httpRes.Content.ReadAsByteArrayAsync(); + using var ms = new MemoryStream(bytes); + img2 = (Bitmap)Image.FromStream(ms); + } } } - } - // set status image - if (!int.IsNegative(status)) - { - switch (status) + Bitmap combined = new(139, 138); + using var g = Graphics.FromImage(combined); + + g.Clear(Color.Transparent); + g.CompositingMode = CompositingMode.SourceOver; + g.DrawImage(img1, 4, 6, 128, 128); + + if (img2 != null) { - case 0: - img3 = Resources.OfflineIcon; - break; - case 1: - img3 = Resources.OnlineIcon; - break; - case 2: - img3 = Resources.AwayIcon; - break; - case 3: - img3 = Resources.DNDIcon; - break; + img2.MakeTransparent(); + g.DrawImage(img2, 0, 0, 139, 138); } - } - // finally, combine the images - Graphics g = Graphics.FromImage(combined); - g.Clear(Color.Transparent); - g.CompositingMode = CompositingMode.SourceOver; + if (!int.IsNegative(status)) + { + img3 = status switch + { + 1 => Resources.OnlineIcon, + 2 => Resources.AwayIcon, + 3 => Resources.DNDIcon, + _ => Resources.OfflineIcon + }; - g.DrawImage(img1, 4, 6, 128, 128); + img3.MakeTransparent(); + g.DrawImage(img3, 104, 0, 35, 35); + } - if (img2 != null) - { - img2.MakeTransparent(); - g.DrawImage(img2, 0, 0, 139, 138); - } - - if (!int.IsNegative(status)) - { - img3.MakeTransparent(); - g.DrawImage(img3, 104, 0, 35, 35); - } - - return combined; + return combined; + }); } public async Task GetStoreItemThumb(int id)