0
0
mirror of https://github.com/mrusme/neonmodem.git synced 2025-06-30 22:18:39 -04:00

Implemented draft post loading

This commit is contained in:
マリウス 2023-01-02 14:24:07 -05:00
parent d2f42ff754
commit 50e683f0fd
No known key found for this signature in database
GPG Key ID: 272ED814BF63261F
4 changed files with 47 additions and 2 deletions

View File

@ -8,6 +8,7 @@ const (
WinOpen CallType = iota WinOpen CallType = iota
WinFocus WinFocus
WinBlur WinBlur
WinRefreshData
ViewFocus ViewFocus
ViewBlur ViewBlur
@ -55,3 +56,13 @@ func (cmd *Command) GetArg(name string) interface{} {
return nil return nil
} }
func (cmd *Command) GetArgs() []Arg {
var args []Arg
for name, value := range cmd.Args {
args = append(args, Arg{Name: name, Value: value})
}
return args
}

View File

@ -74,6 +74,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds := make([]tea.Cmd, 0) cmds := make([]tea.Cmd, 0)
switch msg := msg.(type) { switch msg := msg.(type) {
case tea.KeyMsg: case tea.KeyMsg:
switch { switch {
case key.Matches(msg, m.keymap.Close): case key.Matches(msg, m.keymap.Close):
@ -108,6 +109,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
msg.Target, msg.Target,
postdialog.NewModel(m.ctx), postdialog.NewModel(m.ctx),
[4]int{3, 2, 10, 6}, [4]int{3, 2, 10, 6},
msg.GetArgs()...,
) )
m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds) m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds)
default: default:
@ -116,6 +118,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
cmds = append(cmds, ccmds...) cmds = append(cmds, ccmds...)
default:
m.ctx.Logger.Debugf("updating all with default: %v\n", msg)
cmds = append(cmds, m.wm.UpdateAll(msg)...)
} }
v, vcmd := m.views[m.currentView].Update(msg) v, vcmd := m.views[m.currentView].Update(msg)

View File

@ -23,7 +23,7 @@ func New() *WM {
return wm return wm
} }
func (wm *WM) Open(id string, win windows.Window, xywh [4]int) []tea.Cmd { func (wm *WM) Open(id string, win windows.Window, xywh [4]int, args ...cmd.Arg) []tea.Cmd {
var tcmds []tea.Cmd var tcmds []tea.Cmd
if wm.IsOpen(id) { if wm.IsOpen(id) {
@ -44,6 +44,11 @@ func (wm *WM) Open(id string, win windows.Window, xywh [4]int) []tea.Cmd {
Width: item.XYWH[2], Width: item.XYWH[2],
Height: item.XYWH[3], Height: item.XYWH[3],
})) }))
tcmds = append(tcmds, wm.Update(id, *cmd.New(
cmd.WinRefreshData,
id,
args...,
)))
fcmds := wm.Focus(id) fcmds := wm.Focus(id)
tcmds = append(tcmds, fcmds...) tcmds = append(tcmds, fcmds...)

View File

@ -154,13 +154,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// cmds = append(cmds, viewport.Sync(m.viewport)) // cmds = append(cmds, viewport.Sync(m.viewport))
case *post.Post: case *post.Post:
m.viewport.SetContent(m.renderViewport(msg)) m.ctx.Logger.Debug("got *post.Post")
m.activePost = msg
m.viewport.SetContent(m.renderViewport(m.activePost))
m.ctx.Loading = false m.ctx.Loading = false
return m, nil return m, nil
case cmd.Command: case cmd.Command:
m.ctx.Logger.Debugf("got command: %v\n", msg) m.ctx.Logger.Debugf("got command: %v\n", msg)
switch msg.Call { switch msg.Call {
case cmd.WinRefreshData:
if msg.Target == "post" {
m.activePost = msg.GetArg("post").(*post.Post)
m.ctx.Logger.Debugf("loading post: %v", m.activePost.ID)
m.ctx.Loading = true
return m, m.loadPost(m.activePost)
}
return m, nil
case cmd.WinFocus: case cmd.WinFocus:
if msg.Target == "post" { if msg.Target == "post" {
m.focused = true m.focused = true
@ -171,6 +181,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.focused = false m.focused = false
} }
return m, nil return m, nil
default:
m.ctx.Logger.Debugf("received unhandled command: %v\n", msg)
} }
default: default:
@ -185,6 +197,16 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...) return m, tea.Batch(cmds...)
} }
func (m *Model) loadPost(p *post.Post) tea.Cmd {
return func() tea.Msg {
m.ctx.Logger.Debug("------ EXECUTED -----")
if err := m.a.LoadPost(p); err != nil {
m.ctx.Logger.Error(err)
}
return p
}
}
func (m Model) View() string { func (m Model) View() string {
return m.buildView(true) return m.buildView(true)
} }