Login
1 branch 0 tags
Ben (T14/NixOS) Fixed a bug in the button blur handler bf34ce6 1 month ago 56 Commits
moon / src / screens / queue_dialog_screen.c
#include "queue_dialog_screen.h"
#include "../audio_player.h"
#include "../playlist.h"
#include "../topbar.h"
#include "audiobooks_screen.h"
#include "files_screen.h"
#include "music_screen.h"
#include <stdlib.h>
#include <string.h>

// Navigate back to the return screen with correct directory/focus
static void navigate_back(void) {
  free_screen();
  switch (app_state.return_screen) {
  case SCREEN_MUSIC:
    ui_state =
        setup_music_screen(app_state.return_cwd, app_state.return_focus);
    break;
  case SCREEN_AUDIOBOOKS:
    ui_state = setup_audiobooks_screen(app_state.return_cwd,
                                       app_state.return_focus);
    break;
  case SCREEN_FILES:
    ui_state =
        setup_files_screen(app_state.return_cwd, app_state.return_focus);
    break;
  default:
    ui_state = setup_screen(SCREEN_HOME);
    break;
  }
}

static void play_now_cb(lv_event_t *e) {
  if (lv_event_get_code(e) != LV_EVENT_CLICKED)
    return;

  audio_player_stop();
  playlist_clear();
  playlist_add(app_state.pending_path, app_state.pending_type);
  playlist_set_index(0);
  playlist_save();
  navigate_to(SCREEN_NOW_PLAYING);
}

static void add_to_queue_cb(lv_event_t *e) {
  if (lv_event_get_code(e) != LV_EVENT_CLICKED)
    return;

  playlist_add(app_state.pending_path, app_state.pending_type);
  playlist_save();
  navigate_back();
}

static void back_cb(lv_event_t *e) {
  if (lv_event_get_code(e) != LV_EVENT_CLICKED)
    return;
  navigate_back();
}

static void cancel_event_cb(lv_event_t *e) {
  (void)e;
  navigate_back();
}

ui_state_t setup_queue_dialog_screen(void) {
  queue_dialog_screen_state_t *state =
      calloc(1, sizeof(queue_dialog_screen_state_t));

  topbar_set_title("Queue");

  state->list = bvs_list_create();

  lv_obj_t *queue_btn =
      bvs_list_add_button(state->list, NULL, "Add to queue");
  lv_obj_add_event_cb(queue_btn, add_to_queue_cb, LV_EVENT_CLICKED, NULL);
  lv_obj_add_event_cb(queue_btn, cancel_event_cb, LV_EVENT_CANCEL, NULL);
  lv_group_add_obj(lv_group_get_default(), queue_btn);

  lv_obj_t *play_btn =
      bvs_list_add_button(state->list, NULL, "Play now");
  lv_obj_add_event_cb(play_btn, play_now_cb, LV_EVENT_CLICKED, NULL);
  lv_obj_add_event_cb(play_btn, cancel_event_cb, LV_EVENT_CANCEL, NULL);
  lv_group_add_obj(lv_group_get_default(), play_btn);

  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_obj_add_event_cb(back_btn, cancel_event_cb, LV_EVENT_CANCEL, NULL);
  lv_group_add_obj(lv_group_get_default(), back_btn);

  lv_group_focus_obj(queue_btn);

  return (ui_state_t){.type = SCREEN_QUEUE_DIALOG, .queue_dialog = state};
}

void free_queue_dialog_screen(queue_dialog_screen_state_t *state) {
  if (!state)
    return;
  bvs_list_clear(state->list);
  free(state);
}

void update_queue_dialog_screen(queue_dialog_screen_state_t *state) {
  (void)state;
}