ui: txs and channels sortable

pull/10/head
Edouard Paris 5 years ago
parent a3611150ee
commit 5390ab627e

@ -217,7 +217,7 @@ func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error {
switch view.Name() {
case views.CHANNELS:
index := c.views.Channels.Index()
c.models.SetCurrentChannel(index)
c.models.Channels.SetCurrent(index)
c.views.SetPrevious(view)
err := c.views.Channel.Set(g, 0, 6, maxX-1, maxY)
if err != nil {
@ -283,7 +283,7 @@ func (c *controller) OnEnter(g *gocui.Gui, v *gocui.View) error {
}
case views.TRANSACTIONS:
index := c.views.Transactions.Index()
c.models.SetCurrentTransaction(index)
c.models.Transactions.SetCurrent(index)
c.views.SetPrevious(view)
err := c.views.Transaction.Set(g, 0, 6, maxX-1, maxY)
if err != nil {

@ -1,15 +1,19 @@
package models
import (
"sort"
"sync"
"github.com/edouardparis/lntop/network/models"
)
type ChannelsSort func(*models.Channel, *models.Channel) bool
type Channels struct {
current int
current *models.Channel
index map[string]*models.Channel
list []*models.Channel
sort ChannelsSort
mu sync.RWMutex
}
@ -21,8 +25,24 @@ func (c *Channels) Len() int {
return len(c.list)
}
func (c *Channels) Swap(i, j int) {
c.list[i], c.list[j] = c.list[j], c.list[i]
}
func (c *Channels) Less(i, j int) bool {
return c.sort(c.list[i], c.list[j])
}
func (c *Channels) WithSort(s ChannelsSort) {
c.sort = s
}
func (c *Channels) Current() *models.Channel {
return c.Get(c.current)
return c.current
}
func (c *Channels) SetCurrent(index int) {
c.current = c.Get(index)
}
func (c *Channels) Get(index int) *models.Channel {
@ -59,6 +79,9 @@ func (c *Channels) Update(newChannel *models.Channel) {
oldChannel, ok := c.index[newChannel.ChannelPoint]
if !ok {
c.Add(newChannel)
if c.sort != nil {
sort.Sort(c)
}
return
}

@ -78,18 +78,6 @@ func (m *Models) RefreshChannels(ctx context.Context) error {
return nil
}
func (m *Models) SetCurrentChannel(index int) {
if index < m.Channels.Len()-1 {
m.Channels.current = index
}
}
func (m *Models) SetCurrentTransaction(index int) {
if index < m.Transactions.Len()-1 {
m.Transactions.current = index
}
}
type WalletBalance struct {
*models.WalletBalance
}

@ -6,13 +6,20 @@ import (
"github.com/edouardparis/lntop/network/models"
)
type TransactionsSort func(*models.Transaction, *models.Transaction) bool
type Transactions struct {
current int
current *models.Transaction
list []*models.Transaction
sort TransactionsSort
}
func (t Transactions) Current() *models.Transaction {
return t.Get(t.current)
return t.current
}
func (t *Transactions) SetCurrent(index int) {
t.current = t.Get(index)
}
func (t Transactions) List() []*models.Transaction {
@ -23,6 +30,18 @@ func (t *Transactions) Len() int {
return len(t.list)
}
func (t *Transactions) Swap(i, j int) {
t.list[i], t.list[j] = t.list[j], t.list[i]
}
func (t *Transactions) Less(i, j int) bool {
return t.sort(t.list[i], t.list[j])
}
func (t *Transactions) WithSort(s TransactionsSort) {
t.sort = s
}
func (t *Transactions) Get(index int) *models.Transaction {
if index < 0 || index > len(t.list)-1 {
return nil

Loading…
Cancel
Save