99 lines
2.7 KiB
C#
99 lines
2.7 KiB
C#
using Microsoft.AspNetCore.Components;
|
|
using Blog3000.Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Blog3000.Client.Shared
|
|
{
|
|
public partial class TopTopics: IDisposable
|
|
{
|
|
|
|
[Parameter]
|
|
public EventCallback<string> OnTopicClick { get; set; }
|
|
|
|
|
|
[Inject]
|
|
private BlogDb BlogDb { get; set; }
|
|
|
|
[Inject]
|
|
private Config Config { get; set; }
|
|
|
|
public class Topic {
|
|
public string DispId;
|
|
public List<BlogPostHeader> Posts = new List<BlogPostHeader>();
|
|
}
|
|
|
|
|
|
private List<Topic> topics = new List<Topic>();
|
|
|
|
public void Dispose()
|
|
{
|
|
BlogDb.BlogPostsChanged -= BlogDb_BlogPostsChanged;
|
|
Config.ConfigChanged -= Config_ConfigChanged;
|
|
}
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
topics = await UpdateTopicsAsync();
|
|
BlogDb.BlogPostsChanged += BlogDb_BlogPostsChanged;
|
|
Config.ConfigChanged += Config_ConfigChanged; ;
|
|
}
|
|
|
|
|
|
private async void BlogDb_BlogPostsChanged(object sender, EventArgs e)
|
|
{
|
|
await RefreshAsync();
|
|
}
|
|
|
|
|
|
private async void Config_ConfigChanged(object sender, EventArgs e)
|
|
{
|
|
await RefreshAsync();
|
|
}
|
|
|
|
|
|
private async Task RefreshAsync()
|
|
{
|
|
topics = new List<Topic>(); // Clear to force rebuild
|
|
StateHasChanged();
|
|
topics = await UpdateTopicsAsync();
|
|
StateHasChanged();
|
|
}
|
|
|
|
|
|
private async Task<List<Topic>> UpdateTopicsAsync()
|
|
{
|
|
var dict = new Dictionary<string, Topic>();
|
|
|
|
foreach (var bp in await BlogDb.GetHeadersAsync().ToListAsync()) /*FromCachedPosts*/
|
|
{
|
|
if (bp != null && bp.Topics != null)
|
|
{
|
|
foreach (var t in bp.Topics)
|
|
{
|
|
foreach (var tt in Config?.Main?.TopTopics ?? new string[] { })
|
|
{
|
|
if (String.Equals(tt, t))
|
|
{
|
|
Topic tp;
|
|
if (!dict.TryGetValue(t, out tp))
|
|
{
|
|
tp = new Topic();
|
|
tp.DispId = t;
|
|
dict.Add(tp.DispId, tp);
|
|
}
|
|
tp.Posts.Add(bp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new List<Topic>(dict.Values);
|
|
}
|
|
|
|
}
|
|
}
|