Login
1 branch 0 tags
Ben (Desktop/Arch) Settings dialog c862468 1 month ago 35 Commits
moon / firmware / src / screens / settings_screen.c
#include "settings_screen.h"
#include "bookmark.h"
#include <stdlib.h>

static void clear_view(settings_screen_state_t *state) {
    if (!state->list)
        return;
    uint32_t count = lv_obj_get_child_count(state->list);
    for (uint32_t i = 0; i < count; i++)
        lv_group_remove_obj(lv_obj_get_child(state->list, i));
    lv_obj_delete(state->list);
    state->list = NULL;
}

static void show_menu(settings_screen_state_t *state);
static void show_confirm(settings_screen_state_t *state);

static void menu_cancel_cb(lv_event_t *e) {
    (void)e;
    navigate_to(SCREEN_HOME);
}

static void back_cb(lv_event_t *e) {
    if (lv_event_get_code(e) == LV_EVENT_CLICKED)
        navigate_to(SCREEN_HOME);
}

static void reset_clicked_cb(lv_event_t *e) {
    if (lv_event_get_code(e) == LV_EVENT_CLICKED) {
        settings_screen_state_t *state = ui_state.settings;
        clear_view(state);
        show_confirm(state);
    }
}

static void confirm_cancel_cb(lv_event_t *e) {
    (void)e;
    settings_screen_state_t *state = ui_state.settings;
    clear_view(state);
    show_menu(state);
}

static void confirm_ok_cb(lv_event_t *e) {
    if (lv_event_get_code(e) == LV_EVENT_CLICKED) {
        bookmark_clear_all();
        settings_screen_state_t *state = ui_state.settings;
        clear_view(state);
        show_menu(state);
    }
}

static void show_menu(settings_screen_state_t *state) {
    state->list = lv_list_create(lv_screen_active());
    lv_obj_set_style_pad_hor(state->list, 4, 0);
    lv_obj_set_size(state->list, LV_PCT(100), LV_PCT(100));
    lv_obj_center(state->list);

    bvs_list_add_header(state->list, "Settings");

    lv_obj_t *wifi_btn = bvs_list_add_button(state->list, NULL, "WiFi");

    lv_obj_t *reset_btn =
        bvs_list_add_button(state->list, NULL, "Reset Bookmarks");
    lv_obj_add_event_cb(reset_btn, reset_clicked_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *back_btn = bvs_list_add_button(state->list, NULL, "Back");
    lv_obj_add_event_cb(back_btn, back_cb, LV_EVENT_CLICKED, NULL);

    lv_group_add_obj(lv_group_get_default(), wifi_btn);
    lv_group_add_obj(lv_group_get_default(), reset_btn);
    lv_group_add_obj(lv_group_get_default(), back_btn);

    lv_obj_add_event_cb(wifi_btn, menu_cancel_cb, LV_EVENT_CANCEL, NULL);
    lv_obj_add_event_cb(reset_btn, menu_cancel_cb, LV_EVENT_CANCEL, NULL);
    lv_obj_add_event_cb(back_btn, menu_cancel_cb, LV_EVENT_CANCEL, NULL);

    lv_group_focus_obj(reset_btn);
}

static void show_confirm(settings_screen_state_t *state) {
    state->list = lv_list_create(lv_screen_active());
    lv_obj_set_style_pad_hor(state->list, 4, 0);
    lv_obj_set_size(state->list, LV_PCT(100), LV_PCT(100));
    lv_obj_center(state->list);

    bvs_list_add_header(state->list, "Clear bookmarks?");

    lv_obj_t *ok_btn = bvs_list_add_button(state->list, NULL, "Confirm");
    lv_obj_add_event_cb(ok_btn, confirm_ok_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(ok_btn, confirm_cancel_cb, LV_EVENT_CANCEL, NULL);
    lv_obj_set_style_bg_color(ok_btn, lv_palette_main(LV_PALETTE_RED), 0);
    lv_obj_set_style_bg_opa(ok_btn, LV_OPA_COVER, 0);

    lv_obj_t *cancel_btn = bvs_list_add_button(state->list, NULL, "Cancel");
    lv_obj_add_event_cb(cancel_btn, confirm_cancel_cb, LV_EVENT_CLICKED, NULL);
    lv_obj_add_event_cb(cancel_btn, confirm_cancel_cb, LV_EVENT_CANCEL, NULL);

    lv_group_add_obj(lv_group_get_default(), ok_btn);
    lv_group_add_obj(lv_group_get_default(), cancel_btn);
    lv_group_focus_obj(ok_btn);
}

ui_state_t setup_settings_screen(void) {
    settings_screen_state_t *state = calloc(1, sizeof(settings_screen_state_t));
    show_menu(state);
    return (ui_state_t){.type = SCREEN_SETTINGS, .settings = state};
}

void free_settings_screen(settings_screen_state_t *state) {
    if (!state)
        return;
    clear_view(state);
    free(state);
}

void update_settings_screen(settings_screen_state_t *state) {
    (void)state;
}