From eecdd3acda59c159e299834af6b1750408735d7d Mon Sep 17 00:00:00 2001 From: gullradriel <3157857+gullradriel@users.noreply.github.com> Date: Wed, 7 Jun 2023 10:17:55 +0200 Subject: [PATCH] fixing edit freq,edit desc, delete freq (#1117) * fixing edit freq,edit desc, delete freq * fixing scrolling issues * fixing indent --- firmware/application/apps/ui_freqman.cpp | 136 +++++++++++------------ firmware/application/apps/ui_freqman.hpp | 11 +- firmware/application/freqman.cpp | 39 +------ firmware/application/freqman.hpp | 1 - firmware/application/ui/ui_freqlist.cpp | 15 ++- firmware/application/ui/ui_freqlist.hpp | 3 +- 6 files changed, 89 insertions(+), 116 deletions(-) diff --git a/firmware/application/apps/ui_freqman.cpp b/firmware/application/apps/ui_freqman.cpp index 65c8a7309..2e6be9294 100644 --- a/firmware/application/apps/ui_freqman.cpp +++ b/firmware/application/apps/ui_freqman.cpp @@ -29,24 +29,22 @@ using namespace portapack; namespace ui { -static int32_t last_category_id{0}; +static int32_t current_category_id = 0; FreqManBaseView::FreqManBaseView( NavigationView& nav) : nav_(nav) { - file_list = get_freqman_files(); - - add_children({&label_category, + add_children({&options_category, + &label_category, &button_exit}); - if (file_list.size()) { - add_child(&options_category); - populate_categories(); - } else - error_ = ERROR_NOFILES; - // initialize - change_category(last_category_id); + refresh_list(); + + options_category.on_change = [this](size_t category_id, int32_t) { + if (on_change_category) + on_change_category(category_id); + }; // Default function on_change_category = [this](int32_t category_id) { @@ -70,8 +68,44 @@ void FreqManBaseView::focus() { } } -void FreqManBaseView::populate_categories() { +void FreqManBaseView::get_freqman_files() { + std::vector().swap(file_list); + + auto files = scan_root_files(u"FREQMAN", u"*.TXT"); + + for (auto file : files) { + std::string file_name = file.stem().string(); + // don't propose tmp / hidden files in freqman's list + if (file_name.length() && file_name[0] != '.') { + file_list.emplace_back(file_name); + } + } +}; + +void FreqManBaseView::change_category(int32_t category_id) { + if (!file_list.size()) return; + + current_category_id = category_id; + + std::vector().swap(database); + + if (!load_freqman_file(file_list[categories[current_category_id].second], database)) { + error_ = ERROR_ACCESS; + } + menu_view.set_db(database); + menu_view.set_dirty(); + if (!database.size()) { + if (on_refresh_widgets) + on_refresh_widgets(true); + } else { + if (on_refresh_widgets) + on_refresh_widgets(false); + } +} + +void FreqManBaseView::refresh_list() { categories.clear(); + get_freqman_files(); for (size_t n = 0; n < file_list.size(); n++) categories.emplace_back(std::make_pair(file_list[n].substr(0, 14), n)); @@ -82,56 +116,17 @@ void FreqManBaseView::populate_categories() { }); options_category.set_options(categories); - options_category.set_selected_index(last_category_id); + if ((unsigned)current_category_id >= categories.size()) + current_category_id = categories.size() - 1; - options_category.on_change = [this](size_t category_id, int32_t) { - if (on_change_category) - on_change_category(category_id); - }; -} - -void FreqManBaseView::change_category(int32_t category_id) { - if (!file_list.size()) return; - - last_category_id = current_category_id = category_id; - - std::vector().swap(database); - menu_view.set_db(database); - - if (!load_freqman_file(file_list[categories[current_category_id].second], database)) - error_ = ERROR_ACCESS; - else { - menu_view.set_db(database); - refresh_list(); - } -} - -void FreqManBaseView::refresh_list() { - if (!database.size()) { - if (on_refresh_widgets) - on_refresh_widgets(true); - } else { - if (on_refresh_widgets) - on_refresh_widgets(false); + if (categories.size() > 0) { + options_category.set_selected_index(current_category_id); } } void FrequencySaveView::save_current_file() { - if (database.size() > FREQMAN_MAX_PER_FILE) { - nav_.display_modal( - "Error", "Too many entries, maximum is\n" FREQMAN_MAX_PER_FILE_STR ". Trim list ?", - YESNO, - [this](bool choice) { - if (choice) { - database.resize(FREQMAN_MAX_PER_FILE); - save_freqman_file(file_list[categories[current_category_id].second], database); - } - nav_.pop(); - }); - } else { - save_freqman_file(file_list[categories[current_category_id].second], database); - nav_.pop(); - } + save_freqman_file(file_list[categories[current_category_id].second], database); + nav_.pop(); } void FrequencySaveView::on_save_name() { @@ -155,11 +150,11 @@ FrequencySaveView::FrequencySaveView( // Todo: add back ? /*for (size_t n = 0; n < database.size(); n++) { - if (database[n].value == value_) { - error_ = ERROR_DUPLICATE; - break; - } - }*/ + if (database[n].value == value_) { + error_ = ERROR_DUPLICATE; + break; + } + }*/ add_children({&labels, &big_display, @@ -197,9 +192,6 @@ FrequencyLoadView::FrequencyLoadView( // Resize menu view to fill screen menu_view.set_parent_rect({0, 3 * 8, 240, 30 * 8}); - change_category(last_category_id); - refresh_list(); - menu_view.on_select = [&nav, this](FreqManUIList&) { nav_.pop(); @@ -229,8 +221,8 @@ void FrequencyManagerView::on_edit_freq(rf::Frequency f) { void FrequencyManagerView::on_edit_desc(NavigationView& nav) { text_prompt(nav, desc_buffer, 28, [this](std::string& buffer) { database[menu_view.get_index()].description = buffer; - refresh_list(); save_freqman_file(file_list[categories[current_category_id].second], database); + refresh_list(); }); } @@ -238,9 +230,8 @@ void FrequencyManagerView::on_new_category(NavigationView& nav) { text_prompt(nav, desc_buffer, 12, [this](std::string& buffer) { File freqman_file; create_freqman_file(buffer, freqman_file); + refresh_list(); }); - populate_categories(); - refresh_list(); } void FrequencyManagerView::on_delete() { @@ -255,6 +246,7 @@ void FrequencyManagerView::refresh_widgets(const bool v) { button_delete.hidden(v); menu_view.hidden(v); text_empty.hidden(!v); + labels.hidden(v); // display.fill_rectangle(menu_view.screen_rect(), Color::black()); set_dirty(); } @@ -278,8 +270,8 @@ FrequencyManagerView::FrequencyManagerView( &button_edit_desc, &button_delete}); - change_category(last_category_id); refresh_list(); + change_category(current_category_id); menu_view.on_select = [this](FreqManUIList&) { button_edit_freq.focus(); @@ -303,11 +295,7 @@ FrequencyManagerView::FrequencyManagerView( }; button_delete.on_select = [this, &nav](Button&) { - nav.push("Confirm", "Are you sure ?", YESNO, - [this](bool choice) { - if (choice) - on_delete(); - }); + on_delete(); }; } diff --git a/firmware/application/apps/ui_freqman.hpp b/firmware/application/apps/ui_freqman.hpp index 15491f2b6..3d742c2c6 100644 --- a/firmware/application/apps/ui_freqman.hpp +++ b/firmware/application/apps/ui_freqman.hpp @@ -49,14 +49,13 @@ class FreqManBaseView : public View { std::function on_change_category{nullptr}; std::function on_select_frequency{nullptr}; std::function on_refresh_widgets{nullptr}; - std::vector file_list{}; - int32_t current_category_id{0}; - void populate_categories(); + void get_freqman_files(); void change_category(int32_t category_id); void refresh_list(); freqman_db database{}; + std::vector file_list{}; Labels label_category{ {{0, 4}, "Category:", Color::light_grey()}}; @@ -75,8 +74,10 @@ class FreqManBaseView : public View { }; Button button_exit{ - {20 * 8, 34 * 8, 10 * 8, 4 * 8}, + {16 * 8, 34 * 8, 14 * 8, 4 * 8}, "Exit"}; + + private: }; class FrequencySaveView : public FreqManBaseView { @@ -154,7 +155,7 @@ class FrequencyManagerView : public FreqManBaseView { "Description"}; Button button_delete{ - {18 * 8, 27 * 8, 12 * 8, 32}, + {16 * 8, 29 * 8, 14 * 8, 32}, "Delete"}; }; diff --git a/firmware/application/freqman.cpp b/firmware/application/freqman.cpp index 993074704..9dbf264ef 100644 --- a/firmware/application/freqman.cpp +++ b/firmware/application/freqman.cpp @@ -86,22 +86,6 @@ options_t freqman_entry_steps_short = { {"500kHz", 500000}, {"1MHz", 1000000}}; -std::vector get_freqman_files() { - std::vector file_list; - - auto files = scan_root_files(u"FREQMAN", u"*.TXT"); - - for (auto file : files) { - std::string file_name = file.stem().string(); - // don't propose tmp / hidden files in freqman's list - if (file_name.length() && file_name[0] != '.') { - file_list.emplace_back(file_name); - } - } - - return file_list; -}; - bool load_freqman_file(std::string& file_stem, freqman_db& db) { return load_freqman_file_ex(file_stem, db, true, true, true, FREQMAN_MAX_PER_FILE); } @@ -299,26 +283,15 @@ bool get_freq_string(freqman_entry& entry, std::string& item_string) { bool save_freqman_file(std::string& file_stem, freqman_db& db) { File freqman_file; - - std::string freq_file_path = "FREQMAN/" + file_stem + ".TXT"; - std::string tmp_freq_file_path = "FREQMAN/" + file_stem + ".TXT.TMP"; - - if (!db.size()) { - delete_file("FREQMAN/" + file_stem + ".TXT"); - return true; - } - - delete_file(tmp_freq_file_path); - auto result = freqman_file.open(tmp_freq_file_path); + std::string freq_file_path = "/FREQMAN/" + file_stem + ".TXT"; + delete_file(freq_file_path); + auto result = freqman_file.create(freq_file_path); if (!result.is_valid()) { for (size_t n = 0; n < db.size(); n++) { - std::string item_string; - auto& entry = db[n]; - get_freq_string(entry, item_string); - freqman_file.write_line(item_string); + std::string line; + get_freq_string(db[n], line); + freqman_file.write_line(line); } - delete_file(freq_file_path); - rename_file(tmp_freq_file_path, freq_file_path); return true; } return false; diff --git a/firmware/application/freqman.hpp b/firmware/application/freqman.hpp index 8f07ae6a3..8d2bcb7c6 100644 --- a/firmware/application/freqman.hpp +++ b/firmware/application/freqman.hpp @@ -95,7 +95,6 @@ struct freqman_entry { using freqman_db = std::vector; -std::vector get_freqman_files(); bool load_freqman_file(std::string& file_stem, freqman_db& db); bool load_freqman_file_ex(std::string& file_stem, freqman_db& db, bool load_freqs, bool load_ranges, bool load_hamradios, uint8_t limit); bool get_freq_string(freqman_entry& entry, std::string& item_string); diff --git a/firmware/application/ui/ui_freqlist.cpp b/firmware/application/ui/ui_freqlist.cpp index e3f5a9f46..748fe7fed 100644 --- a/firmware/application/ui/ui_freqlist.cpp +++ b/firmware/application/ui/ui_freqlist.cpp @@ -108,8 +108,19 @@ void FreqManUIList::paint(Painter& painter) { void FreqManUIList::set_db(freqman_db& db) { freqlist_db = db; - current_index = 0; - highlighted_index = 0; + if (db.size() == 0) { + current_index = 0; + highlighted_index = 0; + } else { + if ((unsigned)(current_index + highlighted_index) >= db.size()) { + current_index = db.size() - 1 - highlighted_index; + } + if (current_index < 0) { + current_index = 0; + if (highlighted_index > 0) + highlighted_index--; + } + } } void FreqManUIList::on_focus() { diff --git a/firmware/application/ui/ui_freqlist.hpp b/firmware/application/ui/ui_freqlist.hpp index f10d21dd7..1d5eca75a 100644 --- a/firmware/application/ui/ui_freqlist.hpp +++ b/firmware/application/ui/ui_freqlist.hpp @@ -56,8 +56,9 @@ class FreqManUIList : public Widget { bool on_touch(const TouchEvent event) override; bool on_encoder(EncoderEvent delta) override; - void set_highlighted_index(int index); // set highlighted_index and return capped highlighted_index value to set in caller + void set_highlighted_index(int index); // internal set highlighted_index in list handler uint8_t get_index(); // return highlighed + index + uint8_t set_index(uint8_t index); // try to set current_index + highlighed from index, return capped index void set_db(freqman_db& db); private: