Skip to content

Commit

Permalink
Merge pull request #69 from sahin-a/67-select-accounts-that-should-be…
Browse files Browse the repository at this point in the history
…-prevent-from-being-displayed

67 select accounts that should be prevent from being displayed
  • Loading branch information
sahin-a committed Aug 24, 2023
2 parents e7a31ef + 1c31d54 commit ad7fd34
Show file tree
Hide file tree
Showing 15 changed files with 405 additions and 173 deletions.
18 changes: 16 additions & 2 deletions SteamAccountManager.AvaloniaUI/Mappers/AccountMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@
using SteamAccountManager.AvaloniaUI.Common.Utils;
using SteamAccountManager.AvaloniaUI.Models;
using SteamAccountManager.AvaloniaUI.Services;
using SteamAccountManager.Domain.Steam.Blacklisting.Storage;

namespace SteamAccountManager.AvaloniaUI.Mappers
{
public class AccountMapper
{
private AvatarService _avatarService;
private readonly IBlacklistedAccountsStorage _blacklistedAccountsStorage;
private readonly AvatarService _avatarService;

public AccountMapper(AvatarService avatarService)
public AccountMapper(AvatarService avatarService, IBlacklistedAccountsStorage blacklistedAccountsStorage)
{
_avatarService = avatarService;
_blacklistedAccountsStorage = blacklistedAccountsStorage;
}

private async Task<bool> IsBlacklisted(string steamId)
{
var blacklistedIds = await _blacklistedAccountsStorage.Get();

if (blacklistedIds is null)
return false;

return blacklistedIds.BlacklistedIds.Contains(steamId);
}

private string GetTimePassedFormatted(long minutesPassed)
Expand Down Expand Up @@ -62,6 +75,7 @@ public async Task<Account> FromSteamAccount(Domain.Steam.Model.Account steamAcco
LastLogin = timePassedSinceLastLogin,
Rank = rank,
IsLoggedIn = steamAccount.IsLoggedIn,
IsBlacklisted = await IsBlacklisted(steamAccount.Id),
};
}
}
Expand Down
18 changes: 17 additions & 1 deletion SteamAccountManager.AvaloniaUI/Models/Account.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using Avalonia.Media.Imaging;
using ReactiveUI;

namespace SteamAccountManager.AvaloniaUI.Models
{
public class Account
public class Account : ReactiveObject
{
public IBitmap? ProfilePicture { get; set; }
public Uri? ProfilePictureUrl { get; set; }
Expand All @@ -15,6 +16,21 @@ public class Account
public bool IsCommunityBanned { get; set; }
public string LastLogin { get; set; } = string.Empty;
public bool IsLoggedIn { get; set; }
private bool _isBlacklisted;

public bool IsBlacklisted
{
get => _isBlacklisted;
set => this.RaiseAndSetIfChanged(ref _isBlacklisted, value);
}

private bool _isBlacklistToggleVisible;

public bool IsBlacklistToggleVisible
{
get => _isBlacklistToggleVisible;
set => this.RaiseAndSetIfChanged(ref _isBlacklistToggleVisible, value);
}

public Rank Rank { get; set; } = new()
{
Expand Down
15 changes: 10 additions & 5 deletions SteamAccountManager.AvaloniaUI/Services/InfoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ namespace SteamAccountManager.AvaloniaUI.Services;

public class InfoService
{
public void ShowRepository()
private const string RetrieveApiKey = "https://steamcommunity.com/dev/apikey";
private const string GithubRepository = "https://github.com/sahin-a/SteamAccountSwitcher/";

public void ShowRepository() => ShowWebPage(GithubRepository);

public void ShowRetrieveApiKey() => ShowWebPage(RetrieveApiKey);

private void ShowWebPage(string url)
{
if (OperatingSystem.IsWindows())
Process.Start("explorer", "https://github.com/sahin-a/SteamAccountManager/");
else
Process.Start("xdg-open", "https://github.com/sahin-a/SteamAccountManager/");
var targetExecutable = OperatingSystem.IsWindows() ? "explorer" : "xdg-open";
Process.Start(targetExecutable, url);
}
}
42 changes: 38 additions & 4 deletions SteamAccountManager.AvaloniaUI/SteamAccountSwitcherStyles.xaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
<Styles xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style Selector="Window">
<Setter Property="Background" Value="#1c1b1b" />
<Setter Property="Background" Value="#000000" />
<Setter Property="Icon" Value="/Assets/sam.ico" />
<Setter Property="ExtendClientAreaToDecorationsHint" Value="True" />
</Style>

<Style Selector="Button:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="#0a0a0a" />
</Style>

<Style Selector="Button:pressed /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="#1a1a1a" />
</Style>

<Style Selector="Button.InfoButton">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="Button.InfoButton:pointerover /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="Button.InfoButton:pressed /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Background" Value="Transparent" />
</Style>

<Style Selector="Grid.NavBar">
<Setter Property="Background" Value="#1c1b1b" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="Button.NavTab">
<Setter Property="HorizontalContentAlignment" Value="Center" />
Expand All @@ -20,7 +38,7 @@
</Style>

<Style Selector="Grid.ButtonBar">
<Setter Property="Background" Value="#1c1b1b" />
<Setter Property="Background" Value="Transparent" />
</Style>
<Style Selector="Button.BarItem">
<Setter Property="FontSize" Value="25" />
Expand All @@ -33,18 +51,34 @@
<Setter Property="Margin" Value="0" />
</Style>

<Style Selector="Panel.AccountListItemPanel">
<Setter Property="Margin" Value="12, 0, 12, 8" />
<Setter Property="Border.CornerRadius" Value="10" />
</Style>

<Style Selector="Border.AccountListItem">
<Setter Property="Padding" Value="12" />
<Setter Property="CornerRadius" Value="10" />
<Setter Property="Background" Value="#0f0f0f" />
<Setter Property="Margin" Value="4" />
</Style>

<Style Selector="TextBox.PasswordInput">
<Setter Property="PasswordChar" Value="" />
<Setter Property="FontSize" Value="12" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>

<Style Selector="Border.GroupContainer">
<Setter Property="Background" Value="#262626" />
<Setter Property="Background" Value="#0f0f0f" />
<Setter Property="CornerRadius" Value="10" />
<Setter Property="Padding" Value="12" />
</Style>

<Style Selector="Border.AccountListItemActionBar">
<Setter Property="Margin" Value="0,12,0,0" />
</Style>

<Style Selector="TextBlock.Title">
<Setter Property="FontSize" Value="16" />
</Style>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
using SteamAccountManager.AvaloniaUI.Services;
using SteamAccountManager.AvaloniaUI.ViewModels.Commands;
using SteamAccountManager.Domain.Common.EventSystem;
using SteamAccountManager.Domain.Steam.Blacklisting.Model;
using SteamAccountManager.Domain.Steam.Blacklisting.Storage;
using SteamAccountManager.Domain.Steam.Configuration.Model;
using SteamAccountManager.Domain.Steam.Service;
using SteamAccountManager.Domain.Steam.Storage;
using SteamAccountManager.Domain.Steam.UseCase;

namespace SteamAccountManager.AvaloniaUI.ViewModels
{
// TODO: looks ridicilous, I should refactor all of this but I don't feel bored enough yet
// TOOD: switch to reactive commands etc.
public class AccountSwitcherViewModel : RoutableViewModel
{
private readonly IGetAccountsWithDetailsUseCase _getAccountsUseCase;
Expand All @@ -28,12 +28,26 @@ public class AccountSwitcherViewModel : RoutableViewModel
private readonly EventBus _eventBus;
private readonly InfoService _infoService;
private readonly INotificationConfigStorage _notificationConfigStorage;
private readonly IBlacklistedAccountsStorage _blacklistedAccountsStorage;

public AdvancedObservableCollection<Account> Accounts { get; private set; }
public List<Account> AllAccounts { get; private set; } = new();
public List<Account> WhitelistedAccounts { get; private set; } = new();
public AdvancedObservableCollection<Account> AccountsForDisplay { get; private set; }
public ICommand AccountSelectedCommand { get; set; }
public ICommand ProfileClickedCommand { get; }
public ICommand RefreshAccountsCommand { get; }
public ICommand ShowInfoCommand { get; }
public ICommand AddAccountCommand { get; }
public ICommand BlacklistAccountCommand { get; set; }
public ICommand ToggleBlacklistingModeCommand { get; set; }
private bool _isBlacklistToggleVisible;

public bool IsBlacklistToggleVisible
{
get => _isBlacklistToggleVisible;
set => this.RaiseAndSetIfChanged(ref _isBlacklistToggleVisible, value);
}

public VisibilityConfig Config { get; private set; } = new();

private bool _isLoading;
Expand All @@ -53,6 +67,7 @@ public AccountSwitcherViewModel
ILocalNotificationService notificationService,
IPrivacyConfigStorage privacyConfigStorage,
INotificationConfigStorage notificationConfigStorage,
IBlacklistedAccountsStorage blacklistedAccountsStorage,
EventBus eventBus,
InfoService infoService
) : base(screen)
Expand All @@ -63,21 +78,26 @@ InfoService infoService
_notificationService = notificationService;
_privacyConfigStorage = privacyConfigStorage;
_notificationConfigStorage = notificationConfigStorage;
_blacklistedAccountsStorage = blacklistedAccountsStorage;
_eventBus = eventBus;
_infoService = infoService;

Accounts = new AdvancedObservableCollection<Account>();
AccountsForDisplay = new AdvancedObservableCollection<Account>();
ProfileClickedCommand = new ProfileClickedCommand();
RefreshAccountsCommand = new QuickCommand(LoadAccounts);
ShowInfoCommand = new QuickCommand(ShowInfo);
AddAccountCommand = new QuickCommand(AddAccount);
AccountSelectedCommand = ReactiveCommand.Create((Account account) => OnAccountSelected(account));
BlacklistAccountCommand =
ReactiveCommand.Create((Account account) => ToggleBlacklistingForAccount(account));
ToggleBlacklistingModeCommand = ReactiveCommand.Create(ToggleBlacklistingMode);

RegisterSubscriptions();
SubscribeToEventBus();
LoadVisibilityConfig();
LoadAccounts();
}

private void RegisterSubscriptions()
private void SubscribeToEventBus()
{
_eventBus.Subscribe(subscriberKey: GetType().Name, Events.ACCOUNTS_UPDATED,
_ => LoadAccounts()
Expand Down Expand Up @@ -113,20 +133,66 @@ private async void LoadVisibilityConfig()
}
}

private void DisplayAccountsForCurrentState()
{
AccountsForDisplay.SetItems(IsBlacklistToggleVisible
? SortAccountsForManagement(AllAccounts)
: SortAccounts(WhitelistedAccounts));
}

private Task ToggleBlacklistingMode() => Task.Run(() =>
{
IsBlacklistToggleVisible = !IsBlacklistToggleVisible;
DisplayAccountsForCurrentState();
});

private async Task WhitelistAccount(Account account, AccountBlacklist blacklist)
{
blacklist.BlacklistedIds.Remove(account.SteamId);
account.IsBlacklisted = false;
WhitelistedAccounts.Add(account);
await _blacklistedAccountsStorage.Set(blacklist);
}

private async Task BlacklistAccount(Account account, AccountBlacklist blacklist)
{
blacklist.BlacklistedIds.Add(account.SteamId);
account.IsBlacklisted = true;
WhitelistedAccounts.Remove(account);
await _blacklistedAccountsStorage.Set(blacklist);
}

private async Task ToggleBlacklistingForAccount(Account account)
{
var blacklist = await _blacklistedAccountsStorage.Get(new());

if (blacklist.BlacklistedIds.Contains(account.SteamId))
await WhitelistAccount(account, blacklist);
else
await BlacklistAccount(account, blacklist);
}

private async void AddAccount()
{
await _switchAccountUseCase.Execute(string.Empty);
}

private IEnumerable<Account> SortAccounts(Account[] accounts) => accounts.OrderByDescending(x => x.IsLoggedIn)
private List<Account> SortAccountsForManagement(List<Account> accounts) => accounts
.OrderBy(x => x.IsBlacklisted)
.ToList();

private List<Account> SortAccounts(List<Account> accounts) => accounts
.OrderByDescending(x => x.IsLoggedIn)
.ThenByDescending(x => x.Rank.Level)
.ThenBy(x => x.Name)
.ThenBy(x => x.IsVacBanned);
.ThenBy(x => x.IsVacBanned)
.ToList();

private async Task<IEnumerable<Account>> GetAccounts()
private async Task<List<Account>> GetAccounts()
{
var steamAccounts = await _getAccountsUseCase.Execute();
var accounts = await Task.WhenAll(steamAccounts.ConvertAll(x => _accountMapper.FromSteamAccount(x)));
var accounts = (await Task.WhenAll(steamAccounts.ConvertAll(x => _accountMapper.FromSteamAccount(x))))
.ToList();
return SortAccounts(accounts);
}

Expand All @@ -138,7 +204,11 @@ private async void LoadAccounts()
return;

IsLoading = true;
await Task.Run(async () => Accounts.SetItems((await GetAccounts()).ToList()));
var accounts = await GetAccounts();
var whitelistedAccounts = accounts.Where(x => !x.IsBlacklisted).ToList();
AllAccounts = accounts;
WhitelistedAccounts = whitelistedAccounts;
await Task.Run(DisplayAccountsForCurrentState);
IsLoading = false;
}

Expand Down
Loading

0 comments on commit ad7fd34

Please sign in to comment.