177 lines
6.0 KiB
C#
177 lines
6.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Globalization;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
|
|
namespace CryptoCalc
|
|
{
|
|
public partial class MainWindow : Window
|
|
{
|
|
private readonly Random rand = new();
|
|
|
|
private readonly DBInteraction<DBClasses> DB = new();
|
|
private readonly DBInteraction<Transaction> transactionDB = new();
|
|
private readonly DBInteraction<Wallet> walletDB = new();
|
|
|
|
private readonly Transaction genericT = new();
|
|
private readonly Wallet genericW = new();
|
|
|
|
private List<Wallet> walletList = new();
|
|
private Wallet selectedWallet = new();
|
|
|
|
public MainWindow()
|
|
{
|
|
InitializeComponent();
|
|
|
|
//Create the DB file if none exist
|
|
DB.CreateDB();
|
|
|
|
//Create tables in DB if none exist
|
|
transactionDB.ExecuteQuery(genericT, genericT.CreateTable(genericW.DBTableName));
|
|
walletDB.ExecuteQuery(genericW, genericW.CreateTable());
|
|
}
|
|
|
|
private void saveButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
transactionDB.SaveData(DummyTransaction());
|
|
}
|
|
|
|
private void saveRandomWalletButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
walletDB.SaveData(DummyWallet());
|
|
}
|
|
|
|
private void saveButtonFromInput_Click(object sender, RoutedEventArgs e)
|
|
private void SaveTransactionButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
transactionDB.SaveData
|
|
(
|
|
new Transaction(
|
|
(DateTime)insertTransactionDatePicker.SelectedDate,
|
|
selectedWallet.Index,
|
|
inputCurrency.Text,
|
|
Convert.ToDecimal(inputAmount.Text, CultureInfo.InvariantCulture),
|
|
inputType.Text
|
|
)
|
|
);
|
|
}
|
|
|
|
private void saveWalletButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
walletDB.SaveData(
|
|
new Wallet(
|
|
(DateTime)inputWalletCreationDate.SelectedDate,
|
|
inputWalletPlatform.Text,
|
|
inputWalletName.Text,
|
|
inputWalletCurrency.Text,
|
|
inputWalletNote.Text
|
|
)
|
|
);
|
|
}
|
|
|
|
private void readButton_click(object sender, RoutedEventArgs e)
|
|
{
|
|
string query = $"select * from {genericT.DBTableName}";
|
|
IEnumerable<Transaction> transactions = transactionDB.ReturnQuery(genericT, query);
|
|
foreach (Transaction t in transactions)
|
|
{
|
|
Debug.WriteLine($"{DBClasses.GetLocalTimeFromUnixTime(t.UnixTime)} *** {t.Currency} - {t.Amount}");
|
|
}
|
|
}
|
|
private void searchButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
string query = $"SELECT * from {genericT.DBTableName} WHERE {nameof(genericT.Currency)}=\'{currencyText.Text}\'";
|
|
IEnumerable<Transaction> transactions = transactionDB.ReturnQuery(genericT, query);
|
|
transactionsFoundListBox.Items.Clear();
|
|
foreach (Transaction x in transactions)
|
|
{
|
|
transactionsFoundListBox.Items.Add(x.FullInfo);
|
|
}
|
|
}
|
|
|
|
private void show_Wallets_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
selectedWallet = null;
|
|
walletList.Clear();
|
|
|
|
string query = $"SELECT * from {genericW.DBTableName}";
|
|
IEnumerable<Wallet> wallets = walletDB.ReturnQuery(genericW, query);
|
|
listBox_Wallets.Items.Clear();
|
|
|
|
foreach (Wallet wallet in wallets)
|
|
{
|
|
walletList.Add(wallet);
|
|
listBox_Wallets.Items.Add($"{wallet.Name}-{wallet.Currency}-{wallet.Balance}");
|
|
}
|
|
}
|
|
|
|
private Transaction DummyTransaction()
|
|
{
|
|
Transaction t = new();
|
|
t.WalletID = selectedWallet.Index;
|
|
t.SaveUnixTimeNow();
|
|
t.Currency = "SOL";
|
|
(t.Amount, t.AmountDecimal) = Util.SplitDecimal(Convert.ToDecimal(30m * (decimal)rand.NextDouble()));
|
|
t.TransactionType = "BUY";
|
|
t.Platform = "Firi";
|
|
t.Note = "Test";
|
|
return t;
|
|
}
|
|
|
|
private Wallet DummyWallet()
|
|
{
|
|
Wallet w = new();
|
|
w.SaveUnixTimeNow();
|
|
w.UnixTimeCreated = DBClasses.GetUnixTime(new DateTime(rand.Next(2011, DateTime.Now.Year), rand.Next(1, 12), rand.Next(1, 28)));
|
|
w.Platform = "Ledger";
|
|
w.Name = "DefaultBTCWallet";
|
|
w.Currency = "BTC";
|
|
w.Balance = 0;//Convert.ToSingle(30 * rand.NextDouble());
|
|
w.DefaultWallet = 1;
|
|
w.Note = "Main BTC Wallet";
|
|
return w;
|
|
}
|
|
|
|
|
|
private void inputAmount_LostFocus(object sender, RoutedEventArgs e)
|
|
{
|
|
//inputAmount.Text = inputAmount.Text.Replace(".", ",");
|
|
inputAmount.Text = inputAmount.Text.Replace(",", ".");
|
|
}
|
|
|
|
private void currencyText_LostFocus(object sender, RoutedEventArgs e)
|
|
{
|
|
currencyText.Text = currencyText.Text.ToUpper();
|
|
}
|
|
|
|
private void inputCurrency_LostFocus(object sender, RoutedEventArgs e)
|
|
{
|
|
inputCurrency.Text = inputCurrency.Text.ToUpper();
|
|
}
|
|
|
|
|
|
private void listBox_Wallets_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
|
|
{
|
|
if (listBox_Wallets.Items.Count > 0)
|
|
{
|
|
if (listBox_Wallets.SelectedIndex < walletList.Count)
|
|
{
|
|
selectedWallet = walletList[listBox_Wallets.SelectedIndex];
|
|
|
|
if (selectedWallet != null)
|
|
{
|
|
Debug.WriteLine($"{selectedWallet.Name} {selectedWallet.Balance}");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Debug.WriteLine("No wallet selected yet");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|