refac remove current model

pull/10/head
Edouard Paris 5 years ago
parent 12e8a8c125
commit a3611150ee

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

@ -7,9 +7,10 @@ import (
)
type Channels struct {
index map[string]*models.Channel
list []*models.Channel
mu sync.RWMutex
current int
index map[string]*models.Channel
list []*models.Channel
mu sync.RWMutex
}
func (c *Channels) List() []*models.Channel {
@ -20,6 +21,10 @@ func (c *Channels) Len() int {
return len(c.list)
}
func (c *Channels) Current() *models.Channel {
return c.Get(c.current)
}
func (c *Channels) Get(index int) *models.Channel {
if index < 0 || index > len(c.list)-1 {
return nil
@ -91,7 +96,3 @@ func NewChannels() *Channels {
index: make(map[string]*models.Channel),
}
}
type Channel struct {
Item *models.Channel
}

@ -11,28 +11,24 @@ import (
)
type Models struct {
logger logging.Logger
network *network.Network
Info *Info
Channels *Channels
CurrentChannel *Channel
WalletBalance *WalletBalance
ChannelsBalance *ChannelsBalance
Transactions *Transactions
CurrentTransaction *Transaction
logger logging.Logger
network *network.Network
Info *Info
Channels *Channels
WalletBalance *WalletBalance
ChannelsBalance *ChannelsBalance
Transactions *Transactions
}
func New(app *app.App) *Models {
return &Models{
logger: app.Logger.With(logging.String("logger", "models")),
network: app.Network,
Info: &Info{},
Channels: NewChannels(),
WalletBalance: &WalletBalance{},
ChannelsBalance: &ChannelsBalance{},
CurrentChannel: &Channel{},
Transactions: &Transactions{},
CurrentTransaction: &Transaction{},
logger: app.Logger.With(logging.String("logger", "models")),
network: app.Network,
Info: &Info{},
Channels: NewChannels(),
WalletBalance: &WalletBalance{},
ChannelsBalance: &ChannelsBalance{},
Transactions: &Transactions{},
}
}
@ -82,22 +78,16 @@ func (m *Models) RefreshChannels(ctx context.Context) error {
return nil
}
func (m *Models) SetCurrentChannel(ctx context.Context, index int) error {
channel := m.Channels.Get(index)
if channel == nil {
return nil
func (m *Models) SetCurrentChannel(index int) {
if index < m.Channels.Len()-1 {
m.Channels.current = index
}
*m.CurrentChannel = Channel{Item: channel}
return nil
}
func (m *Models) SetCurrentTransaction(ctx context.Context, index int) error {
transaction := m.Transactions.Get(index)
if transaction == nil {
return nil
func (m *Models) SetCurrentTransaction(index int) {
if index < m.Transactions.Len()-1 {
m.Transactions.current = index
}
*m.CurrentTransaction = Transaction{Item: transaction}
return nil
}
type WalletBalance struct {

@ -7,7 +7,12 @@ import (
)
type Transactions struct {
list []*models.Transaction
current int
list []*models.Transaction
}
func (t Transactions) Current() *models.Transaction {
return t.Get(t.current)
}
func (t Transactions) List() []*models.Transaction {
@ -34,7 +39,3 @@ func (m *Models) RefreshTransactions(ctx context.Context) error {
*m.Transactions = Transactions{list: transactions}
return nil
}
type Transaction struct {
Item *models.Transaction
}

@ -18,8 +18,8 @@ const (
)
type Channel struct {
view *gocui.View
channel *models.Channel
view *gocui.View
channels *models.Channels
}
func (c Channel) Name() string {
@ -27,7 +27,7 @@ func (c Channel) Name() string {
}
func (c Channel) Empty() bool {
return c.channel == nil
return c.channels == nil
}
func (c *Channel) Wrap(v *gocui.View) view {
@ -112,7 +112,7 @@ func (c *Channel) display() {
p := message.NewPrinter(language.English)
v := c.view
v.Clear()
channel := c.channel.Item
channel := c.channels.Current()
green := color.Green()
cyan := color.Cyan()
red := color.Red()
@ -176,6 +176,6 @@ func (c *Channel) display() {
}
}
func NewChannel(channel *models.Channel) *Channel {
return &Channel{channel: channel}
func NewChannel(channels *models.Channels) *Channel {
return &Channel{channels: channels}
}

@ -18,8 +18,8 @@ const (
)
type Transaction struct {
view *gocui.View
transaction *models.Transaction
view *gocui.View
transactions *models.Transactions
}
func (c Transaction) Name() string {
@ -27,7 +27,7 @@ func (c Transaction) Name() string {
}
func (c Transaction) Empty() bool {
return c.transaction == nil
return c.transactions == nil
}
func (c *Transaction) Wrap(v *gocui.View) view {
@ -112,7 +112,7 @@ func (c *Transaction) display() {
p := message.NewPrinter(language.English)
v := c.view
v.Clear()
transaction := c.transaction.Item
transaction := c.transactions.Current()
green := color.Green()
cyan := color.Cyan()
fmt.Fprintln(v, green(" [ Transaction ]"))
@ -133,6 +133,6 @@ func (c *Transaction) display() {
fmt.Fprintln(v, green("[ addresses ]"))
}
func NewTransaction(transaction *models.Transaction) *Transaction {
return &Transaction{transaction: transaction}
func NewTransaction(transactions *models.Transactions) *Transaction {
return &Transaction{transactions: transactions}
}

@ -109,9 +109,9 @@ func New(cfg config.Views, m *models.Models) *Views {
Menu: NewMenu(),
Summary: NewSummary(m.Info, m.ChannelsBalance, m.WalletBalance, m.Channels),
Channels: main,
Channel: NewChannel(m.CurrentChannel),
Channel: NewChannel(m.Channels),
Transactions: NewTransactions(m.Transactions),
Transaction: NewTransaction(m.CurrentTransaction),
Transaction: NewTransaction(m.Transactions),
Main: main,
}
}

Loading…
Cancel
Save