Skip to content

Commit

Permalink
Fix coins and portfolio row selection
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Feb 26, 2021
1 parent 3270d12 commit bea5653
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 14 deletions.
5 changes: 5 additions & 0 deletions cointop/coins_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,8 @@ func (ct *Cointop) GetCoinsTable() *table.Table {

return t
}

// TableCoinsLen returns the number of coins in coins table
func (ct *Cointop) TableCoinsLen() int {
return len(ct.GetTableCoinsSlice())
}
12 changes: 11 additions & 1 deletion cointop/navigation.go
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,16 @@ func (ct *Cointop) TableRowsLen() int {
if ct.IsPriceAlertsVisible() {
return ct.ActivePriceAlertsLen()
}
return ct.TableCoinsLen()
}

return len(ct.State.coins)
// GetActiveTableSlice returns the rows slice for the active table
func (ct *Cointop) GetActiveTableSlice() []*Coin {
if ct.IsFavoritesVisible() {
return ct.GetFavoritesSlice()
}
if ct.IsPortfolioVisible() {
return ct.GetPortfolioSlice()
}
return ct.GetTableCoinsSlice()
}
30 changes: 27 additions & 3 deletions cointop/portfolio.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,6 @@ func (ct *Cointop) ShowPortfolioUpdateMenu() error {
return nil
}

ct.State.lastSelectedRowIndex = ct.HighlightedPageRowIndex()
ct.State.portfolioUpdateMenuVisible = true
ct.UpdatePortfolioUpdateMenu()
ct.ui.SetCursor(true)
Expand Down Expand Up @@ -426,23 +425,29 @@ func (ct *Cointop) SetPortfolioHoldings() error {
}
}

idx := ct.GetPortfolioCoinIndex(coin)
if err := ct.SetPortfolioEntry(coin.Name, holdings); err != nil {
return err
}

if shouldDelete {
ct.RemovePortfolioEntry(coin.Name)
ct.UpdateTable()
if idx > 0 {
idx -= 1
}
} else {
ct.UpdateTable()
ct.GoToPageRowIndex(ct.State.lastSelectedRowIndex)
ct.ToggleShowPortfolio()
idx = ct.GetPortfolioCoinIndex(coin)
}

ct.HighlightRow(idx)

if err := ct.Save(); err != nil {
return err
}

ct.ToggleShowPortfolio()
return nil
}

Expand Down Expand Up @@ -814,6 +819,25 @@ func (ct *Cointop) PrintTotalHoldings(options *TablePrintOptions) error {
return nil
}

// GetPortfolioCoinIndex returns the row index of coin in portfolio
func (ct *Cointop) GetPortfolioCoinIndex(coin *Coin) int {
coins := ct.GetPortfolioSlice()
for i, c := range coins {
if c.ID == coin.ID {
return i
}
}
return 0
}

func (ct *Cointop) GetLastPortfolioRowIndex() int {
l := ct.PortfolioLen()
if l > 0 {
l -= 1
}
return l
}

// IsPortfolioVisible returns true if portfolio view is visible
func (ct *Cointop) IsPortfolioVisible() bool {
return ct.State.selectedView == PortfolioView
Expand Down
32 changes: 25 additions & 7 deletions cointop/price_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ func (ct *Cointop) UpdatePriceAlertsUpdateMenu(isNew bool) error {
func (ct *Cointop) ShowPriceAlertsAddMenu() error {
ct.debuglog("showPriceAlertsAddMenu()")
ct.SetSelectedView(PriceAlertsView)
ct.State.lastSelectedRowIndex = ct.HighlightedPageRowIndex()
ct.UpdatePriceAlertsUpdateMenu(true)
ct.ui.SetCursor(true)
ct.SetActiveView(ct.Views.Menu.Name())
Expand All @@ -300,7 +299,6 @@ func (ct *Cointop) ShowPriceAlertsAddMenu() error {
func (ct *Cointop) ShowPriceAlertsUpdateMenu() error {
ct.debuglog("showPriceAlertsUpdateMenu()")
ct.SetSelectedView(PriceAlertsView)
ct.State.lastSelectedRowIndex = ct.HighlightedPageRowIndex()
ct.UpdatePriceAlertsUpdateMenu(false)
ct.ui.SetCursor(true)
ct.SetActiveView(ct.Views.Menu.Name())
Expand Down Expand Up @@ -357,14 +355,18 @@ func (ct *Cointop) CreatePriceAlert() error {
if err != nil {
return err
}

if err := ct.SetPriceAlert(coinName, operator, targetPrice); err != nil {
return err
shouldDelete := targetPrice == -1
if shouldDelete {
ct.RemovePriceAlert(ct.State.priceAlertEditID)
} else {
if err := ct.SetPriceAlert(coinName, operator, targetPrice); err != nil {
return err
}
}

ct.UpdateTable()
if isNew {
ct.GoToPageRowIndex(0)
if isNew || shouldDelete {
ct.HighlightRow(0)
}

return nil
Expand All @@ -383,6 +385,9 @@ func (ct *Cointop) ReadAndParsePriceAlertInput() (string, float64, error) {
}

inputValue := string(b)
if inputValue == "" {
return "", -1, nil
}
operator, targetPrice, err := ct.ParsePriceAlertInput(inputValue)
if err != nil {
return "", 0, err
Expand All @@ -404,6 +409,9 @@ func (ct *Cointop) ParsePriceAlertInput(value string) (string, float64, error) {
amountValue = matches[2]
}
amountValue = normalizeFloatString(amountValue, false)
if amountValue == "" {
return "", -1, nil
}
targetPrice, err := strconv.ParseFloat(amountValue, 64)
if err != nil {
return "", 0, err
Expand Down Expand Up @@ -451,6 +459,16 @@ func (ct *Cointop) SetPriceAlert(coinName string, operator string, targetPrice f
return nil
}

// RemovePriceAlert removes a price alert entry
func (ct *Cointop) RemovePriceAlert(id string) {
ct.debuglog("removePriceAlert()")
for i, entry := range ct.State.priceAlerts.Entries {
if entry.ID == ct.State.priceAlertEditID {
ct.State.priceAlerts.Entries = append(ct.State.priceAlerts.Entries[:i], ct.State.priceAlerts.Entries[i+1:]...)
}
}
}

// ActivePriceAlerts returns the active price alerts
func (ct *Cointop) ActivePriceAlerts() []*PriceAlert {
var filtered []*PriceAlert
Expand Down
10 changes: 7 additions & 3 deletions cointop/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cointop

import (
"fmt"
"math"
"net/url"
"strings"

Expand Down Expand Up @@ -176,6 +175,11 @@ func (ct *Cointop) HighlightedPageRowIndex() int {
return idx
}

// GetLastSelectedRowCoinIndex returns the index of the last selected row coin
func (ct *Cointop) GetLastSelectedRowCoinIndex() int {
return ct.State.lastSelectedRowIndex
}

// RowLink returns the row url link
func (ct *Cointop) RowLink() string {
ct.debuglog("RowLink()")
Expand Down Expand Up @@ -248,7 +252,7 @@ func (ct *Cointop) SetSelectedView(viewName string) {
// ToggleSelectedView toggles between current table view and last selected table view
func (ct *Cointop) ToggleSelectedView(viewName string) {
if !(ct.IsPortfolioVisible() || ct.IsFavoritesVisible()) {
ct.State.lastSelectedRowIndex = ct.HighlightedPageRowIndex()
ct.State.lastSelectedRowIndex = ct.HighlightedRowIndex()
}
if ct.State.lastSelectedView == "" || ct.State.selectedView != viewName {
ct.SetSelectedView(viewName)
Expand All @@ -264,6 +268,6 @@ func (ct *Cointop) ToggleSelectedView(viewName string) {
ct.HighlightRow(l - 1)
}
} else {
ct.GoToPageRowIndex(int(math.Min(float64(l-1), float64(ct.State.lastSelectedRowIndex))))
ct.HighlightRow(ct.State.lastSelectedRowIndex)
}
}

0 comments on commit bea5653

Please sign in to comment.