Skip to content

Commit

Permalink
feat: added simple navigation back stack (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
ardevd authored Jan 8, 2024
1 parent c82d81a commit b778842
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 27 deletions.
28 changes: 24 additions & 4 deletions internal/tui/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,44 @@ import (
)

// Base model that handles logic common to all views

type BaseModel struct {
NavStack []tea.Model // Navigation stack to store views
}

func NewBaseModel(m tea.Model) *BaseModel {
initialNavStack := []tea.Model{m}
return &BaseModel{NavStack: initialNavStack}
}

func NewBaseModel() *BaseModel {
return &BaseModel{}
func (b *BaseModel) pushView(m tea.Model) {
// Push a new view onto the stack
b.NavStack = append(b.NavStack, m)
}

func (b *BaseModel) popView() tea.Model {
// Don't pop the last view from the stack
if len(b.NavStack) > 1 {
b.NavStack = b.NavStack[:len(b.NavStack)-1]
}
return b.NavStack[len(b.NavStack)-1]
}

func (m *BaseModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {

case tea.WindowSizeMsg:
windowSizeMsg = msg
case tea.KeyMsg:
switch {
case key.Matches(msg, Keymap.Quit):
return m, tea.Quit
case key.Matches(msg, Keymap.Back):
newModel := m.popView()
return newModel, nil
}
}

return m, nil
return nil, nil
}

func (m BaseModel) Init() tea.Cmd {
Expand Down
8 changes: 4 additions & 4 deletions internal/tui/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ type ChannelModel struct {
base *BaseModel
}

func NewChannelModel(service *lndclient.GrpcLndServices, channel lnd.Channel, backModel tea.Model) *ChannelModel {
m := ChannelModel{lndService: service, ctx: context.Background(), channel: channel}
func NewChannelModel(service *lndclient.GrpcLndServices, channel lnd.Channel, backModel tea.Model, base *BaseModel) *ChannelModel {
m := ChannelModel{lndService: service, ctx: context.Background(), channel: channel, base: base}
m.styles = GetDefaultStyles()
m.base.pushView(&m)
return &m
}

Expand All @@ -35,14 +36,13 @@ func (m *ChannelModel) initData(width, height int) {
func (m *ChannelModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Base model logic
model, cmd := m.base.Update(msg)
if cmd != nil {
if model != nil {
return model, cmd
}

switch msg := msg.(type) {
case tea.WindowSizeMsg:
windowSizeMsg = msg

v, h := m.styles.BorderedStyle.GetFrameSize()
m.initData(windowSizeMsg.Width-h, windowSizeMsg.Height-v)

Expand Down
7 changes: 4 additions & 3 deletions internal/tui/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var formSelection string
func InitDashboard(service *lndclient.GrpcLndServices, nodeData lnd.NodeData) *DashboardModel {
m := DashboardModel{lndService: service, ctx: context.Background(), nodeData: nodeData}
m.styles = GetDefaultStyles()
m.base = *NewBaseModel()
return &m
}

Expand All @@ -44,6 +43,8 @@ func (m *DashboardModel) initData(width, height int) {
m.lists[channels].SetItems(m.nodeData.GetChannelsAsListItems())
m.lists[payments].Title = "Latest Payments"
m.lists[payments].SetItems(m.nodeData.GetPaymentsAsListItems())

m.base = *NewBaseModel(m)
}

func (m DashboardModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand Down Expand Up @@ -225,7 +226,7 @@ func (m *DashboardModel) generateMessageToolsForm() *huh.Form {

func (m *DashboardModel) handleChannelClick() (tea.Model, tea.Cmd) {
selectedChannel := m.lists[m.focused].SelectedItem().(lnd.Channel)
return NewChannelModel(m.lndService, selectedChannel, m).Update(windowSizeMsg)
return NewChannelModel(m.lndService, selectedChannel, m, &m.base).Update(windowSizeMsg)
}

func (m *DashboardModel) handleFormClick() (tea.Model, tea.Cmd) {
Expand All @@ -234,7 +235,7 @@ func (m *DashboardModel) handleFormClick() (tea.Model, tea.Cmd) {
}

func (m *DashboardModel) getInvoiceModel() InvoiceModel {
return NewInvoiceModel(m.ctx, m.lndService, StateNone, m)
return NewInvoiceModel(m.ctx, &m.base, m.lndService, StateNone)
}

// Navigation
Expand Down
24 changes: 8 additions & 16 deletions internal/tui/invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ type InvoiceModel struct {
lndService *lndclient.GrpcLndServices
ctx context.Context
invoiceState InvoiceState
dashboard *DashboardModel
base *BaseModel
}

Expand Down Expand Up @@ -88,9 +87,8 @@ func isFormReady(v bool) error {
}

// Invoice generation form
func NewInvoiceModel(context context.Context, service *lndclient.GrpcLndServices, state InvoiceState, dashboard *DashboardModel) InvoiceModel {
m := InvoiceModel{width: maxWidth, lndService: service, ctx: context, invoiceState: state, dashboard: dashboard}
m.base = NewBaseModel()
func NewInvoiceModel(context context.Context, base *BaseModel, service *lndclient.GrpcLndServices, state InvoiceState) InvoiceModel {
m := InvoiceModel{width: maxWidth, base: base, lndService: service, ctx: context, invoiceState: state}
m.lg = lipgloss.DefaultRenderer()
m.styles = NewStyles(m.lg)
m.form = huh.NewForm(
Expand All @@ -117,9 +115,8 @@ func NewInvoiceModel(context context.Context, service *lndclient.GrpcLndServices
Affirmative("Submit").
Negative("Cancel"),
),
).
WithShowHelp(false).
WithShowErrors(false)
).WithShowHelp(false).WithShowErrors(false)
m.base.pushView(m)
return m
}

Expand All @@ -128,18 +125,14 @@ func (m InvoiceModel) Init() tea.Cmd {
return m.form.Init()
}

// Navigate back to the dashboard model
func (m InvoiceModel) backToDashboard() (tea.Model, tea.Cmd) {
return m.dashboard.Update(windowSizeMsg)
}

// Handle update messages for the model
func (m InvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Base model logic
model, cmd := m.base.Update(msg)
if cmd != nil {
if model != nil {
return model, cmd
}

switch msg := msg.(type) {
case tea.WindowSizeMsg:
windowSizeMsg = msg
Expand All @@ -152,11 +145,10 @@ func (m InvoiceModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

case tea.KeyMsg:
switch {
case key.Matches(msg, Keymap.Back):
return m.backToDashboard()

case key.Matches(msg, Keymap.Enter):
if m.invoiceState == StateSettled || m.invoiceState == StateExpired || m.invoiceState == StateError {
return m.backToDashboard()
return m.base.popView().Update(windowSizeMsg)
}
}

Expand Down

0 comments on commit b778842

Please sign in to comment.