Skip to content

Commit

Permalink
All - Better Currency String Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Mar 13, 2024
1 parent 1a20c9a commit 88579c3
Show file tree
Hide file tree
Showing 36 changed files with 151 additions and 114 deletions.
2 changes: 1 addition & 1 deletion docs/po/denaro.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:11-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
7 changes: 7 additions & 0 deletions libdenaro/include/helpers/currencyhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ namespace Nickvision::Money::Shared::CurrencyHelpers
* @return The currency string for the amount
*/
std::string toAmountString(double amount, const Models::Currency& currency, bool showCurrencySymbol = true, bool overwriteDecimal = false);
/**
* @brief Converts a currency string to an amount.
* @param amount The amount string to convert
* @param currency The currency to use to convert the string
* @return The amount for the currency string
*/
double toAmount(std::string amount, const Models::Currency& currency);
}

#endif //CURRENCYHELPERS_H
16 changes: 16 additions & 0 deletions libdenaro/src/helpers/currencyhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#include <locale>
#include <memory>
#include <sstream>
#include <libnick/helpers/stringhelpers.h>

using namespace Nickvision;
using namespace Nickvision::Money::Shared::Models;

class NumberFormat : public std::numpunct<char>
Expand Down Expand Up @@ -110,4 +112,18 @@ namespace Nickvision::Money::Shared
}
return builder.str();
}

double CurrencyHelpers::toAmount(std::string amount, const Currency& currency)
{
if(amount.find(currency.getSymbol()) != std::string::npos)
{
amount = StringHelpers::replace(amount, currency.getSymbol(), "");
}
std::stringstream builder;
builder.imbue({ builder.getloc(), new NumberFormat(currency) });
builder << amount;
double result{ 0 };
builder >> result;
return result;
}
}
4 changes: 4 additions & 0 deletions libdenaro/src/models/currency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ namespace Nickvision::Money::Shared::Models
void Currency::setCode(const std::string& code)
{
m_code = code;
if(m_code[m_code.size() - 1] == ' ')
{
m_code = m_code.substr(0, m_code.size() - 1);
}
}

char Currency::getDecimalSeparator() const
Expand Down
26 changes: 20 additions & 6 deletions libdenaro/tests/currencytests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ TEST(CurrencyHelpers, GetUSCurrency)
ASSERT_EQ(currency.getAmountStyle(), Models::AmountStyle::SymbolNumber);
}

TEST(CurrencyHelpers, USAmountString1)
TEST(CurrencyHelpers, ToUSAmountString1)
{
ASSERT_EQ(CurrencyHelpers::toAmountString(12347.89, CurrencyHelpers::getSystemCurrency()), "$12,347.89");
}

TEST(CurrencyHelpers, USAmountString2)
TEST(CurrencyHelpers, ToUSAmountString2)
{
ASSERT_EQ(CurrencyHelpers::toAmountString(100, CurrencyHelpers::getSystemCurrency()), "$100.00");
}

TEST(CurrencyHelpers, USAmountString3)
TEST(CurrencyHelpers, ToUSAmountString3)
{
ASSERT_EQ(CurrencyHelpers::toAmountString(5.50, CurrencyHelpers::getSystemCurrency(), false), "5.50");
}

TEST(CurrencyHelpers, CustomCurrency1)
TEST(CurrencyHelpers, FromUSAmountString1)
{
ASSERT_EQ(CurrencyHelpers::toAmount("$12,347.89", CurrencyHelpers::getSystemCurrency()), 12347.89);
}

TEST(CurrencyHelpers, ToCustomCurrency1)
{
Currency currency{ "#", "HAS" };
currency.setDecimalSeparator(',');
Expand All @@ -40,7 +45,16 @@ TEST(CurrencyHelpers, CustomCurrency1)
ASSERT_EQ(CurrencyHelpers::toAmountString(12347.89, currency, false), "12.347,89");
}

TEST(CurrencyHelpers, CustomCurrency2)
TEST(CurrencyHelpers, FromCustomCurrency1)
{
Currency currency{ "#", "HAS" };
currency.setDecimalSeparator(',');
currency.setGroupSeparator('.');
currency.setAmountStyle(Models::AmountStyle::NumberSpaceSymbol);
ASSERT_EQ(CurrencyHelpers::toAmount("1.567,21 #", currency), 1567.21);
}

TEST(CurrencyHelpers, ToCustomCurrency2)
{
Currency currency{ "@", "HAS" };
currency.setDecimalSeparator('-');
Expand All @@ -50,7 +64,7 @@ TEST(CurrencyHelpers, CustomCurrency2)
ASSERT_EQ(CurrencyHelpers::toAmountString(2765.9, currency), "@ 2*765-90");
}

TEST(CurrencyHelpers, CustomCurrency3)
TEST(CurrencyHelpers, ToCustomCurrency3)
{
Currency currency{ "@", "HAS" };
currency.setDecimalSeparator('-');
Expand Down
4 changes: 4 additions & 0 deletions org.nickvision.money.gnome/include/views/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ namespace Nickvision::Money::GNOME::Views
* @brief Opens the application's keyboard shortcut dialog.
*/
void keyboardShortcuts();
/**
* @brief Opens the application's help documentation.
*/
void help();
/**
* @brief Opens the application's about dialog.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,14 @@ namespace Nickvision::Money::GNOME::Controls
{
std::string sourceText{ gtk_editable_get_text(GTK_EDITABLE(gtk_builder_get_object(m_builder, "sourceAmountRow"))) };
std::string resultText{ gtk_editable_get_text(GTK_EDITABLE(gtk_builder_get_object(m_builder, "resultAmountRow"))) };
gtk_widget_remove_css_class(GTK_WIDGET(gtk_builder_get_object(m_builder, "sourceAmountRow")), "error");
gtk_widget_remove_css_class(GTK_WIDGET(gtk_builder_get_object(m_builder, "resultAmountRow")), "error");
if(sourceText.empty())
{
gtk_editable_set_text(GTK_EDITABLE(gtk_builder_get_object(m_builder, "resultAmountRow")), "");
}
else
{
double sourceAmount{ 0 };
try
{
sourceAmount = std::stod(sourceText);
}
catch(const std::exception&)
{
gtk_widget_add_css_class(GTK_WIDGET(gtk_builder_get_object(m_builder, "sourceAmountRow")), "error");
gtk_editable_set_text(GTK_EDITABLE(gtk_builder_get_object(m_builder, "resultAmountRow")), "");
return;
}
double sourceAmount{ CurrencyHelpers::toAmount(sourceText, CurrencyHelpers::getSystemCurrency()) };
std::string sourceCurrency{ gtk_string_list_get_string(m_currencyList, adw_combo_row_get_selected(ADW_COMBO_ROW(gtk_builder_get_object(m_builder, "sourceCurrencyRow")))) };
std::string resultCurrency{ gtk_string_list_get_string(m_currencyList, adw_combo_row_get_selected(ADW_COMBO_ROW(gtk_builder_get_object(m_builder, "resultCurrencyRow")))) };
std::optional<CurrencyConversion> conversion{ CurrencyConversionService::convert(sourceCurrency, sourceAmount, resultCurrency) };
Expand Down
18 changes: 16 additions & 2 deletions org.nickvision.money.gnome/src/views/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
#include <libnick/app/appinfo.h>
#include <libnick/notifications/shellnotification.h>
#include <libnick/localization/gettext.h>
#include <libnick/localization/documentation.h>
#include "controls/currencyconverterdialog.h"
#include "helpers/builder.h"
#include "views/preferencesdialog.h"

using namespace Nickvision::Money::GNOME::Controls;
using namespace Nickvision::Money::Shared::Controllers;
using namespace Nickvision::App;
using namespace Nickvision::Events;
using namespace Nickvision::Localization;
using namespace Nickvision::Money::GNOME::Controls;
using namespace Nickvision::Money::Shared::Controllers;
using namespace Nickvision::Notifications;

namespace Nickvision::Money::GNOME::Views
Expand Down Expand Up @@ -53,6 +55,11 @@ namespace Nickvision::Money::GNOME::Views
g_signal_connect(actKeyboardShortcuts, "activate", G_CALLBACK(+[](GSimpleAction*, GVariant*, gpointer data){ reinterpret_cast<MainWindow*>(data)->keyboardShortcuts(); }), this);
g_action_map_add_action(G_ACTION_MAP(m_window), G_ACTION(actKeyboardShortcuts));
SET_ACCEL_FOR_ACTION(m_app, "win.keyboardShortcuts", "<Ctrl>question");
//Preferences Action
GSimpleAction* actHelp{ g_simple_action_new("help", nullptr) };
g_signal_connect(actHelp, "activate", G_CALLBACK(+[](GSimpleAction*, GVariant*, gpointer data){ reinterpret_cast<MainWindow*>(data)->help(); }), this);
g_action_map_add_action(G_ACTION_MAP(m_window), G_ACTION(actHelp));
SET_ACCEL_FOR_ACTION(m_app, "win.help", "F1");
//About Action
GSimpleAction* actAbout{ g_simple_action_new("about", nullptr) };
g_signal_connect(actAbout, "activate", G_CALLBACK(+[](GSimpleAction*, GVariant*, gpointer data){ reinterpret_cast<MainWindow*>(data)->about(); }), this);
Expand Down Expand Up @@ -123,6 +130,13 @@ namespace Nickvision::Money::GNOME::Views
gtk_window_present(GTK_WINDOW(shortcuts));
}

void MainWindow::help()
{
std::string helpUrl{ Documentation::getHelpUrl("index") };
GtkUriLauncher* launcher{ gtk_uri_launcher_new(helpUrl.c_str()) };
gtk_uri_launcher_launch(launcher, GTK_WINDOW(m_window), nullptr, GAsyncReadyCallback(+[](GObject* source, GAsyncResult* res, gpointer) { gtk_uri_launcher_launch_finish(GTK_URI_LAUNCHER(source), res, nullptr); }), nullptr);
}

void MainWindow::about()
{
std::string extraDebug;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,14 @@ namespace winrt::Nickvision::Money::WinUI::Controls::implementation

void CurrencyConverterDialog::onCurrencyChange()
{
RowSourceCurrency().Title(winrt::to_hstring(_("Source")));
RowResultCurrency().Title(winrt::to_hstring(_("Result")));
if(TxtSourceAmount().Text().empty())
{
TxtResultAmount().Text(L"");
}
else
{
double sourceAmount{ 0 };
try
{
sourceAmount = std::stod(winrt::to_string(TxtSourceAmount().Text()));
}
catch(const std::exception&)
{
RowSourceCurrency().Title(winrt::to_hstring(_("Source (Error)")));
TxtResultAmount().Text(L"");
return;
}
double sourceAmount{ CurrencyHelpers::toAmount(winrt::to_string(TxtSourceAmount().Text()), CurrencyHelpers::getSystemCurrency())};
std::string sourceCurrency{ winrt::to_string(CmbSourceCurrency().SelectedItem().as<winrt::hstring>()) };
std::string resultCurrency{ winrt::to_string(CmbResultCurrency().SelectedItem().as<winrt::hstring>()) };
std::optional<CurrencyConversion> conversion{ CurrencyConversionService::convert(sourceCurrency, sourceAmount, resultCurrency) };
Expand Down
6 changes: 3 additions & 3 deletions resources/po/ar.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:03-0400\n"
"PO-Revision-Date: 2023-12-06 07:04+0000\n"
"Last-Translator: ButterflyOfFire <[email protected]>\n"
"Language-Team: Arabic <https://hosted.weblate.org/projects/nickvision-money/"
Expand Down Expand Up @@ -369,12 +369,12 @@ msgstr "أغلق"
msgid "Result was copied to clipboard."
msgstr "تم نسخ النتائج إلى الحافظة."

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:94
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:101
#: org.nickvision.money.winui/MainWindow.xaml.cpp:82
msgid "Open"
msgstr "افتح"

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:146
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:160
#: org.nickvision.money.winui/MainWindow.xaml.cpp:75
msgid "GitHub Repo"
msgstr "مستودع جت‌هب"
Expand Down
6 changes: 3 additions & 3 deletions resources/po/cs.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:03-0400\n"
"PO-Revision-Date: 2023-11-17 11:26+0000\n"
"Last-Translator: Fjuro <[email protected]>\n"
"Language-Team: Czech <https://hosted.weblate.org/projects/nickvision-money/"
Expand Down Expand Up @@ -371,12 +371,12 @@ msgstr "Zavřít"
msgid "Result was copied to clipboard."
msgstr "Výsledek zkopírován do schránky."

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:94
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:101
#: org.nickvision.money.winui/MainWindow.xaml.cpp:82
msgid "Open"
msgstr "Otevřít"

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:146
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:160
#: org.nickvision.money.winui/MainWindow.xaml.cpp:75
msgid "GitHub Repo"
msgstr "Repozitář GitHub"
Expand Down
6 changes: 3 additions & 3 deletions resources/po/da.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:03-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -370,12 +370,12 @@ msgstr "Luk"
msgid "Result was copied to clipboard."
msgstr ""

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:94
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:101
#: org.nickvision.money.winui/MainWindow.xaml.cpp:82
msgid "Open"
msgstr "Åbn"

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:146
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:160
#: org.nickvision.money.winui/MainWindow.xaml.cpp:75
msgid "GitHub Repo"
msgstr "GitHub Depot"
Expand Down
6 changes: 3 additions & 3 deletions resources/po/de.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:03-0400\n"
"PO-Revision-Date: 2023-08-29 18:53+0000\n"
"Last-Translator: Ettore Atalan <[email protected]>\n"
"Language-Team: German <https://hosted.weblate.org/projects/nickvision-money/"
Expand Down Expand Up @@ -375,12 +375,12 @@ msgstr "Schließen"
msgid "Result was copied to clipboard."
msgstr "Ergebnis wurde in die Zwischenablage kopiert."

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:94
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:101
#: org.nickvision.money.winui/MainWindow.xaml.cpp:82
msgid "Open"
msgstr "Öffnen"

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:146
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:160
#: org.nickvision.money.winui/MainWindow.xaml.cpp:75
msgid "GitHub Repo"
msgstr "GitHub-Repo"
Expand Down
6 changes: 3 additions & 3 deletions resources/po/denaro.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:11-0400\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -352,12 +352,12 @@ msgstr ""
msgid "Result was copied to clipboard."
msgstr ""

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:94
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:101
#: org.nickvision.money.winui/MainWindow.xaml.cpp:82
msgid "Open"
msgstr ""

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:146
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:160
#: org.nickvision.money.winui/MainWindow.xaml.cpp:75
msgid "GitHub Repo"
msgstr ""
Expand Down
6 changes: 3 additions & 3 deletions resources/po/es.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-03-12 19:14-0400\n"
"POT-Creation-Date: 2024-03-12 22:03-0400\n"
"PO-Revision-Date: 2023-12-29 06:12+0000\n"
"Last-Translator: Óscar Fernández Díaz <[email protected]."
"weblate.org>\n"
Expand Down Expand Up @@ -375,12 +375,12 @@ msgstr "Cerrar"
msgid "Result was copied to clipboard."
msgstr "Resultado copiado al portapapeles."

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:94
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:101
#: org.nickvision.money.winui/MainWindow.xaml.cpp:82
msgid "Open"
msgstr "Abrir"

#: org.nickvision.money.gnome/src/views/mainwindow.cpp:146
#: org.nickvision.money.gnome/src/views/mainwindow.cpp:160
#: org.nickvision.money.winui/MainWindow.xaml.cpp:75
msgid "GitHub Repo"
msgstr "Repositorio de GitHub"
Expand Down
Loading

0 comments on commit 88579c3

Please sign in to comment.