Pushing currently broken code.
This commit is contained in:
1 parent
435d01ef90
commit
4b2504b48c
10 files changed
+108
-36
No files matched your search
@@ -17,7 +17,7 @@ public partial class App : Application {
|
||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
|
||||
var coins = new CoinDatabase();
|
||||
desktop.MainWindow = new MainWindow {
|
||||
DataContext = new MainWindowViewModel(),
|
||||
DataContext = new MainWindowViewModel(coins, !coins.TryInitApiClient()),
|
||||
};
|
||||
|
||||
// Allow the application to save the database when it exits.
|
||||
|
||||
@@ -10,9 +10,14 @@ public class CoinDatabase {
|
||||
public SQLiteConnection Connection { get; set; }
|
||||
private Boolean IsConnected { get; set; }
|
||||
public ObservableCollection<Coin> Collection { get; set; }
|
||||
public Boolean IsClientConnected { get; set; }
|
||||
|
||||
private PcgsClient _pcgsClient;
|
||||
|
||||
private PcgsClient? _pcgsClient;
|
||||
|
||||
/// <summary>
|
||||
/// Creates and initilizes a new instance of the CoinDatabase.
|
||||
/// Must call TryInitApiClient() after creating a new instance.
|
||||
/// </summary>
|
||||
public CoinDatabase() {
|
||||
// Create a connection to SQLite
|
||||
Connection = CreateConnection();
|
||||
@@ -23,33 +28,6 @@ public class CoinDatabase {
|
||||
Collection = new ObservableCollection<Coin>();
|
||||
GetCollection("CollectionTable");
|
||||
Console.WriteLine("Got collection.");
|
||||
|
||||
// Initialize the PCGS client. Must get the API key from the database.
|
||||
// Check the database for an API key.
|
||||
SQLiteCommand cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = "SELECT API_KEY FROM ApiTable";
|
||||
using (var reader = cmd.ExecuteReader()) {
|
||||
// If there's no API key, we need to get one from the user.
|
||||
// Eventually there will be a window that pops up adking for the API key.
|
||||
var apiKey = "";
|
||||
if (reader.Read()) {
|
||||
// Initialize the client.
|
||||
apiKey = reader.GetString(0);
|
||||
}
|
||||
else {
|
||||
// Get the API key from the user.
|
||||
// TODO Create and open a window that asks for the API key.
|
||||
// Since this needs to build before the main window is created, we can't do that yet.
|
||||
Console.WriteLine("No API key found. Using default...");
|
||||
do {
|
||||
// Console.Write("Enter a valid API key: ");
|
||||
// apiKey = Console.ReadLine();
|
||||
// Eventually we actually want to throw an error here.
|
||||
apiKey = "E8xivTVN_k4Q8ubk8Xv_uU11U91WX0wDht4S4m4BAf2HPmKnGrZ0J2f9nccM7Ns1VkGoT1SXYR74I7aNaMKqXlJGEy3oobseKV0nGiqu1IKvFkOTVjhV37SWUVExSDFMC6FLzs7xJQIHwkcxr5JFOQNmc4ZytEh_-hmCN3NL-z0fEk98tzBmPJ_F7bq_xNru86QMD2A9yalu_dWe-lCysfyE2bCA2EDqwvWenRYf-0XnNtp6qlxxCMlCd-TDqqRKLC3qefCwpXSemwBiNSP5gbC9pwcKrZugkruuJ0nyxxThZymm";
|
||||
} while (apiKey == "" || apiKey == null);
|
||||
}
|
||||
_pcgsClient = new PcgsClient(apiKey);
|
||||
}
|
||||
}
|
||||
|
||||
~CoinDatabase() {
|
||||
@@ -59,6 +37,32 @@ public class CoinDatabase {
|
||||
// Close the connection.
|
||||
Connection.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to intitialize the API client by selecting the API key from the database.
|
||||
/// </summary>
|
||||
/// <returns>True if the API key was found, false otherwise.</returns>
|
||||
public bool TryInitApiClient() {
|
||||
SQLiteCommand cmd = Connection.CreateCommand();
|
||||
cmd.CommandText = "SELECT API_KEY FROM ApiTable";
|
||||
|
||||
using (var reader = cmd.ExecuteReader()) {
|
||||
if (reader.Read()) {
|
||||
var apiKey = reader.GetString(0);
|
||||
_pcgsClient = new PcgsClient(apiKey);
|
||||
|
||||
var insertCmd = Connection.CreateCommand();
|
||||
insertCmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUE ({apiKey}, 1000)";
|
||||
insertCmd.ExecuteNonQuery();
|
||||
|
||||
IsClientConnected = true;
|
||||
}
|
||||
else {
|
||||
IsClientConnected = false;
|
||||
}
|
||||
}
|
||||
return IsClientConnected;
|
||||
}
|
||||
|
||||
private SQLiteConnection CreateConnection() {
|
||||
// Check if database exists.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
using ReactiveUI;
|
||||
using System.Reactive;
|
||||
|
||||
namespace PcgsInvUi.ViewModels;
|
||||
|
||||
public class ApiKeyViewModel : ViewModelBase {
|
||||
public string ApiKeyInput {
|
||||
get => _apiKeyInput;
|
||||
set => this.RaiseAndSetIfChanged(ref _apiKeyInput, value);
|
||||
}
|
||||
private string _apiKeyInput = "";
|
||||
|
||||
public bool OkEnabled {
|
||||
get => _okEnabled;
|
||||
set => this.RaiseAndSetIfChanged(ref _okEnabled, value);
|
||||
}
|
||||
private bool _okEnabled;
|
||||
|
||||
public ReactiveCommand<Unit, string> OkCommand { get; }
|
||||
|
||||
public ApiKeyViewModel() {
|
||||
var okEnabled = this.WhenAnyValue(
|
||||
x => x.ApiKeyInput,
|
||||
x => !string.IsNullOrWhiteSpace(x));
|
||||
|
||||
|
||||
// Get the API key from the user.
|
||||
OkCommand = ReactiveCommand.Create(() => ApiKeyInput, okEnabled);
|
||||
}
|
||||
}
|
||||
@@ -39,10 +39,24 @@ public class MainWindowViewModel : ViewModelBase {
|
||||
public MainWindowViewModel(CoinDatabase coins, bool askForApiKey) {
|
||||
ConnectedCoinDatabase = coins;
|
||||
var newViewModel = new NewViewModel();
|
||||
_sideContent = newViewModel;
|
||||
// DisplayedList = CoinCollection = new ObservableCollection<Coin>(coins.GetItems());
|
||||
DisplayedList = ConnectedCoinDatabase.Collection;
|
||||
TotalValue = DisplayedList.Sum(x => x.TotalPrice);
|
||||
|
||||
// Setup interaction to show ApiKeyWindow
|
||||
// Check if API key must be entered.
|
||||
if (askForApiKey) {
|
||||
Console.WriteLine("API key must be entered by user.");
|
||||
// Show the API key input view.
|
||||
var apiKeyViewModel = new ApiKeyViewModel();
|
||||
_sideContent = apiKeyViewModel;
|
||||
// Subscribe to the OkCommand in the ApiKeyViewModel.
|
||||
apiKeyViewModel.OkCommand
|
||||
.Subscribe(apiKey => {
|
||||
// Attempt to connect to the PCGS API and set view accordingly.
|
||||
if (ConnectedCoinDatabase.TryInitApiClient()) _sideContent = newViewModel;
|
||||
});
|
||||
} else _sideContent = newViewModel;
|
||||
|
||||
// Set up the ability to show the error window.
|
||||
ShowErrorWindow = new Interaction<ErrorWindowViewModel, bool>();
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:viewModels="clr-namespace:PcgsInvUi.ViewModels"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="PcgsInvUi.Views.ApiKeyView" x:DataType="viewModels:ApiKeyViewModel" >
|
||||
<StackPanel>
|
||||
<Label Content="API key was not found or did not authenticate." />
|
||||
<Label Content="Please enter your API key below:" />
|
||||
<TextBox Margin="10" Text="{Binding ApiKeyInput}" />
|
||||
<Button HorizontalAlignment="Center" Content="Submit" Command="{Binding OkCommand}" />
|
||||
</StackPanel>
|
||||
</UserControl>
|
||||
@@ -0,0 +1,9 @@
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace PcgsInvUi.Views;
|
||||
|
||||
public partial class ApiKeyView : UserControl {
|
||||
public ApiKeyView() {
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reactive;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Platform.Storage;
|
||||
@@ -36,6 +35,7 @@ public partial class MainWindow : ReactiveWindow<MainWindowViewModel> {
|
||||
|
||||
private async Task ShowFilePickerAsync(InteractionContext<Unit, Uri> interationContext) {
|
||||
var topLevel = GetTopLevel(this);
|
||||
if (topLevel is null) throw new Exception("Could not find top level of the file path.");
|
||||
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions {
|
||||
Title = "Save CSV...",
|
||||
DefaultExtension = ".csv",
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Markup.Xaml;
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace PcgsInvUi.Views;
|
||||
|
||||
@@ -8,4 +6,4 @@ public partial class NewView : UserControl {
|
||||
public NewView() {
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user