~chrisppy/beagles

ref: 86a60492d542f47e0b686a7c3dab6fbb27f1b95e beagles/ui/list.go -rw-r--r-- 3.7 KiB
86a60492Chris Palmer Fix remove from sub 8 months ago
                                                                                
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// This file is part of beagles.
//
// Copyright © 2020-2021 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package ui

import (
	"fmt"
	"sort"
	"strings"
	"time"

	"git.sr.ht/~chrisppy/beagles/db"
	tui "gitlab.com/tslocum/cview"
)

type listType int

const (
	queue listType = iota
	favorite
)

type list struct {
	Widget    *tui.List
	Dates     map[string]time.Time
	AudioIcon string
}

func (i *UI) newList(l listType) *list {
	var m map[string]bool
	switch l {
	case queue:
		m = i.DB.Queue
	case favorite:
		m = i.DB.Favorites
	default:
		panic("Invalid list type")
	}

	view := tui.NewList()
	view.ShowSecondaryText(false)

	view.SetBackgroundColor(i.Config.Theme.ListBGColor)
	view.SetMainTextColor(i.Config.Theme.ListFGColor)

	w := &list{
		Widget:    view,
		Dates:     make(map[string]time.Time),
		AudioIcon: i.Config.Theme.AudioIcon,
	}

	if len(i.DB.Items) < 1 {
		return w
	}

	index := 0
	li := make([]*db.Item, len(m))
	for k := range m {
		li[index] = i.DB.Items[k]
		index++
	}
	sort.Slice(li, func(index, j int) bool { return li[index].Compare(li[j]) })

	for in, item := range li {
		w.Widget.InsertItem(in, w.convert(item))
		w.Dates[item.Link] = item.Date
	}

	w.Widget.SetCurrentItem(0)

	return w
}

func (w *list) onChange(handler func(index int, item *tui.ListItem)) {
	w.Widget.SetChangedFunc(handler)
}

func (w *list) increment() {
	w.Widget.Transform(tui.TransformNextItem)
}

func (w *list) decrement() {
	w.Widget.Transform(tui.TransformPreviousItem)
}

func (w *list) pageUp() {
	w.Widget.Transform(tui.TransformPreviousPage)
}

func (w *list) pageDown() {
	w.Widget.Transform(tui.TransformNextPage)
}

func (w *list) ref(item *tui.ListItem) string {
	if item == nil {
		return ""
	}

	return item.GetSecondaryText()
}

func (w *list) getKey() string {
	return w.ref(w.Widget.GetCurrentItem())
}

func (w *list) setIndex(i int) {
	w.Widget.SetCurrentItem(i)
}

func (w *list) index() int {
	return w.Widget.GetCurrentItemIndex()
}

func (w *list) size() int {
	return w.Widget.GetItemCount()
}

func (w *list) remove() string {
	key := w.getKey()

	w.Widget.RemoveItem(w.index())
	delete(w.Dates, key)

	return key
}

func (w *list) find(item *db.Item) (bool, int) {
	if item == nil {
		return false, 0
	}

	indices := w.Widget.FindItems(item.Title, item.Link, true, false)

	if len(indices) < 1 {
		return false, 0
	}

	return true, indices[0]
}

func (w *list) removeByKey(item *db.Item) {
	if item == nil {
		return
	}

	found, index := w.find(item)
	if !found {
		return
	}

	w.Widget.RemoveItem(index)
	delete(w.Dates, item.Link)
}

func (w *list) convert(item *db.Item) *tui.ListItem {
	name := item.Title
	if strings.Contains(item.Type, "audio") {
		name = fmt.Sprintf("%s %s", w.AudioIcon, name)
	} else {
		name = fmt.Sprintf("  %s", name)
	}

	it := tui.NewListItem(name)
	it.SetSecondaryText(item.Link)

	return it
}

func (w *list) insert(item *db.Item) {
	if found, _ := w.find(item); found {
		return
	}

	items := w.Widget.GetItems()
	date := item.Date

	i := sort.Search(len(items), func(i int) bool {
		return w.Dates[w.ref(items[i])].Sub(date) > 0
	})

	w.Widget.InsertItem(i, w.convert(item))

	w.Dates[item.Link] = date
}