SharpRSS/WebSharpRSS/Shared/SideGuide.razor
2023-05-25 15:48:56 +02:00

84 lines
3.5 KiB
Plaintext

@using SharpRss.Models;
@using MudBlazor.Utilities
@using CodeHollow.FeedReader
@using Serilog
@using SharpRss.Services
@using WebSharpRSS.Models;
@using ToolQit.Extensions;
@inject RssService _rssService
@inject NavigationManager Navigation
<MudStack Spacing="2">
<MudTreeView Items="_guideItems" @bind-SelectedValue="SelectedItem" Hover="true">
<ItemTemplate>
<MudTreeViewItem @bind-Expanded="@context.IsExpanded" Value="@context" Items="@context.Children" CanExpand="@context.HasChild" @onclick="ItemClicked">
<Content>
<div style="display: grid; grid-template-columns: 1fr auto; align-items: center; width: 100%">
<div style="justify-self: start;" class="d-flex align-center">
<MudTreeViewItemToggleButton ExpandedChanged="@(() => ExpandedChanged(context))" Loading="@context.Loading" Visible="@context.HasChild" LoadingIconColor="Color.Info" />
@if (context.FaviconUrl == null && context.Icon != null)
{
<MudIcon Icon="@context.Icon" Style="@($"color:{context.GroupModel?.HexColor ?? Theme.Palette.Primary.Value}")"/>
}
else
{
<MudImage Src="@context.FaviconUrl" ObjectFit="ObjectFit.ScaleDown"/>
}
<MudText Class="ml-2">@context.Title</MudText>
</div>
<div style="justify-self: end;">
@*<MudText Color="Color.Dark" Style="justify-self: end;" Typo="Typo.caption">@context.FeeditemCount</MudText>*@
</div>
</div>
</Content>
</MudTreeViewItem>
</ItemTemplate>
</MudTreeView>
</MudStack>
@code {
private MudTheme Theme = new MudTheme();
private readonly HashSet<TreeItemData> _guideItems = new HashSet<TreeItemData>();
private TreeItemData? _selectedItem;
private TreeItemData? SelectedItem
{
get => _selectedItem;
set
{
_selectedItem = value;
ItemClicked();
}
}
private void ItemClicked()
{
if (_selectedItem == null) return;
if (_selectedItem.FeedModel != null)
{
Navigation.NavigateTo($"/read?fid={_selectedItem.FeedModel.Id}");
}
else if (_selectedItem.GroupModel != null)
{
Navigation.NavigateTo($"/read?gid={_selectedItem.GroupModel.Id}");
}
}
private async void ExpandedChanged(TreeItemData treeItemData)
{
treeItemData.Loading = true;
var groupedItems = await _rssService.GetFeedsAsync(treeItemData.GroupModel?.Id);
treeItemData.Children = ModelToTreeItem(groupedItems);
treeItemData.IsExpanded = !treeItemData.IsExpanded;
treeItemData.Loading = false;
StateHasChanged();
}
protected override async void OnInitialized()
{
Log.Verbose("Loading guide data...");
HashSet<object> items = await _rssService.GetGroupsFeedsAsync();
_guideItems.UnionWith(ModelToTreeItem(items));
StateHasChanged();
Log.Verbose("Guide initialized!");
}
private HashSet<TreeItemData> ModelToTreeItem<T>(HashSet<T> model) => model.Select(x => x is GroupModel model ? new TreeItemData(model) : x is FeedModel feedModel ? new TreeItemData(feedModel) : throw new ArgumentException("Item arg is invalid!")).ToHashSet();
}