refactor: generic SliceContains

This commit is contained in:
Drew Weymouth
2023-04-29 10:21:40 -07:00
parent 329b290d61
commit 31e82ae367
3 changed files with 7 additions and 16 deletions
+1 -1
View File
@@ -125,7 +125,7 @@ func (a *App) setupMPV() error {
a.Player.SetAudioDevice(desiredDevice) a.Player.SetAudioDevice(desiredDevice)
rgainOpts := []string{ReplayGainNone, ReplayGainAlbum, ReplayGainTrack} 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.Config.ReplayGain.Mode = ReplayGainNone
} }
a.Player.SetReplayGainOptions(player.ReplayGainOptions{ a.Player.SetReplayGainOptions(player.ReplayGainOptions{
+5 -14
View File
@@ -7,18 +7,9 @@ import (
"github.com/dweymouth/go-subsonic/subsonic" "github.com/dweymouth/go-subsonic/subsonic"
) )
func StringSliceContains(slice []string, str string) bool { func SliceContains[T comparable](ts []T, t T) bool {
for _, s := range slice { for _, x := range ts {
if s == str { if x == t {
return true
}
}
return false
}
func IntSliceContains(slice []int, i int) bool {
for _, x := range slice {
if x == i {
return true return true
} }
} }
@@ -85,7 +76,7 @@ func ReorderTracks(tracks []*subsonic.Child, idxToMove []int, op TrackReorderOp)
topIdx := 0 topIdx := 0
botIdx := len(idxToMove) botIdx := len(idxToMove)
for i, t := range tracks { for i, t := range tracks {
if IntSliceContains(idxToMove, i) { if SliceContains(idxToMove, i) {
newTracks[topIdx] = t newTracks[topIdx] = t
topIdx++ topIdx++
} else { } else {
@@ -97,7 +88,7 @@ func ReorderTracks(tracks []*subsonic.Child, idxToMove []int, op TrackReorderOp)
topIdx := 0 topIdx := 0
botIdx := len(tracks) - len(idxToMove) botIdx := len(tracks) - len(idxToMove)
for i, t := range tracks { for i, t := range tracks {
if IntSliceContains(idxToMove, i) { if SliceContains(idxToMove, i) {
newTracks[botIdx] = t newTracks[botIdx] = t
botIdx++ botIdx++
} else { } else {
+1 -1
View File
@@ -67,7 +67,7 @@ func NewAlbumsPage(cfg *backend.AlbumsPageConfig, contr *controller.Controller,
SizeName: theme.SizeNameHeadingText, SizeName: theme.SizeNameHeadingText,
} }
a.sortOrder = NewSelect(backend.AlbumSortOrders, a.onSortOrderChanged) 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) cfg.SortOrder = string(backend.AlbumSortRecentlyAdded)
} }
a.sortOrder.Selected = cfg.SortOrder a.sortOrder.Selected = cfg.SortOrder