2016-07-25 10:21:27 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ui_freqman.hpp"
|
|
|
|
|
|
|
|
#include "portapack.hpp"
|
|
|
|
#include "event_m0.hpp"
|
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
2016-12-06 07:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
FreqManBaseView::FreqManBaseView(
|
2017-09-02 03:28:29 -04:00
|
|
|
NavigationView& nav
|
|
|
|
) : nav_ (nav)
|
2017-06-22 04:08:37 -04:00
|
|
|
{
|
|
|
|
file_list = get_freqman_files();
|
|
|
|
|
|
|
|
add_children({
|
|
|
|
&label_category,
|
|
|
|
&button_exit
|
|
|
|
});
|
|
|
|
|
2017-09-02 03:28:29 -04:00
|
|
|
if (file_list.size()) {
|
|
|
|
add_child(&options_category);
|
|
|
|
populate_categories();
|
2017-10-04 22:42:46 -04:00
|
|
|
} else
|
|
|
|
error_ = ERROR_NOFILES;
|
2017-06-22 04:08:37 -04:00
|
|
|
|
2017-09-02 03:28:29 -04:00
|
|
|
// Default function
|
2017-06-22 04:08:37 -04:00
|
|
|
on_change_category = [this](int32_t category_id) {
|
|
|
|
change_category(category_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
button_exit.on_select = [this, &nav](Button&) {
|
|
|
|
nav.pop();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
void FreqManBaseView::focus() {
|
2017-09-02 03:28:29 -04:00
|
|
|
button_exit.focus();
|
2017-06-22 04:08:37 -04:00
|
|
|
|
2017-09-02 03:28:29 -04:00
|
|
|
if (error_ == ERROR_ACCESS) {
|
|
|
|
nav_.display_modal("Error", "File acces error", ABORT, nullptr);
|
|
|
|
} else if (error_ == ERROR_NOFILES) {
|
2017-10-04 22:42:46 -04:00
|
|
|
nav_.display_modal("Error", "No database files\nin /freqman", ABORT, nullptr);
|
2017-06-22 04:08:37 -04:00
|
|
|
} else {
|
2017-09-02 03:28:29 -04:00
|
|
|
options_category.focus();
|
2017-06-22 04:08:37 -04:00
|
|
|
}
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
2017-01-29 20:09:00 -05:00
|
|
|
|
2017-10-04 22:42:46 -04:00
|
|
|
void FreqManBaseView::populate_categories() {
|
2017-06-22 04:08:37 -04:00
|
|
|
categories.clear();
|
|
|
|
|
2017-09-02 03:28:29 -04:00
|
|
|
for (size_t n = 0; n < file_list.size(); n++)
|
2017-06-22 04:08:37 -04:00
|
|
|
categories.emplace_back(std::make_pair(file_list[n], n));
|
|
|
|
|
2017-10-04 22:42:46 -04:00
|
|
|
// Alphabetical sort
|
|
|
|
std::sort(categories.begin(), categories.end(), [](auto &left, auto &right) {
|
|
|
|
return left.first < right.first;
|
|
|
|
});
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
options_category.set_options(categories);
|
|
|
|
options_category.set_selected_index(0);
|
|
|
|
|
2017-12-10 21:40:43 -05:00
|
|
|
options_category.on_change = [this](size_t category_id, int32_t) {
|
2017-06-22 04:08:37 -04:00
|
|
|
if (on_change_category)
|
|
|
|
on_change_category(category_id);
|
|
|
|
};
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
void FreqManBaseView::change_category(int32_t category_id) {
|
2017-09-02 03:28:29 -04:00
|
|
|
|
|
|
|
if (!file_list.size()) return;
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
current_category_id = category_id;
|
|
|
|
|
2017-12-10 21:40:43 -05:00
|
|
|
if (!load_freqman_file(file_list[categories[current_category_id].second], database))
|
2017-09-02 03:28:29 -04:00
|
|
|
error_ = ERROR_ACCESS;
|
2017-06-22 04:08:37 -04:00
|
|
|
else
|
|
|
|
refresh_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreqManBaseView::refresh_list() {
|
2017-12-10 21:40:43 -05:00
|
|
|
if (!database.size()) {
|
2017-06-22 19:13:13 -04:00
|
|
|
if (on_refresh_widgets)
|
|
|
|
on_refresh_widgets(true);
|
2017-01-30 08:10:30 -05:00
|
|
|
} else {
|
2017-06-22 19:13:13 -04:00
|
|
|
if (on_refresh_widgets)
|
|
|
|
on_refresh_widgets(false);
|
2017-06-22 04:08:37 -04:00
|
|
|
|
|
|
|
menu_view.clear();
|
|
|
|
|
2017-12-10 21:40:43 -05:00
|
|
|
for (size_t n = 0; n < database.size(); n++) {
|
2017-06-22 04:08:37 -04:00
|
|
|
menu_view.add_item({
|
2017-12-10 21:40:43 -05:00
|
|
|
freqman_item_string(database[n], 26),
|
2017-07-22 14:30:20 -04:00
|
|
|
ui::Color::white(),
|
2017-06-22 04:08:37 -04:00
|
|
|
nullptr,
|
|
|
|
[this](){
|
|
|
|
if (on_select_frequency)
|
|
|
|
on_select_frequency();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-10-04 22:42:46 -04:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
menu_view.set_highlighted(0); // Refresh
|
2017-01-30 08:10:30 -05:00
|
|
|
}
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-10-04 22:42:46 -04:00
|
|
|
void FrequencySaveView::save_current_file() {
|
2017-12-10 21:40:43 -05:00
|
|
|
if (database.size() > FREQMAN_MAX_PER_FILE) {
|
2017-10-04 22:42:46 -04:00
|
|
|
nav_.display_modal(
|
|
|
|
"Error", "Too many entries, maximum is\n" FREQMAN_MAX_PER_FILE_STR ". Trim list ?",
|
|
|
|
YESNO,
|
|
|
|
[this](bool choice) {
|
|
|
|
if (choice) {
|
2017-12-10 21:40:43 -05:00
|
|
|
database.resize(FREQMAN_MAX_PER_FILE);
|
|
|
|
save_freqman_file(file_list[categories[current_category_id].second], database);
|
2017-10-04 22:42:46 -04:00
|
|
|
}
|
|
|
|
nav_.pop();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
} else {
|
2017-12-10 21:40:43 -05:00
|
|
|
save_freqman_file(file_list[categories[current_category_id].second], database);
|
2017-10-04 22:42:46 -04:00
|
|
|
nav_.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
void FrequencySaveView::on_save_name() {
|
|
|
|
text_prompt(nav_, &desc_buffer, 28, [this](std::string * buffer) {
|
2017-12-10 21:40:43 -05:00
|
|
|
database.push_back({ value_, 0, *buffer, SINGLE });
|
2017-10-04 22:42:46 -04:00
|
|
|
save_current_file();
|
2017-06-22 04:08:37 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencySaveView::on_save_timestamp() {
|
2017-12-10 21:40:43 -05:00
|
|
|
database.push_back({ value_, 0, live_timestamp.string(), SINGLE });
|
2017-10-04 22:42:46 -04:00
|
|
|
save_current_file();
|
2017-06-22 04:08:37 -04:00
|
|
|
}
|
|
|
|
|
2016-12-06 07:31:38 -05:00
|
|
|
FrequencySaveView::FrequencySaveView(
|
|
|
|
NavigationView& nav,
|
|
|
|
const rf::Frequency value
|
2017-09-02 03:28:29 -04:00
|
|
|
) : FreqManBaseView(nav),
|
2016-12-25 19:31:38 -05:00
|
|
|
value_ (value)
|
|
|
|
{
|
2017-06-10 21:53:06 -04:00
|
|
|
desc_buffer.reserve(28);
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
// Todo: add back ?
|
2017-12-10 21:40:43 -05:00
|
|
|
/*for (size_t n = 0; n < database.size(); n++) {
|
|
|
|
if (database[n].value == value_) {
|
2017-06-22 04:08:37 -04:00
|
|
|
error_ = ERROR_DUPLICATE;
|
2017-01-30 08:10:30 -05:00
|
|
|
break;
|
|
|
|
}
|
2017-06-22 04:08:37 -04:00
|
|
|
}*/
|
2017-01-30 08:10:30 -05:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2017-09-20 02:50:59 -04:00
|
|
|
&labels,
|
2016-12-09 12:21:47 -05:00
|
|
|
&big_display,
|
|
|
|
&button_save_name,
|
|
|
|
&button_save_timestamp,
|
2017-09-20 02:50:59 -04:00
|
|
|
&live_timestamp
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-12-09 12:21:47 -05:00
|
|
|
|
|
|
|
big_display.set(value);
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
button_save_name.on_select = [this, &nav](Button&) {
|
2017-06-10 21:53:06 -04:00
|
|
|
on_save_name();
|
2016-12-25 19:31:38 -05:00
|
|
|
};
|
|
|
|
button_save_timestamp.on_select = [this, &nav](Button&) {
|
2017-02-03 11:22:12 -05:00
|
|
|
on_save_timestamp();
|
2016-12-25 19:31:38 -05:00
|
|
|
};
|
2016-12-09 12:21:47 -05:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:13:13 -04:00
|
|
|
void FrequencyLoadView::refresh_widgets(const bool v) {
|
|
|
|
menu_view.hidden(v);
|
|
|
|
text_empty.hidden(!v);
|
|
|
|
//display.fill_rectangle(menu_view.screen_rect(), Color::black());
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
FrequencyLoadView::FrequencyLoadView(
|
2016-12-25 19:31:38 -05:00
|
|
|
NavigationView& nav
|
2017-09-02 03:28:29 -04:00
|
|
|
) : FreqManBaseView(nav)
|
2016-12-25 19:31:38 -05:00
|
|
|
{
|
2017-06-22 19:13:13 -04:00
|
|
|
on_refresh_widgets = [this](bool v) {
|
|
|
|
refresh_widgets(v);
|
|
|
|
};
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2016-12-25 19:31:38 -05:00
|
|
|
&menu_view,
|
2017-06-22 04:08:37 -04:00
|
|
|
&text_empty
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-07-25 10:21:27 -04:00
|
|
|
|
2017-06-22 19:13:13 -04:00
|
|
|
// Resize menu view to fill screen
|
|
|
|
menu_view.set_parent_rect({ 0, 3 * 8, 240, 29 * 8 });
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
// Just to allow exit on left
|
2017-06-22 04:08:37 -04:00
|
|
|
menu_view.on_left = [&nav, this]() {
|
|
|
|
nav.pop();
|
2017-02-03 10:10:27 -05:00
|
|
|
};
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
change_category(0);
|
|
|
|
refresh_list();
|
|
|
|
|
|
|
|
on_select_frequency = [&nav, this]() {
|
|
|
|
nav_.pop();
|
2017-12-08 13:58:46 -05:00
|
|
|
|
2017-12-10 21:40:43 -05:00
|
|
|
auto& entry = database[menu_view.highlighted_index()];
|
2017-12-08 13:58:46 -05:00
|
|
|
|
|
|
|
if (entry.type == RANGE) {
|
|
|
|
// User chose a frequency range entry
|
|
|
|
if (on_range_loaded)
|
|
|
|
on_range_loaded(entry.frequency_a, entry.frequency_b);
|
|
|
|
else if (on_frequency_loaded)
|
|
|
|
on_frequency_loaded(entry.frequency_a);
|
|
|
|
// TODO: Maybe return center of range if user choses a range when the app needs a unique frequency, instead of frequency_a ?
|
|
|
|
} else {
|
|
|
|
// User chose an unique frequency entry
|
|
|
|
if (on_frequency_loaded)
|
|
|
|
on_frequency_loaded(entry.frequency_a);
|
|
|
|
}
|
2016-12-06 07:31:38 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
void FrequencyManagerView::on_edit_freq(rf::Frequency f) {
|
2017-12-10 21:40:43 -05:00
|
|
|
database[menu_view.highlighted_index()].frequency_a = f;
|
|
|
|
save_freqman_file(file_list[categories[current_category_id].second], database);
|
2017-06-22 04:08:37 -04:00
|
|
|
refresh_list();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
void FrequencyManagerView::on_edit_desc(NavigationView& nav) {
|
2017-06-20 22:25:27 -04:00
|
|
|
text_prompt(nav, &desc_buffer, 28, [this](std::string * buffer) {
|
2017-12-10 21:40:43 -05:00
|
|
|
database[menu_view.highlighted_index()].description = *buffer;
|
2017-06-22 04:08:37 -04:00
|
|
|
refresh_list();
|
2017-12-10 21:40:43 -05:00
|
|
|
save_freqman_file(file_list[categories[current_category_id].second], database);
|
2017-06-22 04:08:37 -04:00
|
|
|
});
|
2017-02-03 10:10:27 -05:00
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
void FrequencyManagerView::on_new_category(NavigationView& nav) {
|
|
|
|
text_prompt(nav, &desc_buffer, 8, [this](std::string * buffer) {
|
|
|
|
File freqman_file;
|
|
|
|
create_freqman_file(*buffer, freqman_file);
|
|
|
|
});
|
|
|
|
populate_categories();
|
|
|
|
refresh_list();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
void FrequencyManagerView::on_delete() {
|
2017-12-10 21:40:43 -05:00
|
|
|
database.erase(database.begin() + menu_view.highlighted_index());
|
|
|
|
save_freqman_file(file_list[categories[current_category_id].second], database);
|
2017-06-22 04:08:37 -04:00
|
|
|
refresh_list();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-06-22 19:13:13 -04:00
|
|
|
void FrequencyManagerView::refresh_widgets(const bool v) {
|
|
|
|
button_edit_freq.hidden(v);
|
|
|
|
button_edit_desc.hidden(v);
|
|
|
|
button_delete.hidden(v);
|
|
|
|
menu_view.hidden(v);
|
|
|
|
text_empty.hidden(!v);
|
|
|
|
//display.fill_rectangle(menu_view.screen_rect(), Color::black());
|
|
|
|
set_dirty();
|
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
FrequencyManagerView::~FrequencyManagerView() {
|
2017-12-10 21:40:43 -05:00
|
|
|
//save_freqman_file(file_list[categories[current_category_id].second], database);
|
2016-07-25 10:21:27 -04:00
|
|
|
}
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
FrequencyManagerView::FrequencyManagerView(
|
2016-07-25 10:21:27 -04:00
|
|
|
NavigationView& nav
|
2017-09-02 03:28:29 -04:00
|
|
|
) : FreqManBaseView(nav)
|
2016-12-25 19:31:38 -05:00
|
|
|
{
|
2017-06-22 19:13:13 -04:00
|
|
|
on_refresh_widgets = [this](bool v) {
|
|
|
|
refresh_widgets(v);
|
|
|
|
};
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2017-06-22 04:08:37 -04:00
|
|
|
&labels,
|
|
|
|
&button_new_category,
|
2016-12-25 19:31:38 -05:00
|
|
|
&menu_view,
|
2017-06-22 04:08:37 -04:00
|
|
|
&text_empty,
|
2016-12-25 19:31:38 -05:00
|
|
|
&button_edit_freq,
|
|
|
|
&button_edit_desc,
|
2017-06-22 04:08:37 -04:00
|
|
|
&button_delete
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
// Just to allow exit on left
|
|
|
|
menu_view.on_left = [&nav, this]() {
|
|
|
|
nav.pop();
|
|
|
|
};
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
change_category(0);
|
|
|
|
refresh_list();
|
2017-02-03 10:10:27 -05:00
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
on_select_frequency = [this]() {
|
|
|
|
button_edit_freq.focus();
|
2017-02-03 10:10:27 -05:00
|
|
|
};
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
button_new_category.on_select = [this, &nav](Button&) {
|
2017-06-22 19:13:13 -04:00
|
|
|
desc_buffer = "";
|
2017-06-22 04:08:37 -04:00
|
|
|
on_new_category(nav);
|
2017-02-03 10:10:27 -05:00
|
|
|
};
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
button_edit_freq.on_select = [this, &nav](Button&) {
|
2017-12-10 21:40:43 -05:00
|
|
|
auto new_view = nav.push<FrequencyKeypadView>(database[menu_view.highlighted_index()].frequency_a);
|
2016-12-25 19:31:38 -05:00
|
|
|
new_view->on_changed = [this](rf::Frequency f) {
|
|
|
|
on_edit_freq(f);
|
2016-07-25 10:21:27 -04:00
|
|
|
};
|
2016-12-25 19:31:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
button_edit_desc.on_select = [this, &nav](Button&) {
|
2017-12-10 21:40:43 -05:00
|
|
|
desc_buffer = database[menu_view.highlighted_index()].description;
|
2016-12-25 19:31:38 -05:00
|
|
|
on_edit_desc(nav);
|
|
|
|
};
|
|
|
|
|
2017-06-22 04:08:37 -04:00
|
|
|
button_delete.on_select = [this, &nav](Button&) {
|
2016-12-25 19:31:38 -05:00
|
|
|
nav.push<ModalMessageView>("Confirm", "Are you sure ?", YESNO,
|
|
|
|
[this](bool choice) {
|
2017-06-22 04:08:37 -04:00
|
|
|
if (choice)
|
2016-12-25 19:31:38 -05:00
|
|
|
on_delete();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2016-07-25 10:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|