Skip to content

Commit

Permalink
Merge pull request #55 from sahin-a/54-toggle-windows-notifications
Browse files Browse the repository at this point in the history
54 toggle windows notifications
  • Loading branch information
sahin-a committed Mar 26, 2023
2 parents 3c6ce96 + 863c661 commit 4808c62
Show file tree
Hide file tree
Showing 15 changed files with 223 additions and 63 deletions.
10 changes: 10 additions & 0 deletions SteamAccountManager.AvaloniaUI/Models/VisibilityConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace SteamAccountManager.AvaloniaUI.Models;

public class VisibilityConfig
{
public bool ShowUsername { get; set; } = true;
public bool ShowLoginName { get; set; } = true;
public bool ShowAvatar { get; set; } = true;
public bool ShowLevel { get; set; } = true;
public bool ShowBanStatus { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@
<Version>0.2</Version>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**"/>
<None Remove=".gitignore"/>
<None Remove="Assets\avatar_placeholder.jpg"/>
<None Remove="Assets\github_mark_light.png"/>
<None Remove="Assets\sam.ico"/>
<None Remove="SteamAccountSwitcherStyles.xaml"/>
<AvaloniaResource Include="Assets\**" />
<None Remove=".gitignore" />
<None Remove="Assets\avatar_placeholder.jpg" />
<None Remove="Assets\github_mark_light.png" />
<None Remove="Assets\sam.ico" />
<None Remove="SteamAccountSwitcherStyles.xaml" />
</ItemGroup>
<ItemGroup>
<AvaloniaXaml Include="SteamAccountSwitcherStyles.xaml">
<Generator>MSBuild:Compile</Generator>
</AvaloniaXaml>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.19"/>
<PackageReference Include="Avalonia.Desktop" Version="0.10.19"/>
<PackageReference Include="Avalonia" Version="0.10.19" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.19" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.19"/>
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.19"/>
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Condition="'$(TargetFramework)' != 'net6.0'" Version="7.1.3"/>
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.19" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.19" />
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Condition="'$(TargetFramework)' != 'net6.0'" Version="7.1.3" />
</ItemGroup>
<ItemGroup Condition="'$(OS)' == 'Windows_NT'">
<Compile Remove="NullImpl_WindowsLocalNotificationService.cs"/>
<Compile Remove="NullImpl_WindowsLocalNotificationService.cs" />
</ItemGroup>

<ItemGroup Condition="'$(OS)' != 'Windows_NT'">
<Compile Remove="WindowsLocalNotificationService.cs"/>
<Compile Remove="WindowsLocalNotificationService.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DI\DI.csproj"/>
<ProjectReference Include="..\DI\DI.csproj" />
</ItemGroup>
<ItemGroup>
<Compile Update="Views\AccountSwitcherView.axaml.cs">
Expand Down
10 changes: 10 additions & 0 deletions SteamAccountManager.AvaloniaUI/SteamAccountSwitcherStyles.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,14 @@
<Setter Property="FontSize" Value="12" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>

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

<Style Selector="TextBlock.Title">
<Setter Property="FontSize" Value="16" />
</Style>
</Styles>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class AccountSwitcherViewModel : RoutableViewModel
private readonly IPrivacyConfigStorage _privacyConfigStorage;
private readonly EventBus _eventBus;
private readonly InfoService _infoService;
private readonly INotificationConfigStorage _notificationConfigStorage;

public AdvancedObservableCollection<Account> Accounts { get; private set; }
public ICommand ProfileClickedCommand { get; }
Expand All @@ -44,6 +45,7 @@ public AccountSwitcherViewModel
AccountMapper accountMapper,
ILocalNotificationService notificationService,
IPrivacyConfigStorage privacyConfigStorage,
INotificationConfigStorage notificationConfigStorage,
EventBus eventBus,
InfoService infoService
) : base(screen)
Expand All @@ -53,6 +55,7 @@ InfoService infoService
_accountMapper = accountMapper;
_notificationService = notificationService;
_privacyConfigStorage = privacyConfigStorage;
_notificationConfigStorage = notificationConfigStorage;
_eventBus = eventBus;
_infoService = infoService;

Expand Down Expand Up @@ -119,39 +122,38 @@ private async Task<IEnumerable<Account>> GetAccounts()
return SortAccounts(accounts);
}

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

public async void OnAccountSelected(Account selectedAccount)
private void SendNotification(Account account)
{
await _switchAccountUseCase.Execute(selectedAccount.Name);
SelectedAccount = selectedAccount;
if (_notificationConfigStorage.Get()?.IsAllowedToSendNotification != true)
return;

_notificationService.Send
(
new Notification
(
selectedAccount.Name,
selectedAccount.Username,
selectedAccount.ProfilePictureUrl
title: account.Name,
message: account.Username,
account.ProfilePictureUrl
)
);
}

public void ShowInfo()
public async void OnAccountSelected(Account selectedAccount)
{
_infoService.ShowRepository();
await _switchAccountUseCase.Execute(selectedAccount.Name);
SelectedAccount = selectedAccount;
SendNotification(selectedAccount);
}
}

public class VisibilityConfig
{
public bool ShowUsername { get; set; } = true;
public bool ShowLoginName { get; set; } = true;
public bool ShowAvatar { get; set; } = true;
public bool ShowLevel { get; set; } = true;
public bool ShowBanStatus { get; set; } = true;
private void ShowInfo()
{
_infoService.ShowRepository();
}
}
}
61 changes: 52 additions & 9 deletions SteamAccountManager.AvaloniaUI/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using DynamicData;
Expand All @@ -15,10 +16,10 @@ public class SettingsViewModel : RoutableViewModel
private readonly ISteamApiKeyStorage _steamApiKeyStorage;
private readonly IPrivacyConfigStorage _privacyConfigStorage;
private readonly EventBus _eventBus;

private readonly INotificationConfigStorage _notificationConfigStorage;

public AdvancedObservableCollection<AccountDetailToggle> AccountDetailsToggles { get; } = new();

public AdvancedObservableCollection<SettingsToggle> SettingsToggles { get; } = new();
public ICommand SaveApiKeyCommand { get; }
public string WebApiKey { get; set; }

Expand All @@ -27,11 +28,13 @@ public SettingsViewModel
IScreen screen,
ISteamApiKeyStorage apiKeyStorage,
IPrivacyConfigStorage privacyConfigStorage,
INotificationConfigStorage notificationConfigStorage,
EventBus eventBus
) : base(screen)
{
_steamApiKeyStorage = apiKeyStorage;
_privacyConfigStorage = privacyConfigStorage;
_notificationConfigStorage = notificationConfigStorage;
_eventBus = eventBus;

SaveApiKeyCommand = ReactiveCommand.Create((string key) => SaveApiKey(key));
Expand All @@ -43,6 +46,26 @@ EventBus eventBus
private void InitializeControls()
{
CreateAccountDetailToggles();
CreateSettingsToggles();
}

private void CreateSettingsToggles()
{
SettingsToggles.SetItems(
new List<SettingsToggle>
{
new
(
title: "Allow Notifications",
isToggled: _notificationConfigStorage.Get()?.IsAllowedToSendNotification == true,
toggledCommand: ReactiveCommand.Create<SettingsToggle>(toggle =>
_notificationConfigStorage.Set(
new NotificationConfig(isAllowedToSendNotification: toggle.IsToggled)
)
)
)
}
);
}

private void PrefillFields()
Expand All @@ -63,22 +86,26 @@ private void PrefillFields()
private void CreateAccountDetailToggles()
{
var detailToggledCommand =
ReactiveCommand.Create((AccountDetailToggle detailToggle) => DetailToggled(detailToggle));
ReactiveCommand.Create((SettingsToggle settingsToggle) => DetailToggled(settingsToggle));

var toggles = Enum.GetValues<AccountDetailType>().Select(x =>
new AccountDetailToggle(x, title: x.ToTitle(), detailToggledCommand, isToggled: true));
new AccountDetailToggle(detailType: x, title: x.ToTitle(), isToggled: true, detailToggledCommand));

AccountDetailsToggles.SetItems(toggles.ToList());
}

private void DetailToggled(AccountDetailToggle detailToggle)
private void DetailToggled(SettingsToggle detailToggle)
{
if (detailToggle is not AccountDetailToggle toggle)
return;

var privacyConfig = _privacyConfigStorage.Get();
if (privacyConfig is not null)
{
var setting = privacyConfig.DetailSettings.FirstOrDefault(x => x.DetailType == detailToggle.DetailType);
var setting =
privacyConfig.DetailSettings.FirstOrDefault(x => x.DetailType == toggle.DetailType);
privacyConfig.DetailSettings.ReplaceOrAdd(original: setting,
replaceWith: new(detailToggle.DetailType, detailToggle.IsToggled));
replaceWith: new(toggle.DetailType, detailToggle.IsToggled));

_privacyConfigStorage.Set(privacyConfig);
}
Expand Down Expand Up @@ -118,15 +145,31 @@ public static string ToTitle(this AccountDetailType type)
}
}

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

public SettingsToggle(string title, bool isToggled, ICommand toggledCommand)
{
Title = title;
ToggledCommand = toggledCommand;
IsToggled = isToggled;
}
}

public class AccountDetailToggle : SettingsToggle
{
public AccountDetailType DetailType { get; set; }

public AccountDetailToggle(AccountDetailType detailType, string title, ICommand toggledCommand, bool isToggled)
public AccountDetailToggle
(
AccountDetailType detailType,
string title,
bool isToggled,
ICommand toggledCommand
) : base(title, isToggled, toggledCommand)
{
Title = title;
ToggledCommand = toggledCommand;
Expand Down
62 changes: 47 additions & 15 deletions SteamAccountManager.AvaloniaUI/Views/SettingsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,62 @@
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SteamAccountManager.AvaloniaUI.Views.SettingsView">

<UserControl.Styles>
<StyleInclude Source="/SteamAccountSwitcherStyles.xaml" />
</UserControl.Styles>

<Grid Margin="24">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<ItemsRepeater Grid.Column="0" Items="{Binding AccountDetailsToggles}">
<ItemsRepeater.Layout>
<UniformGridLayout Orientation="Horizontal" MaximumRowsOrColumns="2" ItemsStretch="Fill"
ItemsJustification="SpaceBetween" MinRowSpacing="12" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<ToggleSwitch Content="{Binding Title}" IsChecked="{Binding IsToggled}"
Command="{Binding ToggledCommand}" CommandParameter="{Binding}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>

<StackPanel Grid.Column="1">
<StackPanel Grid.Column="0" Spacing="16">

<Border Classes="GroupContainer" Margin="0, 0, 6, 0">
<StackPanel Orientation="Vertical" Spacing="16">
<TextBlock Classes="Title" Text="Details Visibility" />

<ItemsRepeater Items="{Binding AccountDetailsToggles}">
<ItemsRepeater.Layout>
<UniformGridLayout Orientation="Horizontal" MaximumRowsOrColumns="2" ItemsStretch="Fill"
ItemsJustification="SpaceBetween" MinRowSpacing="12" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<ToggleSwitch Content="{Binding Title}" IsChecked="{Binding IsToggled}"
Command="{Binding ToggledCommand}" CommandParameter="{Binding}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</StackPanel>
</Border>

<Border Classes="GroupContainer" Margin="0, 0, 6, 0">
<StackPanel Orientation="Vertical" Spacing="16">
<TextBlock Classes="Title" Text="Visual Feedback" />

<ItemsRepeater Grid.Column="0" Items="{Binding SettingsToggles}">
<ItemsRepeater.Layout>
<UniformGridLayout Orientation="Horizontal" MaximumRowsOrColumns="2" ItemsStretch="Fill"
ItemsJustification="SpaceBetween" MinRowSpacing="12" />
</ItemsRepeater.Layout>
<ItemsRepeater.ItemTemplate>
<DataTemplate>
<ToggleSwitch Content="{Binding Title}" IsChecked="{Binding IsToggled}"
Command="{Binding ToggledCommand}" CommandParameter="{Binding}" />
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
</StackPanel>
</Border>

</StackPanel>

<StackPanel Grid.Column="1" Margin="6, 0, 0, 0">
<Label Content="Steam Web API Key" />
<TextBox Classes="PasswordInput" x:Name="ApiKeyInputField" Text="{Binding WebApiKey}"
Watermark="Enter your key here.." Margin="0, 12, 0, 12" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SteamAccountManager.Domain.Common.CodeExtensions;

public static class ScopeExtensions
{
public static R Let<T, R>(this T value, Func<T, R> action)
{
return action(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
public interface IObjectStorage<T> where T : class
{
public T? Get();
public T Get(T defaultValue);
public void Set(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SteamAccountManager.Domain.Steam.Configuration.Model;

public class NotificationConfig
{
public bool IsAllowedToSendNotification { get; set; }

public NotificationConfig(bool isAllowedToSendNotification)
{
IsAllowedToSendNotification = isAllowedToSendNotification;
}
}
Loading

0 comments on commit 4808c62

Please sign in to comment.