refac POC models

pull/1/head
Edouard Paris 5 years ago
parent 7611eea6a0
commit 97c59ed809

@ -47,20 +47,16 @@ func cursorUp(g *gocui.Gui, v *gocui.View) error {
} }
func (c *controller) Update(ctx context.Context) error { func (c *controller) Update(ctx context.Context) error {
info, err := c.models.App.Network.Info(ctx) err := c.models.RefreshInfo(ctx)
if err != nil { if err != nil {
return err return err
} }
alias := info.Alias
if c.models.App.Config.Network.Name != "" { // c.views.Summary.UpdateChannelsStats(
alias = c.models.App.Config.Network.Name // info.NumPendingChannels,
} // info.NumActiveChannels,
c.views.Header.Update(alias, "lnd", info.Version) // info.NumInactiveChannels,
c.views.Summary.UpdateChannelsStats( //)
info.NumPendingChannels,
info.NumActiveChannels,
info.NumInactiveChannels,
)
channels, err := c.models.App.Network.ListChannels(ctx) channels, err := c.models.App.Network.ListChannels(ctx)
if err != nil { if err != nil {
@ -94,8 +90,9 @@ func (c *controller) setKeyBinding(g *gocui.Gui) error {
} }
func newController(app *app.App) *controller { func newController(app *app.App) *controller {
m := models.New(app)
return &controller{ return &controller{
models: models.New(app), models: m,
views: views.New(), views: views.New(m),
} }
} }

@ -1,11 +1,33 @@
package models package models
import "github.com/edouardparis/lntop/app" import (
"context"
"github.com/edouardparis/lntop/app"
"github.com/edouardparis/lntop/network/models"
)
type Models struct { type Models struct {
App *app.App App *app.App
Info *Info
} }
func New(app *app.App) *Models { func New(app *app.App) *Models {
return &Models{App: app} return &Models{
App: app,
Info: &Info{},
}
}
func (m *Models) RefreshInfo(ctx context.Context) error {
info, err := m.App.Network.Info(ctx)
if err != nil {
return err
}
*m.Info = Info{info}
return nil
}
type Info struct {
*models.Info
} }

@ -5,6 +5,7 @@ import (
"regexp" "regexp"
"github.com/edouardparis/lntop/ui/color" "github.com/edouardparis/lntop/ui/color"
"github.com/edouardparis/lntop/ui/models"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
) )
@ -15,9 +16,7 @@ const (
var versionReg = regexp.MustCompile(`(\d+\.)?(\d+\.)?(\*|\d+)`) var versionReg = regexp.MustCompile(`(\d+\.)?(\d+\.)?(\*|\d+)`)
type Header struct { type Header struct {
alias string Info *models.Info
kind string
version string
} }
func (h *Header) Set(g *gocui.Gui, x0, y0, x1, y1 int) error { func (h *Header) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
@ -29,25 +28,21 @@ func (h *Header) Set(g *gocui.Gui, x0, y0, x1, y1 int) error {
} }
v.Frame = false v.Frame = false
version := h.version version := h.Info.Version
matches := versionReg.FindStringSubmatch(h.version) matches := versionReg.FindStringSubmatch(h.Info.Version)
if len(matches) > 0 { if len(matches) > 0 {
version = matches[0] version = matches[0]
} }
fmt.Fprintln(v, fmt.Sprintf("%s %s", fmt.Fprintln(v, fmt.Sprintf("%s %s %s %s",
color.CyanBg(h.alias), color.CyanBg(h.Info.Alias),
color.Cyan(fmt.Sprintf("%s-v%s", h.kind, version)), color.Cyan(fmt.Sprintf("%s-v%s", "lnd", version)),
fmt.Sprintf("%s %d", color.Cyan("height:"), h.Info.BlockHeight),
fmt.Sprintf("%s %d", color.Cyan("peers:"), h.Info.NumPeers),
)) ))
return nil return nil
} }
func (h *Header) Update(alias, kind, version string) { func NewHeader(info *models.Info) *Header {
h.alias = alias return &Header{Info: info}
h.kind = kind
h.version = version
}
func NewHeader() *Header {
return &Header{}
} }

@ -1,6 +1,9 @@
package views package views
import "github.com/jroimartin/gocui" import (
"github.com/edouardparis/lntop/ui/models"
"github.com/jroimartin/gocui"
)
type Views struct { type Views struct {
Header *Header Header *Header
@ -28,9 +31,9 @@ func (v *Views) Layout(g *gocui.Gui, maxX, maxY int) error {
return v.Footer.Set(g, 0, maxY-2, maxX, maxY) return v.Footer.Set(g, 0, maxY-2, maxX, maxY)
} }
func New() *Views { func New(m *models.Models) *Views {
return &Views{ return &Views{
Header: NewHeader(), Header: NewHeader(m.Info),
Footer: NewFooter(), Footer: NewFooter(),
Summary: NewSummary(), Summary: NewSummary(),
Channels: NewChannels(), Channels: NewChannels(),

Loading…
Cancel
Save