Skip to content

Commit

Permalink
Best Practices
Browse files Browse the repository at this point in the history
  • Loading branch information
Advaith3600 committed May 24, 2024
1 parent 9b94774 commit 1e8ba35
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>Community.PowerToys.Run.Plugin.CurrencyConverter</RootNamespace>
<AssemblyName>Community.PowerToys.Run.Plugin.CurrencyConverter</AssemblyName>
<Version>1.1.3</Version>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<Platforms>AnyCPU;x64;ARM64</Platforms>
<Version>1.1.4</Version>
<Platforms>x64;ARM64</Platforms>
<PlatformTarget>$(Platform)</PlatformTarget>
</PropertyGroup>

<ItemGroup Condition="'$(Platform)' == 'x64'">
Expand Down
96 changes: 65 additions & 31 deletions Community.PowerToys.Run.Plugin.CurrencyConverter/Main.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net;
using System.Text;
using System.Net.Http;
using System.Text.Json;
using System.Windows.Input;
using System.Globalization;
using System.Text.RegularExpressions;

Expand All @@ -9,11 +11,10 @@
using Microsoft.PowerToys.Settings.UI.Library;

using Clipboard = System.Windows.Clipboard;
using System.Text;

namespace Community.PowerToys.Run.Plugin.CurrencyConverter
{
public class Main : IPlugin, ISettingProvider
public class Main : IPlugin, IContextMenu, ISettingProvider, IDisposable
{
public static string PluginID => "EF1F634F20484459A3679B4DE7B07999";

Expand All @@ -23,9 +24,9 @@ public class Main : IPlugin, ISettingProvider

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

private bool Disposed { get; set; }

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

private int ConversionDirection, OutputStyle, OutputPrecision;
private string LocalCurrency;
Expand Down Expand Up @@ -73,7 +74,7 @@ public class Main : IPlugin, ISettingProvider
DisplayLabel = "Quick Conversion Local Currency",
DisplayDescription = "Set your local currency.",
PluginOptionType = PluginAdditionalOption.AdditionalOptionType.Textbox,
TextValue = regionInfo.ISOCurrencySymbol,
TextValue = (new RegionInfo(CultureInfo.CurrentCulture.Name)).ISOCurrencySymbol,
},
new PluginAdditionalOption()
{
Expand All @@ -94,6 +95,7 @@ public void UpdateSettings(PowerLauncherPluginSettings settings)
OutputStyle = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ConversionOutputStyle")?.ComboBoxValue ?? 0;
OutputPrecision = (int) (settings.AdditionalOptions.FirstOrDefault(x => x.Key == "ConversionOutputPrecision")?.NumberValue ?? 2);

RegionInfo regionInfo = new RegionInfo(CultureInfo.CurrentCulture.Name);
string _LocalCurrency = settings.AdditionalOptions.FirstOrDefault(x => x.Key == "QuickConversionLocalCurrency")?.TextValue ?? "";
LocalCurrency = _LocalCurrency == "" ? regionInfo.ISOCurrencySymbol : _LocalCurrency;

Expand Down Expand Up @@ -123,6 +125,7 @@ private double GetConversionRate(string fromCurrency, string toCurrency)
string url = $"https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/{fromCurrency}.min.json";
try
{
HttpClient Client = new HttpClient();
var response = Client.GetAsync(url).Result;
if (!response.IsSuccessStatusCode)
{
Expand Down Expand Up @@ -197,19 +200,19 @@ private double GetConversionRate(string fromCurrency, string toCurrency)
}

string fromFormatted = amountToConvert.ToString("N", CultureInfo.CurrentCulture);
string toFormatted = convertedAmount.ToString($"N{precision}", CultureInfo.CurrentCulture);
string toFormatted = (amountToConvert < 0 ? convertedAmount * -1 : convertedAmount).ToString($"N{precision}", CultureInfo.CurrentCulture);

string compressedOutput = $"{toFormatted} {toCurrency.ToUpper()}";
string expandedOutput = $"{fromFormatted} {fromCurrency.ToUpper()} = {toFormatted} {toCurrency.ToUpper()}";

return new Result
{
Title = OutputStyle == 0 ? $"{toFormatted} {toCurrency.ToUpper()}" : $"{fromFormatted} {fromCurrency.ToUpper()} = {toFormatted} {toCurrency.ToUpper()}",
Title = OutputStyle == 0 ? compressedOutput : expandedOutput,
SubTitle = $"Currency conversion from {fromCurrency.ToUpper()} to {toCurrency.ToUpper()}",
QueryTextDisplay = $"{toFormatted} {toCurrency.ToUpper()}",
QueryTextDisplay = compressedOutput,
IcoPath = IconPath,
Action = e =>
{
Clipboard.SetText(toFormatted);
return true;
}
ContextData = toFormatted,
ToolTipData = new ToolTipData(compressedOutput, expandedOutput),
};
}

Expand Down Expand Up @@ -311,12 +314,7 @@ public double Evaluate(string expression)
}

string fromCurrency = match.Groups["from"].Value;
string toCurrency = "";

if (!string.IsNullOrEmpty(match.Groups["to"].Value))
{
toCurrency = match.Groups["to"].Value;
}
string toCurrency = string.IsNullOrEmpty(match.Groups["to"].Value) ? "" : match.Groups["to"].Value;

if (string.IsNullOrEmpty(fromCurrency))
{
Expand Down Expand Up @@ -383,31 +381,67 @@ public List<Result> Query(Query query)

public void Init(PluginInitContext context)
{
Context = context;
Context = context ?? throw new ArgumentNullException(nameof(context));
Context.API.ThemeChanged += OnThemeChanged;
UpdateIconPath(Context.API.GetCurrentTheme());
}

private void UpdateIconPath(Theme theme)
public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
{
if (theme == Theme.Light || theme == Theme.HighContrastWhite)
{
IconPath = "images/icon-black.png";
}
else
if (selectedResult?.ContextData is string characters)
{
IconPath = "images/icon-white.png";
return
[
new ContextMenuResult
{
PluginName = Name,
Title = "Copy (Enter)",
FontFamily = "Segoe Fluent Icons,Segoe MDL2 Assets",
Glyph = "\xE8C8",
AcceleratorKey = Key.Enter,
Action = _ => CopyToClipboard(characters.ToString()),
},
];
}

return [];
}

private void OnThemeChanged(Theme currentTheme, Theme newTheme)
private void UpdateIconPath(Theme theme) => IconPath = theme == Theme.Light || theme == Theme.HighContrastWhite ? Context?.CurrentPluginMetadata.IcoPathLight : Context?.CurrentPluginMetadata.IcoPathDark;

private void OnThemeChanged(Theme currentTheme, Theme newTheme) => UpdateIconPath(newTheme);

System.Windows.Controls.Control ISettingProvider.CreateSettingPanel() => throw new NotImplementedException();

public void Dispose()
{
UpdateIconPath(newTheme);
Dispose(true);
GC.SuppressFinalize(this);
}

public System.Windows.Controls.Control CreateSettingPanel()
protected virtual void Dispose(bool disposing)
{
throw new NotImplementedException();
if (Disposed || !disposing)
{
return;
}

if (Context?.API != null)
{
Context.API.ThemeChanged -= OnThemeChanged;
}

Disposed = true;
}

private static bool CopyToClipboard(string? value)
{
if (value != null)
{
Clipboard.SetText(value);
}

return true;
}
}
}
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.3",
"Version": "1.1.4",
"Language": "csharp",
"Website": "https://github.com/advaith3600/PowerToys-Run-Currency-Converter",
"ExecuteFileName": "Community.PowerToys.Run.Plugin.CurrencyConverter.dll",
Expand Down

0 comments on commit 1e8ba35

Please sign in to comment.