Skip to content

Commit

Permalink
Quick Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Advaith3600 committed Jan 18, 2024
1 parent 8124ae1 commit b8e3492
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 35 deletions.
113 changes: 81 additions & 32 deletions PowerToysRunCurrencyConverter/Main.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,94 @@
using System.Net.Http;
using System.Globalization;
using System.Net.Http;
using System.Text.Json;
using System.Windows;
using ManagedCommon;
using Wox.Plugin;
using Microsoft.PowerToys.Settings.UI.Library;


namespace PowerToysRunCurrencyConverter
{
public class Main : IPlugin
public class Main : IPlugin, ISettingProvider
{
public static string PluginID => "EF1F634F20484459A3679B4DE7B07999";

private string IconPath { get; set; }
private PluginInitContext Context { get; set; }
public string Name => "Currency Converter";

public string Description => "This plugins converts currency";
public string Description => "Currency Converter Plugin";

private Dictionary<string, (double, DateTime)> cache = new Dictionary<string, (double, DateTime)>();
private Dictionary<string, (double, DateTime)> ConversionCache = new Dictionary<string, (double, DateTime)>();
private readonly HttpClient Client = new HttpClient();
private readonly RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.LCID);

private List<Result> InvalidFormat()
private int ConversionDirection;
private string LocalCurrency, GlobalCurrency;

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
return new List<Result>
new PluginAdditionalOption()
{
new Result
Key = "QuickConversionDirection",
DisplayLabel = "Quick Convertion Direction",
DisplayDescription = "Set in which direction you want to convert.",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
ComboBoxItems = new List<KeyValuePair<string, string>>
{
Title = "Invalid Format",
SubTitle = "$$ 100 inr to usd - please use this format",
IcoPath = IconPath,

new KeyValuePair<string, string>("From local to global", "0"),
new KeyValuePair<string, string>("From global to local", "1"),
},
};
ComboBoxValue = ConversionDirection,
},
new PluginAdditionalOption()
{
Key = "QuickConversionLocalCurrency",
DisplayLabel = "Quick Convertion Local Currency",
DisplayDescription = "Set your local currency.",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = regionInfo.ISOCurrencySymbol,
},
new PluginAdditionalOption()
{
Key = "QuickConversionGlobalCurrency",
DisplayLabel = "Quick Convertion Global Currency",
DisplayDescription = "Set your global currency.",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = "USD",
},
};

public void UpdateSettings(PowerLauncherPluginSettings settings)
{
if (settings != null && settings.AdditionalOptions != null)
{
ConversionDirection = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "QuickConversionDirection")?.ComboBoxValue ?? 0;
string _LocalCurrency = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "QuickConversionLocalCurrency").TextValue;
LocalCurrency = _LocalCurrency == "" ? regionInfo.ISOCurrencySymbol : _LocalCurrency;

string _GlobalCurrency = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "QuickConversionGlobalCurrency").TextValue;
GlobalCurrency = _GlobalCurrency == "" ? "USD" : _GlobalCurrency;
}
}

private double? GetConversionRate(string fromCurrency, string toCurrency)
{
string key = $"{fromCurrency}-{toCurrency}";
if (cache.ContainsKey(key) && cache[key].Item2 > DateTime.Now.AddHours(-1)) // cache for 1 hour
if (ConversionCache.ContainsKey(key) && ConversionCache[key].Item2 > DateTime.Now.AddHours(-1)) // cache for 1 hour
{
return cache[key].Item1;
return ConversionCache[key].Item1;
}
else
{
string url = $"https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/{fromCurrency}/{toCurrency}.json";
try
{
HttpClient client = new HttpClient();
var response = client.GetStringAsync(url).Result;
var response = Client.GetStringAsync(url).Result;
JsonDocument document = JsonDocument.Parse(response);
JsonElement root = document.RootElement;
double conversionRate = root.GetProperty(toCurrency).GetDouble();
cache[key] = (conversionRate, DateTime.Now);
ConversionCache[key] = (conversionRate, DateTime.Now);
return conversionRate;
}
catch (Exception ex)
Expand All @@ -61,27 +100,26 @@ private List<Result> InvalidFormat()

public List<Result> Query(Query query)
{
var strs = query.RawQuery.Split("$$");
if (strs.Length != 2)
double amountToConvert = 0;
var parts = query.Search.Trim().Split(" ");
if (! ((parts.Length == 1 || parts.Length == 4) && double.TryParse(parts[0], out amountToConvert)))
{
return InvalidFormat();
return new List<Result>();
}

var parts = strs[1].Trim().Split(" ");
if (parts.Length != 4)
string fromCurrency, toCurrency;

if (parts.Length == 1)
{
return InvalidFormat();
fromCurrency = (ConversionDirection == 0 ? LocalCurrency : GlobalCurrency).ToLower();
toCurrency = (ConversionDirection == 0 ? GlobalCurrency : LocalCurrency).ToLower();
}

double amountToConvert;
if (!double.TryParse(parts[0], out amountToConvert))
else
{
return InvalidFormat();
fromCurrency = parts[1].ToLower();
toCurrency = parts[3].ToLower();
}

string fromCurrency = parts[1];
string toCurrency = parts[3];

double? conversionRate = GetConversionRate(fromCurrency, toCurrency);

if (conversionRate == null)
Expand All @@ -91,6 +129,7 @@ public List<Result> Query(Query query)
new Result
{
Title = "Something went wrong.",
SubTitle = "Please try again.",
IcoPath = IconPath,
}
};
Expand All @@ -102,9 +141,14 @@ public List<Result> Query(Query query)
{
new Result
{
Title = $"{convertedAmount} {toCurrency}",
SubTitle = $"Currency conversion from {fromCurrency} to {toCurrency}",
Title = $"{convertedAmount} {toCurrency.ToUpper()}",
SubTitle = $"Currency conversion from {fromCurrency.ToUpper()} to {toCurrency.ToUpper()}",
IcoPath = IconPath,
Action = e =>
{
Clipboard.SetText(convertedAmount.ToString());
return true;
}
}
};
}
Expand Down Expand Up @@ -132,5 +176,10 @@ private void OnThemeChanged(Theme currentTheme, Theme newTheme)
{
UpdateIconPath(newTheme);
}

public System.Windows.Controls.Control CreateSettingPanel()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
<Reference Include="Wox.Plugin">
<HintPath>..\libs\Wox.Plugin.dll</HintPath>
</Reference>
<Reference Include="PowerToys.Settings.UI.Lib">
<HintPath>..\libs\PowerToys.Settings.UI.Lib.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion PowerToysRunCurrencyConverter/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ActionKeyword": "$$",
"Name": "Currency Converter",
"Author": "advaith3600",
"Version": "1.0.0",
"Version": "1.0.1",
"Language": "csharp",
"Website": "https://github.com/advaith3600/powertoys-run-currency-converter",
"ExecuteFileName": "PowerToysRunCurrencyConverter.dll",
Expand Down
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
# Currency Converter

This is a plugin developed for PowerToys Run to convert currencies
This is a plugin developed for PowerToys Run to convert currencies.

![Screenshot](Screenshot.png)
![Screenshot](screenshots/screenshot1.png)

## Usage

```
$$ 100 inr to usd
```

### Changing / Removing prefix

You can change the `$$` prefix from the settings page. To use this plugin without any prefix just check the "Include in global result" checkbox. With that option checked, you can use this plugin without any prefix like

```
1 eur to usd
```

![Screenshot](screenshots/screenshot4.png)

### Crypto and other currencies

This plugin also converters real currencies to crypto currencies and vice versa. Refer [here](https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies.json) for the full list of available conversions.

Example Usage:

```
$$ 1 btc to usd
```

![Screenshot](screenshots/screenshot2.png)

### Quick Conversions

You can quickly convert from your local currency to a global currency by just typing the number.

```
$$ 102.2
```

![Screenshot](screenshots/screenshot3.png)

Your local currency, global currency and the quick conversion direction can also be changed from the settings page in PowerToys Run under this plugin.

## Installation

1. Download the latest release of the Currency Converter from the [releases page](https://github.com/advaith3600/powertoys-run-currency-converter/releases).
Expand Down
Binary file removed Screenshot.png
Binary file not shown.
Binary file modified libs/PowerToys.Common.UI.dll
Binary file not shown.
Binary file modified libs/PowerToys.ManagedCommon.dll
Binary file not shown.
Binary file added libs/PowerToys.Settings.UI.Lib.dll
Binary file not shown.
Binary file modified libs/Wox.Infrastructure.dll
Binary file not shown.
Binary file modified libs/Wox.Plugin.dll
Binary file not shown.
Binary file added screenshots/screenshot1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/screenshot2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/screenshot3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/screenshot4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b8e3492

Please sign in to comment.