diff --git a/Application/Feeds/Account.cs b/Application/Accounts/Account.cs similarity index 65% rename from Application/Feeds/Account.cs rename to Application/Accounts/Account.cs index 59f2c78..c26e275 100644 --- a/Application/Feeds/Account.cs +++ b/Application/Accounts/Account.cs @@ -1,11 +1,12 @@ -using System; +using FeedCenter.Feeds; +using Realms; +using System; using System.Collections; using System.ComponentModel; using System.Linq; using System.Threading.Tasks; -using Realms; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; public class Account : RealmObject, INotifyDataErrorInfo { @@ -25,6 +26,15 @@ public class Account : RealmObject, INotifyDataErrorInfo _dataErrorDictionary.ErrorsChanged += DataErrorDictionaryErrorsChanged; } + private IAccountReader _accountReader; + + private IAccountReader GetAccountReader() + { + _accountReader ??= AccountReaderFactory.CreateAccountReader(this); + + return _accountReader; + } + [PrimaryKey] public Guid Id { get; set; } = Guid.NewGuid(); @@ -34,23 +44,9 @@ public class Account : RealmObject, INotifyDataErrorInfo set => TypeRaw = value.ToString(); } - public bool SupportsFeedEdit => Type switch - { - AccountType.Fever => false, - AccountType.GoogleReader => false, - AccountType.Miniflux => true, - AccountType.Local => true, - _ => throw new NotSupportedException() - }; + public bool SupportsFeedEdit => GetAccountReader().SupportsFeedEdit; - public bool SupportsFeedDelete => Type switch - { - AccountType.Fever => false, - AccountType.GoogleReader => false, - AccountType.Miniflux => true, - AccountType.Local => true, - _ => throw new NotSupportedException() - }; + public bool SupportsFeedDelete => GetAccountReader().SupportsFeedDelete; private string TypeRaw { get; set; } @@ -161,17 +157,9 @@ public class Account : RealmObject, INotifyDataErrorInfo return new Account { Name = DefaultName, Type = AccountType.Local }; } - public async Task GetProgressSteps(Account account, AccountReadInput accountReadInput) + public async Task GetProgressSteps(AccountReadInput accountReadInput) { - var progressSteps = Type switch - { - // Delegate to the right reader based on the account type - AccountType.Fever => await new FeverReader(account).GetProgressSteps(accountReadInput), - AccountType.GoogleReader => await new GoogleReaderReader(account).GetProgressSteps(accountReadInput), - AccountType.Miniflux => await new MinifluxReader(account).GetProgressSteps(accountReadInput), - AccountType.Local => await new LocalReader(account).GetProgressSteps(accountReadInput), - _ => throw new NotSupportedException() - }; + var progressSteps = await GetAccountReader().GetProgressSteps(accountReadInput); return progressSteps; } @@ -193,25 +181,7 @@ public class Account : RealmObject, INotifyDataErrorInfo return AccountReadResult.NotDue; } - AccountReadResult accountReadResult; - switch (Type) - { - // Delegate to the right reader based on the account type - case AccountType.Fever: - accountReadResult = await new FeverReader(this).Read(accountReadInput); - break; - case AccountType.GoogleReader: - accountReadResult = await new GoogleReaderReader(this).Read(accountReadInput); - break; - case AccountType.Miniflux: - accountReadResult = await new MinifluxReader(this).Read(accountReadInput); - break; - case AccountType.Local: - accountReadResult = await new LocalReader(this).Read(accountReadInput); - break; - default: - throw new NotSupportedException(); - } + var accountReadResult = await GetAccountReader().Read(accountReadInput); return accountReadResult; } diff --git a/Application/Feeds/AccountReadInput.cs b/Application/Accounts/AccountReadInput.cs similarity index 93% rename from Application/Feeds/AccountReadInput.cs rename to Application/Accounts/AccountReadInput.cs index 3d1ebfe..920bf70 100644 --- a/Application/Feeds/AccountReadInput.cs +++ b/Application/Accounts/AccountReadInput.cs @@ -1,6 +1,6 @@ using System; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; public class AccountReadInput(FeedCenterEntities entities, Guid? feedId, bool forceRead, Action incrementProgress) { diff --git a/Application/Feeds/AccountReadResult.cs b/Application/Accounts/AccountReadResult.cs similarity index 67% rename from Application/Feeds/AccountReadResult.cs rename to Application/Accounts/AccountReadResult.cs index 0ac9207..f8e51c8 100644 --- a/Application/Feeds/AccountReadResult.cs +++ b/Application/Accounts/AccountReadResult.cs @@ -1,4 +1,4 @@ -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; public enum AccountReadResult { diff --git a/Application/Accounts/AccountReaderFactory.cs b/Application/Accounts/AccountReaderFactory.cs new file mode 100644 index 0000000..326f634 --- /dev/null +++ b/Application/Accounts/AccountReaderFactory.cs @@ -0,0 +1,16 @@ +using System; + +namespace FeedCenter.Accounts; + +internal static class AccountReaderFactory +{ + internal static IAccountReader CreateAccountReader(Account account) => + account.Type switch + { + AccountType.Miniflux => new MinifluxReader(account), + AccountType.Local => new LocalReader(account), + AccountType.Fever => new FeverReader(account), + AccountType.GoogleReader => new GoogleReaderReader(account), + _ => throw new NotSupportedException($"Account type '{account.Type}' is not supported."), + }; +} \ No newline at end of file diff --git a/Application/Feeds/AccountType.cs b/Application/Accounts/AccountType.cs similarity index 69% rename from Application/Feeds/AccountType.cs rename to Application/Accounts/AccountType.cs index 697c85c..f4ad6c5 100644 --- a/Application/Feeds/AccountType.cs +++ b/Application/Accounts/AccountType.cs @@ -1,4 +1,4 @@ -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; public enum AccountType { diff --git a/Application/Feeds/FeverReader.cs b/Application/Accounts/FeverReader.cs similarity index 97% rename from Application/Feeds/FeverReader.cs rename to Application/Accounts/FeverReader.cs index 1101334..64de34f 100644 --- a/Application/Feeds/FeverReader.cs +++ b/Application/Accounts/FeverReader.cs @@ -4,8 +4,9 @@ using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; +using FeedCenter.Feeds; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; internal class FeverReader(Account account) : IAccountReader { @@ -142,6 +143,10 @@ internal class FeverReader(Account account) : IAccountReader await feverClient.MarkFeedItemAsRead(int.Parse(feedItemId)); } + public bool SupportsFeedDelete => false; + + public bool SupportsFeedEdit => false; + private static string GetApiKey(Account account) { var input = $"{account.Username}:{account.Password}"; diff --git a/Application/Feeds/GoogleReaderReader.cs b/Application/Accounts/GoogleReaderReader.cs similarity index 97% rename from Application/Feeds/GoogleReaderReader.cs rename to Application/Accounts/GoogleReaderReader.cs index f7d209d..1b13dd0 100644 --- a/Application/Feeds/GoogleReaderReader.cs +++ b/Application/Accounts/GoogleReaderReader.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; internal class GoogleReaderReader(Account account) : IAccountReader { @@ -134,6 +134,10 @@ internal class GoogleReaderReader(Account account) : IAccountReader return Task.CompletedTask; } + public bool SupportsFeedDelete => false; + + public bool SupportsFeedEdit => false; + //private static string GetApiKey(Account account) //{ // var input = $"{account.Username}:{account.Password}"; diff --git a/Application/Feeds/IAccountReader.cs b/Application/Accounts/IAccountReader.cs similarity index 69% rename from Application/Feeds/IAccountReader.cs rename to Application/Accounts/IAccountReader.cs index 9cb7e1a..4c8eb70 100644 --- a/Application/Feeds/IAccountReader.cs +++ b/Application/Accounts/IAccountReader.cs @@ -1,10 +1,12 @@ using System.Threading.Tasks; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; public interface IAccountReader { public Task GetProgressSteps(AccountReadInput accountReadInput); public Task Read(AccountReadInput accountReadInput); public Task MarkFeedItemRead(string feedItemId); + public bool SupportsFeedDelete { get; } + public bool SupportsFeedEdit { get; } } \ No newline at end of file diff --git a/Application/Feeds/LocalReader.cs b/Application/Accounts/LocalReader.cs similarity index 91% rename from Application/Feeds/LocalReader.cs rename to Application/Accounts/LocalReader.cs index 3096f99..d3ef81d 100644 --- a/Application/Feeds/LocalReader.cs +++ b/Application/Accounts/LocalReader.cs @@ -2,8 +2,9 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using FeedCenter.Feeds; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; public class LocalReader(Account account) : IAccountReader { @@ -45,4 +46,8 @@ public class LocalReader(Account account) : IAccountReader { throw new NotImplementedException(); } + + public bool SupportsFeedDelete => true; + + public bool SupportsFeedEdit => true; } \ No newline at end of file diff --git a/Application/Feeds/MinifluxReader.cs b/Application/Accounts/MinifluxReader.cs similarity index 97% rename from Application/Feeds/MinifluxReader.cs rename to Application/Accounts/MinifluxReader.cs index 9f30f80..5d19926 100644 --- a/Application/Feeds/MinifluxReader.cs +++ b/Application/Accounts/MinifluxReader.cs @@ -2,8 +2,9 @@ using System.Linq; using System.Threading.Tasks; using ChrisKaczor.MinifluxClient; +using FeedCenter.Feeds; -namespace FeedCenter.Feeds; +namespace FeedCenter.Accounts; internal class MinifluxReader(Account account) : IAccountReader { @@ -143,4 +144,8 @@ internal class MinifluxReader(Account account) : IAccountReader await minifluxClient.MarkFeedEntries([long.Parse(feedItemId)], FeedEntryStatus.Read); } + + public bool SupportsFeedDelete => true; + + public bool SupportsFeedEdit => true; } \ No newline at end of file diff --git a/Application/Entities.cs b/Application/Entities.cs index 2571e8e..dab5462 100644 --- a/Application/Entities.cs +++ b/Application/Entities.cs @@ -3,6 +3,7 @@ using FeedCenter.Options; using Realms; using System; using System.Linq; +using FeedCenter.Accounts; using FeedCenter.Feeds; namespace FeedCenter; diff --git a/Application/Feeds/Category.cs b/Application/Feeds/Category.cs index b617bd4..12b35d4 100644 --- a/Application/Feeds/Category.cs +++ b/Application/Feeds/Category.cs @@ -2,6 +2,7 @@ using System.Collections; using System.ComponentModel; using System.Linq; +using FeedCenter.Accounts; using JetBrains.Annotations; using Realms; diff --git a/Application/Feeds/Feed.cs b/Application/Feeds/Feed.cs index f7b345f..91e73d7 100644 --- a/Application/Feeds/Feed.cs +++ b/Application/Feeds/Feed.cs @@ -12,6 +12,7 @@ using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using ChrisKaczor.ApplicationUpdate; +using FeedCenter.Accounts; using FeedCenter.FeedParsers; using FeedCenter.Properties; using FeedCenter.Xml; diff --git a/Application/Feeds/FeedItem.cs b/Application/Feeds/FeedItem.cs index 40affe0..33728e1 100644 --- a/Application/Feeds/FeedItem.cs +++ b/Application/Feeds/FeedItem.cs @@ -1,8 +1,8 @@ using System; -using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; using System.Threading.Tasks; +using FeedCenter.Accounts; using FeedCenter.Options; using Realms; @@ -86,24 +86,7 @@ public partial class FeedItem : RealmObject if (feed == null || feed.Account.Type == AccountType.Local) return; - switch (feed.Account.Type) - { - case AccountType.Fever: - // Delegate to the right reader based on the account type - await new FeverReader(feed.Account).MarkFeedItemRead(RemoteId); - - break; - case AccountType.Miniflux: - // Delegate to the right reader based on the account type - await new MinifluxReader(feed.Account).MarkFeedItemRead(RemoteId); - - break; - case AccountType.Local: - break; - default: - Debug.Assert(false); - break; - } + await AccountReaderFactory.CreateAccountReader(feed.Account).MarkFeedItemRead(RemoteId); } [GeneratedRegex("\\n")] diff --git a/Application/MainWindow/FeedList.cs b/Application/MainWindow/FeedList.cs index 9419734..1c1e7f8 100644 --- a/Application/MainWindow/FeedList.cs +++ b/Application/MainWindow/FeedList.cs @@ -2,7 +2,6 @@ using FeedCenter.Properties; using System; using System.Linq; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Input; diff --git a/Application/MainWindow/FeedReading.cs b/Application/MainWindow/FeedReading.cs index 2d2934c..9005afa 100644 --- a/Application/MainWindow/FeedReading.cs +++ b/Application/MainWindow/FeedReading.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using System.Windows; +using FeedCenter.Accounts; namespace FeedCenter; @@ -47,7 +48,7 @@ public partial class MainWindow var accountReadInput = new AccountReadInput(_database, _currentFeed.Id, forceRead, () => { }); // Switch to progress mode - SetProgressMode(true, await _currentFeed.Account.GetProgressSteps(_currentFeed.Account, accountReadInput)); + SetProgressMode(true, await _currentFeed.Account.GetProgressSteps(accountReadInput)); // Start reading await HandleFeedReadWorkerStart(forceRead, _currentFeed.Id); @@ -77,7 +78,7 @@ public partial class MainWindow foreach (var account in _database.Accounts) { - progressSteps += await account.GetProgressSteps(account, accountReadInput); + progressSteps += await account.GetProgressSteps(accountReadInput); } // Switch to progress mode diff --git a/Application/Options/AccountTypeItem.cs b/Application/Options/AccountTypeItem.cs index c94f867..595ef1f 100644 --- a/Application/Options/AccountTypeItem.cs +++ b/Application/Options/AccountTypeItem.cs @@ -1,5 +1,5 @@ using System.Collections.Generic; -using FeedCenter.Feeds; +using FeedCenter.Accounts; namespace FeedCenter.Options; diff --git a/Application/Options/AccountTypeToNameConverter.cs b/Application/Options/AccountTypeToNameConverter.cs index 9c916c9..7452880 100644 --- a/Application/Options/AccountTypeToNameConverter.cs +++ b/Application/Options/AccountTypeToNameConverter.cs @@ -2,7 +2,7 @@ using System; using System.Globalization; using System.Linq; using System.Windows.Data; -using FeedCenter.Feeds; +using FeedCenter.Accounts; namespace FeedCenter.Options; diff --git a/Application/Options/AccountWindow.xaml b/Application/Options/AccountWindow.xaml index b712284..46451e5 100644 --- a/Application/Options/AccountWindow.xaml +++ b/Application/Options/AccountWindow.xaml @@ -4,11 +4,12 @@ xmlns:properties="clr-namespace:FeedCenter.Properties" xmlns:feedCenter="clr-namespace:FeedCenter" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" - d:DataContext="{d:DesignInstance Type=feeds:Account}" + d:DataContext="{d:DesignInstance Type=accounts:Account}" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls" xmlns:options="clr-namespace:FeedCenter.Options" xmlns:feeds="clr-namespace:FeedCenter.Feeds" + xmlns:accounts="clr-namespace:FeedCenter.Accounts" mc:Ignorable="d" Title="AccountWindow" Height="350" diff --git a/Application/Options/AccountWindow.xaml.cs b/Application/Options/AccountWindow.xaml.cs index e332a53..de8eb8c 100644 --- a/Application/Options/AccountWindow.xaml.cs +++ b/Application/Options/AccountWindow.xaml.cs @@ -1,9 +1,8 @@ using ChrisKaczor.Wpf.Validation; -using FeedCenter.Feeds; using System; using System.Linq; using System.Windows; -using System.Windows.Threading; +using FeedCenter.Accounts; namespace FeedCenter.Options; @@ -58,7 +57,7 @@ public partial class AccountWindow var accountReadInput = new AccountReadInput(_entities, null, true, () => AccountReadProgressBar.Value++); AccountReadProgressBar.Value = 0; - AccountReadProgressBar.Maximum = await _account.GetProgressSteps(_account, accountReadInput); + AccountReadProgressBar.Maximum = await _account.GetProgressSteps(accountReadInput); AccountReadProgress.Visibility = Visibility.Visible; ButtonPanel.Visibility = Visibility.Collapsed; diff --git a/Application/Options/AccountsOptionsPanel.xaml b/Application/Options/AccountsOptionsPanel.xaml index ba0e369..26c3ec3 100644 --- a/Application/Options/AccountsOptionsPanel.xaml +++ b/Application/Options/AccountsOptionsPanel.xaml @@ -8,6 +8,7 @@ xmlns:controls="clr-namespace:ChrisKaczor.Wpf.Controls;assembly=ChrisKaczor.Wpf.Controls.Link" xmlns:feedCenter="clr-namespace:FeedCenter" xmlns:feeds="clr-namespace:FeedCenter.Feeds" + xmlns:accounts="clr-namespace:FeedCenter.Accounts" mc:Ignorable="d" d:DesignHeight="311" d:DesignWidth="425"> @@ -47,7 +48,7 @@ BorderBrush="{DynamicResource {x:Static SystemColors.ActiveBorderBrushKey}}" AllowDrop="True" Background="{x:Null}" - d:DataContext="{d:DesignInstance feeds:Account }"> + d:DataContext="{d:DesignInstance accounts:Account }">