using System; using System.Collections.Generic; using System.Linq; using CodeHollow.FeedReader; using MudBlazor; using Serilog; using SharpRss.Models; using SharpRss.Services; using ToolQit; namespace WebSharpRSS.Models { public class TreeItemData { public TreeItemData(GroupModel catModel, RssService rssService) { _service = rssService; CategoryModel = catModel; Initialize(); } public TreeItemData(FeedModel feedModel, RssService rssService) { _service = rssService; FeedModel = feedModel; Initialize(); } private readonly RssService _service; public readonly GroupModel? CategoryModel; public readonly FeedModel? FeedModel; private HashSet _feedModels; public string Title { get; set; } = string.Empty; public bool IsSelected { get; set; } public string? Icon { get; set; } public string? FaviconUrl { get; set; } // Category public bool HasChild => _feedModels != null && _feedModels.Any(); public bool IsExpanded { get; set; } public HashSet? Feeds { get; set; } // Feed public Feed? Feed { get; private set; } public int FeeditemCount { get { if (CategoryModel != null && Feeds != null) { int feedsCount = 0; foreach (var item in Feeds) { if (item.Feed != null) feedsCount += item.FeeditemCount; } return feedsCount; } else if (Feed != null) return Feed.Items.Count; return 0; } } private async void Initialize() { if (CategoryModel != null) { Title = CategoryModel.Name; Icon = Icons.Material.Filled.Category; /*_feedModels = await _service.GetFeedsAsync(CategoryModel); if (_feedModels.Any()) Feeds = _feedModels.Select(x => new TreeItemData(x, _service)).OrderBy(x => x.Title).ToHashSet();*/ } if (FeedModel != null) { try { //Feed = await _service.GetFeedAsync(FeedModel.FeedUrl); } catch (Exception e) { Log.Error(e, "Error fetching feed: {FeedUrl}", FeedModel.FeedUrl); Feed = null; return; } Icon = Icons.Material.Filled.RssFeed; Title = Feed.Title; string faviconAddress = Feed.Link.Remove(Feed.Link.IndexOf("http", StringComparison.Ordinal), Feed.Link.IndexOf("://", StringComparison.Ordinal) + 3); FaviconUrl = string.Format(Caretaker.Settings["Paths"].GetString("FaviconResolveUrl"), faviconAddress); } } } }