fixing edit freq,edit desc, delete freq (#1117)

* fixing edit freq,edit desc, delete freq
* fixing scrolling issues
* fixing indent
This commit is contained in:
gullradriel 2023-06-07 10:17:55 +02:00 committed by GitHub
parent c66df8c807
commit eecdd3acda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 116 deletions

View File

@ -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<std::string>().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<freqman_entry>().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<freqman_entry>().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<ModalMessageView>("Confirm", "Are you sure ?", YESNO,
[this](bool choice) {
if (choice)
on_delete();
});
on_delete();
};
}

View File

@ -49,14 +49,13 @@ class FreqManBaseView : public View {
std::function<void(int32_t category_id)> on_change_category{nullptr};
std::function<void(void)> on_select_frequency{nullptr};
std::function<void(bool)> on_refresh_widgets{nullptr};
std::vector<std::string> 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<std::string> 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"};
};

View File

@ -86,22 +86,6 @@ options_t freqman_entry_steps_short = {
{"500kHz", 500000},
{"1MHz", 1000000}};
std::vector<std::string> get_freqman_files() {
std::vector<std::string> 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;

View File

@ -95,7 +95,6 @@ struct freqman_entry {
using freqman_db = std::vector<freqman_entry>;
std::vector<std::string> 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);

View File

@ -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() {

View File

@ -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: