From 50e683f0fd18f45bee5a73095fe299edb3aa2aa3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Mon, 2 Jan 2023 14:24:07 -0500 Subject: [PATCH] Implemented draft post loading --- ui/cmd/cmd.go | 11 +++++++++++ ui/ui.go | 7 +++++++ ui/windowmanager/windowmanager.go | 7 ++++++- ui/windows/postdialog/postdialog.go | 24 +++++++++++++++++++++++- 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/ui/cmd/cmd.go b/ui/cmd/cmd.go index ea48da4..1ed75bf 100644 --- a/ui/cmd/cmd.go +++ b/ui/cmd/cmd.go @@ -8,6 +8,7 @@ const ( WinOpen CallType = iota WinFocus WinBlur + WinRefreshData ViewFocus ViewBlur @@ -55,3 +56,13 @@ func (cmd *Command) GetArg(name string) interface{} { 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 +} diff --git a/ui/ui.go b/ui/ui.go index a48afa2..aedc43a 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -74,6 +74,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds := make([]tea.Cmd, 0) switch msg := msg.(type) { + case tea.KeyMsg: switch { case key.Matches(msg, m.keymap.Close): @@ -108,6 +109,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { msg.Target, postdialog.NewModel(m.ctx), [4]int{3, 2, 10, 6}, + msg.GetArgs()..., ) m.ctx.Logger.Debugf("got back ccmds: %v\n", ccmds) default: @@ -116,6 +118,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } 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) diff --git a/ui/windowmanager/windowmanager.go b/ui/windowmanager/windowmanager.go index d028d48..534f42f 100644 --- a/ui/windowmanager/windowmanager.go +++ b/ui/windowmanager/windowmanager.go @@ -23,7 +23,7 @@ func New() *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 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], Height: item.XYWH[3], })) + tcmds = append(tcmds, wm.Update(id, *cmd.New( + cmd.WinRefreshData, + id, + args..., + ))) fcmds := wm.Focus(id) tcmds = append(tcmds, fcmds...) diff --git a/ui/windows/postdialog/postdialog.go b/ui/windows/postdialog/postdialog.go index 863cb12..5a82495 100644 --- a/ui/windows/postdialog/postdialog.go +++ b/ui/windows/postdialog/postdialog.go @@ -154,13 +154,23 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { // cmds = append(cmds, viewport.Sync(m.viewport)) 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 return m, nil case cmd.Command: m.ctx.Logger.Debugf("got command: %v\n", msg) 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: if msg.Target == "post" { m.focused = true @@ -171,6 +181,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.focused = false } return m, nil + default: + m.ctx.Logger.Debugf("received unhandled command: %v\n", msg) } default: @@ -185,6 +197,16 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { 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 { return m.buildView(true) }