Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sahin-a committed Mar 12, 2023
1 parent 9f05973 commit 703bca0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class AccountSwitcherViewModel : RoutableViewModel
private readonly AccountMapper _accountMapper;
private readonly ILocalNotificationService _notificationService;
private readonly IPrivacyConfigStorage _privacyConfigStorage;
private readonly EventBus _eventBus;

public AdvancedObservableCollection<Account> Accounts { get; private set; }
public ICommand ProfileClickedCommand { get; }
Expand All @@ -51,19 +52,30 @@ EventBus eventBus
_accountMapper = accountMapper;
_notificationService = notificationService;
_privacyConfigStorage = privacyConfigStorage;
_eventBus = eventBus;

Accounts = new AdvancedObservableCollection<Account>();
ProfileClickedCommand = new ProfileClickedCommand();
RefreshAccountsCommand = new QuickCommand(LoadAccounts);
ShowInfoCommand = new QuickCommand(ShowInfo);
AddAccountCommand = new QuickCommand(AddAccount);
eventBus.Subscribe(subscriberKey: GetType().Name, Events.ACCOUNTS_UPDATED, _ => LoadAccounts());

InitializeVisibilityConfig();
RegisterSubscriptions();
LoadVisibilityConfig();
LoadAccounts();
}

private void InitializeVisibilityConfig()
private void RegisterSubscriptions()
{
_eventBus.Subscribe(subscriberKey: GetType().Name, Events.ACCOUNTS_UPDATED,
_ => LoadAccounts()
);
_eventBus.Subscribe(subscriberKey: GetType().Name, Events.PRIVACY_CONFIG_UPDATED,
_ => LoadVisibilityConfig()
);
}

private void LoadVisibilityConfig()
{
var config = _privacyConfigStorage.Get()?.DetailSettings;
if (config is null)
Expand Down Expand Up @@ -107,7 +119,7 @@ private async Task<IEnumerable<Account>> GetAccounts()

public async void LoadAccounts()
{
InitializeVisibilityConfig();
LoadVisibilityConfig();
Accounts.SetItems((await GetAccounts()).ToList());
}

Expand Down
43 changes: 25 additions & 18 deletions SteamAccountManager.AvaloniaUI/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,17 @@
using DynamicData;
using ReactiveUI;
using SteamAccountManager.AvaloniaUI.Common;
using SteamAccountManager.Domain.Common.EventSystem;
using SteamAccountManager.Domain.Steam.Configuration.Model;
using SteamAccountManager.Domain.Steam.Storage;

namespace SteamAccountManager.AvaloniaUI.ViewModels
{
public class AccountDetailToggle
{
public string Title { get; set; }
public ICommand ToggledCommand { get; set; }
public bool IsToggled { get; set; }

public AccountDetailType DetailType { get; set; }

public AccountDetailToggle(AccountDetailType detailType, string title, ICommand toggledCommand, bool isToggled)
{
Title = title;
ToggledCommand = toggledCommand;
IsToggled = isToggled;
DetailType = detailType;
}
}

public class SettingsViewModel : RoutableViewModel
{
private readonly ISteamApiKeyStorage _steamApiKeyStorage;
private readonly IPrivacyConfigStorage _privacyConfigStorage;
private readonly EventBus _eventBus;


public AdvancedObservableCollection<AccountDetailToggle> AccountDetailsToggles { get; } = new();
Expand All @@ -41,11 +26,14 @@ public SettingsViewModel
(
IScreen screen,
ISteamApiKeyStorage apiKeyStorage,
IPrivacyConfigStorage privacyConfigStorage
IPrivacyConfigStorage privacyConfigStorage,
EventBus eventBus
) : base(screen)
{
_steamApiKeyStorage = apiKeyStorage;
_privacyConfigStorage = privacyConfigStorage;
_eventBus = eventBus;

SaveApiKeyCommand = ReactiveCommand.Create((string key) => SaveApiKey(key));

InitializeControls();
Expand Down Expand Up @@ -99,6 +87,8 @@ private void DetailToggled(AccountDetailToggle detailToggle)
var settings = AccountDetailsToggles.Select(x => new DetailSetting(x.DetailType, x.IsToggled));
_privacyConfigStorage.Set(new PrivacyConfig(settings.ToList()));
}

_eventBus.Notify(Events.PRIVACY_CONFIG_UPDATED, null);
}

private void SaveApiKey(string key)
Expand Down Expand Up @@ -127,4 +117,21 @@ public static string ToTitle(this AccountDetailType type)
return "";
}
}

public class AccountDetailToggle
{
public string Title { get; set; }
public ICommand ToggledCommand { get; set; }
public bool IsToggled { get; set; }

public AccountDetailType DetailType { get; set; }

public AccountDetailToggle(AccountDetailType detailType, string title, ICommand toggledCommand, bool isToggled)
{
Title = title;
ToggledCommand = toggledCommand;
IsToggled = isToggled;
DetailType = detailType;
}
}
}
17 changes: 3 additions & 14 deletions SteamAccountManager.Domain/Common/EventSystem/EventBus.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
using SteamAccountManager.Domain.Common.CodeExtensions;

namespace SteamAccountManager.Domain.Common.EventSystem;
namespace SteamAccountManager.Domain.Common.EventSystem;

public class EventBus
{
private readonly List<Subscription> _subscribers = new();

private void RemoveDeadSubscribers()
{
_subscribers.RemoveAll(x => !x.handler.TryGetTarget(out _));
}

public void Subscribe(string subscriberKey, string eventKey, Action<object?> handler)
{
RemoveDeadSubscribers();
var subscription = new Subscription(subscriberKey, eventKey, new WeakReference<Action<object?>>(handler));
var subscription = new Subscription(subscriberKey, eventKey, handler);
var index = _subscribers.FindIndex(x => x == subscription);
if (index == -1)
{
Expand All @@ -27,15 +19,12 @@ public void Subscribe(string subscriberKey, string eventKey, Action<object?> han

public void Unsubscribe(string subscriberKey)
{
RemoveDeadSubscribers();
_subscribers.RemoveAll(x => x.SubscriberKey == subscriberKey);
}

public void Notify(string eventKey, object? value)
{
_subscribers.FindAll(x => x.EventKey == eventKey)
.ForEach(x =>
x.handler.TryGetTarget(out Action<object?>? action).IfTrue(() => action?.Invoke(value))
);
.ForEach(x => x.handler(value));
}
}
1 change: 1 addition & 0 deletions SteamAccountManager.Domain/Common/EventSystem/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
public static class Events
{
public const string ACCOUNTS_UPDATED = "ACCOUNTS_UPDATED";
public const string PRIVACY_CONFIG_UPDATED = "PRIVACY_CONFIG_UPDATED";
}
4 changes: 2 additions & 2 deletions SteamAccountManager.Domain/Common/EventSystem/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ class Subscription
{
public string SubscriberKey { get; set; }
public string EventKey { get; set; }
public WeakReference<Action<object?>> handler { get; set; }
public Action<object?> handler { get; set; }

public Subscription(string subscriberKey, string eventKey, WeakReference<Action<object?>> handler)
public Subscription(string subscriberKey, string eventKey, Action<object?> handler)
{
SubscriberKey = subscriberKey;
EventKey = eventKey;
Expand Down

0 comments on commit 703bca0

Please sign in to comment.