Change errHandler from func(bool, err) to func(err)

This commit is contained in:
aditya-K2
2023-04-12 19:50:38 +05:30
parent dcabc44f40
commit 4adf71a1b4
9 changed files with 58 additions and 54 deletions

View File

@@ -40,9 +40,9 @@ var (
// If not cached or if the snapshotID for the playlist changes, it retrieves
// the playlist from Spotify API and caches it. It uses a background go routine
// to fetch all pages of tracks for the playlist and appends them to the tracks
// list in the Playlist object. When done, it sends a true value to the done
// callback if successful, otherwise an error.
func GetPlaylist(playlistId spotify.ID, done func(bool, error)) (*Playlist, error) {
// list in the Playlist object. When done, it passes the error value to the
// error handler.
func GetPlaylist(playlistId spotify.ID, errHandler func(error)) (*Playlist, error) {
if fp, err := Client.GetPlaylist(ctx(), playlistId); err != nil {
return nil, err
} else {
@@ -55,10 +55,10 @@ func GetPlaylist(playlistId spotify.ID, done func(bool, error)) (*Playlist, erro
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), &fp.Tracks); perr == spotify.ErrNoMorePages {
done(true, nil)
errHandler(nil)
break
} else if perr != nil {
done(false, perr)
errHandler(perr)
return
}
addTracks()
@@ -72,7 +72,7 @@ func GetPlaylist(playlistId spotify.ID, done func(bool, error)) (*Playlist, erro
playlistCache[fp.ID] = p
return p, nil
} else {
done(true, nil)
errHandler(nil)
return playlistCache[fp.ID], nil
}
}
@@ -83,8 +83,8 @@ func GetPlaylist(playlistId spotify.ID, done func(bool, error)) (*Playlist, erro
// If not, it retrieves the album from the Spotify API and caches it.
// It uses a background go routine to fetch all pages of tracks for the album
// and appends them to the tracks list in the Album object.
// When done, it sends a true value to the done callback if successful
func GetAlbum(albumID spotify.ID, done func(bool, error)) (*Album, error) {
// When done, it passes the error value to the error handler.
func GetAlbum(albumID spotify.ID, errHandler func(error)) (*Album, error) {
if _, ok := albumCache[albumID]; !ok {
fa, err := Client.GetAlbum(ctx(), albumID)
if err != nil {
@@ -98,10 +98,10 @@ func GetAlbum(albumID spotify.ID, done func(bool, error)) (*Album, error) {
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), &fa.Tracks); perr == spotify.ErrNoMorePages {
done(true, nil)
errHandler(nil)
break
} else if perr != nil {
done(false, perr)
errHandler(perr)
return
}
addTracks()
@@ -114,7 +114,7 @@ func GetAlbum(albumID spotify.ID, done func(bool, error)) (*Album, error) {
albumCache[fa.ID] = p
return p, nil
} else {
done(true, nil)
errHandler(nil)
return albumCache[albumID], nil
}
@@ -122,10 +122,9 @@ func GetAlbum(albumID spotify.ID, done func(bool, error)) (*Album, error) {
// CurrentUserSavedAlbums returns the SavedAlbums of the current user in a
// specific manner. It returns the first page and then starts a go routine
// in the background and keeps updating the SavedAlbums and calls the done
// function with a status of true and nil error if successful else calls the
// done function with a status of false and the corresponding error.
func CurrentUserSavedAlbums(done func(status bool, err error)) (*SavedAlbums, error) {
// in the background and keeps updating the SavedAlbums
// When done, it passes the error value to the error handler.
func CurrentUserSavedAlbums(errHandler func(err error)) (*SavedAlbums, error) {
_a := make(SavedAlbums, 0)
albums := &_a
if sp, err := Client.CurrentUsersAlbums(ctx()); err != nil {
@@ -138,10 +137,10 @@ func CurrentUserSavedAlbums(done func(status bool, err error)) (*SavedAlbums, er
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), sp); perr == spotify.ErrNoMorePages {
done(true, nil)
errHandler(nil)
break
} else if perr != nil {
done(false, perr)
errHandler(perr)
return
}
addAlbums()
@@ -153,10 +152,9 @@ func CurrentUserSavedAlbums(done func(status bool, err error)) (*SavedAlbums, er
// CurrentUserPlaylists returns the UserPlaylists of the current user in a
// specific manner. It returns the first page and then starts a go routine in
// the background and keeps updating the UserPlaylists and calls the done
// function with a status of true and nil error if successful else calls the
// done function with a status of false and the corresponding error.
func CurrentUserPlaylists(done func(status bool, err error)) (*UserPlaylists, error) {
// the background and keeps updating the UserPlaylists
// When done, it passes the error value to the error handler.
func CurrentUserPlaylists(errHandler func(err error)) (*UserPlaylists, error) {
_p := make(UserPlaylists, 0)
playlists := &_p
if spp, err := Client.CurrentUsersPlaylists(ctx()); err != nil {
@@ -169,10 +167,10 @@ func CurrentUserPlaylists(done func(status bool, err error)) (*UserPlaylists, er
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), spp); perr == spotify.ErrNoMorePages {
done(true, nil)
errHandler(nil)
break
} else if perr != nil {
done(false, perr)
errHandler(perr)
return
}
addPlaylists()
@@ -184,10 +182,9 @@ func CurrentUserPlaylists(done func(status bool, err error)) (*UserPlaylists, er
// CurrentUserSavedTracks returns the LikedSongs of the current user in a
// specific manner. It returns the first page and then starts a go routine in
// the background and keeps updating the LikedSongs and calls the done
// function with a status of true and nil error if successful else calls the
// done function with a status of false and the corresponding error.
func CurrentUserSavedTracks(done func(status bool, err error)) (*LikedSongs, error) {
// the background and keeps updating the LikedSongs
// When done, it passes the error value to the error handler.
func CurrentUserSavedTracks(errHandler func(err error)) (*LikedSongs, error) {
_p := make(LikedSongs, 0)
playlists := &_p
if ls, err := Client.CurrentUsersTracks(ctx()); err != nil {
@@ -200,10 +197,10 @@ func CurrentUserSavedTracks(done func(status bool, err error)) (*LikedSongs, err
go func() {
for page := 1; ; page++ {
if perr := Client.NextPage(ctx(), ls); perr == spotify.ErrNoMorePages {
done(true, nil)
errHandler(nil)
break
} else if perr != nil {
done(false, perr)
errHandler(perr)
return
}
addTracks()

View File

@@ -74,12 +74,13 @@ func NewApplication() *Application {
NavMenu.Table.SetBorder(true)
NavMenu.Table.SetSelectable(true, false)
done := func(s bool, err error) {
if s {
App.Draw()
playlistNav, err := NewPlaylistNav(func(err error) {
if err != nil {
panic(err)
}
}
playlistNav, err := NewPlaylistNav(done)
// Draw the App again after all the user playlists are retrieved.
App.Draw()
})
if err != nil {
panic(err)
}

View File

@@ -42,12 +42,12 @@ type PlaylistNav struct {
Table *tview.Table
Playlists *spt.UserPlaylists
c chan bool
done func(bool, error)
done func(error)
}
var PlaylistActions map[string]*Action
func NewPlaylistNav(done func(s bool, e error)) (*PlaylistNav, error) {
func NewPlaylistNav(done func(e error)) (*PlaylistNav, error) {
T := tview.NewTable()
T.SetSelectable(true, false).SetBorder(true)
p, err := spt.CurrentUserPlaylists(done)

View File

@@ -26,8 +26,8 @@ func (a *AlbumView) Content() func() [][]Content {
if a.currentAlbum != nil {
if a.currentFullAlbum == nil {
msg := SendNotificationWithChan(fmt.Sprintf("Loading %s....", a.currentAlbum.Name))
al, err := spt.GetAlbum(a.currentAlbum.ID, func(s bool, err error) {
if !s {
al, err := spt.GetAlbum(a.currentAlbum.ID, func(err error) {
if err != nil {
msg <- err.Error()
} else {
msg <- "Album Loaded Succesfully!"
@@ -57,7 +57,8 @@ func (a *AlbumView) ContextHandler() func(start, end, sel int) {
// Assuming that there are no external effects on the user's playlists
// (i.e Any Creation or Deletion of Playlists while the context Menu is
// open
userPlaylists, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
// TODO: Better Error Handler
userPlaylists, err := spt.CurrentUserPlaylists(func(err error) {})
if err != nil {
SendNotification("Error Retrieving User Playlists")
return

View File

@@ -14,9 +14,9 @@ func (a *AlbumsView) Content() func() [][]Content {
c := make([][]Content, 0)
if a.savedAlbums == nil {
msg := SendNotificationWithChan("Loading Albums from your Library...")
sa, err := spt.CurrentUserSavedAlbums(func(s bool, err error) {
sa, err := spt.CurrentUserSavedAlbums(func(err error) {
go func() {
if !s {
if err != nil {
msg <- err.Error()
} else {
msg <- "Albums loaded Succesfully!"

View File

@@ -9,9 +9,10 @@ func (d *DefaultView) ContextOpener() func(m *Root, s func(s int)) {
return func(m *Root, s func(s int)) {
c := NewMenu()
cc := []string{}
plist, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
// TODO: Better Error Handling
plist, err := spt.CurrentUserPlaylists(func(err error) {})
if err != nil {
SendNotification("Error Retrieving User Playlists")
SendNotification(err.Error())
return
}
for _, v := range *(plist) {

View File

@@ -18,10 +18,10 @@ func (p *LikedSongsView) Content() func() [][]Content {
c := make([][]Content, 0)
if p.likedSongs == nil {
msg := SendNotificationWithChan("Loading Liked Songs...")
if err := p.refreshState(func(s bool, e error) {
if err := p.refreshState(func(err error) {
go func() {
if !s {
msg <- e.Error()
if err != nil {
msg <- err.Error()
} else {
msg <- "Liked Songs Loaded Succesfully!"
}
@@ -47,7 +47,8 @@ func (l *LikedSongsView) ContextHandler() func(start, end, sel int) {
// Assuming that there are no external effects on the user's playlists
// (i.e Any Creation or Deletion of Playlists while the context Menu is
// open
userPlaylists, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
// TODO: Better Error Handler
userPlaylists, err := spt.CurrentUserPlaylists(func(err error) {})
if err != nil {
SendNotification("Error Retrieving User Playlists")
return
@@ -78,7 +79,7 @@ func (l *LikedSongsView) ExternalInputCapture() func(e *tcell.EventKey) *tcell.E
}
}
func (l *LikedSongsView) refreshState(f func(bool, error)) error {
func (l *LikedSongsView) refreshState(f func(error)) error {
pf, err := spt.CurrentUserSavedTracks(f)
if err == nil {
l.likedSongs = pf
@@ -89,7 +90,8 @@ func (l *LikedSongsView) refreshState(f func(bool, error)) error {
func (l *LikedSongsView) Name() string { return "LikedSongsView" }
func (l *LikedSongsView) RefreshState() {
if err := l.refreshState(func(bool, error) {}); err != nil {
// TODO: Better Error Handler
if err := l.refreshState(func(error) {}); err != nil {
SendNotification(err.Error())
}
}

View File

@@ -26,10 +26,10 @@ func (p *PlaylistView) Content() func() [][]Content {
if p.currentPlaylist != nil {
if p.currentUserFullPlaylist == nil {
msg := SendNotificationWithChan(fmt.Sprintf("Loading %s....", p.currentPlaylist.Name))
pf, err := spt.GetPlaylist(p.currentPlaylist.ID, func(s bool, e error) {
pf, err := spt.GetPlaylist(p.currentPlaylist.ID, func(err error) {
go func() {
if !s {
msg <- e.Error()
if err != nil {
msg <- err.Error()
} else {
msg <- "Playlist Loaded Succesfully!"
}
@@ -58,7 +58,8 @@ func (p *PlaylistView) ContextHandler() func(start, end, sel int) {
// Assuming that there are no external effects on the user's playlists
// (i.e Any Creation or Deletion of Playlists while the context Menu is
// open
userPlaylists, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
// TODO: Better Error Handling
userPlaylists, err := spt.CurrentUserPlaylists(func(err error) {})
if err != nil {
SendNotification("Error Retrieving User Playlists")
return

View File

@@ -45,7 +45,8 @@ func (r *RecentlyPlayedView) ContextHandler() func(start, end, sel int) {
// Assuming that there are no external effects on the user's playlists
// (i.e Any Creation or Deletion of Playlists while the context Menu is
// open
userPlaylists, err := spt.CurrentUserPlaylists(func(s bool, err error) {})
// TODO: Better Error Handler
userPlaylists, err := spt.CurrentUserPlaylists(func(err error) {})
if err != nil {
SendNotification("Error Retrieving User Playlists")
return