Wifi app improvements (#107)
This commit is contained in:
committed by
GitHub
parent
50ee77d572
commit
422bc01fdb
@@ -1,7 +1,7 @@
|
||||
#include "View.h"
|
||||
#include "WifiManage.h"
|
||||
|
||||
#include "Log.h"
|
||||
#include "State.h"
|
||||
#include "service/statusbar/Statusbar.h"
|
||||
#include "service/wifi/Wifi.h"
|
||||
#include "lvgl/Style.h"
|
||||
@@ -14,13 +14,18 @@ namespace tt::app::wifimanage {
|
||||
|
||||
#define TAG "wifi_main_view"
|
||||
|
||||
std::shared_ptr<WifiManage> _Nullable optWifiManage();
|
||||
|
||||
static void on_enable_switch_changed(lv_event_t* event) {
|
||||
lv_event_code_t code = lv_event_get_code(event);
|
||||
auto* enable_switch = static_cast<lv_obj_t*>(lv_event_get_target(event));
|
||||
if (code == LV_EVENT_VALUE_CHANGED) {
|
||||
bool is_on = lv_obj_has_state(enable_switch, LV_STATE_CHECKED);
|
||||
auto* bindings = static_cast<Bindings*>(lv_event_get_user_data(event));
|
||||
bindings->onWifiToggled(is_on);
|
||||
|
||||
auto wifi = optWifiManage();
|
||||
auto bindings = wifi->getBindings();
|
||||
|
||||
bindings.onWifiToggled(is_on);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,11 +38,6 @@ static void on_enable_on_boot_switch_changed(lv_event_t* event) {
|
||||
}
|
||||
}
|
||||
|
||||
static void on_disconnect_pressed(lv_event_t* event) {
|
||||
auto* bindings = static_cast<Bindings*>(lv_event_get_user_data(event));
|
||||
bindings->onDisconnect();
|
||||
}
|
||||
|
||||
// region Secondary updates
|
||||
|
||||
static void connect(lv_event_t* event) {
|
||||
@@ -63,7 +63,7 @@ static void showDetails(lv_event_t* event) {
|
||||
bindings->onShowApSettings(ssid);
|
||||
}
|
||||
|
||||
void View::createSsidListItem(Bindings* bindings, const service::wifi::WifiApRecord& record, bool isConnecting) {
|
||||
void View::createSsidListItem(const service::wifi::WifiApRecord& record, bool isConnecting) {
|
||||
lv_obj_t* wrapper = lv_obj_create(networks_list);
|
||||
lv_obj_add_event_cb(wrapper, &connect, LV_EVENT_CLICKED, bindings);
|
||||
lv_obj_set_user_data(wrapper, bindings);
|
||||
@@ -109,7 +109,7 @@ void View::createSsidListItem(Bindings* bindings, const service::wifi::WifiApRec
|
||||
}
|
||||
}
|
||||
|
||||
void View::updateNetworkList(State* state, Bindings* bindings) {
|
||||
void View::updateNetworkList() {
|
||||
lv_obj_clean(networks_list);
|
||||
|
||||
switch (state->getRadioState()) {
|
||||
@@ -129,7 +129,7 @@ void View::updateNetworkList(State* state, Bindings* bindings) {
|
||||
for (auto &record : ap_records) {
|
||||
if (record.ssid == connection_target) {
|
||||
lv_list_add_text(networks_list, "Connected");
|
||||
createSsidListItem(bindings, record, false);
|
||||
createSsidListItem(record, false);
|
||||
added_connected = true;
|
||||
break;
|
||||
}
|
||||
@@ -148,7 +148,7 @@ void View::updateNetworkList(State* state, Bindings* bindings) {
|
||||
!connection_target.empty();
|
||||
bool skip = connection_target_match && added_connected;
|
||||
if (!skip) {
|
||||
createSsidListItem(bindings, record, is_connecting);
|
||||
createSsidListItem(record, is_connecting);
|
||||
}
|
||||
used_ssids.insert(record.ssid);
|
||||
}
|
||||
@@ -172,7 +172,7 @@ void View::updateNetworkList(State* state, Bindings* bindings) {
|
||||
}
|
||||
}
|
||||
|
||||
void View::updateScanning(State* state) {
|
||||
void View::updateScanning() {
|
||||
if (state->getRadioState() == service::wifi::WIFI_RADIO_ON && state->isScanning()) {
|
||||
lv_obj_clear_flag(scanning_spinner, LV_OBJ_FLAG_HIDDEN);
|
||||
} else {
|
||||
@@ -180,7 +180,7 @@ void View::updateScanning(State* state) {
|
||||
}
|
||||
}
|
||||
|
||||
void View::updateWifiToggle(State* state) {
|
||||
void View::updateWifiToggle() {
|
||||
lv_obj_clear_state(enable_switch, LV_STATE_ANY);
|
||||
switch (state->getRadioState()) {
|
||||
case service::wifi::WIFI_RADIO_ON:
|
||||
@@ -214,7 +214,7 @@ void View::updateEnableOnBootToggle() {
|
||||
|
||||
// region Main
|
||||
|
||||
void View::init(const AppContext& app, Bindings* bindings, lv_obj_t* parent) {
|
||||
void View::init(const AppContext& app, lv_obj_t* parent) {
|
||||
root = parent;
|
||||
|
||||
// Toolbar
|
||||
@@ -263,11 +263,11 @@ void View::init(const AppContext& app, Bindings* bindings, lv_obj_t* parent) {
|
||||
lv_obj_align(networks_list, LV_ALIGN_TOP_LEFT, 0, 44);
|
||||
}
|
||||
|
||||
void View::update(Bindings* bindings, State* state) {
|
||||
updateWifiToggle(state);
|
||||
void View::update() {
|
||||
updateWifiToggle();
|
||||
updateEnableOnBootToggle();
|
||||
updateScanning(state);
|
||||
updateNetworkList(state, bindings);
|
||||
updateScanning();
|
||||
updateNetworkList();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -8,24 +8,29 @@
|
||||
namespace tt::app::wifimanage {
|
||||
|
||||
class View {
|
||||
|
||||
private:
|
||||
|
||||
Bindings* bindings;
|
||||
State* state;
|
||||
lv_obj_t* root = nullptr;
|
||||
lv_obj_t* enable_switch = nullptr;
|
||||
lv_obj_t* enable_on_boot_switch = nullptr;
|
||||
lv_obj_t* scanning_spinner = nullptr;
|
||||
lv_obj_t* networks_list = nullptr;
|
||||
public:
|
||||
View() {}
|
||||
void init(const AppContext& app, Bindings* bindings, lv_obj_t* parent);
|
||||
void update(Bindings* bindings, State* state);
|
||||
|
||||
private:
|
||||
|
||||
void updateWifiToggle(State* state);
|
||||
void updateWifiToggle();
|
||||
void updateEnableOnBootToggle();
|
||||
void updateScanning(State* state);
|
||||
void updateNetworkList(State* state, Bindings* bindings);
|
||||
void createSsidListItem(Bindings* bindings, const service::wifi::WifiApRecord& record, bool isConnecting);
|
||||
void updateScanning();
|
||||
void updateNetworkList();
|
||||
void createSsidListItem(const service::wifi::WifiApRecord& record, bool isConnecting);
|
||||
|
||||
public:
|
||||
|
||||
View(Bindings* bindings, State* state) : bindings(bindings), state(state) {}
|
||||
|
||||
void init(const AppContext& app, lv_obj_t* parent);
|
||||
void update();
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -14,6 +14,18 @@ namespace tt::app::wifimanage {
|
||||
|
||||
#define TAG "wifi_manage"
|
||||
|
||||
extern const AppManifest manifest;
|
||||
|
||||
/** Returns the app data if the app is active. Note that this could clash if the same app is started twice and a background thread is slow. */
|
||||
std::shared_ptr<WifiManage> _Nullable optWifiManage() {
|
||||
app::AppContext* app = service::loader::getCurrentApp();
|
||||
if (app->getManifest().id == manifest.id) {
|
||||
return std::static_pointer_cast<WifiManage>(app->getData());
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static void onConnect(const char* ssid) {
|
||||
service::wifi::settings::WifiApSettings settings;
|
||||
if (service::wifi::settings::load(ssid, &settings)) {
|
||||
@@ -61,7 +73,7 @@ void WifiManage::requestViewUpdate() {
|
||||
lock();
|
||||
if (isViewEnabled) {
|
||||
if (lvgl::lock(1000)) {
|
||||
view.update(&bindings, &state);
|
||||
view.update();
|
||||
lvgl::unlock();
|
||||
} else {
|
||||
TT_LOG_E(TAG, "failed to lock lvgl");
|
||||
@@ -108,8 +120,8 @@ void WifiManage::onShow(AppContext& app, lv_obj_t* parent) {
|
||||
lock();
|
||||
isViewEnabled = true;
|
||||
state.setConnectSsid("Connected"); // TODO update with proper SSID
|
||||
view.init(app, &bindings, parent);
|
||||
view.update(&bindings, &state);
|
||||
view.init(app, parent);
|
||||
view.update();
|
||||
unlock();
|
||||
|
||||
service::wifi::WifiRadioState radio_state = service::wifi::getRadioState();
|
||||
|
||||
@@ -11,9 +11,9 @@ class WifiManage {
|
||||
|
||||
PubSubSubscription* wifiSubscription = nullptr;
|
||||
Mutex mutex;
|
||||
tt::app::wifimanage::State state;
|
||||
tt::app::wifimanage::View view;
|
||||
Bindings bindings = { };
|
||||
State state;
|
||||
View view = View(&bindings, &state);
|
||||
bool isViewEnabled = false;
|
||||
|
||||
public:
|
||||
@@ -26,6 +26,7 @@ public:
|
||||
void onShow(AppContext& app, lv_obj_t* parent);
|
||||
void onHide(AppContext& app);
|
||||
|
||||
Bindings& getBindings() { return bindings; }
|
||||
State& getState() { return state; }
|
||||
|
||||
void requestViewUpdate();
|
||||
|
||||
Reference in New Issue
Block a user