|
@@ -1,5 +1,6 @@
|
|
|
using System;
|
|
|
using System.Linq;
|
|
|
+using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Media.Imaging;
|
|
@@ -17,26 +18,49 @@ namespace PRSDesktop;
|
|
|
|
|
|
public class ForeignCurrencyGridSettings : BaseObject, IGlobalConfigurationSettings
|
|
|
{
|
|
|
- [TextBoxEditor]
|
|
|
- [EditorSequence(1)]
|
|
|
- public string ApiKey { get; set; }
|
|
|
+ [TextBoxEditor]
|
|
|
+ [EditorSequence(1)]
|
|
|
+ public string ApiKey { get; set; } = "";
|
|
|
|
|
|
[TextBoxEditor]
|
|
|
[EditorSequence(2)]
|
|
|
public string BaseCurrency { get; set; } = "AUD";
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+public class ForeignCurrencyGridPreferences : BaseObject, IUserConfigurationSettings
|
|
|
+{
|
|
|
+ public DynamicGridSelectedFilterSettings Filters { get; set; } = new();
|
|
|
}
|
|
|
|
|
|
public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
|
|
|
{
|
|
|
- private BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
|
|
|
+ private readonly BitmapImage tick = PRSDesktop.Resources.tick.AsBitmapImage();
|
|
|
|
|
|
- private Button update;
|
|
|
+ private readonly Button update;
|
|
|
|
|
|
private ForeignCurrencyGridSettings _settings;
|
|
|
|
|
|
+ private ForeignCurrencyGridPreferences _preferences;
|
|
|
+
|
|
|
public ForeignCurrencyGrid()
|
|
|
{
|
|
|
- _settings = new GlobalConfiguration<ForeignCurrencyGridSettings>().Load();
|
|
|
+ Task[] tasks = {
|
|
|
+ Task.Run(() =>
|
|
|
+ {
|
|
|
+ _settings = new GlobalConfiguration<ForeignCurrencyGridSettings>().Load();
|
|
|
+ }),
|
|
|
+
|
|
|
+ Task.Run(() =>
|
|
|
+ {
|
|
|
+ _preferences = new UserConfiguration<ForeignCurrencyGridPreferences>().Load();
|
|
|
+ }),
|
|
|
+ };
|
|
|
+ Task.WaitAll(tasks);
|
|
|
+
|
|
|
+ FilterComponent.SetSettings(_preferences!.Filters, false);
|
|
|
+ FilterComponent.OnFiltersSelected += FilterComponent_OnFilterSelected;
|
|
|
+
|
|
|
HiddenColumns.Add(x=>x.Code);
|
|
|
HiddenColumns.Add(x=>x.Description);
|
|
|
HiddenColumns.Add(x=>x.Identifier);
|
|
@@ -44,7 +68,13 @@ public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
|
|
|
ActionColumns.Add(new DynamicTickColumn<ForeignCurrency,bool>(x=>x.Active, tick, tick, null, ToggleActive));
|
|
|
AddButton(null, PRSDesktop.Resources.autoupdate.AsBitmapImage(), UpdateSettings);
|
|
|
update = AddButton("Update", PRSDesktop.Resources.payment.AsBitmapImage(), UpdateExchangeRates);
|
|
|
- update.Visibility = !string.IsNullOrEmpty(_settings.ApiKey) ? Visibility.Visible : Visibility.Collapsed;
|
|
|
+ update.Visibility = !string.IsNullOrEmpty(_settings!.ApiKey) ? Visibility.Visible : Visibility.Collapsed;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void FilterComponent_OnFilterSelected(DynamicGridSelectedFilterSettings settings)
|
|
|
+ {
|
|
|
+ _preferences.Filters = settings;
|
|
|
+ new UserConfiguration<ForeignCurrencyGridPreferences>().Save(_preferences);
|
|
|
}
|
|
|
|
|
|
private bool UpdateSettings(Button button, CoreRow[] rows)
|
|
@@ -63,13 +93,13 @@ public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
|
|
|
return false;
|
|
|
var fc = row.ToObject<ForeignCurrency>();
|
|
|
fc.Active = !fc.Active;
|
|
|
- new Client<ForeignCurrency>().Save(fc, string.Format("Set to {0} ", fc.Active ? "Active" : "InActive"));
|
|
|
+ new Client<ForeignCurrency>().Save(fc, $"Set to {(fc.Active ? "Active" : "InActive")} ");
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
protected override BaseEditor? GetEditor(object item, DynamicGridColumn column)
|
|
|
{
|
|
|
- BaseEditor? editor = null;
|
|
|
+ BaseEditor? editor;
|
|
|
if (column.ColumnName.Equals(nameof(ForeignCurrency.Identifier)) && string.IsNullOrWhiteSpace(_settings.ApiKey))
|
|
|
{
|
|
|
editor = base.GetEditor(item, column)?.CloneEditor() ?? new NullEditor();
|
|
@@ -87,9 +117,10 @@ public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- Progress.ShowModal("Retrieving Rates", progress =>
|
|
|
+ Progress.ShowModal("Retrieving Currencies", progress =>
|
|
|
{
|
|
|
- var data = Data.ToObjects<ForeignCurrency>().ToList();
|
|
|
+ var data = new Client<ForeignCurrency>().Query().ToObjects<ForeignCurrency>().ToList();
|
|
|
+ progress.Report("Updating Data");
|
|
|
using (var client = new OpenExchangeRatesClient("ff16587e173b43788a34c035a2c53edd"))
|
|
|
{
|
|
|
var currencies = client.GetCurrenciesAsync().Result;
|
|
@@ -138,9 +169,9 @@ public class ForeignCurrencyGrid : DynamicDataGrid<ForeignCurrency>
|
|
|
progress.Report($"Updating {updates.Length} records...");
|
|
|
new Client<ForeignCurrency>().Save(updates, "Updated from openexchangerates.org");
|
|
|
}
|
|
|
-
|
|
|
|
|
|
});
|
|
|
+
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|