summaryrefslogtreecommitdiff
path: root/lua/playing_menu.lua
blob: 2b5a6c9879df8d3108953c7ca755b6deb23c1880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

-- SPDX-FileCopyrightText: 2025 ailurux <ailuruxx@gmail.com>
--
-- SPDX-License-Identifier: GPL-3.0-only

local lvgl = require("lvgl")
local widgets = require("widgets")
local backstack = require("backstack")
local font = require("font")
local playback = require("playback")
local queue = require("queue")
local screen = require("screen")
local theme = require("theme")
local track_info = require("track_info")
local styles = require("styles")

return screen:new {
  create_ui = function(self)
    self.root = lvgl.Object(nil, {
      flex = {
        flex_direction = "column",
        flex_wrap = "wrap",
        justify_content = "center",
        align_items = "center",
        align_content = "center",
      },
      w = lvgl.HOR_RES(),
      h = lvgl.VER_RES(),
    })
    self.root:center()

    self.status_bar = widgets.StatusBar(self, {
      back_cb = backstack.pop,
      transparent_bg = true,
    })


    local menu_items = lvgl.List(self.root, {
      w = lvgl.PCT(100),
      h = lvgl.PCT(100),
      flex_grow = 1,
    })

    local info_btn = menu_items:add_btn(nil, "Track Info")
    info_btn:onClicked(function()
      backstack.push(track_info:new())
    end)
    info_btn:add_style(styles.list_item)

    local current_artist = nil
    local album_artist = nil
    local current_album = nil

    local artist_btn = menu_items:add_btn(nil, "Go To Artist")
    artist_btn:add_style(styles.list_item)
    artist_btn:onClicked(function()
      local found_iter = nil
      local media_type = nil
      for _, idx in ipairs(database.indexes()) do
        if idx:id() == database.IndexTypes.ALL_ARTISTS then
          -- Find the sub-index for this artist.
          local artist_iter = idx:iter()
          -- Workaround for lack of pairs/ipairs on these iterators.
          local artist_record = artist_iter:next()
          while artist_record do
            if artist_record:title() == current_artist then
              found_iter = artist_record:contents()
              media_type = idx:type()
              goto artist_done
            end
            artist_record = artist_iter:next()
          end
        end
      end
      ::artist_done::
      if found_iter then
        backstack.push(require("browser"):new {
          title = current_artist,
          iterator = found_iter,
          mediatype = media_type,
        })
      end
    end)

    local album_btn = menu_items:add_btn(nil, "Go To Album")
    album_btn:add_style(styles.list_item)
    album_btn:onClicked(function()
      local found_iter = nil
      local media_type = nil
      for _, idx in ipairs(database.indexes()) do
        if idx:id() == database.IndexTypes.ALBUMS_BY_ARTIST then
          -- Find the sub-index for this artist.
          local artist_iter = idx:iter()
          -- Workaround for lack of pairs/ipairs on these iterators.
          local artist_record = artist_iter:next()
          while artist_record do
            if artist_record:title() == album_artist then
              -- Find the sub-sub-index for this album.
              local album_iter = artist_record:contents()
              local album_record = album_iter:next()
              while album_record do
                if album_record:title() == current_album then
                  found_iter = album_record:contents()
                  media_type = idx:type()
                  goto album_done
                end
                album_record = album_iter:next()
              end
            end
            artist_record = artist_iter:next()
          end
        end
      end
      ::album_done::
      if found_iter then
        backstack.push(require("browser"):new {
          title = current_album,
          iterator = found_iter,
          mediatype = media_type,
        })
      end
    end)
    

    local clear_btn = menu_items:add_btn(nil, "Clear Queue")
    clear_btn:onClicked(function()
        queue.clear()
        backstack.pop()
    end)
    clear_btn:add_style(styles.list_item)

    self.bindings = self.bindings + {
      playback.track:bind(function(track)
        if not track then
          artist_btn:add_flag(lvgl.FLAG.HIDDEN)
          album_btn:add_flag(lvgl.FLAG.HIDDEN)
          return
        end
        current_artist = track.artist
        if not current_artist then
          artist_btn:add_flag(lvgl.FLAG.HIDDEN)
        else
          artist_btn:clear_flag(lvgl.FLAG.HIDDEN)
        end
        current_album = track.album
        if not current_album then
          album_btn:add_flag(lvgl.FLAG.HIDDEN)
        else
          album_btn:clear_flag(lvgl.FLAG.HIDDEN)
        end
        album_artist = track.album_artist
      end),
    }

  end
}