neofeels/app/subscriptions.go

97 lines
2.7 KiB
Go

package app
import (
"strconv"
"git.sr.ht/~rockorager/vaxis"
"git.tilde.town/nbsp/neofeels/ttbp"
"git.tilde.town/nbsp/neofeels/ui"
)
type Subscriptions struct {
title string
list ui.List
help string
neighbors []ttbp.User
subscriptions *ttbp.Subscriptions
}
func NewSubscriptions() *Subscriptions {
users := ttbp.SortUsersByRecent(ttbp.GetUsers())
subscriptions := ttbp.GetSubscriptions()
list := []string{}
neighbors := []ttbp.User{}
for _, user := range users {
if subscriptions.IsSubscribed(user) {
list = append(list, formatNeighbor(user, subscriptions))
neighbors = append(neighbors, user)
}
}
return &Subscriptions{
title,
ui.NewList(list),
"↑↓/kj move ↵ enter s subscribe q return",
neighbors,
subscriptions,
}
}
func (subscriptions *Subscriptions) Event(state *ui.State, event vaxis.Event) (processed bool) {
if key, ok := event.(vaxis.Key); ok && key.EventType == vaxis.EventPress {
switch key.String() {
case "Ctrl+c", "Ctrl+d":
close(ui.Quit)
case "Down", "j":
subscriptions.list.Down()
case "Up", "k":
subscriptions.list.Up()
case "End", "Shift+g":
subscriptions.list.End()
case "Home", "g":
subscriptions.list.Home()
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
i, _ := strconv.Atoi(key.String())
subscriptions.list.SetIndex(i)
case "q", "h", "Left":
subscriptions.subscriptions.Write()
ui.ViewChange <- NewMainMenu()
case "s":
if len(subscriptions.list.Items()) > 0 {
user := subscriptions.neighbors[subscriptions.list.Index()]
if subscriptions.subscriptions.IsSubscribed(user) {
subscriptions.subscriptions.Unsubscribe(user)
} else {
subscriptions.subscriptions.Subscribe(user)
}
subscriptions.list.SetItem(subscriptions.list.Index(), formatNeighbor(user, subscriptions.subscriptions))
}
case "Enter", "l", "Right":
if len(subscriptions.list.Items()) > 0 {
subscriptions.subscriptions.Write()
ui.ViewChange <- NewUserPage(subscriptions.neighbors[subscriptions.list.Index()].Name)
}
}
processed = true
}
subscriptions.Draw(state)
return
}
func (subscriptions *Subscriptions) Draw(state *ui.State) {
win := state.Window()
win.New(win.Width/2-10, win.Height/2-8, 20, 5).Print(vaxis.Segment{Text: subscriptions.title})
subscriptions.list.Draw(vaxis.Window{
Vx: win.Vx,
Parent: nil,
Column: win.Width/2 - 40,
Row: win.Height/2 - 2,
Width: 80,
Height: 10,
})
if len(subscriptions.list.Items()) == 0 {
win.New(win.Width/2-22, win.Height/2-1, 44, 2).Print(vaxis.Segment{Text: "no subscriptions yet\nvisit your neighbors to subscribe to them!"})
}
win.New(win.Width/2-22, win.Height/2+9, 44, 1).Print(vaxis.Segment{Text: subscriptions.help})
}