Add custom sorting to the twitch channels widget

this allows the channels to be sorted as defined in the config while
still keeping the live channels on the top. the wording could probably
be improved, "custom" is too broad but i'm not sure what else to call
it.
This commit is contained in:
fawn 2024-05-18 03:07:34 +03:00
parent 21d491843f
commit 0681a04607
No known key found for this signature in database
GPG Key ID: E6C9811A14B38F80
3 changed files with 21 additions and 1 deletions

View File

@ -1063,6 +1063,7 @@ Preview:
| ---- | ---- | -------- | ------- |
| channels | array | yes | |
| collapse-after | integer | no | 5 |
| sort-by | string | no | viewers |
##### `channels`
A list of channels to display.
@ -1070,6 +1071,9 @@ A list of channels to display.
##### `collapse-after`
How many channels are visible before the "SHOW MORE" button appears. Set to `-1` to never collapse.
##### `sort-by`
Can be used to specify the order in which the channels are displayed. Possible values are `viewers` and `custom`.
### Twitch top games
Display a list of games with the most viewers on Twitch.

View File

@ -44,6 +44,12 @@ func (channels TwitchChannels) SortByViewers() {
})
}
func (channels TwitchChannels) SortByLive() {
sort.SliceStable(channels, func(i, j int) bool {
return channels[i].IsLive && !channels[j].IsLive
})
}
type twitchOperationResponse struct {
Data json.RawMessage
Extensions struct {

View File

@ -14,6 +14,7 @@ type TwitchChannels struct {
ChannelsRequest []string `yaml:"channels"`
Channels []feed.TwitchChannel `yaml:"-"`
CollapseAfter int `yaml:"collapse-after"`
SortBy string `yaml:"sort-by"`
}
func (widget *TwitchChannels) Initialize() error {
@ -23,6 +24,10 @@ func (widget *TwitchChannels) Initialize() error {
widget.CollapseAfter = 5
}
if widget.SortBy != "viewers" && widget.SortBy != "custom" {
widget.SortBy = "viewers"
}
return nil
}
@ -33,7 +38,12 @@ func (widget *TwitchChannels) Update(ctx context.Context) {
return
}
channels.SortByViewers()
if widget.SortBy == "viewers" {
channels.SortByViewers()
} else if widget.SortBy == "custom" {
channels.SortByLive()
}
widget.Channels = channels
}