Add Device Menu

This commit is contained in:
aditya-K2
2023-04-15 00:59:57 +05:30
parent d741fcac45
commit b96539f4e5
3 changed files with 45 additions and 1 deletions

View File

@@ -297,3 +297,16 @@ func Search(s string) (*spotify.SearchResult, error) {
spotify.SearchTypeTrack|
spotify.SearchTypeArtist)
}
func UserDevices() ([]spotify.PlayerDevice, error) {
return Client.PlayerDevices(ctx())
}
func TransferPlayback(deviceId spotify.ID) error {
s, err := GetPlayerState()
if err != nil {
return errors.New("Unable to get Current Player State!")
}
err = Client.PauseOpt(ctx(), &spotify.PlayOptions{DeviceID: &s.Device.ID})
return Client.TransferPlayback(ctx(), deviceId, true)
}

View File

@@ -239,7 +239,7 @@ func NewApplication() *Application {
for {
_ImgX, _ImgY, _ImgW, _ImgH := Ui.CoverArt.GetRect()
if start {
RefreshProgress()
RefreshProgress(false)
start = false
}
if _ImgX != ImgX || _ImgY != ImgY ||
@@ -285,6 +285,10 @@ func NewApplication() *Application {
Ui.App.SetFocus(Main.Table)
return nil
}
if e.Rune() == 'd' {
OpenDeviceMenu()
return nil
}
return e
})
Ui = &Application{

27
ui/device.go Normal file
View File

@@ -0,0 +1,27 @@
package ui
import "github.com/aditya-K2/gspt/spt"
func OpenDeviceMenu() {
m := NewMenu()
cc := []string{}
// TODO: Better Error Handling
devices, err := spt.UserDevices()
if err != nil {
SendNotification(err.Error())
return
}
for _, v := range devices {
cc = append(cc, v.Name)
}
m.Content(cc)
m.Title("Choose A Device")
m.SetSelectionHandler(func(s int) {
if err := spt.TransferPlayback(devices[s].ID); err != nil {
SendNotification(err.Error())
} else {
RefreshProgress(true)
}
})
Ui.Root.AddCenteredWidget(m)
}