Login
1 branch 0 tags
Ben (Desktop/Arch) Added clang-format 2f88780 1 month ago 66 Commits
moon / src / screens / settings_screen.c
#include "settings_screen.h"
#include <stdlib.h>
#include "../playlist.h"
#include "../topbar.h"
#include "bookmark.h"

static void clear_view(settings_screen_state_t* state) {
	if (!state->list) {
		return;
	}
	bvs_list_clear(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();
		playlist_clear();
		playlist_save();
		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 = bvs_list_create();
	topbar_set_title("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 = bvs_list_create();
	topbar_set_title("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_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;
}