-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.go
95 lines (79 loc) · 1.63 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package control
import (
"strings"
"github.com/zmb3/spotify"
)
type Playlist struct {
spotify.FullPlaylist
isFull bool
isFollowed bool
}
func plFromSimple(p spotify.SimplePlaylist) Playlist {
return Playlist{
spotify.FullPlaylist{SimplePlaylist: p},
false,
false,
}
}
type PlaylistCache []Playlist
func (c *PlaylistCache) insertFull(index int, p spotify.FullPlaylist) {
left := append((*c)[:index], Playlist{p, true, false})
right := (*c)[index:]
*c = append(left, right...)
}
func (c *PlaylistCache) push(p Playlist) {
*c = append(PlaylistCache{p}, (*c)...)
}
func (c *PlaylistCache) remove(id spotify.ID) {
index := -1
for i, p := range *c {
if p.ID == id {
index = i
break
}
}
if index < 0 {
return
}
if index+1 == len(*c) {
*c = (*c)[:index]
return
}
left := (*c)[:index]
right := (*c)[index+1:]
*c = append(left, right...)
}
func (c *PlaylistCache) pushSimple(p spotify.SimplePlaylist) {
*c = append(*c, plFromSimple(p))
}
func (c *PlaylistCache) findByName(s string) *Playlist {
for i := range *c {
if strings.EqualFold((*c)[i].Name, s) {
return &(*c)[i]
}
}
return nil
}
func (c *PlaylistCache) pushFollowed(p spotify.SimplePlaylist) {
pl := Playlist{
FullPlaylist: spotify.FullPlaylist{SimplePlaylist: p},
isFollowed: true,
}
c.push(pl)
}
func (c *PlaylistCache) ownedNames() []string {
names := make([]string, 0, len(*c))
for _, p := range *c {
if !p.isFollowed {
names = append(names, p.Name)
}
}
return names
}
func (c *PlaylistCache) names() []string {
names := make([]string, 0, len(*c))
for _, p := range *c {
names = append(names, p.Name)
}
return names
}