SharpRSS/WebSharpRSS/Shared/SideGuide.razor

84 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 CodeHollow.FeedReader
@using Serilog
2023-05-18 01:27:11 +02:00
@using SharpRss.Services
@using WebSharpRSS.Models;
@using ToolQit.Extensions;
2023-05-07 02:42:37 +02:00
@inject RssService _rssService
2023-05-25 15:48:56 +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-22 15:55:21 +02:00
<MudTreeView Items="_guideItems" @bind-SelectedValue="SelectedItem" Hover="true">
<ItemTemplate>
2023-05-23 15:04:02 +02:00
<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">
2023-05-24 12:40:46 +02:00
<MudTreeViewItemToggleButton ExpandedChanged="@(() => ExpandedChanged(context))" Loading="@context.Loading" Visible="@context.HasChild" LoadingIconColor="Color.Info" />
2023-05-23 15:04:02 +02:00
@if (context.FaviconUrl == null && context.Icon != null)
{
2023-05-23 15:04:02 +02:00
<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;">
2023-05-23 15:04:02 +02:00
@*<MudText Color="Color.Dark" Style="justify-self: end;" Typo="Typo.caption">@context.FeeditemCount</MudText>*@
</div>
</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 {
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;
if (_selectedItem.FeedModel != null)
{
Navigation.NavigateTo($"/read?fid={_selectedItem.FeedModel.Id}");
}
else if (_selectedItem.GroupModel != null)
{
Navigation.NavigateTo($"/read?gid={_selectedItem.GroupModel.Id}");
}
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-05-25 15:48:56 +02:00
var groupedItems = await _rssService.GetFeedsAsync(treeItemData.GroupModel?.Id);
2023-05-24 12:40:46 +02:00
treeItemData.Children = ModelToTreeItem(groupedItems);
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-05-24 19:27:22 +02:00
HashSet<object> items = await _rssService.GetGroupsFeedsAsync();
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-05-23 15:04:02 +02:00
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();
2023-04-28 21:58:36 +02:00
}