-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathTemplateFetcher.cs
44 lines (38 loc) · 1.36 KB
/
TemplateFetcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ReadyPlayerMe.AvatarCreator
{
public class TemplateFetcher
{
private readonly CancellationToken ctx;
private readonly AvatarApi avatarApi;
private readonly List<TemplateData> templates;
public TemplateFetcher(CancellationToken ctx = default)
{
this.ctx = ctx;
avatarApi = new AvatarApi(ctx);
templates = new List<TemplateData>();
}
public async Task<List<TemplateData>> GetTemplates()
{
var avatarTemplates = await avatarApi.GetTemplates();
await GetAllTemplateRenders(avatarTemplates);
return templates;
}
private async Task GetAllTemplateRenders(IEnumerable<TemplateData> templateAvatars)
{
var downloadRenderTasks = templateAvatars.Select(GetAvatarRender).ToList();
while (!downloadRenderTasks.All(x => x.IsCompleted) && !ctx.IsCancellationRequested)
{
await Task.Yield();
}
}
private async Task GetAvatarRender(TemplateData templateData)
{
templateData.Texture = await ImageApi.DownloadImageAsync(templateData.ImageUrl, ctx: ctx);
templates.Add(templateData);
}
}
}