add sorting to playlists page list view

This commit is contained in:
Drew Weymouth
2023-10-09 17:06:59 -07:00
parent f84a03c703
commit 4485908776
+85 -21
View File
@@ -3,6 +3,7 @@ package browsing
import ( import (
"fmt" "fmt"
"log" "log"
"sort"
"strconv" "strconv"
"strings" "strings"
@@ -38,6 +39,7 @@ type PlaylistsPage struct {
titleDisp *widget.RichText titleDisp *widget.RichText
container *fyne.Container container *fyne.Container
listView *PlaylistList listView *PlaylistList
listSort widgets.ListHeaderSort
gridView *widgets.GridView gridView *widgets.GridView
} }
@@ -46,15 +48,16 @@ func NewPlaylistsPage(contr *controller.Controller, pool *util.WidgetPool, cfg *
if cfg.InitialView == "Grid" { if cfg.InitialView == "Grid" {
activeView = 1 activeView = 1
} }
return newPlaylistsPage(contr, pool, cfg, mp, "", activeView) return newPlaylistsPage(contr, pool, cfg, mp, "", activeView, widgets.ListHeaderSort{})
} }
func newPlaylistsPage(contr *controller.Controller, pool *util.WidgetPool, cfg *backend.PlaylistsPageConfig, mp mediaprovider.MediaProvider, searchText string, activeView int) *PlaylistsPage { func newPlaylistsPage(contr *controller.Controller, pool *util.WidgetPool, cfg *backend.PlaylistsPageConfig, mp mediaprovider.MediaProvider, searchText string, activeView int, listSort widgets.ListHeaderSort) *PlaylistsPage {
a := &PlaylistsPage{ a := &PlaylistsPage{
pool: pool, pool: pool,
cfg: cfg, cfg: cfg,
mp: mp, mp: mp,
contr: contr, contr: contr,
listSort: listSort,
titleDisp: widget.NewRichTextWithText("Playlists"), titleDisp: widget.NewRichTextWithText("Playlists"),
} }
a.ExtendBaseWidget(a) a.ExtendBaseWidget(a)
@@ -92,7 +95,7 @@ func (a *PlaylistsPage) load(searchOnLoad bool) {
} }
func (a *PlaylistsPage) createListView() { func (a *PlaylistsPage) createListView() {
a.listView = NewPlaylistList() a.listView = NewPlaylistList(a.listSort)
a.listView.OnNavTo = a.showPlaylistPage a.listView.OnNavTo = a.showPlaylistPage
} }
@@ -140,9 +143,9 @@ func (a *PlaylistsPage) showListView() {
if a.listView == nil { if a.listView == nil {
a.createListView() a.createListView()
if a.searcher.Entry.Text != "" { if a.searcher.Entry.Text != "" {
a.listView.Playlists = a.searchedPlaylists a.listView.SetPlaylists(a.searchedPlaylists)
} else { } else {
a.listView.Playlists = a.playlists a.listView.SetPlaylists(a.playlists)
} }
} }
a.container.Objects[0].(*fyne.Container).Objects[0] = a.listView a.container.Objects[0].(*fyne.Container).Objects[0] = a.listView
@@ -204,7 +207,7 @@ func (a *PlaylistsPage) onSearched(query string) {
// refresh the active view // refresh the active view
func (a *PlaylistsPage) refreshView(playlists []*mediaprovider.Playlist) { func (a *PlaylistsPage) refreshView(playlists []*mediaprovider.Playlist) {
if a.listView != nil { if a.listView != nil {
a.listView.Playlists = playlists a.listView.SetPlaylists(playlists)
} }
if a.gridView != nil { if a.gridView != nil {
a.gridView.ResetFixed(createPlaylistGridViewModel(playlists)) a.gridView.ResetFixed(createPlaylistGridViewModel(playlists))
@@ -243,6 +246,9 @@ func (a *PlaylistsPage) Save() SavedPage {
a.gridView.Clear() a.gridView.Clear()
a.pool.Release(util.WidgetTypeGridView, a.gridView) a.pool.Release(util.WidgetTypeGridView, a.gridView)
} }
if a.listView != nil {
s.listSort = a.listView.sorting
}
return s return s
} }
@@ -253,10 +259,11 @@ type savedPlaylistsPage struct {
mp mediaprovider.MediaProvider mp mediaprovider.MediaProvider
searchText string searchText string
activeView int activeView int
listSort widgets.ListHeaderSort
} }
func (s *savedPlaylistsPage) Restore() Page { func (s *savedPlaylistsPage) Restore() Page {
return newPlaylistsPage(s.contr, s.pool, s.cfg, s.mp, s.searchText, s.activeView) return newPlaylistsPage(s.contr, s.pool, s.cfg, s.mp, s.searchText, s.activeView, s.listSort)
} }
func (a *PlaylistsPage) buildContainer(initialView fyne.CanvasObject) { func (a *PlaylistsPage) buildContainer(initialView fyne.CanvasObject) {
@@ -274,8 +281,11 @@ func (a *PlaylistsPage) CreateRenderer() fyne.WidgetRenderer {
type PlaylistList struct { type PlaylistList struct {
widget.BaseWidget widget.BaseWidget
Playlists []*mediaprovider.Playlist OnNavTo func(string)
OnNavTo func(string)
playlistsOrigOrder []*mediaprovider.Playlist
playlists []*mediaprovider.Playlist
sorting widgets.ListHeaderSort
columnsLayout *layouts.ColumnsLayout columnsLayout *layouts.ColumnsLayout
header *widgets.ListHeader header *widgets.ListHeader
@@ -283,14 +293,15 @@ type PlaylistList struct {
container *fyne.Container container *fyne.Container
} }
func NewPlaylistList() *PlaylistList { func NewPlaylistList(initialSort widgets.ListHeaderSort) *PlaylistList {
a := &PlaylistList{ a := &PlaylistList{
sorting: initialSort,
columnsLayout: layouts.NewColumnsLayout([]float32{-1, -1, 200, 125}), columnsLayout: layouts.NewColumnsLayout([]float32{-1, -1, 200, 125}),
} }
a.buildHeader() a.buildHeader()
a.list = widget.NewList( a.list = widget.NewList(
func() int { func() int {
return len(a.Playlists) return len(a.playlists)
}, },
func() fyne.CanvasObject { func() fyne.CanvasObject {
r := NewPlaylistListRow(a.columnsLayout) r := NewPlaylistListRow(a.columnsLayout)
@@ -299,11 +310,11 @@ func NewPlaylistList() *PlaylistList {
}, },
func(id widget.ListItemID, item fyne.CanvasObject) { func(id widget.ListItemID, item fyne.CanvasObject) {
row := item.(*PlaylistListRow) row := item.(*PlaylistListRow)
row.ID = a.Playlists[id].ID row.ID = a.playlists[id].ID
row.nameLabel.Text = a.Playlists[id].Name row.nameLabel.Text = a.playlists[id].Name
row.descrptionLabel.Text = a.Playlists[id].Description row.descrptionLabel.Text = a.playlists[id].Description
row.ownerLabel.Text = a.Playlists[id].Owner row.ownerLabel.Text = a.playlists[id].Owner
row.trackCountLabel.Text = strconv.Itoa(a.Playlists[id].TrackCount) row.trackCountLabel.Text = strconv.Itoa(a.playlists[id].TrackCount)
row.Refresh() row.Refresh()
}, },
) )
@@ -314,12 +325,18 @@ func NewPlaylistList() *PlaylistList {
func (p *PlaylistList) buildHeader() { func (p *PlaylistList) buildHeader() {
p.header = widgets.NewListHeader([]widgets.ListColumn{ p.header = widgets.NewListHeader([]widgets.ListColumn{
{"Name", fyne.TextAlignLeading, false}, {Text: "Name", Alignment: fyne.TextAlignLeading, CanToggleVisible: false},
{"Description", fyne.TextAlignLeading, false}, {Text: "Description", Alignment: fyne.TextAlignLeading, CanToggleVisible: false},
{"Owner", fyne.TextAlignLeading, false}, {Text: "Owner", Alignment: fyne.TextAlignLeading, CanToggleVisible: false},
{"Track Count", fyne.TextAlignTrailing, false}}, p.columnsLayout) {Text: "Track Count", Alignment: fyne.TextAlignTrailing, CanToggleVisible: false}}, p.columnsLayout)
p.header.DisableSorting = true p.header.SetSorting(p.sorting)
p.header.OnColumnSortChanged = p.onSorted
}
// Sets the playlists in the list. Does not issue Refresh call.
func (p *PlaylistList) SetPlaylists(playlists []*mediaprovider.Playlist) {
p.playlistsOrigOrder = playlists
p.doSortPlaylists()
} }
func (p *PlaylistList) CreateRenderer() fyne.WidgetRenderer { func (p *PlaylistList) CreateRenderer() fyne.WidgetRenderer {
@@ -332,6 +349,53 @@ func (p *PlaylistList) onRowTapped(id string) {
} }
} }
func (p *PlaylistList) onSorted(sort widgets.ListHeaderSort) {
p.sorting = sort
p.doSortPlaylists()
p.Refresh()
}
func (p *PlaylistList) doSortPlaylists() {
if p.sorting.Type == widgets.SortNone {
p.playlists = p.playlistsOrigOrder
}
switch p.sorting.ColNumber {
case 0: //Name
p.stringSort(func(p *mediaprovider.Playlist) string { return p.Name })
case 1: // Description
p.stringSort(func(p *mediaprovider.Playlist) string { return p.Description })
case 2: // Owner
p.stringSort(func(p *mediaprovider.Playlist) string { return p.Owner })
case 3: // Track Count
p.intSort(func(p *mediaprovider.Playlist) int { return p.TrackCount })
}
}
func (p *PlaylistList) stringSort(fieldFn func(*mediaprovider.Playlist) string) {
new := make([]*mediaprovider.Playlist, len(p.playlistsOrigOrder))
copy(new, p.playlistsOrigOrder)
sort.SliceStable(new, func(i, j int) bool {
cmp := strings.Compare(fieldFn(new[i]), fieldFn(new[j]))
if p.sorting.Type == widgets.SortDescending {
return cmp > 0
}
return cmp < 0
})
p.playlists = new
}
func (p *PlaylistList) intSort(fieldFn func(*mediaprovider.Playlist) int) {
new := make([]*mediaprovider.Playlist, len(p.playlistsOrigOrder))
copy(new, p.playlistsOrigOrder)
sort.SliceStable(new, func(i, j int) bool {
if p.sorting.Type == widgets.SortDescending {
return fieldFn(new[i]) > fieldFn(new[j])
}
return fieldFn(new[i]) < fieldFn(new[j])
})
p.playlists = new
}
type PlaylistListRow struct { type PlaylistListRow struct {
widget.BaseWidget widget.BaseWidget