simplify init of searchdialog
This commit is contained in:
@@ -14,18 +14,8 @@ type QuickSearch struct {
|
||||
}
|
||||
|
||||
func NewQuickSearch(mp mediaprovider.MediaProvider, im util.ImageFetcher) *QuickSearch {
|
||||
|
||||
q := &QuickSearch{
|
||||
mp: mp,
|
||||
}
|
||||
|
||||
sd := NewSearchDialog(
|
||||
im,
|
||||
"Quick Search",
|
||||
q.onSearched,
|
||||
nil,
|
||||
)
|
||||
q.SearchDialog = sd
|
||||
q := &QuickSearch{mp: mp}
|
||||
q.SearchDialog = NewSearchDialog(im, "Quick Search", "Close", q.onSearched)
|
||||
return q
|
||||
}
|
||||
|
||||
|
||||
+34
-54
@@ -26,30 +26,34 @@ type SearchDialog struct {
|
||||
|
||||
PlaceholderText string
|
||||
|
||||
// Additional item that can be placed to the left
|
||||
// of the dismiss buttons
|
||||
ActionItem fyne.CanvasObject
|
||||
|
||||
OnDismiss func()
|
||||
OnNavigateTo func(mediaprovider.ContentType, string)
|
||||
OnSearched func(string) []*mediaprovider.SearchResult
|
||||
OnInit func() ([]*mediaprovider.SearchResult, *widget.Check)
|
||||
|
||||
imgSource util.ImageFetcher
|
||||
resultsMutex sync.RWMutex
|
||||
searchResults []*mediaprovider.SearchResult
|
||||
selectedIndex int
|
||||
|
||||
searchEntry *searchEntry
|
||||
loadingDots *widgets.LoadingDots
|
||||
list *widget.List
|
||||
placeholderTitle string
|
||||
content *fyne.Container
|
||||
searchEntry *searchEntry
|
||||
loadingDots *widgets.LoadingDots
|
||||
list *widget.List
|
||||
dialogTitle string
|
||||
dismissText string
|
||||
content *fyne.Container
|
||||
}
|
||||
|
||||
func NewSearchDialog(im util.ImageFetcher, placeholderTitle string, onSearched func(string) []*mediaprovider.SearchResult, onInit func() ([]*mediaprovider.SearchResult, *widget.Check)) *SearchDialog {
|
||||
func NewSearchDialog(im util.ImageFetcher, title, dismissBtn string, onSearched func(string) []*mediaprovider.SearchResult) *SearchDialog {
|
||||
sd := &SearchDialog{
|
||||
imgSource: im,
|
||||
loadingDots: widgets.NewLoadingDots(),
|
||||
OnSearched: onSearched,
|
||||
OnInit: onInit,
|
||||
placeholderTitle: placeholderTitle,
|
||||
imgSource: im,
|
||||
loadingDots: widgets.NewLoadingDots(),
|
||||
OnSearched: onSearched,
|
||||
dialogTitle: title,
|
||||
dismissText: dismissBtn,
|
||||
}
|
||||
sd.ExtendBaseWidget(sd)
|
||||
|
||||
@@ -95,8 +99,8 @@ func (sd *SearchDialog) SearchQuery() string {
|
||||
}
|
||||
|
||||
func (sd *SearchDialog) Show() {
|
||||
sd.onInit()
|
||||
sd.BaseWidget.Show()
|
||||
go sd.onSearched("")
|
||||
}
|
||||
|
||||
func (sd *SearchDialog) Refresh() {
|
||||
@@ -155,47 +159,6 @@ func (sd *SearchDialog) setResults(results []*mediaprovider.SearchResult) {
|
||||
sd.list.Select(0)
|
||||
}
|
||||
|
||||
func (sd *SearchDialog) SetContent(checkBox *widget.Check) {
|
||||
dismissBtn := widget.NewButton("Close", sd.onDismiss)
|
||||
title := widget.NewRichText(&widget.TextSegment{Text: sd.placeholderTitle, Style: util.BoldRichTextStyle})
|
||||
title.Segments[0].(*widget.TextSegment).Style.Alignment = fyne.TextAlignCenter
|
||||
if checkBox != nil {
|
||||
sd.content = container.NewStack(
|
||||
container.NewBorder(
|
||||
container.NewVBox(title, sd.searchEntry),
|
||||
container.NewVBox(widget.NewSeparator(), container.NewHBox(checkBox, layout.NewSpacer(), dismissBtn)),
|
||||
nil, nil, sd.list),
|
||||
container.NewCenter(sd.loadingDots),
|
||||
)
|
||||
} else {
|
||||
sd.content = container.NewStack(
|
||||
container.NewBorder(
|
||||
container.NewVBox(title, sd.searchEntry),
|
||||
container.NewVBox(widget.NewSeparator(), container.NewHBox(layout.NewSpacer(), dismissBtn)),
|
||||
nil, nil, sd.list),
|
||||
container.NewCenter(sd.loadingDots),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (sd *SearchDialog) onInit() {
|
||||
if sd.OnInit == nil {
|
||||
sd.SetContent(nil)
|
||||
return
|
||||
}
|
||||
sd.loadingDots.Start()
|
||||
var results []*mediaprovider.SearchResult
|
||||
res, checkBox := sd.OnInit()
|
||||
if len(res) == 0 {
|
||||
log.Println("No results")
|
||||
} else {
|
||||
results = res
|
||||
}
|
||||
sd.SetContent(checkBox)
|
||||
sd.loadingDots.Stop()
|
||||
sd.setResults(results)
|
||||
}
|
||||
|
||||
func (sd *SearchDialog) onSearched(query string) {
|
||||
sd.loadingDots.Start()
|
||||
var results []*mediaprovider.SearchResult
|
||||
@@ -210,6 +173,23 @@ func (sd *SearchDialog) onSearched(query string) {
|
||||
}
|
||||
|
||||
func (sd *SearchDialog) CreateRenderer() fyne.WidgetRenderer {
|
||||
dismissBtn := widget.NewButton(sd.dismissText, sd.onDismiss)
|
||||
title := widget.NewRichText(&widget.TextSegment{Text: sd.dialogTitle, Style: util.BoldRichTextStyle})
|
||||
title.Segments[0].(*widget.TextSegment).Style.Alignment = fyne.TextAlignCenter
|
||||
bottomRow := container.NewHBox()
|
||||
if sd.ActionItem != nil {
|
||||
bottomRow.Objects = []fyne.CanvasObject{sd.ActionItem, layout.NewSpacer(), dismissBtn}
|
||||
} else {
|
||||
bottomRow.Objects = []fyne.CanvasObject{layout.NewSpacer(), dismissBtn}
|
||||
}
|
||||
sd.content = container.NewStack(
|
||||
container.NewBorder(
|
||||
container.NewVBox(title, sd.searchEntry),
|
||||
container.NewVBox(widget.NewSeparator(), bottomRow),
|
||||
nil, nil, sd.list),
|
||||
container.NewCenter(sd.loadingDots),
|
||||
)
|
||||
|
||||
return widget.NewSimpleRenderer(sd.content)
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/data/binding"
|
||||
"fyne.io/fyne/v2/widget"
|
||||
"github.com/deluan/sanitize"
|
||||
"github.com/dweymouth/supersonic/backend/mediaprovider"
|
||||
@@ -23,39 +24,34 @@ type SelectPlaylist struct {
|
||||
}
|
||||
|
||||
func NewSelectPlaylistDialog(mp mediaprovider.MediaProvider, im util.ImageFetcher, loggedInUser string) *SelectPlaylist {
|
||||
|
||||
sp := &SelectPlaylist{
|
||||
mp: mp,
|
||||
loggedInUser: loggedInUser,
|
||||
SkipDuplicates: false,
|
||||
}
|
||||
sp.fetchUserOwnedPlaylists()
|
||||
sd := NewSearchDialog(
|
||||
im,
|
||||
"Select playlist",
|
||||
"Add to playlist",
|
||||
"Cancel",
|
||||
sp.onSearched,
|
||||
sp.onInit,
|
||||
)
|
||||
sd.ActionItem = widget.NewCheckWithData("Skip duplicate tracks", binding.BindBool(&sp.SkipDuplicates))
|
||||
sd.PlaceholderText = "Search playlists or new playlist name"
|
||||
sp.SearchDialog = sd
|
||||
return sp
|
||||
}
|
||||
|
||||
func (sp *SelectPlaylist) onInit() ([]*mediaprovider.SearchResult, *widget.Check) {
|
||||
var results []*mediaprovider.SearchResult
|
||||
func (sp *SelectPlaylist) fetchUserOwnedPlaylists() {
|
||||
playlists, err := sp.mp.GetPlaylists()
|
||||
if err != nil {
|
||||
// TODO: surface this error to user
|
||||
log.Printf("error getting playlists: %s", err.Error())
|
||||
return results, nil
|
||||
}
|
||||
userPlaylists := sharedutil.FilterSlice(playlists, func(playlist *mediaprovider.Playlist) bool {
|
||||
return playlist.Owner == sp.loggedInUser
|
||||
})
|
||||
sp.allPlaylistResuts = sharedutil.MapSlice(userPlaylists, sp.playlistToSearchResult)
|
||||
skipDuplicatesCheck := widget.NewCheck("Skip duplicate tracks", func(checked bool) {
|
||||
sp.SkipDuplicates = checked
|
||||
})
|
||||
return results, skipDuplicatesCheck
|
||||
}
|
||||
|
||||
func (sp *SelectPlaylist) playlistToSearchResult(playlist *mediaprovider.Playlist) *mediaprovider.SearchResult {
|
||||
|
||||
Reference in New Issue
Block a user