2
0
mirror of https://github.com/edouardparis/lntop synced 2024-11-13 13:10:34 +00:00
lntop/ui/models/transactions.go
Edouard f72c5ca099
V0.1.0 (#10)
* config: add Views

* refac views channels

* fix config default

* views: menu

* config default: add comment

* fix config default

* README: enhance config section

* fix README

* controller: F2 Menu

* views: transactions

* fix views: set current in layout

* fix menu

* ft transactions

* fix Help

* fix controller Menu

* view: transaction

* refac controller

* fix ui controller

* try some bold

* fix cursor

* refac color

* focus column transactions

* controller: add keyBinding Menu m

* help view: add menu

* fix cursor: push to the right

* refac remove current model

* ui: txs and channels sortable

* fix focus column

* view transaction: transaction dest addresses

* fix menu

* refac controller

* channels: sort

* rename current column

* refac cursor

* refac currentColumnIndex

* set cursor if view deleted

* remove previous

* clean view.View

* controller: ToggleView

* fix menu

* view txs: add config

* feat order

* fix channels sort
* feat transactions sort
* feat help: add asc/desc
* fix README
* color: magenta
* fix color
* fix views menu
* fix views help
* network backend: SubscribeTransactions
* pubsub: transactions
* controller.Listen: refresh transactions
* fix controller
* fix controller pubsub: no need for wallet ticker
* fix models transactions
* views channels: column SENT and RECEIVED
* update version
* fix README
* fix README
* fix models sort
* fix readme and default config
* fix readme
2019-05-14 18:13:59 +02:00

120 lines
2.0 KiB
Go

package models
import (
"context"
"sort"
"sync"
"github.com/edouardparis/lntop/network/models"
)
type TransactionsSort func(*models.Transaction, *models.Transaction) bool
type Transactions struct {
current *models.Transaction
list []*models.Transaction
sort TransactionsSort
mu sync.RWMutex
}
func (t *Transactions) Current() *models.Transaction {
return t.current
}
func (t *Transactions) SetCurrent(index int) {
t.current = t.Get(index)
}
func (t *Transactions) List() []*models.Transaction {
return t.list
}
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) Sort(s TransactionsSort) {
if s == nil {
return
}
t.sort = s
sort.Sort(t)
}
func (t *Transactions) Get(index int) *models.Transaction {
if index < 0 || index > len(t.list)-1 {
return nil
}
return t.list[index]
}
func (t *Transactions) Contains(tx *models.Transaction) bool {
if tx == nil {
return false
}
for i := range t.list {
if t.list[i].TxHash == tx.TxHash {
return true
}
}
return false
}
func (t *Transactions) Add(tx *models.Transaction) {
t.mu.Lock()
defer t.mu.Unlock()
if t.Contains(tx) {
return
}
t.list = append(t.list, tx)
if t.sort != nil {
sort.Sort(t)
}
}
func (t *Transactions) Update(tx *models.Transaction) {
if tx == nil {
return
}
if !t.Contains(tx) {
t.Add(tx)
return
}
t.mu.Lock()
defer t.mu.Unlock()
for i := range t.list {
if t.list[i].TxHash == tx.TxHash {
t.list[i].NumConfirmations = tx.NumConfirmations
t.list[i].BlockHeight = tx.BlockHeight
}
}
if t.sort != nil {
sort.Sort(t)
}
}
func (m *Models) RefreshTransactions(ctx context.Context) error {
transactions, err := m.network.GetTransactions(ctx)
if err != nil {
return err
}
for i := range transactions {
m.Transactions.Update(transactions[i])
}
return nil
}