-
Notifications
You must be signed in to change notification settings - Fork 8
/
topic.go
140 lines (124 loc) · 2.6 KB
/
topic.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"fmt"
"github.com/0neSe7en/jikefm/jike"
)
type Topic struct {
id string
name string
playing int
playlist []jike.Message
skip string
more chan bool
ui *TopicUI
}
type TopicList map[string]*Topic
var topicOrder = []string{
"55483ddee4b03ccbae843925",
"5899dc1063ed1a0015bade08",
"5a1ccf936e6e7c0011037480",
}
var topics = TopicList{
"55483ddee4b03ccbae843925": {
id: "55483ddee4b03ccbae843925",
name: "晚安电台",
playing: -1,
},
"5899dc1063ed1a0015bade08": {
id: "5899dc1063ed1a0015bade08",
name: "早安歌曲",
playing: -1,
},
"5a1ccf936e6e7c0011037480": {
id: "5a1ccf936e6e7c0011037480",
name: "即友在听什么歌",
playing: -1,
},
}
func init() {
for _, topic := range topics {
topic.more = make(chan bool)
go topic.FetchMore()
}
}
func (t *Topic) FetchMore() {
for {
<- t.more
msgs := t.Feed()
UI.app.QueueUpdateDraw(func() {
t.AddToPlaylist(msgs)
})
}
}
func (t *Topic) ChangeSong(from int, target int) {
if from >= 0 {
t.ui.side.SetItemText(
from,
normalText(from + 1, t.playlist[from].GetTitle()),
"",
)
}
if target >= 0 {
t.ui.side.SetItemText(
target,
playingText(target + 1, t.playlist[target].GetTitle()),
"",
)
}
}
func (t *Topic) AddToPlaylist(messages []jike.Message) {
for _, msg := range messages {
t.playlist = append(t.playlist, msg)
t.ui.side.AddItem(normalText(len(t.playlist), msg.GetTitle()), "", 0, nil)
}
t.ui.side.SetTitle(fmt.Sprintf(" 歌曲数: %d", len(t.playlist)))
}
func (t *Topic) BindUI(ui *TopicUI) *Topic {
t.ui = ui
t.ui.side.
SetChangedFunc(t.onSelect).
ShowSecondaryText(false)
return t
}
func (t *Topic) SetSelectedFunc(handler func (topicId string, index int)) *Topic {
if t.ui == nil { return t}
t.ui.side.SetSelectedFunc(func (index int, _ string, _ string, _ rune) {
handler(t.id, index)
})
return t
}
func (t *Topic) fetch() []jike.Message {
res, next, _ := jike.FetchMoreSelectedFM(CurrentSession, t.id, t.skip)
t.skip = next
return res
}
func (t *Topic) Feed() []jike.Message {
msgs := t.fetch()
for len(msgs) < 20 {
for _, msg := range t.fetch() {
msgs = append(msgs, msg)
}
}
return msgs
}
func (t *Topic) onSelect(index int, _ string, _ string, _ rune) {
i := index
if index < 0 {
i = len(t.playlist) + index
}
if index >= len(t.playlist) {
i = index - len(t.playlist)
}
if index == len(t.playlist)-1 {
t.queueMore()
}
msg := t.playlist[i]
t.ui.content.SetText(msg.Content)
t.ui.author.SetText("[green]@" + msg.User.ScreenName)
}
func (t *Topic) queueMore() {
select {
case t.more <- true:
default:
}
}