瀏覽代碼

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.
fawn 1 年之前
父節點
當前提交
0681a04607
共有 3 個文件被更改,包括 21 次插入1 次删除
  1. 4 0
      docs/configuration.md
  2. 6 0
      internal/feed/twitch.go
  3. 11 1
      internal/widget/twitch-channels.go

+ 4 - 0
docs/configuration.md

@@ -1063,6 +1063,7 @@ Preview:
 | ---- | ---- | -------- | ------- |
 | ---- | ---- | -------- | ------- |
 | channels | array | yes | |
 | channels | array | yes | |
 | collapse-after | integer | no | 5 |
 | collapse-after | integer | no | 5 |
+| sort-by | string | no | viewers |
 
 
 ##### `channels`
 ##### `channels`
 A list of channels to display.
 A list of channels to display.
@@ -1070,6 +1071,9 @@ A list of channels to display.
 ##### `collapse-after`
 ##### `collapse-after`
 How many channels are visible before the "SHOW MORE" button appears. Set to `-1` to never collapse.
 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
 ### Twitch top games
 Display a list of games with the most viewers on Twitch.
 Display a list of games with the most viewers on Twitch.
 
 

+ 6 - 0
internal/feed/twitch.go

@@ -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 {
 type twitchOperationResponse struct {
 	Data       json.RawMessage
 	Data       json.RawMessage
 	Extensions struct {
 	Extensions struct {

+ 11 - 1
internal/widget/twitch-channels.go

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