diff --git a/PcgsInvUi/Services/CoinDatabase.cs b/PcgsInvUi/Services/CoinDatabase.cs index c9c82a9..98fa5a6 100644 --- a/PcgsInvUi/Services/CoinDatabase.cs +++ b/PcgsInvUi/Services/CoinDatabase.cs @@ -43,6 +43,7 @@ public class CoinDatabase { /// /// True if the API key was found, false otherwise. public bool TryInitApiClient() { + Console.Write("Attempting to initialize API client... "); SQLiteCommand cmd = Connection.CreateCommand(); cmd.CommandText = "SELECT API_KEY FROM ApiTable"; @@ -56,14 +57,28 @@ public class CoinDatabase { insertCmd.ExecuteNonQuery(); IsClientConnected = true; + Console.WriteLine("Done!"); } else { IsClientConnected = false; + Console.WriteLine("Failed. API key not found."); } } return IsClientConnected; } + public bool TryInitApiClient(string apiKey) { + Console.Write("Attempting to initialize API client with key param... "); + _pcgsClient = new PcgsClient(apiKey); + // TODO Check if the API key is valid and if it is, then add to the database. + IsClientConnected = true; + var cmd = Connection.CreateCommand(); + cmd.CommandText = $"INSERT INTO ApiTable (API_KEY, REQUESTS_REMAINING) VALUES ({apiKey}, 1000)"; + // cmd.ExecuteNonQuery(); + Console.WriteLine("Done!"); + return IsClientConnected; + } + private SQLiteConnection CreateConnection() { // Check if database exists. if (!System.IO.File.Exists("coin_collection")) { diff --git a/PcgsInvUi/Services/PcgsClient.cs b/PcgsInvUi/Services/PcgsClient.cs index 2cfbf91..244bf21 100644 --- a/PcgsInvUi/Services/PcgsClient.cs +++ b/PcgsInvUi/Services/PcgsClient.cs @@ -31,7 +31,6 @@ public class PcgsClient : ReactiveObject { } public async Task<(ErrorType, Coin?)> GetCoinFactsByGrade(int pcgsNumber, string grade, bool plusGrade = false) { - Coin newCoin = null; using (HttpClient client = new HttpClient()) { var gradeParam = GetGradeFromString(grade); diff --git a/PcgsInvUi/ViewModels/ApiKeyViewModel.cs b/PcgsInvUi/ViewModels/ApiKeyViewModel.cs index 468b2dd..6f2f9a7 100644 --- a/PcgsInvUi/ViewModels/ApiKeyViewModel.cs +++ b/PcgsInvUi/ViewModels/ApiKeyViewModel.cs @@ -1,5 +1,6 @@ using ReactiveUI; using System.Reactive; +using System; namespace PcgsInvUi.ViewModels; diff --git a/PcgsInvUi/ViewModels/MainWindowViewModel.cs b/PcgsInvUi/ViewModels/MainWindowViewModel.cs index 6eb6124..4464f05 100644 --- a/PcgsInvUi/ViewModels/MainWindowViewModel.cs +++ b/PcgsInvUi/ViewModels/MainWindowViewModel.cs @@ -39,23 +39,22 @@ public class MainWindowViewModel : ViewModelBase { public MainWindowViewModel(CoinDatabase coins, bool askForApiKey) { ConnectedCoinDatabase = coins; var newViewModel = new NewViewModel(); + var apiKeyViewModel = new ApiKeyViewModel(); // DisplayedList = CoinCollection = new ObservableCollection(coins.GetItems()); DisplayedList = ConnectedCoinDatabase.Collection; TotalValue = DisplayedList.Sum(x => x.TotalPrice); // Setup interaction to show ApiKeyWindow + apiKeyViewModel.OkCommand.Subscribe(apiKey => { + if (ConnectedCoinDatabase.TryInitApiClient(apiKey)) + SidebarContent = newViewModel; // Must assign this to the observable property. + }); + // 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. @@ -115,7 +114,8 @@ public class MainWindowViewModel : ViewModelBase { break; } - // TODO Clear the text boxes. + // Clear the text boxes. + newViewModel.ClearText(); }); // Update TotalValue anytime a coin's quantity changes. diff --git a/PcgsInvUi/ViewModels/NewViewModel.cs b/PcgsInvUi/ViewModels/NewViewModel.cs index dca166d..3909324 100644 --- a/PcgsInvUi/ViewModels/NewViewModel.cs +++ b/PcgsInvUi/ViewModels/NewViewModel.cs @@ -40,9 +40,7 @@ public class NewViewModel : ViewModelBase { okEnabled); ClearCommand = ReactiveCommand.Create(() => { - PcgsNumber = ""; - Grade = ""; - Quantity = ""; + ClearText(); }); // Initialize grade values for use in the view. @@ -83,4 +81,13 @@ public class NewViewModel : ViewModelBase { public ReactiveCommand OkCommand { get; } public ReactiveCommand ClearCommand { get; } -} \ No newline at end of file + + /// + /// Clears the text fields. + /// + public void ClearText() { + PcgsNumber = ""; + Grade = ""; + Quantity = ""; + } +}