From 50848b2dd97093dd407ed7199118bca011f1aa4c Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 16 Aug 2025 15:36:17 +0200 Subject: more refactor work --- src/administration.cpp | 36 ++++++++++++++++++++++++++++-------- src/ui/helpers.cpp | 42 +++--------------------------------------- src/ui/ui_contacts.cpp | 2 +- src/ui/ui_invoices.cpp | 30 +++++++++++++++--------------- src/ui/ui_main.cpp | 1 - 5 files changed, 47 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/administration.cpp b/src/administration.cpp index 245809b..4d8233e 100644 --- a/src/administration.cpp +++ b/src/administration.cpp @@ -760,7 +760,7 @@ bool administration_billing_item_add_to_invoice(invoice* invoice, billing_item i snprintf(tb->id, sizeof(tb->id), "B/%d", administration_create_id()); strops_copy(tb->invoice_id, invoice->id, sizeof(tb->invoice_id)); list_append(&invoice->billing_items, tb); - strops_copy(tb->currency, invoice->currency, CURRENCY_LENGTH); // Set billing item currency to invoice currency. + strops_copy(tb->currency, invoice->currency, MAX_LEN_CURRENCY); // Set billing item currency to invoice currency. g_administration.next_id++; @@ -817,7 +817,7 @@ invoice administration_invoice_create_empty() result.delivered_at = time(NULL); result.expires_at = time(NULL) + administration_get_default_invoice_expire_duration(); list_init(&result.billing_items); // @leak - strops_copy(result.currency, administration_get_default_currency_for_country(g_administration.company_info.address.country_code), CURRENCY_LENGTH); + strops_copy(result.currency, administration_get_default_currency_for_country(g_administration.company_info.address.country_code), MAX_LEN_CURRENCY); return result; } @@ -944,13 +944,13 @@ bool administration_billing_item_update_in_invoice(invoice* invoice, billing_ite void administration_invoice_set_currency(invoice* invoice, char* currency) { - strops_copy(invoice->currency, currency, CURRENCY_LENGTH); + strops_copy(invoice->currency, currency, MAX_LEN_CURRENCY); list_iterator_start(&invoice->billing_items); while (list_iterator_hasnext(&invoice->billing_items)) { billing_item* c = (billing_item *)list_iterator_next(&invoice->billing_items); - strops_copy(c->currency, currency, CURRENCY_LENGTH); + strops_copy(c->currency, currency, MAX_LEN_CURRENCY); } list_iterator_stop(&invoice->billing_items); } @@ -1003,6 +1003,27 @@ bool administration_invoice_update(invoice* inv) return false; } +invoice administration_invoice_create_copy(invoice* inv) +{ + invoice new_inv = administration_invoice_create_empty(); + list_t billing_items = new_inv.billing_items; + + memcpy((void*)&new_inv, (void*)inv, sizeof(invoice)); + new_inv.billing_items = billing_items; + + list_iterator_start(&inv->billing_items); + while (list_iterator_hasnext(&inv->billing_items)) { + billing_item* c = (billing_item *)list_iterator_next(&inv->billing_items); + + billing_item* item_copy = (billing_item*)malloc(sizeof(billing_item)); + memcpy(item_copy, c, sizeof(billing_item)); + list_append(&new_inv.billing_items, item_copy); + } + list_iterator_stop(&inv->billing_items); + + return new_inv; +} + bool administration_invoice_add(invoice* inv) { if (!administration_invoice_is_valid(inv)) return false; @@ -1026,9 +1047,10 @@ bool administration_invoice_add(invoice* inv) memcpy(&inv->addressee, &inv->customer, sizeof(contact)); } + invoice copy = administration_invoice_create_copy(inv); // Create copy to make copy of billing item list. invoice* new_inv = (invoice*)malloc(sizeof(invoice)); - memcpy((void*)new_inv, (void*)inv, sizeof(invoice)); - + memcpy(new_inv, ©, sizeof(invoice)); + new_inv->status = invoice_status::INVOICE_CONCEPT; list_append(&g_administration.invoices, new_inv); @@ -1047,9 +1069,7 @@ u32 administration_invoice_count() u32 administration_invoice_get_partial_list(u32 page_index, u32 page_size, invoice* buffer) { assert(buffer); - u32 write_cursor = 0; - u32 read_start = page_index * page_size; list_iterator_start(&g_administration.invoices); diff --git a/src/ui/helpers.cpp b/src/ui/helpers.cpp index 0cce207..7bde384 100644 --- a/src/ui/helpers.cpp +++ b/src/ui/helpers.cpp @@ -3,43 +3,6 @@ #include "locales.hpp" #include "strops.hpp" -static float toast_timer = 0.0f; -static const char* toast_msg = nullptr; -void ui_helper_show_toast(const char* msg) -{ - toast_msg = msg; - toast_timer = 3.0f; -} - -void ui_helper_draw_toasts() -{ - if (toast_timer > 0.0f && toast_msg) - { - toast_timer -= ImGui::GetIO().DeltaTime; - - const float pad = 10.0f; - const ImVec2 window_pos = ImVec2( - ImGui::GetIO().DisplaySize.x - pad, - ImGui::GetIO().DisplaySize.y - pad - ); - const ImVec2 window_pos_pivot = ImVec2(1.0f, 1.0f); - - ImGui::SetNextWindowPos(window_pos, ImGuiCond_Always, window_pos_pivot); - ImGui::SetNextWindowBgAlpha(0.85f); // semi-transparent - - ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | - ImGuiWindowFlags_AlwaysAutoResize | - ImGuiWindowFlags_NoFocusOnAppearing | - ImGuiWindowFlags_NoNav; - - if (ImGui::Begin("##Toast", nullptr, flags)) - { - ImGui::TextUnformatted(toast_msg); - } - ImGui::End(); - } -} - void ui_helper_draw_required_tag() { ImDrawList* draw_list = ImGui::GetWindowDrawList(); @@ -65,7 +28,7 @@ void ui_helper_draw_required_tag() ImGui::PopStyleColor(); } -int ui_helper_TextInputWithAutocomplete(const char* label, const char* hint, char* buffer, size_t buf_size, +int TextInputWithAutocomplete(const char* label, const char* hint, char* buffer, size_t buf_size, char* suggestions[], int suggestion_count) { int result = -1; @@ -110,4 +73,5 @@ int ui_helper_TextInputWithAutocomplete(const char* label, const char* hint, cha } } return result; -} \ No newline at end of file +} + diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp index 4db726d..a55fd73 100644 --- a/src/ui/ui_contacts.cpp +++ b/src/ui/ui_contacts.cpp @@ -102,7 +102,7 @@ void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_ autocomplete_strings[i] = autocomplete_list[i].name; } - int autocomplete_index = ui_helper_TextInputWithAutocomplete(localize("contact.form.fullname"), localize("contact.form.fullname"), + int autocomplete_index = TextInputWithAutocomplete(localize("contact.form.fullname"), localize("contact.form.fullname"), buffer->name, IM_ARRAYSIZE(buffer->name), (char**)autocomplete_strings, autocomplete_count); if (on_autocomplete) { diff --git a/src/ui/ui_invoices.cpp b/src/ui/ui_invoices.cpp index 0256fbd..c08dd76 100644 --- a/src/ui/ui_invoices.cpp +++ b/src/ui/ui_invoices.cpp @@ -39,8 +39,6 @@ void ui_setup_invoices() tax_bracket_list_buffer = (country_tax_bracket*) malloc(sizeof(country_tax_bracket) * tax_bracket_count); // @leak } -// TODO move custom ui functions to helpers.cpp - void draw_tax_bracket_selector(char* tax_bracket_id) { country_tax_bracket* selected_tax_bracket = NULL; @@ -134,7 +132,7 @@ bool draw_currency_selector(char* currency) if (ImGui::Selectable(currencies[n], isSelected)) { result = true; - strops_copy(currency, currencies[n], CURRENCY_LENGTH); + strops_copy(currency, currencies[n], MAX_LEN_CURRENCY); } if (isSelected) @@ -349,7 +347,7 @@ static void draw_invoice_items_form(invoice* invoice) free(buffer); } -void draw_invoice_form(invoice* buffer, bool viewing_only = false) +static void draw_invoice_form(invoice* buffer, bool viewing_only = false) { ImGui::BeginDisabled(); @@ -447,7 +445,7 @@ void draw_invoice_form(invoice* buffer, bool viewing_only = false) if (viewing_only) ImGui::EndDisabled(); } -void draw_invoices_list() +static void ui_draw_invoices_list() { const u32 items_per_page = 50; static s32 current_page = 0; @@ -529,7 +527,7 @@ void draw_invoices_list() { snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.change"), i); if (ImGui::Button(btn_name)) { - active_invoice = c; + active_invoice = administration_invoice_create_copy(&c); // We create a copy because of billing item list pointers. current_view_state = view_state::EDIT; } } @@ -612,20 +610,22 @@ static void ui_draw_invoice_create() if (!can_save) ImGui::EndDisabled(); } +static void ui_draw_invoice_view() +{ + if (ImGui::Button(localize("form.back"))) { + current_view_state = view_state::LIST; + } + + draw_invoice_form(&active_invoice, true); +} + void ui_draw_invoices() { switch(current_view_state) { - case view_state::LIST: draw_invoices_list(); break; + case view_state::LIST: ui_draw_invoices_list(); break; case view_state::CREATE: ui_draw_invoice_create(); break; case view_state::EDIT: ui_draw_invoice_update(); break; - case view_state::VIEW: - { - if (ImGui::Button(localize("form.back"))) { - current_view_state = view_state::LIST; - } - - draw_invoice_form(&active_invoice, true); - } break; + case view_state::VIEW: ui_draw_invoice_view(); break; } } \ No newline at end of file diff --git a/src/ui/ui_main.cpp b/src/ui/ui_main.cpp index 2d79129..d8fd5d4 100644 --- a/src/ui/ui_main.cpp +++ b/src/ui/ui_main.cpp @@ -108,7 +108,6 @@ void ui_draw_main() // Main content ImGui::Begin("AccountingMainWindow", nullptr, ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoCollapse); if (drawcalls[dashboard_state]) drawcalls[dashboard_state](); - ui_helper_draw_toasts(); ImGui::End(); // Status bar. -- cgit v1.2.3-70-g09d2