Ability to download tracks
- Adding the option to download the selected track or tracks. When the download is clicked it opens a dialog that lets you choose where you want to save the track and the file name (by default the file name is the track number, the track name and the extension, example "03 - my song.mp3") If there are many tracks selected it will open one dialog for each track - Next steps: download an album and a playlist using zip
This commit is contained in:
@@ -3,7 +3,10 @@ package controller
|
||||
import (
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/dweymouth/supersonic/backend"
|
||||
@@ -113,6 +116,7 @@ func (m *Controller) ConnectTracklistActions(tracklist *widgets.Tracklist) {
|
||||
tracklist.OnColumnVisibilityMenuShown = func(pop *widget.PopUp) {
|
||||
m.ClosePopUpOnEscape(pop)
|
||||
}
|
||||
tracklist.OnShowDownloadDialog = m.ShowDownloadDialog
|
||||
}
|
||||
|
||||
func (m *Controller) ConnectAlbumGridActions(grid *widgets.GridView) {
|
||||
@@ -516,3 +520,49 @@ func (c *Controller) SetTrackRatings(trackIDs []string, rating int) {
|
||||
c.App.PlaybackManager.OnTrackRatingChanged(id, rating)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Controller) ShowDownloadDialog(track *mediaprovider.Track) {
|
||||
parts := strings.Split(track.FilePath, "/")
|
||||
fileName := parts[len(parts)-1]
|
||||
|
||||
dg := dialog.NewFileSave(
|
||||
func(file fyne.URIWriteCloser, err error) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
if file == nil {
|
||||
return
|
||||
}
|
||||
|
||||
filePath := file.URI().Path()
|
||||
go c.downloadTrack(track.ID, filePath)
|
||||
},
|
||||
c.MainWindow)
|
||||
dg.SetFileName(fileName)
|
||||
dg.Show()
|
||||
}
|
||||
|
||||
func (c *Controller) downloadTrack(trackID, filePath string) {
|
||||
reader, err := c.App.ServerManager.Server.DownloadTrack(trackID)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
saveDir, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
defer saveDir.Close()
|
||||
|
||||
_, err = io.Copy(saveDir, reader)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Saved song to: %s\n", saveDir.Name())
|
||||
}
|
||||
|
||||
+17
-6
@@ -70,12 +70,13 @@ type Tracklist struct {
|
||||
DisableSorting bool
|
||||
|
||||
// user action callbacks
|
||||
OnPlayTrackAt func(int)
|
||||
OnPlaySelection func(tracks []*mediaprovider.Track, shuffle bool)
|
||||
OnAddToQueue func(trackIDs []*mediaprovider.Track)
|
||||
OnAddToPlaylist func(trackIDs []string)
|
||||
OnSetFavorite func(trackIDs []string, fav bool)
|
||||
OnSetRating func(trackIDs []string, rating int)
|
||||
OnPlayTrackAt func(int)
|
||||
OnPlaySelection func(tracks []*mediaprovider.Track, shuffle bool)
|
||||
OnAddToQueue func(trackIDs []*mediaprovider.Track)
|
||||
OnAddToPlaylist func(trackIDs []string)
|
||||
OnSetFavorite func(trackIDs []string, fav bool)
|
||||
OnSetRating func(trackIDs []string, rating int)
|
||||
OnShowDownloadDialog func(track *mediaprovider.Track)
|
||||
|
||||
OnShowArtistPage func(artistID string)
|
||||
OnShowAlbumPage func(albumID string)
|
||||
@@ -508,6 +509,10 @@ func (t *Tracklist) onShowContextMenu(e *fyne.PointEvent, trackIdx int) {
|
||||
t.OnAddToPlaylist(t.SelectedTrackIDs())
|
||||
}
|
||||
}))
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items,
|
||||
fyne.NewMenuItem("Download", func() {
|
||||
t.onDownload(t.selectedTracks())
|
||||
}))
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items, fyne.NewMenuItemSeparator())
|
||||
t.ctxMenu.Items = append(t.ctxMenu.Items,
|
||||
fyne.NewMenuItem("Set favorite", func() {
|
||||
@@ -582,6 +587,12 @@ func (t *Tracklist) onAlbumTapped(albumID string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tracklist) onDownload(tracks []*mediaprovider.Track) {
|
||||
for _, track := range tracks {
|
||||
t.OnShowDownloadDialog(track)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Tracklist) findTrackByID(id string) *mediaprovider.Track {
|
||||
idx := sharedutil.Find(t.tracks, func(tr *trackModel) bool {
|
||||
return tr.track.ID == id
|
||||
|
||||
Reference in New Issue
Block a user