mirror of
https://github.com/hmaxnl/SharpRSS.git
synced 2024-11-10 07:54:20 +01:00
81 lines
3.5 KiB
Plaintext
81 lines
3.5 KiB
Plaintext
@using SharpRss.Models;
|
|
@using MudBlazor.Utilities
|
|
@using Serilog
|
|
@using SharpRss.Services
|
|
@using WebSharpRSS.Models;
|
|
@using ToolQit.Extensions;
|
|
|
|
@inject SyndicationService _syndicationService
|
|
@inject NavigationManager _navigation
|
|
|
|
<MudStack Spacing="2">
|
|
<MudTreeView Color="Color.Success" Items="_guideItems" @bind-SelectedValue="SelectedItem" Hover="true">
|
|
<ItemTemplate>
|
|
<MudTreeViewItem @bind-Expanded="@context.IsExpanded" Value="@context" Items="@context.Children" CanExpand="@context.HasChildren" @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.HasChildren" LoadingIconColor="Color.Info" />
|
|
@if (context.FaviconUrl == null && context.Icon != null)
|
|
{
|
|
<MudIcon Icon="@context.Icon" Style="@($"color:{context.CategoryModel?.HexColor ?? _theme.Palette.Primary.Value}")"/>
|
|
}
|
|
else
|
|
{
|
|
<MudImage Src="@context.FaviconUrl" ObjectFit="ObjectFit.ScaleDown"/>
|
|
}
|
|
<MudText Class="ml-2">@context.Title</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($"/list?fid={_selectedItem.FeedModel.EncodedUrl}");
|
|
}
|
|
else if (_selectedItem.CategoryModel != null)
|
|
{
|
|
_navigation.NavigateTo($"/list?cid={_selectedItem.CategoryModel.Id}");
|
|
}
|
|
}
|
|
private async void ExpandedChanged(TreeItemData treeItemData)
|
|
{
|
|
treeItemData.Loading = true;
|
|
var groupedItems = await _syndicationService.GetFeedsAsync(treeItemData.CategoryModel?.Id);
|
|
treeItemData.Children = ModelToTreeItem(groupedItems);
|
|
treeItemData.IsExpanded = !treeItemData.IsExpanded;
|
|
treeItemData.Loading = false;
|
|
StateHasChanged();
|
|
}
|
|
protected override async void OnInitialized()
|
|
{
|
|
Log.Verbose("Loading guide data...");
|
|
_guideItems.Add(new TreeItemData(new CategoryModel() { Name = "All", Icon = Icons.Material.Filled.Home, HexColor = Colors.Blue.Accent1, Id = string.Empty }));
|
|
HashSet<object> items = await _syndicationService.GetCategoriesAndFeedsAsync();
|
|
_guideItems.UnionWith(ModelToTreeItem(items));
|
|
|
|
StateHasChanged();
|
|
Log.Verbose("Guide initialized!");
|
|
}
|
|
private HashSet<TreeItemData> ModelToTreeItem<T>(HashSet<T> model) => model.Select(x => x is CategoryModel model ? new TreeItemData(model) : x is FeedModel feedModel ? new TreeItemData(feedModel) : throw new ArgumentException("Item arg is invalid!")).ToHashSet();
|
|
} |