Skip to content

Commit

Permalink
Merge pull request #45 from sahin-a/20-settings-tab-steam-api-key
Browse files Browse the repository at this point in the history
20 settings tab steam api key
  • Loading branch information
sahin-a committed Mar 12, 2023
2 parents 53c840a + dca17e3 commit 1021d62
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 28 deletions.
34 changes: 24 additions & 10 deletions SteamAccountManager.AvaloniaUI/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
using ReactiveUI;
using SteamAccountManager.AvaloniaUI.ViewModels.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Input;
using ReactiveUI;
using SteamAccountManager.Infrastructure.Steam.Local.Storage;

namespace SteamAccountManager.AvaloniaUI.ViewModels
{
public class SettingsViewModel : RoutableViewModel
{
public SettingsViewModel(IScreen screen) : base(screen)
private readonly SteamApiKeyStorage _steamApiKeyStorage;

public ICommand SaveApiKeyCommand { get; }
public string WebApiKey { get; set; }

public SettingsViewModel(IScreen screen, SteamApiKeyStorage apiKeyStorage) : base(screen)
{
_steamApiKeyStorage = apiKeyStorage;
SaveApiKeyCommand = ReactiveCommand.Create((string key) => SaveApiKey(key));

PrefillFields();
}

private void PrefillFields()
{
WebApiKey = _steamApiKeyStorage.Get();
}

private void SaveApiKey(string key)
{
_steamApiKeyStorage.Set(key);
}
}
}
}
24 changes: 21 additions & 3 deletions SteamAccountManager.AvaloniaUI/Views/SettingsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SteamAccountManager.AvaloniaUI.Views.SettingsView">


</UserControl>

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

<StackPanel>
<Label Content="Steam Web API Key" />
<TextBox x:Name="ApiKeyInputField" Text="{Binding WebApiKey}" Watermark="Enter your key here.."
Margin="0,8, 0, 8" />
<Button Content="Save" Command="{Binding SaveApiKeyCommand}"
CommandParameter="{Binding Text, ElementName=ApiKeyInputField}" HorizontalContentAlignment="Center"
HorizontalAlignment="Stretch" />
</StackPanel>
</Grid>

</UserControl>
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using Newtonsoft.Json;
using SteamAccountManager.Application.Steam.Local.Logger;
using SteamAccountManager.Infrastructure.Steam.Local.Dto;
using System;
using System;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using SteamAccountManager.Application.Steam.Local.Logger;
using SteamAccountManager.Infrastructure.Steam.Local.Dto;

namespace SteamAccountManager.Infrastructure.Steam.Local.Storage
{

public class SteamApiKeyStorage : IStorage<string>
{
private const string FileName = "api_key.json";
Expand Down Expand Up @@ -38,7 +37,6 @@ private void Load()
_logger.LogException("File doesn't exist", e);
Save();
}

}

private void Save()
Expand Down Expand Up @@ -70,4 +68,4 @@ public void Set(string value)
Save();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
using Newtonsoft.Json;
using System.Threading.Tasks;
using Newtonsoft.Json;
using RestSharp;
using SteamAccountManager.Application.Steam.Local.Logger;
using SteamAccountManager.Infrastructure.Steam.Local.Storage;
using System.Threading.Tasks;

namespace SteamAccountManager.Infrastructure.Steam.Remote.Dao
{
public class SteamWebClient : RestClient, ISteamWebClient
{
private readonly ILogger _logger;
private readonly SteamApiKeyStorage _steamApiKeyStorage;

public SteamWebClient(ILogger logger, SteamApiKeyStorage apiKeyStorage) : base("https://api.steampowered.com")
{
// TODO: retrieve the key from a file (I would do it now, but I don't wanna bother with it yet)
AddDefaultParameter(Parameter.CreateParameter(
name: "key",
value: apiKeyStorage.Get(),
ParameterType.QueryString));
_steamApiKeyStorage = apiKeyStorage;
_logger = logger;
}

public async Task<T> ExecuteAsync<T>(RestRequest request) where T : new()
{
request.AddParameter(name: "key", value: _steamApiKeyStorage.Get());

var response = await ExecuteAsync(request: request);

if (response.ErrorException != null)
Expand All @@ -33,7 +32,6 @@ public async Task<T> ExecuteAsync<T>(RestRequest request) where T : new()

_logger.LogInformation(responseString);

// TODO: extract the deserialization part out of this class
return JsonConvert.DeserializeObject<T>(responseString);
}
}
Expand Down

0 comments on commit 1021d62

Please sign in to comment.