1
0
mirror of https://github.com/mrusme/neonmodem.git synced 2024-12-04 14:46:37 -05:00

Implemented reply feature

This commit is contained in:
マリウス 2023-01-02 20:34:39 -05:00
parent 0e987d1e4f
commit 4a9ed09782
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
4 changed files with 84 additions and 52 deletions

View File

@ -18,6 +18,8 @@ const (
ViewFreshData
MsgError
WMCloseWin
)
type Arg struct {

View File

@ -115,6 +115,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var ccmds []tea.Cmd
switch msg.Call {
case cmd.WinOpen:
switch msg.Target {
case postshow.WIN_ID:
@ -137,12 +138,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
)
}
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)
case cmd.WinClose:
switch msg.Target {
case postcreate.WIN_ID:
m.ctx.Logger.Debugln("received WinClose")
m.renderOnlyFocused = false
}
case cmd.WMCloseWin:
if ok, clcmds := m.wm.Close(msg.Target); ok {
cmds = append(cmds, clcmds...)
}
default:
if msg.Call < cmd.ViewFocus {
m.ctx.Logger.Debugf("updating all with cmd: %v\n", msg)

View File

@ -2,6 +2,7 @@ package postcreate
import (
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/cursor"
@ -48,8 +49,9 @@ type Model struct {
a *aggregator.Aggregator
glam *glamour.TermRenderer
activeReply *reply.Reply
replyToIdx int
replyToIdx int
replyTo string
replyToIface interface{}
viewcache string
viewcacheTextareaXY []int
@ -74,7 +76,9 @@ func NewModel(c *ctx.Ctx) Model {
focused: false,
xywh: [4]int{0, 0, 0, 0},
replyToIdx: 0,
replyToIdx: 0,
replyTo: "",
replyToIface: nil,
viewcache: "",
viewcacheTextareaXY: []int{0, 0, 0, 0},
@ -97,39 +101,38 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, m.keymap.Reply):
// replyToIdx, _ := strconv.Atoi(m.buffer)
//
// m.ctx.Logger.Debugf("replyToIdx: %d", replyToIdx)
//
// var irtID string = ""
// var irtIRT string = ""
// var irtSysIDX int = 0
//
// if replyToIdx == 0 {
// irtID = m.activePost.ID
// irtSysIDX = m.activePost.SysIDX
// } else {
// irt := m.allReplies[(replyToIdx - 1)]
// irtID = strconv.Itoa(replyToIdx + 1)
// irtIRT = irt.InReplyTo
// irtSysIDX = irt.SysIDX
// }
//
// r := reply.Reply{
// ID: irtID,
// InReplyTo: irtIRT,
// Body: m.textarea.Value(),
// SysIDX: irtSysIDX,
// }
// err := m.a.CreateReply(&r)
// if err != nil {
// m.ctx.Logger.Error(err)
// }
//
// m.textarea.Reset()
// m.buffer = ""
// m.WMClose("reply")
// return m, nil
var irtID string = ""
var irtIRT string = ""
var irtSysIDX int = 0
if m.replyToIdx == 0 {
pst := m.replyToIface.(post.Post)
irtID = pst.ID
irtSysIDX = pst.SysIDX
} else {
rply := m.replyToIface.(reply.Reply)
irtID = strconv.Itoa(m.replyToIdx + 1)
irtIRT = rply.InReplyTo
irtSysIDX = rply.SysIDX
}
r := reply.Reply{
ID: irtID,
InReplyTo: irtIRT,
Body: m.textarea.Value(),
SysIDX: irtSysIDX,
}
err := m.a.CreateReply(&r)
if err != nil {
m.ctx.Logger.Error(err)
// TODO
}
m.textarea.Reset()
m.replyToIdx = 0
return m, cmd.New(cmd.WMCloseWin, WIN_ID).Tea()
}
@ -144,6 +147,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case cmd.WinOpen:
if msg.Target == WIN_ID {
m.xywh = msg.GetArg("xywh").([4]int)
m.replyToIdx = msg.GetArg("replyToIdx").(int)
m.replyTo = msg.GetArg("replyTo").(string)
m.replyToIface = msg.GetArg(m.replyTo)
return m, m.textarea.Focus()
}
case cmd.WinClose:
@ -250,7 +256,5 @@ func (m Model) buildView(cached bool) string {
view = strings.Builder{}
view.WriteString(tmp)
// m.viewcache = view.String()
// return m.viewcache
return view.String()
}

View File

@ -2,6 +2,7 @@ package postshow
import (
"fmt"
"strconv"
"strings"
"github.com/charmbracelet/bubbles/key"
@ -97,22 +98,39 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, m.keymap.Reply):
// if m.buffer != "" {
// replyToID, err := strconv.Atoi(m.buffer)
// if err != nil {
// // TODO: Handle error
// }
//
// if replyToID >= len(m.replyIDs) {
// // TODO: Handle error
// }
// }
var replyToIdx int = 0
var err error
m.viewcache = m.buildView(false)
cmd := cmd.New(cmd.WinOpen, postcreate.WIN_ID, cmd.Arg{
Name: "post",
Value: &m.activePost,
})
if m.buffer != "" {
replyToIdx, err = strconv.Atoi(m.buffer)
if err != nil {
// TODO: Handle error
}
if replyToIdx >= len(m.replyIDs) {
// TODO: Handle error
}
}
m.ctx.Logger.Debugf("replyToIdx: %d", replyToIdx)
var rtype cmd.Arg = cmd.Arg{Name: "replyTo"}
var rarg cmd.Arg
var ridx cmd.Arg = cmd.Arg{Name: "replyToIdx", Value: replyToIdx}
if replyToIdx == 0 {
rtype.Value = "post"
rarg.Name = "post"
rarg.Value = *m.activePost
} else {
rtype.Value = "reply"
rarg.Name = "reply"
rarg.Value = *m.allReplies[(replyToIdx - 1)]
}
cmd := cmd.New(cmd.WinOpen, postcreate.WIN_ID, rtype, rarg, ridx)
cmds = append(cmds, cmd.Tea())
m.ctx.Logger.Debugln("caching view")