From 3f124bebecbdeb6a09a877a55fcd92d7985bda82 Mon Sep 17 00:00:00 2001 From: ayntgl <90600421+ayntgl@users.noreply.github.com> Date: Fri, 19 Aug 2022 17:15:07 +0530 Subject: [PATCH] feat(ui): implement guild folders (#182) --- ui/app.go | 60 ++++++++++++++++++++++++++++++++++++-------------- ui/channels.go | 2 +- ui/guilds.go | 6 +++++ 3 files changed, 50 insertions(+), 18 deletions(-) diff --git a/ui/app.go b/ui/app.go index d9ff7e4..e0b4b28 100644 --- a/ui/app.go +++ b/ui/app.go @@ -2,7 +2,6 @@ package ui import ( "context" - "sort" "strings" "github.com/ayntgl/discordo/config" @@ -111,26 +110,53 @@ func (app *App) DrawMainFlex() { } func (app *App) onStateReady(r *gateway.ReadyEvent) { - sort.Slice(r.Guilds, func(a, b int) bool { - found := false - for _, guildID := range r.UserSettings.GuildPositions { - if found && guildID == r.Guilds[b].ID { - return true + rootNode := app.GuildsTree.GetRoot() + for _, gf := range r.UserSettings.GuildFolders { + if gf.ID == 0 { + for _, gID := range gf.GuildIDs { + g, err := app.State.Cabinet.Guild(gID) + if err != nil { + return + } + + guildNode := tview.NewTreeNode(g.Name) + guildNode.SetReference(g.ID) + rootNode.AddChild(guildNode) } - if !found && guildID == r.Guilds[a].ID { - found = true + } else { + var b strings.Builder + + if gf.Color != discord.NullColor { + b.WriteByte('[') + b.WriteString(gf.Color.String()) + b.WriteByte(']') + } else { + b.WriteString("[#ED4245]") + } + + if gf.Name != "" { + b.WriteString(gf.Name) + } else { + b.WriteString("Folder") + } + + b.WriteString("[-]") + + folderNode := tview.NewTreeNode(b.String()) + rootNode.AddChild(folderNode) + + for _, gID := range gf.GuildIDs { + g, err := app.State.Cabinet.Guild(gID) + if err != nil { + return + } + + guildNode := tview.NewTreeNode(g.Name) + guildNode.SetReference(g.ID) + folderNode.AddChild(guildNode) } } - return false - }) - - rootNode := app.GuildsTree.GetRoot() - for _, g := range r.Guilds { - guildNode := tview.NewTreeNode(g.Name) - guildNode.SetReference(g.ID) - - rootNode.AddChild(guildNode) } app.GuildsTree.SetCurrentNode(rootNode) diff --git a/ui/channels.go b/ui/channels.go index fc63817..79d88b5 100644 --- a/ui/channels.go +++ b/ui/channels.go @@ -43,7 +43,7 @@ func (ct *ChannelsTree) onSelected(node *tview.TreeNode) { return } - // If the channel is a category channel, expend the selected node if it is not expanded already. + // If the channel is a category channel, expand the selected node if it is collapsed, otherwise collapse. if c.Type == discord.GuildCategory { node.SetExpanded(!node.IsExpanded()) return diff --git a/ui/guilds.go b/ui/guilds.go index 98e26de..7795388 100644 --- a/ui/guilds.go +++ b/ui/guilds.go @@ -44,6 +44,12 @@ func (gt *GuildsTree) onSelected(node *tview.TreeNode) { SetTitle("") gt.app.MessageInputField.SetText("") + // If the selected node has children (guild folder), expand the selected node if it is collapsed, otherwise collapse. + if len(node.GetChildren()) != 0 { + node.SetExpanded(!node.IsExpanded()) + return + } + ref := node.GetReference() // If the reference of the selected node is nil, it must be the direct messages node. if ref == nil {