summaryrefslogtreecommitdiff
path: root/state.lua
blob: 472e4d28ffe616b83accd77c44eba462e0b11c6f (plain) (blame)
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
-- Copyright 2025 David Vazgenovich Shakaryan

local state = {}
local mt = {}
mt.__index = mt

function state.new()
	return setmetatable({
		menus = {},
		depth = 0,

		favourites = {},
		playing_id = nil,
	}, mt)
end

function mt:menu()
	return self.menus[self.depth]
end

function mt:push_menu(t)
	local menu = {
		options = {},
		cursor = 1,
		view_top = 1,
	}

	for k, v in pairs(t) do
		menu[k] = v
	end

	self.depth = self.depth + 1
	self.menus[self.depth] = menu
end

-- returns index if found
function mt:favourited(id)
	for i, v in ipairs(self.favourites) do
		if v == id then
			return i
		end
	end
end

function mt:add_favourite(id)
	self.favourites[#self.favourites+1] = id
end

function mt:remove_favourite_at(i)
	table.remove(self.favourites, i)
end

-- inserts the given id into the favourites array before the next favourited
-- menu option, starting from the next cursor position, or the end if no such
-- option is found. this is meant for in-place favouriting from the favourites
-- menu.
function mt:insert_favourite_before_next_in_menu(id)
	local menu = self:menu()
	for i = menu.cursor+1, #menu.options do
		local ind = self:favourited(menu.options[i].id)
		if ind then
			table.insert(self.favourites, ind, id)
			return
		end
	end

	self:add_favourite(id)
end

return state