Skip to content

Commit

Permalink
Extra Currencies - Quick Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Advaith3600 committed Apr 4, 2024
1 parent e7e9f79 commit 0c46102
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,46 @@
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>Community.PowerToys.Run.Plugin.CurrencyConverter</RootNamespace>
<AssemblyName>Community.PowerToys.Run.Plugin.CurrencyConverter</AssemblyName>
<Version>1.1.0</Version>
<Version>1.1.1</Version>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Platforms>AnyCPU;x64;ARM64</Platforms>
</PropertyGroup>

<ItemGroup Condition="'$(Platform)' == 'x64'">
<Reference Include="libs\x64\PowerToys.Common.UI.dll" />
<Reference Include="libs\x64\PowerToys.ManagedCommon.dll" />
<Reference Include="libs\x64\PowerToys.Settings.UI.Lib.dll" />
<Reference Include="libs\x64\Wox.Infrastructure.dll" />
<Reference Include="libs\x64\Wox.Plugin.dll" />
<Reference Include="PowerToys.Common.UI">
<HintPath>Libs\x64\PowerToys.Common.UI.dll</HintPath>
</Reference>
<Reference Include="PowerToys.ManagedCommon">
<HintPath>Libs\x64\PowerToys.ManagedCommon.dll</HintPath>
</Reference>
<Reference Include="PowerToys.Settings.UI.Lib">
<HintPath>Libs\x64\PowerToys.Settings.UI.Lib.dll</HintPath>
</Reference>
<Reference Include="Wox.Infrastructure">
<HintPath>Libs\x64\Wox.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Wox.Plugin">
<HintPath>Libs\x64\Wox.Plugin.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup Condition="'$(Platform)' == 'ARM64'">
<Reference Include="libs\ARM64\PowerToys.Common.UI.dll" />
<Reference Include="libs\ARM64\PowerToys.ManagedCommon.dll" />
<Reference Include="libs\ARM64\PowerToys.Settings.UI.Lib.dll" />
<Reference Include="libs\ARM64\Wox.Infrastructure.dll" />
<Reference Include="libs\ARM64\Wox.Plugin.dll" />
<Reference Include="PowerToys.Common.UI">
<HintPath>Libs\ARM64\PowerToys.Common.UI.dll</HintPath>
</Reference>
<Reference Include="PowerToys.ManagedCommon">
<HintPath>Libs\ARM64\PowerToys.ManagedCommon.dll</HintPath>
</Reference>
<Reference Include="PowerToys.Settings.UI.Lib">
<HintPath>Libs\ARM64\PowerToys.Settings.UI.Lib.dll</HintPath>
</Reference>
<Reference Include="Wox.Infrastructure">
<HintPath>Libs\ARM64\Wox.Infrastructure.dll</HintPath>
</Reference>
<Reference Include="Wox.Plugin">
<HintPath>Libs\ARM64\Wox.Plugin.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
91 changes: 67 additions & 24 deletions Community.PowerToys.Run.Plugin.CurrencyConverter/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ public class Main : IPlugin, ISettingProvider
private PluginInitContext Context { get; set; }
public string Name => "Currency Converter";

public string Description => "Currency Converter Plugin";
public string Description => "Convert real and crypto currencies.";

private Dictionary<string, (JsonElement, DateTime)> ConversionCache = new Dictionary<string, (JsonElement, DateTime)>();
private Dictionary<string, (JsonElement, DateTime)> ConversionCache = [];
private readonly HttpClient Client = new HttpClient();
private readonly RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.Name);

private int ConversionDirection;
private string LocalCurrency, GlobalCurrency;
private string[] ExtraCurrencies;

public IEnumerable<PluginAdditionalOption> AdditionalOptions => new List<PluginAdditionalOption>()
{
Expand All @@ -37,12 +38,12 @@ public class Main : IPlugin, ISettingProvider
DisplayLabel = "Quick Convertion Direction",
DisplayDescription = "Set in which direction you want to convert.",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Combobox,
ComboBoxItems = new List<KeyValuePair<string, string>>
{
ComboBoxItems =
[
new KeyValuePair<string, string>("From local to global", "0"),
new KeyValuePair<string, string>("From global to local", "1"),
},
ComboBoxValue = ConversionDirection,
],
ComboBoxValue = 0,
},
new PluginAdditionalOption()
{
Expand All @@ -60,6 +61,14 @@ public class Main : IPlugin, ISettingProvider
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = "USD",
},
new PluginAdditionalOption()
{
Key = "QuickConversionExtraCurrencies",
DisplayLabel = "Extra currencies for quick conversion",
DisplayDescription = "Add currencies comma separated. eg: USD, EUR, BTC",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = "",
},
};

public void UpdateSettings(PowerLauncherPluginSettings settings)
Expand All @@ -70,8 +79,14 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
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;
GlobalCurrency = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "QuickConversionGlobalCurrency").TextValue;

ExtraCurrencies = settings.AdditionalOptions
.FirstOrDefault(x => x.Key == "QuickConversionExtraCurrencies")
.TextValue.Split(',')
.Select(x => x.Trim())
.Where(x => !string.IsNullOrEmpty(x))
.ToArray();
}
}

Expand Down Expand Up @@ -119,12 +134,20 @@ private double GetConversionRate(string fromCurrency, string toCurrency)
}
}

private Result GetConversion(double amountToConvert, string fromCurrency, string toCurrency)
private Result? GetConversion(double amountToConvert, string fromCurrency, string toCurrency)
{
fromCurrency = fromCurrency.ToLower();
toCurrency = toCurrency.ToLower();

if (fromCurrency == toCurrency || fromCurrency == "" || toCurrency == "")
{
return null;
}

double conversionRate = 0;
try
{
conversionRate = GetConversionRate(fromCurrency.ToLower(), toCurrency.ToLower());
conversionRate = GetConversionRate(fromCurrency, toCurrency);
}
catch (Exception e)
{
Expand Down Expand Up @@ -158,17 +181,17 @@ private Result GetConversion(double amountToConvert, string fromCurrency, string
};
}

public List<Result> Query(Query query)
private List<Result?> ParseQuery(string search)
{
double amountToConvert = 0;
string fromCurrency = "";
string toCurrency = "";

var match = Regex.Match(query.Search.Trim(), @"([0-9.,]+) ?(\w*) ?(to)? ?(\w*)");
var match = Regex.Match(search.Trim(), @"([0-9.,]+) ?(\w*) ?(to)? ?(\w*)");

if (! match.Success)
{
return new List<Result>();
return [];
}

if (double.TryParse(match.Groups[1].Value, out amountToConvert))
Expand All @@ -186,25 +209,45 @@ public List<Result> Query(Query query)
fromCurrency = ConversionDirection == 0 ? LocalCurrency : GlobalCurrency;
toCurrency = ConversionDirection == 0 ? GlobalCurrency : LocalCurrency;

return new List<Result>
List<Result?> results = [GetConversion(amountToConvert, fromCurrency, toCurrency)];
foreach (string currency in ExtraCurrencies)
{
GetConversion(amountToConvert, fromCurrency, toCurrency),
GetConversion(amountToConvert, toCurrency, fromCurrency)
};
}
results.Add(GetConversion(amountToConvert, fromCurrency, currency));
}

results.Add(GetConversion(amountToConvert, toCurrency, fromCurrency));
foreach (string currency in ExtraCurrencies)
{
results.Add(GetConversion(amountToConvert, toCurrency, currency));
}

return results;
}
else if (String.IsNullOrEmpty(toCurrency))
{
return new List<Result>
{
List<Result?> results =
[
GetConversion(amountToConvert, fromCurrency, ConversionDirection == 0 ? GlobalCurrency : LocalCurrency),
GetConversion(amountToConvert, fromCurrency, ConversionDirection == 0 ? LocalCurrency : GlobalCurrency)
};
];

foreach (string currency in ExtraCurrencies)
{
results.Add(GetConversion(amountToConvert, fromCurrency, currency));
}

return results;
}

return new List<Result>
{
return
[
GetConversion(amountToConvert, fromCurrency, toCurrency)
};
];
}

public List<Result> Query(Query query)
{
return ParseQuery(query.Search).Where(x => x != null).ToList();
}

public void Init(PluginInitContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"ActionKeyword": "$$",
"Name": "Currency Converter",
"Author": "advaith3600",
"Version": "1.1.0",
"Version": "1.1.1",
"Language": "csharp",
"Website": "https://github.com/advaith3600/PowerToys-Run-Currency-Converter",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.CurrencyConverter.dll",
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ You can change the `$$` prefix from the settings page. To use this plugin withou
1 eur to usd
```

![Screenshot](screenshots/screenshot5.png)
![Screenshot](screenshots/screenshot2.png)

### Crypto and other currencies

Expand All @@ -30,25 +30,25 @@ Example Usage:
$$ 1 btc to usd
```

![Screenshot](screenshots/screenshot2.png)
![Screenshot](screenshots/screenshot3.png)

### Quick Conversions

You can quickly convert from your local currency to a global currency by just typing the number.
You can quickly convert from your local currency to a global currency by just typing the number. This list will also include all the extra currencies that you have configured in the settings panel.

```
$$ 102.2
```

![Screenshot](screenshots/screenshot3.png)
![Screenshot](screenshots/screenshot4.png)

Or, you can convert any currency to your local and global by:
Or, you can convert any currency to your local and global including the extra currencies by:

```
$$ 1 eur
```

![Screenshot](screenshots/screenshot4.png)
![Screenshot](screenshots/screenshot5.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.

Expand Down
Binary file modified 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 modified 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 modified 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.
Binary file modified screenshots/screenshot5.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 0c46102

Please sign in to comment.