Skip to content

Commit 7f10646

Browse files
committed
Load mods incrementally in the UI
1 parent 0621bff commit 7f10646

File tree

4 files changed

+61
-61
lines changed

4 files changed

+61
-61
lines changed

ModBrowser/GithubApi/GithubModSearchEngine.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,16 @@ public GithubModSearchEngine(string userAgent)
1818
client = new(userAgent);
1919
}
2020

21-
public async Task<List<ModListing>> SearchModsAsync(string searchTerm = "")
21+
public async IAsyncEnumerable<ModListing> SearchModsAsync(string searchTerm = "")
2222
{
2323
var repos = await SearchForSporeModRepositoriesAsync(searchTerm);
2424

25-
var mods = new List<ModListing>();
26-
2725
foreach (var repo in repos)
2826
{
2927
var modListing = await GetModListingFromRepositoryAsync(repo);
3028
if (modListing is not null)
31-
mods.Add(modListing);
29+
yield return modListing;
3230
}
33-
34-
return mods;
3531
}
3632

3733
/// <summary>

ModBrowser/IModSearchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ interface IModSearchEngine
1212
/// </summary>
1313
/// <param name="searchTerm">The term to search for.</param>
1414
/// <returns>A list of Spore mods tthat match the given search term.</returns>
15-
public Task<List<ModListing>> SearchModsAsync(string searchTerm = "");
15+
public IAsyncEnumerable<ModListing> SearchModsAsync(string searchTerm = "");
1616
}
1717
}

SporeCommunity.ModBrowser.Blazor/Pages/Index.razor

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,76 @@
44
<MainNavButtons />
55

66
<form @onsubmit="@ExecuteSearch">
7-
<input @bind="searchTerm" placeholder="Search for mods">
8-
<button type="submit" disabled=@isLoading>Search</button>
7+
<input @bind="searchTerm" placeholder="Search for mods">
8+
<button type="submit" disabled=@isLoading>Search</button>
99
</form>
1010

11-
@if (isLoading)
11+
@if (errorMessage is not null)
1212
{
13-
<LoadingSpinner />
13+
<p class="accent-orange">There was an error searching for mods.</p>
14+
<p class="accent-orange">For help, visit the Discord above, and provide all of the information below.</p>
15+
<SupportInformation />
16+
<p>@errorMessage</p>
1417
}
15-
else
18+
else if (mods.Count == 0)
1619
{
17-
if (errorMessage is not null)
18-
{
19-
<p class="accent-orange">There was an error searching for mods.</p>
20-
<p class="accent-orange">For help, visit the Discord above, and provide all of the information below.</p>
21-
<SupportInformation />
22-
<p>@errorMessage</p>
20+
<p>No mods were found. Try using different search terms.</p>
2321
}
24-
else if (mods.Count == 0)
25-
{
26-
<p>No mods were found. Try using different search terms.</p>
27-
}
28-
else
29-
{
30-
<p>Showing @mods.Count mods</p>
31-
@foreach (var mod in mods)
32-
{
33-
<ModListingComponent Mod="@mod" />
22+
else
23+
{
24+
<p>Showing @mods.Count mods</p>
25+
@foreach (var mod in mods)
26+
{
27+
<ModListingComponent Mod="@mod" />
28+
}
3429
}
35-
}
30+
@if (isLoading)
31+
{
32+
<LoadingSpinner />
3633
}
3734

3835
@code {
39-
private bool isLoading = true;
36+
private bool isLoading = true;
37+
38+
private string? errorMessage;
4039

41-
private string? errorMessage;
40+
private GithubModSearchEngine github = new("Spore-Community/ModBrowser");
4241

43-
private GithubModSearchEngine github = new("Spore-Community/ModBrowser");
42+
private string searchTerm = "";
4443

45-
private string searchTerm = "";
44+
private List<ModListing> mods = new();
4645

47-
private List<ModListing> mods = new();
46+
protected override async Task OnInitializedAsync()
47+
{
48+
await ExecuteSearch();
49+
}
4850

49-
protected override async Task OnInitializedAsync()
50-
{
51-
await ExecuteSearch();
52-
}
51+
private async Task ExecuteSearch()
52+
{
53+
isLoading = true;
54+
mods.Clear();
55+
try
56+
{
57+
await foreach (var mod in github.SearchModsAsync(searchTerm))
58+
{
59+
mods.Add(mod);
5360

54-
private async Task ExecuteSearch()
55-
{
56-
isLoading = true;
57-
try
58-
{
59-
mods = await github.SearchModsAsync(searchTerm);
61+
// When listing all mods, sort by download count
62+
if (searchTerm.Length == 0)
63+
{
64+
mods = mods.OrderByDescending(mod => mod.DownloadCount).ToList();
65+
}
6066

61-
// When listing all mods, sort by download count
62-
if(searchTerm.Length == 0)
63-
{
64-
mods = mods.OrderByDescending(mod => mod.DownloadCount).ToList();
65-
}
66-
}
67-
catch (Exception e)
68-
{
69-
errorMessage = e.Message;
70-
Console.WriteLine("Spore Mod Browser encountered an error searching for mods. If requested, please provide the information below.");
71-
Console.WriteLine(e.ToString());
72-
}
73-
isLoading = false;
74-
}
67+
// Refresh UI
68+
StateHasChanged();
69+
}
70+
}
71+
catch (Exception e)
72+
{
73+
errorMessage = e.Message;
74+
Console.WriteLine("Spore Mod Browser encountered an error searching for mods. If requested, please provide the information below.");
75+
Console.WriteLine(e.ToString());
76+
}
77+
isLoading = false;
78+
}
7579
}

SporeCommunity.ModBrowser.Console/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
var github = new GithubModSearchEngine("Spore-Community/ModBrowser");
55

6-
var mods = await github.SearchModsAsync();
6+
var mods = github.SearchModsAsync();
77

8-
Console.WriteLine($"Found {mods.Count} mods.");
8+
//Console.WriteLine($"Found {mods.Count} mods.");
99

10-
foreach (var mod in mods)
10+
await foreach (var mod in mods)
1111
{
1212
Console.WriteLine(mod.DisplayName+" by "+mod.Author);
1313
}

0 commit comments

Comments
 (0)