Skip to content

Commit

Permalink
Shared - Color Improvements + Start Currency Conversion Models
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Feb 2, 2024
1 parent 0f35e31 commit a8c4571
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 7 deletions.
2 changes: 2 additions & 0 deletions libdenaro/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ add_library(libdenaro
"src/controllers/preferencesviewcontroller.cpp"
"src/models/color.cpp"
"src/models/configuration.cpp"
"src/models/currencyconversion.cpp"
"src/models/currencyconversionservice.cpp"
"src/models/recentaccount.cpp")
target_include_directories(libdenaro PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
set_target_properties(libdenaro PROPERTIES VERSION "${PROJECT_VERSION}" SOVERSION "${PROJECT_VERSION}")
Expand Down
40 changes: 40 additions & 0 deletions libdenaro/include/models/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,46 @@ namespace Nickvision::Money::Shared::Models
* @return True if empty, else false
*/
bool empty() const;
/**
* @brief Gets the R color value.
* @return The R color value
*/
int getR() const;
/**
* @brief Sets the R color value.
* @param r The new R color value
*/
void setR(int r);
/**
* @brief Gets the G color value.
* @return The G color value
*/
int getG() const;
/**
* @brief Sets the G color value.
* @param g The new G color value
*/
void setG(int g);
/**
* @brief Gets the B color value.
* @return The B color value
*/
int getB() const;
/**
* @brief Sets the B color value.
* @param b The new B color value
*/
void setB(int b);
/**
* @brief Gets the A color value.
* @return The A color value
*/
int getA() const;
/**
* @brief Sets the A color value.
* @param a The new A color value
*/
void setA(int a);
/**
* @brief Gets the rgb string for this Color object.
* @param header Whether or not to wrap the string in the header: rgb()
Expand Down
56 changes: 56 additions & 0 deletions libdenaro/include/models/currencyconversion.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef CURRENCYCONVERSION_H
#define CURRENCYCONVERSION_H

#include <string>

namespace Nickvision::Money::Shared::Models
{
/**
* @brief A model of a currency conversion.
*/
class CurrencyConversion
{
public:
/**
* @brief Constructs a CurrencyConversion.
* @param sourceCurrency The currency code of the source amount
* @param sourceAmount The source amount to convert
* @param resultCurrency The currency code of the result amount
* @param conversionRate The rate of conversion from the source currency to the result currency
*/
CurrencyConversion(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency, double conversionRate);
/**
* @brief Gets the currency code of the source amount.
* @return The source currency code
*/
const std::string& getSourceCurrency() const;
/**
* @brief Gets the source amount to convert.
* @return The source amount
*/
double getSourceAmount() const;
/**
* @brief Gets the currency code of the result amount.
* @return The result currency code
*/
const std::string& getResultCurrency() const;
/**
* @brief Gets the rate of conversion from the source currency to the result currency.
* @return The conversion rate
*/
double getConversionRate() const;
/**
* @brief Gets the result amount using the conversion rate.
* @return The result amount
*/
double getResultAmount() const;

private:
std::string m_sourceCurrency;
double m_sourceAmount;
std::string m_resultCurrency;
double m_conversionRate;
};
}

#endif //CURRENTCONVERSION_H
28 changes: 28 additions & 0 deletions libdenaro/include/models/currencyconversionservice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef CURRENCYCONVERSIONSERVICE_H
#define CURRENCYCONVERSIONSERVICE_H

#include <string>
#include <optional>
#include <unordered_map>
#include "currencyconversion.h"

namespace Nickvision::Money::Shared::Models::CurrencyConversionService
{
/**
* @brief Converts a sourceAmount from the sourceCurrency to the resultCurrency.
* @param sourceCurrency The currency code of the source amount
* @param sourceAmount The source amount to convert
* @param resultCurrency The currency code of the result amount
* @return The CurrencyConversion object if successful, else std::nullopt
*/
std::optional<CurrencyConversion> convert(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency);
/**
* @brief Gets a map of conversion rates from the sourceCurrency to other currencies.
* @brief This method will cache the data for the sourceCurrency on disk.
* @param sourceCurrency The currency code to get converting rates for
* @return The map of conversion rates
*/
std::unordered_map<std::string, double> getConversionRates(const std::string& sourceCurrency);
}

#endif //CURRENCYCONVERSIONSERVICE_H
58 changes: 51 additions & 7 deletions libdenaro/src/models/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ namespace Nickvision::Money::Shared::Models
}

Color::Color(const std::string& s)
: m_r{ 0 },
m_g{ 0 },
m_b{ 0 },
m_a{ 0 }
{
if(s[0] == '#')
{
Expand Down Expand Up @@ -126,6 +130,46 @@ namespace Nickvision::Money::Shared::Models
return m_r == 0 && m_g == 0 && m_b == 0 && m_a == 0;
}

int Color::getR() const
{
return m_r;
}

void Color::setR(int r)
{
m_r = r;
}

int Color::getG() const
{
return m_g;
}

void Color::setG(int g)
{
m_g = g;
}

int Color::getB() const
{
return m_b;
}

void Color::setB(int b)
{
m_b = b;
}

int Color::getA() const
{
return m_a;
}

void Color::setA(int a)
{
m_a = a;
}

std::string Color::toRGBString(bool header) const
{
std::stringstream builder;
Expand Down Expand Up @@ -165,20 +209,20 @@ namespace Nickvision::Money::Shared::Models
{
std::stringstream builder;
builder << "#";
builder << std::hex << m_r;
builder << std::hex << m_g;
builder << std::hex << m_b;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_r;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_g;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_b;
return builder.str();
}

std::string Color::toRGBAHexString() const
{
std::stringstream builder;
builder << "#";
builder << std::hex << m_r;
builder << std::hex << m_g;
builder << std::hex << m_b;
builder << std::hex << m_a;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_r;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_g;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_b;
builder << std::setfill ('0') << std::setw(2) << std::hex << m_a;
return builder.str();
}

Expand Down
38 changes: 38 additions & 0 deletions libdenaro/src/models/currencyconversion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "models/currencyconversion.h"

namespace Nickvision::Money::Shared::Models
{
CurrencyConversion::CurrencyConversion(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency, double conversionRate)
: m_sourceCurrency{ sourceCurrency },
m_sourceAmount{ sourceAmount },
m_resultCurrency{ resultCurrency },
m_conversionRate{ m_conversionRate }
{

}

const std::string& CurrencyConversion::getSourceCurrency() const
{
return m_sourceCurrency;
}

double CurrencyConversion::getSourceAmount() const
{
return m_sourceAmount;
}

const std::string& CurrencyConversion::getResultCurrency() const
{
return m_resultCurrency;
}

double CurrencyConversion::getConversionRate() const
{
return m_conversionRate;
}

double CurrencyConversion::getResultAmount() const
{
return m_sourceAmount * m_conversionRate;
}
}
23 changes: 23 additions & 0 deletions libdenaro/src/models/currencyconversionservice.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "models/currencyconversionservice.h"

namespace Nickvision::Money::Shared::Models
{
std::optional<CurrencyConversion> CurrencyConversionService::convert(const std::string& sourceCurrency, double sourceAmount, const std::string& resultCurrency)
{
if(sourceCurrency == resultCurrency)
{
return CurrencyConversion{ sourceCurrency, sourceAmount, resultCurrency, 1 };
}
std::unordered_map<std::string, double> rates{ getConversionRates(sourceCurrency) };
if(rates.empty() || !rates.contains(resultCurrency))
{
return std::nullopt;
}
return CurrencyConversion{ sourceCurrency, sourceAmount, resultCurrency, rates[resultCurrency] };
}

std::unordered_map<std::string, double> CurrencyConversionService::getConversionRates(const std::string& sourceCurrency)
{
return {};
}
}

0 comments on commit a8c4571

Please sign in to comment.