SharpRSS/WebSharpRSS/Shared/SideGuide.razor

81 lines
3.5 KiB
Plaintext
Raw Normal View History

2023-05-12 15:48:14 +02:00
@using SharpRss.Models;
2023-04-28 21:58:36 +02:00
@using MudBlazor.Utilities
2023-04-29 19:40:16 +02:00
@using Serilog
2023-05-18 01:27:11 +02:00
@using SharpRss.Services
@using WebSharpRSS.Models;
@using ToolQit.Extensions;
2023-06-15 19:26:54 +02:00
@inject SyndicationService _syndicationService
2023-05-26 21:54:12 +02:00
@inject NavigationManager _navigation
2023-05-07 02:42:37 +02:00
2023-05-23 15:04:02 +02:00
<MudStack Spacing="2">
2023-05-26 21:54:12 +02:00
<MudTreeView Color="Color.Success" Items="_guideItems" @bind-SelectedValue="SelectedItem" Hover="true">
2023-05-22 15:55:21 +02:00
<ItemTemplate>
2023-06-10 20:27:26 +02:00
<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 class="d-flex align-center">
<MudTreeViewItemToggleButton ExpandedChanged="@(() => ExpandedChanged(context))" Loading="@context.Loading" Visible="@context.HasChildren" LoadingIconColor="Color.Info"/>
2023-05-23 15:04:02 +02:00
@if (context.FaviconUrl == null && context.Icon != null)
{
2023-06-10 20:27:26 +02:00
<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>
<MudText Class="d-inline-flex align-self-end" Color="Color.Secondary" Typo="Typo.overline">@context.TotalItems</MudText>
</div>
</Content>
</MudTreeViewItem>
2023-05-22 15:55:21 +02:00
</ItemTemplate>
</MudTreeView>
2023-05-23 15:04:02 +02:00
</MudStack>
2023-04-28 21:58:36 +02:00
@code {
2023-05-26 21:54:12 +02:00
private MudTheme _theme = new MudTheme();
2023-05-22 15:55:21 +02:00
private readonly HashSet<TreeItemData> _guideItems = new HashSet<TreeItemData>();
2023-05-23 15:04:02 +02:00
private TreeItemData? _selectedItem;
2023-05-17 16:03:04 +02:00
private TreeItemData? SelectedItem
{
get => _selectedItem;
2023-05-17 16:03:04 +02:00
set
{
_selectedItem = value;
2023-05-17 16:03:04 +02:00
ItemClicked();
}
2023-05-23 15:04:02 +02:00
}
2023-05-17 16:03:04 +02:00
private void ItemClicked()
{
2023-05-25 15:48:56 +02:00
if (_selectedItem == null) return;
2023-06-16 22:53:26 +02:00
if (_selectedItem.SyndicationModel != null)
2023-05-25 15:48:56 +02:00
{
2023-06-16 22:53:26 +02:00
_navigation.NavigateTo($"/list?fid={_selectedItem.SyndicationModel.EncodedUrl}");
2023-05-25 15:48:56 +02:00
}
2023-06-10 20:27:26 +02:00
else if (_selectedItem.CategoryModel != null)
2023-05-25 15:48:56 +02:00
{
2023-06-10 20:27:26 +02:00
_navigation.NavigateTo($"/list?cid={_selectedItem.CategoryModel.Id}");
2023-05-25 15:48:56 +02:00
}
2023-05-17 16:03:04 +02:00
}
2023-05-24 12:40:46 +02:00
private async void ExpandedChanged(TreeItemData treeItemData)
{
treeItemData.Loading = true;
2023-06-24 10:15:51 +02:00
if (treeItemData.CategoryModel != null)
treeItemData.Children = ModelToTreeItem(treeItemData.CategoryModel.Syndications);
2023-05-24 12:40:46 +02:00
treeItemData.IsExpanded = !treeItemData.IsExpanded;
treeItemData.Loading = false;
StateHasChanged();
}
protected override async void OnInitialized()
2023-04-28 21:58:36 +02:00
{
2023-05-15 20:42:42 +02:00
Log.Verbose("Loading guide data...");
2023-06-16 22:53:26 +02:00
HashSet<object> items = await _syndicationService.GetCategoriesAndSyndicationsAsync();
2023-05-23 15:04:02 +02:00
_guideItems.UnionWith(ModelToTreeItem(items));
2023-05-16 15:37:49 +02:00
2023-05-17 16:03:04 +02:00
StateHasChanged();
Log.Verbose("Guide initialized!");
2023-04-28 21:58:36 +02:00
}
2023-06-16 22:53:26 +02:00
private HashSet<TreeItemData> ModelToTreeItem<T>(HashSet<T> model) => model.Select(x => x is CategoryModel model ? new TreeItemData(model) : x is SyndicationModel feedModel ? new TreeItemData(feedModel) : throw new ArgumentException("Item arg is invalid!")).ToHashSet();
2023-04-28 21:58:36 +02:00
}