diff --git a/src/controllers/accountviewcontroller.cpp b/src/controllers/accountviewcontroller.cpp index 887afbeb4..9ae1ce5f4 100644 --- a/src/controllers/accountviewcontroller.cpp +++ b/src/controllers/accountviewcontroller.cpp @@ -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() }; } diff --git a/src/controllers/transactiondialogcontroller.cpp b/src/controllers/transactiondialogcontroller.cpp index fa71e560a..a984afc48 100644 --- a/src/controllers/transactiondialogcontroller.cpp +++ b/src/controllers/transactiondialogcontroller.cpp @@ -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& groups) : m_response{ "cancel" }, m_currencySymbol{ currencySymbol }, m_transaction{ newId }, m_groups{ groups } { - + m_groupNames.push_back("None"); + for(const std::pair& 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& groups) : m_response{ "cancel" }, m_currencySymbol{ currencySymbol }, m_transaction{ transaction }, m_groups{ groups } { - + m_groupNames.push_back("None"); + for(const std::pair& pair : m_groups) + { + m_groupNames.push_back(pair.second.getName()); + } } const std::string& TransactionDialogController::getResponse() const @@ -70,6 +78,20 @@ int TransactionDialogController::getRepeatIntervalAsInt() const return static_cast(m_transaction.getRepeatInterval()); } +const std::vector& 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; @@ -77,7 +99,7 @@ std::string TransactionDialogController::getAmountAsString() const 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()) { @@ -100,6 +122,16 @@ TransactionCheckStatus TransactionDialogController::updateTransaction(const std: m_transaction.setDescription(description); m_transaction.setType(static_cast(type)); m_transaction.setRepeatInterval(static_cast(repeatInterval)); + if(groupIndex == 0) + { + m_transaction.setGroupId(-1); + } + else + { + std::map::iterator it{ m_groups.begin() }; + std::advance(it, groupIndex - 1); + m_transaction.setGroupId(it->second.getId()); + } m_transaction.setAmount(amount); return TransactionCheckStatus::Valid; } diff --git a/src/controllers/transactiondialogcontroller.hpp b/src/controllers/transactiondialogcontroller.hpp index 167d228c2..b65b22af2 100644 --- a/src/controllers/transactiondialogcontroller.hpp +++ b/src/controllers/transactiondialogcontroller.hpp @@ -1,6 +1,9 @@ #pragma once +#include #include +#include +#include "../models/group.hpp" #include "../models/transaction.hpp" namespace NickvisionMoney::Controllers @@ -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& 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& groups); /** * Gets the response of the dialog * @@ -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& 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; /** @@ -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 m_groups; + std::vector m_groupNames; }; } \ No newline at end of file diff --git a/src/models/account.cpp b/src/models/account.cpp index f61d3757b..56a466b65 100644 --- a/src/models/account.cpp +++ b/src/models/account.cpp @@ -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; diff --git a/src/ui/views/transactiondialog.cpp b/src/ui/views/transactiondialog.cpp index d8c424fa7..155e6e3bd 100644 --- a/src/ui/views/transactiondialog.cpp +++ b/src/ui/views/transactiondialog.cpp @@ -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); @@ -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()); } @@ -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) { diff --git a/src/ui/views/transactiondialog.hpp b/src/ui/views/transactiondialog.hpp index 5fbe2da33..b52ff2827 100644 --- a/src/ui/views/transactiondialog.hpp +++ b/src/ui/views/transactiondialog.hpp @@ -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