From 31e82ae3678e2c879cfb8efb584914facd4833f7 Mon Sep 17 00:00:00 2001 From: Drew Weymouth Date: Sat, 29 Apr 2023 10:21:40 -0700 Subject: [PATCH] refactor: generic SliceContains --- backend/app.go | 2 +- sharedutil/sharedutil.go | 19 +++++-------------- ui/browsing/albumspage.go | 2 +- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/backend/app.go b/backend/app.go index 653b8db..0e07f7b 100644 --- a/backend/app.go +++ b/backend/app.go @@ -125,7 +125,7 @@ func (a *App) setupMPV() error { a.Player.SetAudioDevice(desiredDevice) rgainOpts := []string{ReplayGainNone, ReplayGainAlbum, ReplayGainTrack} - if !sharedutil.StringSliceContains(rgainOpts, a.Config.ReplayGain.Mode) { + if !sharedutil.SliceContains(rgainOpts, a.Config.ReplayGain.Mode) { a.Config.ReplayGain.Mode = ReplayGainNone } a.Player.SetReplayGainOptions(player.ReplayGainOptions{ diff --git a/sharedutil/sharedutil.go b/sharedutil/sharedutil.go index 1729f9c..d5f6b5c 100644 --- a/sharedutil/sharedutil.go +++ b/sharedutil/sharedutil.go @@ -7,18 +7,9 @@ import ( "github.com/dweymouth/go-subsonic/subsonic" ) -func StringSliceContains(slice []string, str string) bool { - for _, s := range slice { - if s == str { - return true - } - } - return false -} - -func IntSliceContains(slice []int, i int) bool { - for _, x := range slice { - if x == i { +func SliceContains[T comparable](ts []T, t T) bool { + for _, x := range ts { + if x == t { return true } } @@ -85,7 +76,7 @@ func ReorderTracks(tracks []*subsonic.Child, idxToMove []int, op TrackReorderOp) topIdx := 0 botIdx := len(idxToMove) for i, t := range tracks { - if IntSliceContains(idxToMove, i) { + if SliceContains(idxToMove, i) { newTracks[topIdx] = t topIdx++ } else { @@ -97,7 +88,7 @@ func ReorderTracks(tracks []*subsonic.Child, idxToMove []int, op TrackReorderOp) topIdx := 0 botIdx := len(tracks) - len(idxToMove) for i, t := range tracks { - if IntSliceContains(idxToMove, i) { + if SliceContains(idxToMove, i) { newTracks[botIdx] = t botIdx++ } else { diff --git a/ui/browsing/albumspage.go b/ui/browsing/albumspage.go index 8c2ed16..121abe2 100644 --- a/ui/browsing/albumspage.go +++ b/ui/browsing/albumspage.go @@ -67,7 +67,7 @@ func NewAlbumsPage(cfg *backend.AlbumsPageConfig, contr *controller.Controller, SizeName: theme.SizeNameHeadingText, } a.sortOrder = NewSelect(backend.AlbumSortOrders, a.onSortOrderChanged) - if !sharedutil.StringSliceContains(backend.AlbumSortOrders, cfg.SortOrder) { + if !sharedutil.SliceContains(backend.AlbumSortOrders, cfg.SortOrder) { cfg.SortOrder = string(backend.AlbumSortRecentlyAdded) } a.sortOrder.Selected = cfg.SortOrder