mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-20 07:25:06 +00:00
* LilyGo T-Deck keyboard support * reverse logic * docs and readability * cleanup * optimize driver buffer * cleanup
150 lines
5.3 KiB
C
150 lines
5.3 KiB
C
#include "wifi_connect_view.h"
|
|
|
|
#include "log.h"
|
|
#include "lvgl.h"
|
|
#include "services/gui/gui.h"
|
|
#include "services/wifi/wifi_credentials.h"
|
|
#include "ui/lvgl_keypad.h"
|
|
#include "ui/spacer.h"
|
|
#include "ui/style.h"
|
|
#include "wifi_connect.h"
|
|
#include "wifi_connect_bundle.h"
|
|
#include "wifi_connect_state.h"
|
|
|
|
#define TAG "wifi_connect"
|
|
|
|
static void show_keyboard(lv_event_t* event) {
|
|
gui_keyboard_show(event->current_target);
|
|
lv_obj_scroll_to_view(event->current_target, LV_ANIM_ON);
|
|
}
|
|
|
|
static void hide_keyboard(TT_UNUSED lv_event_t* event) {
|
|
gui_keyboard_hide();
|
|
}
|
|
|
|
static void on_connect(lv_event_t* event) {
|
|
WifiConnect* wifi = (WifiConnect*)event->user_data;
|
|
WifiConnectView* view = &wifi->view;
|
|
const char* ssid = lv_textarea_get_text(view->ssid_textarea);
|
|
const char* password = lv_textarea_get_text(view->password_textarea);
|
|
|
|
if (strlen(password) > 63) {
|
|
// TODO: UI feedback
|
|
TT_LOG_E(TAG, "Password too long");
|
|
return;
|
|
}
|
|
char password_buffer[64];
|
|
strcpy(password_buffer, password);
|
|
|
|
WifiConnectBindings* bindings = &wifi->bindings;
|
|
bindings->on_connect_ssid(
|
|
ssid,
|
|
password_buffer,
|
|
bindings->on_connect_ssid_context
|
|
);
|
|
|
|
if (lv_obj_get_state(view->remember_switch) == LV_STATE_CHECKED) {
|
|
tt_wifi_credentials_set(ssid, password_buffer);
|
|
}
|
|
}
|
|
|
|
void wifi_connect_view_create_bottom_buttons(WifiConnect* wifi, lv_obj_t* parent) {
|
|
WifiConnectView* view = &wifi->view;
|
|
|
|
lv_obj_t* button_container = lv_obj_create(parent);
|
|
lv_obj_set_width(button_container, LV_PCT(100));
|
|
lv_obj_set_height(button_container, LV_SIZE_CONTENT);
|
|
tt_lv_obj_set_style_no_padding(button_container);
|
|
lv_obj_set_style_border_width(button_container, 0, 0);
|
|
|
|
view->remember_switch = lv_switch_create(button_container);
|
|
lv_obj_add_state(view->remember_switch, LV_STATE_CHECKED);
|
|
lv_obj_align(view->remember_switch, LV_ALIGN_LEFT_MID, 0, 0);
|
|
|
|
lv_obj_t* remember_label = lv_label_create(button_container);
|
|
lv_label_set_text(remember_label, "Remember");
|
|
lv_obj_align(remember_label, LV_ALIGN_CENTER, 0, 0);
|
|
lv_obj_align_to(remember_label, view->remember_switch, LV_ALIGN_OUT_RIGHT_MID, 4, 0);
|
|
|
|
view->connect_button = lv_btn_create(button_container);
|
|
lv_obj_t* connect_label = lv_label_create(view->connect_button);
|
|
lv_label_set_text(connect_label, "Connect");
|
|
lv_obj_align(view->connect_button, LV_ALIGN_RIGHT_MID, 0, 0);
|
|
lv_obj_add_event_cb(view->connect_button, &on_connect, LV_EVENT_CLICKED, wifi);
|
|
}
|
|
|
|
// TODO: Standardize dialogs
|
|
void wifi_connect_view_create(App app, void* wifi, lv_obj_t* parent) {
|
|
WifiConnect* wifi_connect = (WifiConnect*)wifi;
|
|
WifiConnectView* view = &wifi_connect->view;
|
|
// TODO: Standardize this into "window content" function?
|
|
// TODO: It can then be dynamically determined based on screen res and size
|
|
lv_obj_set_flex_flow(parent, LV_FLEX_FLOW_COLUMN);
|
|
lv_obj_set_style_pad_top(parent, 8, 0);
|
|
lv_obj_set_style_pad_bottom(parent, 8, 0);
|
|
lv_obj_set_style_pad_left(parent, 16, 0);
|
|
lv_obj_set_style_pad_right(parent, 16, 0);
|
|
|
|
view->root = parent;
|
|
|
|
lv_obj_t* ssid_label = lv_label_create(parent);
|
|
lv_label_set_text(ssid_label, "Network:");
|
|
view->ssid_textarea = lv_textarea_create(parent);
|
|
lv_textarea_set_one_line(view->ssid_textarea, true);
|
|
|
|
tt_lv_spacer_create(parent, 1, 8);
|
|
|
|
lv_obj_t* password_label = lv_label_create(parent);
|
|
lv_label_set_text(password_label, "Password:");
|
|
view->password_textarea = lv_textarea_create(parent);
|
|
lv_textarea_set_one_line(view->password_textarea, true);
|
|
lv_textarea_set_password_mode(view->password_textarea, true);
|
|
|
|
tt_lv_spacer_create(parent, 1, 8);
|
|
|
|
wifi_connect_view_create_bottom_buttons(wifi, parent);
|
|
|
|
if (gui_keyboard_is_enabled()) {
|
|
lv_obj_add_event_cb(view->ssid_textarea, show_keyboard, LV_EVENT_FOCUSED, NULL);
|
|
lv_obj_add_event_cb(view->ssid_textarea, hide_keyboard, LV_EVENT_DEFOCUSED, NULL);
|
|
lv_obj_add_event_cb(view->ssid_textarea, hide_keyboard, LV_EVENT_READY, NULL);
|
|
lv_obj_add_event_cb(view->password_textarea, show_keyboard, LV_EVENT_FOCUSED, NULL);
|
|
lv_obj_add_event_cb(view->password_textarea, hide_keyboard, LV_EVENT_DEFOCUSED, NULL);
|
|
lv_obj_add_event_cb(view->password_textarea, hide_keyboard, LV_EVENT_READY, NULL);
|
|
}
|
|
|
|
// Init from app parameters
|
|
Bundle* _Nullable bundle = tt_app_get_parameters(app);
|
|
if (bundle) {
|
|
char* ssid;
|
|
if (tt_bundle_opt_string(bundle, WIFI_CONNECT_PARAM_SSID, &ssid)) {
|
|
lv_textarea_set_text(view->ssid_textarea, ssid);
|
|
}
|
|
|
|
char* password;
|
|
if (tt_bundle_opt_string(bundle, WIFI_CONNECT_PARAM_PASSWORD, &password)) {
|
|
lv_textarea_set_text(view->password_textarea, password);
|
|
}
|
|
}
|
|
|
|
// Hardware keyboard("keypad") requires a group
|
|
view->group = lv_group_create();
|
|
lv_group_add_obj(view->group, view->ssid_textarea);
|
|
lv_group_add_obj(view->group, view->password_textarea);
|
|
tt_lvgl_keypad_activate(view->group);
|
|
}
|
|
|
|
void wifi_connect_view_destroy(TT_UNUSED WifiConnectView* view) {
|
|
// Cleanup keypad group
|
|
tt_lvgl_keypad_deactivate();
|
|
lv_group_del(view->group);
|
|
}
|
|
|
|
void wifi_connect_view_update(
|
|
TT_UNUSED WifiConnectView* view,
|
|
TT_UNUSED WifiConnectBindings* bindings,
|
|
TT_UNUSED WifiConnectState* state
|
|
) {
|
|
// NO-OP
|
|
}
|