Skip to content

Commit

Permalink
Fully Implement Groups
Browse files Browse the repository at this point in the history
  • Loading branch information
nlogozzo committed Nov 6, 2022
1 parent 92b6bff commit f10bfe0
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/controllers/accountviewcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ void AccountViewController::deleteTransaction(unsigned int id)

TransactionDialogController AccountViewController::createTransactionDialogController() const
{
return { m_account.getNextAvailableTransactionId(), m_currencySymbol };
return { m_account.getNextAvailableTransactionId(), m_currencySymbol, m_account.getGroups() };
}

TransactionDialogController AccountViewController::createTransactionDialogController(unsigned int id) const
{
return { m_account.getTransactionById(id).value(), m_currencySymbol };
return { m_account.getTransactionById(id).value(), m_currencySymbol, m_account.getGroups() };
}
42 changes: 37 additions & 5 deletions src/controllers/transactiondialogcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@
using namespace NickvisionMoney::Controllers;
using namespace NickvisionMoney::Models;

TransactionDialogController::TransactionDialogController(unsigned int newId, const std::string& currencySymbol) : m_response{ "cancel" }, m_currencySymbol{ currencySymbol }, m_transaction{ newId }
TransactionDialogController::TransactionDialogController(unsigned int newId, const std::string& currencySymbol, const std::map<unsigned int, Group>& groups) : m_response{ "cancel" }, m_currencySymbol{ currencySymbol }, m_transaction{ newId }, m_groups{ groups }
{

m_groupNames.push_back("None");
for(const std::pair<const unsigned int, Group>& pair : m_groups)
{
m_groupNames.push_back(pair.second.getName());
}
}

TransactionDialogController::TransactionDialogController(const Transaction& transaction, const std::string& currencySymbol) : m_response{ "cancel" }, m_currencySymbol{ currencySymbol }, m_transaction{ transaction }
TransactionDialogController::TransactionDialogController(const Transaction& transaction, const std::string& currencySymbol, const std::map<unsigned int, Group>& groups) : m_response{ "cancel" }, m_currencySymbol{ currencySymbol }, m_transaction{ transaction }, m_groups{ groups }
{

m_groupNames.push_back("None");
for(const std::pair<const unsigned int, Group>& pair : m_groups)
{
m_groupNames.push_back(pair.second.getName());
}
}

const std::string& TransactionDialogController::getResponse() const
Expand Down Expand Up @@ -70,14 +78,28 @@ int TransactionDialogController::getRepeatIntervalAsInt() const
return static_cast<int>(m_transaction.getRepeatInterval());
}

const std::vector<std::string>& TransactionDialogController::getGroupNames() const
{
return m_groupNames;
}

int TransactionDialogController::getGroupAsIndex() const
{
if(m_transaction.getGroupId() == -1)
{
return 0;
}
return std::distance(m_groups.begin(), m_groups.find(m_transaction.getGroupId())) + 1;
}

std::string TransactionDialogController::getAmountAsString() const
{
std::stringstream builder;
builder << m_transaction.getAmount();
return builder.str();
}

TransactionCheckStatus TransactionDialogController::updateTransaction(const std::string& dateString, const std::string& description, int type, int repeatInterval, const std::string& amountString)
TransactionCheckStatus TransactionDialogController::updateTransaction(const std::string& dateString, const std::string& description, int type, int repeatInterval, int groupIndex, const std::string& amountString)
{
if(description.empty())
{
Expand All @@ -100,6 +122,16 @@ TransactionCheckStatus TransactionDialogController::updateTransaction(const std:
m_transaction.setDescription(description);
m_transaction.setType(static_cast<TransactionType>(type));
m_transaction.setRepeatInterval(static_cast<RepeatInterval>(repeatInterval));
if(groupIndex == 0)
{
m_transaction.setGroupId(-1);
}
else
{
std::map<unsigned int, Group>::iterator it{ m_groups.begin() };
std::advance(it, groupIndex - 1);
m_transaction.setGroupId(it->second.getId());
}
m_transaction.setAmount(amount);
return TransactionCheckStatus::Valid;
}
42 changes: 39 additions & 3 deletions src/controllers/transactiondialogcontroller.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <map>
#include <string>
#include <vector>
#include "../models/group.hpp"
#include "../models/transaction.hpp"

namespace NickvisionMoney::Controllers
Expand Down Expand Up @@ -28,14 +31,14 @@ namespace NickvisionMoney::Controllers
* @param newId The Id of the new transaction
* @param currencySymbol The currency symbol
*/
TransactionDialogController(unsigned int newId, const std::string& currencySymbol);
TransactionDialogController(unsigned int newId, const std::string& currencySymbol, const std::map<unsigned int, NickvisionMoney::Models::Group>& groups);
/**
* Constructs a TransactionDialogController
*
* @param transaction The transaction to update
* @param currencySymbol The currency symbol
*/
TransactionDialogController(const NickvisionMoney::Models::Transaction& transaction, const std::string& currencySymbol);
TransactionDialogController(const NickvisionMoney::Models::Transaction& transaction, const std::string& currencySymbol, const std::map<unsigned int, NickvisionMoney::Models::Group>& groups);
/**
* Gets the response of the dialog
*
Expand All @@ -56,38 +59,68 @@ namespace NickvisionMoney::Controllers
const std::string& getCurrencySymbol() const;
/**
* Gets the transaction managed by the dialog
*
* @returns The transaction
*/
const NickvisionMoney::Models::Transaction& getTransaction() const;
/**
* Gets the id of the transaction as a string
*
* @returns The id of the transaction
*/
std::string getIdAsString() const;
/**
* Gets the year of the date of the transaction
*
* @returns The year of the date of the transaction
*/
int getYear() const;
/**
* Gets the month of the date of the transaction
*
* @returns The month of the date of the transaction
*/
int getMonth() const;
/**
* Gets the day of the date of the transaction
*
* @returns The day of the date of the transaction
*/
int getDay() const;
/**
* Gets the description of the transaction
*
* @returns The description of the transaction
*/
const std::string& getDescription() const;
/**
* Gets the type of the transaction as an int
*
* @returns The type of the transaction
*/
int getTypeAsInt() const;
/**
* Gets the repeat interval of the transaction as an int
*
* @returns The repeat interval of the transaction
*/
int getRepeatIntervalAsInt() const;
/**
* Get a list of the group names
*
* @returns The list of group names
*/
const std::vector<std::string>& getGroupNames() const;
/**
* Gets the group of the transaction as an index
*
* @returns The index of the group of the transaction
*/
int getGroupAsIndex() const;
/**
* Gets the amount of the transaction as a string
*
* @returns The amount of the transaction
*/
std::string getAmountAsString() const;
/**
Expand All @@ -97,14 +130,17 @@ namespace NickvisionMoney::Controllers
* @param description The description
* @param type The type as an int
* @param repeatInterval The repeat interval as an int
* @param groupIndex The index of the group
* @param amountString The amount string
* @returns The TransactionCheckStatus
*/
TransactionCheckStatus updateTransaction(const std::string& dateString, const std::string& description, int type, int repeatInterval, const std::string& amountString);
TransactionCheckStatus updateTransaction(const std::string& dateString, const std::string& description, int type, int repeatInterval, int groupIndex, const std::string& amountString);

private:
std::string m_response;
std::string m_currencySymbol;
NickvisionMoney::Models::Transaction m_transaction;
std::map<unsigned int, NickvisionMoney::Models::Group> m_groups;
std::vector<std::string> m_groupNames;
};
}
5 changes: 1 addition & 4 deletions src/models/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,7 @@ bool Account::updateTransaction(const Transaction& transaction)
if(qryUpdate.exec() > 0)
{
m_transactions[transaction.getId()] = transaction;
if(transaction.getGroupId() != -1)
{
updateGroupAmounts();
}
updateGroupAmounts();
return true;
}
return false;
Expand Down
14 changes: 13 additions & 1 deletion src/ui/views/transactiondialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ TransactionDialog::TransactionDialog(GtkWindow* parent, NickvisionMoney::Control
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowRepeatInterval), "Repeat Interval");
adw_combo_row_set_model(ADW_COMBO_ROW(m_rowRepeatInterval), G_LIST_MODEL(gtk_string_list_new(new const char*[8]{ "Never", "Daily", "Weekly", "Monthly", "Quarterly", "Yearly", "Biyearly", nullptr })));
adw_preferences_group_add(ADW_PREFERENCES_GROUP(m_preferencesGroup), m_rowRepeatInterval);
//Group
m_rowGroup = adw_combo_row_new();
adw_preferences_row_set_title(ADW_PREFERENCES_ROW(m_rowGroup), "Group");
const char** groupNames{ new const char*[m_controller.getGroupNames().size() + 1] };
for(size_t i = 0; i < m_controller.getGroupNames().size(); i++)
{
groupNames[i] = m_controller.getGroupNames()[i].c_str();
}
groupNames[m_controller.getGroupNames().size()] = nullptr;
adw_combo_row_set_model(ADW_COMBO_ROW(m_rowGroup), G_LIST_MODEL(gtk_string_list_new(groupNames)));
adw_preferences_group_add(ADW_PREFERENCES_GROUP(m_preferencesGroup), m_rowGroup);
//Amount
m_rowAmount = adw_entry_row_new();
gtk_widget_set_size_request(m_rowAmount, 420, -1);
Expand All @@ -66,6 +77,7 @@ TransactionDialog::TransactionDialog(GtkWindow* parent, NickvisionMoney::Control
gtk_editable_set_text(GTK_EDITABLE(m_rowDescription), m_controller.getDescription().c_str());
adw_combo_row_set_selected(ADW_COMBO_ROW(m_rowType), m_controller.getTypeAsInt());
adw_combo_row_set_selected(ADW_COMBO_ROW(m_rowRepeatInterval), m_controller.getRepeatIntervalAsInt());
adw_combo_row_set_selected(ADW_COMBO_ROW(m_rowGroup), m_controller.getGroupAsIndex());
gtk_editable_set_text(GTK_EDITABLE(m_rowAmount), m_controller.getAmountAsString().c_str());
}

Expand All @@ -85,7 +97,7 @@ bool TransactionDialog::run()
if(m_controller.getResponse() == "ok")
{
gtk_widget_hide(m_gobj);
TransactionCheckStatus status{ m_controller.updateTransaction(g_date_time_format(gtk_calendar_get_date(GTK_CALENDAR(m_calendarDate)), "%Y-%m-%d"), gtk_editable_get_text(GTK_EDITABLE(m_rowDescription)), adw_combo_row_get_selected(ADW_COMBO_ROW(m_rowType)), adw_combo_row_get_selected(ADW_COMBO_ROW(m_rowRepeatInterval)), gtk_editable_get_text(GTK_EDITABLE(m_rowAmount))) };
TransactionCheckStatus status{ m_controller.updateTransaction(g_date_time_format(gtk_calendar_get_date(GTK_CALENDAR(m_calendarDate)), "%Y-%m-%d"), gtk_editable_get_text(GTK_EDITABLE(m_rowDescription)), adw_combo_row_get_selected(ADW_COMBO_ROW(m_rowType)), adw_combo_row_get_selected(ADW_COMBO_ROW(m_rowRepeatInterval)), adw_combo_row_get_selected(ADW_COMBO_ROW(m_rowGroup)), gtk_editable_get_text(GTK_EDITABLE(m_rowAmount))) };
//Invalid Transaction
if(status != TransactionCheckStatus::Valid)
{
Expand Down
1 change: 1 addition & 0 deletions src/ui/views/transactiondialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ namespace NickvisionMoney::UI::Views
GtkWidget* m_rowDescription{ nullptr };
GtkWidget* m_rowType{ nullptr };
GtkWidget* m_rowRepeatInterval{ nullptr };
GtkWidget* m_rowGroup{ nullptr };
GtkWidget* m_rowAmount{ nullptr };
/**
* Sets the response
Expand Down

0 comments on commit f10bfe0

Please sign in to comment.