Skip to content

Commit f8b2acf

Browse files
committed
improve filtering & sorting albums
1 parent 4cc92ab commit f8b2acf

File tree

5 files changed

+114
-26
lines changed

5 files changed

+114
-26
lines changed

ui/ui.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func bindDefaultTheme() {
8484
ContrastSecondaryTextColor: 0,
8585
PrimitiveBackgroundColor: colors.Background,
8686
ContrastBackgroundColor: colors.Background,
87-
MoreContrastBackgroundColor: colors.Modal.Background,
87+
MoreContrastBackgroundColor: colors.Status.ProgressBar,
8888
ContextMenuPaddingTop: 0,
8989
ContextMenuPaddingBottom: 0,
9090
ContextMenuPaddingLeft: 1,

ui/widgets/artist_albums.go

+46-22
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,12 @@ type AlbumList struct {
137137
similarFunc func(id models.Id)
138138
similarEnabled bool
139139

140-
sort *sort
141-
filter *filter
142-
filterBtn *button
143-
queryOpts *interfaces.QueryOpts
144-
queryFunc func(opts *interfaces.QueryOpts)
140+
sort *sort
141+
filter *filter
142+
filterBtn *button
143+
filterEnabled bool
144+
queryOpts *interfaces.QueryOpts
145+
queryFunc func(opts *interfaces.QueryOpts)
145146
}
146147

147148
func (a *AlbumList) AddAlbum(c *AlbumCover) {
@@ -153,6 +154,7 @@ func (a *AlbumList) Clear() {
153154
a.list.Clear()
154155
//a.SetArtist(nil)
155156
a.albumCovers = make([]*AlbumCover, 0)
157+
a.filter.Clear()
156158
}
157159

158160
// SetPlaylists sets albumList cover
@@ -194,8 +196,9 @@ func (a *AlbumList) EnableArtistMode(enabled bool) {
194196
func (a *AlbumList) selectPage(n int) {
195197
a.paging.SetPage(n)
196198
a.page.CurrentPage = n
197-
if a.selectPageFunc != nil {
198-
a.selectPageFunc(a.page)
199+
a.queryOpts.Paging = a.page
200+
if a.queryFunc != nil {
201+
a.queryFunc(a.queryOpts)
199202
}
200203
}
201204

@@ -243,6 +246,14 @@ func (a *AlbumList) EnableSimilar(enabled bool) {
243246
a.setButtons()
244247
}
245248

249+
func (a *AlbumList) EnableFilter(enabled bool) {
250+
a.filterEnabled = enabled
251+
if enabled {
252+
a.similarEnabled = false
253+
}
254+
a.setButtons()
255+
}
256+
246257
func (a *AlbumList) setButtons() {
247258
a.Banner.Grid.Clear()
248259
selectables := []twidgets.Selectable{a.prevBtn, a.playBtn}
@@ -251,25 +262,29 @@ func (a *AlbumList) setButtons() {
251262
a.Grid.AddItem(a.playBtn, 3, 2, 1, 1, 1, 10, false)
252263

253264
if a.pagingEnabled {
254-
selectables = append(selectables, a.paging.Previous, a.paging.Next, a.filterBtn)
265+
selectables = append(selectables, a.paging.Previous, a.paging.Next)
255266
a.Grid.AddItem(a.paging, 3, 4, 1, 3, 1, 10, false)
256-
//a.Grid.AddItem(a.sort, 3, 6, 1, 3, 1, 10, false)
257-
a.Grid.AddItem(a.filterBtn, 3, 6, 1, 3, 1, 10, false)
258267
}
259-
/*
260-
if a.similarEnabled {
261-
selectables = append(selectables, a.options)
262-
col := 4
263-
if a.pagingEnabled {
264-
col = 6
265-
}
266-
a.Grid.AddItem(a.options, 3, col, 1, 1, 1, 10, false)
268+
if a.similarEnabled {
269+
selectables = append(selectables, a.options)
270+
col := 4
271+
if a.pagingEnabled {
272+
col = 6
267273
}
274+
a.Grid.AddItem(a.options, 3, col, 1, 1, 1, 10, false)
275+
} else if a.filterEnabled {
276+
selectables = append(selectables, a.sort, a.filterBtn)
277+
col := 4
278+
if a.pagingEnabled {
279+
col = 6
280+
}
281+
a.Grid.AddItem(a.sort, 3, col, 1, 1, 1, 10, false)
282+
a.Grid.AddItem(a.filterBtn, 3, col+2, 1, 1, 1, 10, false)
283+
}
268284

269-
*/
270285
selectables = append(selectables, a.list)
271286
a.Banner.Selectable = selectables
272-
a.Grid.AddItem(a.list, 4, 0, 1, 8, 6, 20, false)
287+
a.Grid.AddItem(a.list, 4, 0, 1, 10, 6, 20, false)
273288
}
274289

275290
//NewAlbumList constructs new albumList view
@@ -302,20 +317,21 @@ func NewAlbumList(selectAlbum func(album *models.Album), context contextOperator
302317
)
303318
}
304319

305-
a.filter = newFilter("artist", a.setFilter)
320+
a.filter = newFilter("album", a.setFilter, a.filterApplied)
306321
a.filterBtn = newButton("Filter")
307322
a.filterBtn.SetSelectedFunc(func() {
308323
if filterFunc != nil {
309324
filterFunc(a.filter, nil)
310325
}
311326
})
327+
a.filterEnabled = true
312328

313329
selectables := []twidgets.Selectable{a.prevBtn, a.playBtn, a.options,
314330
a.paging.Previous, a.paging.Next, a.sort, a.filterBtn, a.list}
315331
a.Banner.Selectable = selectables
316332

317333
a.Grid.SetRows(1, 1, 1, 1, -1)
318-
a.Grid.SetColumns(6, 2, 10, -1, 10, -1, 10, -3)
334+
a.Grid.SetColumns(6, 2, 10, -1, 10, -1, 15, -1, 10, -3)
319335
a.Grid.SetMinSize(1, 6)
320336
a.Grid.SetBackgroundColor(config.Color.Background)
321337
a.list.Grid.SetColumns(1, -1)
@@ -348,6 +364,14 @@ func NewAlbumList(selectAlbum func(album *models.Album), context contextOperator
348364
return a
349365
}
350366

367+
func (a *AlbumList) filterApplied(status bool) {
368+
if status {
369+
a.filterBtn.SetLabel("Filter *")
370+
} else {
371+
a.filterBtn.SetLabel("Filter")
372+
}
373+
}
374+
351375
func (a *AlbumList) InputHandler() func(event *tcell.EventKey, setFocus func(p cview.Primitive)) {
352376
return func(event *tcell.EventKey, setFocus func(p cview.Primitive)) {
353377
if event.Key() == tcell.KeyEnter && event.Modifiers() == tcell.ModAlt {

ui/widgets/filter.go

+61-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type queryFunc = func(opts *interfaces.QueryOpts)
4040
type sortFunc = func(sort interfaces.Sort)
4141
type openFilterFunc = func(m modal.Modal, doneFunc func())
4242

43+
// sort provides dropdown for sorting
4344
type sort struct {
4445
*dropDown
4546
currentIndex int
@@ -90,6 +91,7 @@ func (s *sort) toggleMode() {
9091
}
9192
}
9293

94+
// filter provides a modal for defining filters
9395
type filter struct {
9496
*cview.Form
9597
filterFunc func(interfaces.Filter)
@@ -103,6 +105,8 @@ type filter struct {
103105
itemFavorite *cview.Checkbox
104106

105107
yearRange *cview.InputField
108+
109+
filterChangedFunc func(bool)
106110
}
107111

108112
func (f *filter) SetDoneFunc(doneFunc func()) {
@@ -117,7 +121,7 @@ func (f *filter) SetVisible(visible bool) {
117121
f.visible = visible
118122
}
119123

120-
func newFilter(itemType string, filterFunc func(f interfaces.Filter)) *filter {
124+
func newFilter(itemType string, filterFunc func(f interfaces.Filter), filterChangedFunc func(bool)) *filter {
121125

122126
f := &filter{
123127
Form: cview.NewForm(),
@@ -127,10 +131,11 @@ func newFilter(itemType string, filterFunc func(f interfaces.Filter)) *filter {
127131
itemNotPlayed: cview.NewCheckbox(),
128132
itemFavorite: cview.NewCheckbox(),
129133
yearRange: cview.NewInputField(),
134+
135+
filterChangedFunc: filterChangedFunc,
130136
}
131137

132138
f.SetTitle(fmt.Sprintf(" Filter %ss ", itemType))
133-
134139
f.SetBackgroundColor(config.Color.Modal.Background)
135140
f.SetBorder(true)
136141
f.AddFormItem(f.itemPlayed)
@@ -153,8 +158,19 @@ func newFilter(itemType string, filterFunc func(f interfaces.Filter)) *filter {
153158
f.AddFormItem(f.yearRange)
154159

155160
f.AddButton("Filter", f.ok)
156-
f.AddButton("Cancel", f.closeCb)
161+
f.AddButton("Cancel", f.cancel)
162+
163+
f.GetButton(0).SetInputCapture(f.inputCapture)
164+
f.GetButton(1).SetInputCapture(f.inputCapture)
165+
166+
f.SetInputCapture(f.inputCapture)
167+
168+
f.itemPlayed.SetInputCapture(f.inputCapture)
169+
f.itemNotPlayed.SetInputCapture(f.inputCapture)
170+
f.itemFavorite.SetInputCapture(f.inputCapture)
171+
f.yearRange.SetInputCapture(f.inputCapture)
157172

173+
f.SetCancelFunc(f.cancel)
158174
return f
159175
}
160176

@@ -232,6 +248,26 @@ func (f *filter) ok() {
232248
}
233249
f.filterFunc(filt)
234250
f.closeCb()
251+
252+
if f.filterChangedFunc != nil {
253+
f.filterChangedFunc(true)
254+
}
255+
}
256+
257+
func (f *filter) cancel() {
258+
if f.closeCb != nil {
259+
f.closeCb()
260+
}
261+
}
262+
263+
func (f *filter) Clear() {
264+
f.itemPlayed.SetChecked(false)
265+
f.itemNotPlayed.SetChecked(false)
266+
f.itemFavorite.SetChecked(false)
267+
f.yearRange.SetText("")
268+
if f.filterChangedFunc != nil {
269+
f.filterChangedFunc(false)
270+
}
235271
}
236272

237273
func (f *filter) InputHandler() func(event *tcell.EventKey, setFocus func(p cview.Primitive)) {
@@ -243,3 +279,25 @@ func (f *filter) InputHandler() func(event *tcell.EventKey, setFocus func(p cvie
243279
f.Form.InputHandler()(event, setFocus)
244280
}
245281
}
282+
283+
func (f *filter) inputCapture(e *tcell.EventKey) *tcell.EventKey {
284+
key := e.Key()
285+
r := e.Rune()
286+
287+
switch key {
288+
case tcell.KeyUp:
289+
return tcell.NewEventKey(tcell.KeyBacktab, e.Rune(), e.Modifiers())
290+
case tcell.KeyDown:
291+
return tcell.NewEventKey(tcell.KeyTab, e.Rune(), e.Modifiers())
292+
}
293+
294+
switch r {
295+
case 'k':
296+
return tcell.NewEventKey(tcell.KeyBacktab, e.Rune(), e.Modifiers())
297+
case 'j':
298+
return tcell.NewEventKey(tcell.KeyTab, e.Rune(), e.Modifiers())
299+
}
300+
301+
return e
302+
303+
}

ui/widgets/util.go

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func newDropDown(text string) *dropDown {
9696
d.SetBackgroundColor(config.Color.ButtonBackground)
9797
d.SetFieldBackgroundColor(config.Color.ButtonBackground)
9898
d.SetFieldTextColor(config.Color.Text)
99+
d.SetPrefixTextColor(config.Color.ButtonBackgroundSelected)
99100
d.SetBorder(false)
100101
d.SetBorderPadding(0, 0, 1, 2)
101102

ui/widgets/window.go

+5
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func (w *Window) showSearchResults(itemType models.ItemType, results []models.It
378378
}
379379
w.albumList.Clear()
380380
w.albumList.EnablePaging(false)
381+
w.albumList.EnableFilter(false)
381382
w.albumList.SetLabel(fmt.Sprintf("[yellow::]Search results for '%s'[-::]\n%d albums", query, len(results)))
382383
w.albumList.SetAlbums(albums)
383384
w.albumList.EnableSimilar(false)
@@ -600,11 +601,13 @@ func (w *Window) selectMedia(m MediaSelect) {
600601
albums, total, err = w.mediaItems.GetAlbums(opts)
601602
title = "All Albums"
602603
w.albumList.EnablePaging(true)
604+
w.albumList.EnableFilter(true)
603605
} else if m == MediaFavoriteAlbums {
604606
paging.PageSize = 200
605607
albums, total, err = w.mediaItems.GetFavoriteAlbums(paging)
606608
title = "Favorite albums"
607609
w.albumList.EnablePaging(false)
610+
w.albumList.EnableFilter(false)
608611
list = w.favoriteAlbums
609612
}
610613

@@ -738,6 +741,7 @@ func (w *Window) showAlbumPage(opts *interfaces.QueryOpts) {
738741
w.albumList.SetPage(opts.Paging)
739742
w.albumList.Clear()
740743
w.albumList.EnablePaging(true)
744+
w.albumList.EnableFilter(true)
741745
w.albumList.SetAlbums(albums)
742746
}
743747

@@ -787,6 +791,7 @@ func (w *Window) showSimilarAlbums(album *models.Album) {
787791
w.similarAlbums.Clear()
788792
w.similarAlbums.EnableSimilar(false)
789793
w.similarAlbums.EnablePaging(false)
794+
w.similarAlbums.EnableFilter(false)
790795
w.similarAlbums.SetAlbums(albums)
791796
w.similarAlbums.SetText(fmt.Sprintf("Similar albums: %d", len(albums)))
792797
w.setViewWidget(w.similarAlbums, true)

0 commit comments

Comments
 (0)