Merged broken code.
This commit is contained in:
commit
88d209ab83
10 files changed
+111
-35
No files matched your search
Generated
+1
@@ -4,6 +4,7 @@
|
|||||||
<option name="projectPerEditor">
|
<option name="projectPerEditor">
|
||||||
<map>
|
<map>
|
||||||
<entry key="PcgsInvUi/App.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
<entry key="PcgsInvUi/App.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
||||||
|
<entry key="PcgsInvUi/Views/ApiKeyView.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
||||||
<entry key="PcgsInvUi/Views/CoinListView.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
<entry key="PcgsInvUi/Views/CoinListView.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
||||||
<entry key="PcgsInvUi/Views/DeleteWindow.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
<entry key="PcgsInvUi/Views/DeleteWindow.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
||||||
<entry key="PcgsInvUi/Views/ErrorWindow.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
<entry key="PcgsInvUi/Views/ErrorWindow.axaml" value="PcgsInvUi/PcgsInvUi.csproj" />
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ public partial class App : Application {
|
|||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) {
|
||||||
var coins = new CoinDatabase();
|
var coins = new CoinDatabase();
|
||||||
desktop.MainWindow = new MainWindow {
|
desktop.MainWindow = new MainWindow {
|
||||||
|
<<<<<<< HEAD
|
||||||
DataContext = new MainWindowViewModel(coins, true),
|
DataContext = new MainWindowViewModel(coins, true),
|
||||||
|
=======
|
||||||
|
DataContext = new MainWindowViewModel(coins, !coins.TryInitApiClient()),
|
||||||
|
>>>>>>> 4b2504b48ca36e20013134202175b826584eab21
|
||||||
};
|
};
|
||||||
|
|
||||||
// Allow the application to save the database when it exits.
|
// Allow the application to save the database when it exits.
|
||||||
|
|||||||
@@ -10,9 +10,14 @@ public class CoinDatabase {
|
|||||||
public SQLiteConnection Connection { get; set; }
|
public SQLiteConnection Connection { get; set; }
|
||||||
private Boolean IsConnected { get; set; }
|
private Boolean IsConnected { get; set; }
|
||||||
public ObservableCollection<Coin> Collection { 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() {
|
public CoinDatabase() {
|
||||||
// Create a connection to SQLite
|
// Create a connection to SQLite
|
||||||
Connection = CreateConnection();
|
Connection = CreateConnection();
|
||||||
@@ -23,33 +28,6 @@ public class CoinDatabase {
|
|||||||
Collection = new ObservableCollection<Coin>();
|
Collection = new ObservableCollection<Coin>();
|
||||||
GetCollection("CollectionTable");
|
GetCollection("CollectionTable");
|
||||||
Console.WriteLine("Got collection.");
|
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() {
|
~CoinDatabase() {
|
||||||
@@ -59,6 +37,32 @@ public class CoinDatabase {
|
|||||||
// Close the connection.
|
// Close the connection.
|
||||||
Connection.Close();
|
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() {
|
private SQLiteConnection CreateConnection() {
|
||||||
// Check if database exists.
|
// 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) {
|
public MainWindowViewModel(CoinDatabase coins, bool askForApiKey) {
|
||||||
ConnectedCoinDatabase = coins;
|
ConnectedCoinDatabase = coins;
|
||||||
var newViewModel = new NewViewModel();
|
var newViewModel = new NewViewModel();
|
||||||
_sideContent = newViewModel;
|
|
||||||
// DisplayedList = CoinCollection = new ObservableCollection<Coin>(coins.GetItems());
|
// DisplayedList = CoinCollection = new ObservableCollection<Coin>(coins.GetItems());
|
||||||
DisplayedList = ConnectedCoinDatabase.Collection;
|
DisplayedList = ConnectedCoinDatabase.Collection;
|
||||||
TotalValue = DisplayedList.Sum(x => x.TotalPrice);
|
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.
|
// Set up the ability to show the error window.
|
||||||
ShowErrorWindow = new Interaction<ErrorWindowViewModel, bool>();
|
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;
|
||||||
using System.IO;
|
|
||||||
using System.Reactive;
|
using System.Reactive;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Avalonia.Platform.Storage;
|
using Avalonia.Platform.Storage;
|
||||||
@@ -36,6 +35,7 @@ public partial class MainWindow : ReactiveWindow<MainWindowViewModel> {
|
|||||||
|
|
||||||
private async Task ShowFilePickerAsync(InteractionContext<Unit, Uri> interationContext) {
|
private async Task ShowFilePickerAsync(InteractionContext<Unit, Uri> interationContext) {
|
||||||
var topLevel = GetTopLevel(this);
|
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 {
|
var file = await topLevel.StorageProvider.SaveFilePickerAsync(new FilePickerSaveOptions {
|
||||||
Title = "Save CSV...",
|
Title = "Save CSV...",
|
||||||
DefaultExtension = ".csv",
|
DefaultExtension = ".csv",
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
using Avalonia;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls;
|
|
||||||
using Avalonia.Markup.Xaml;
|
|
||||||
|
|
||||||
namespace PcgsInvUi.Views;
|
namespace PcgsInvUi.Views;
|
||||||
|
|
||||||
@@ -8,4 +6,4 @@ public partial class NewView : UserControl {
|
|||||||
public NewView() {
|
public NewView() {
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
|
||||||
|
<s:Boolean x:Key="/Default/AddReferences/RecentPaths/=C_003A_005CProgram_0020Files_005Cdotnet_005Cpacks_005CMicrosoft_002ENETCore_002EApp_002ERef_005C7_002E0_002E13_005Cref_005Cnet7_002E0_005CSystem_002EData_002Edll/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
||||||
Reference in new issue
Block a user