Fix coins and portfolio row selection

pull/94/head v1.6.3
Miguel Mota 3 years ago
parent 3270d12ab8
commit bea56532fa

@ -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())
}

@ -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()
}

@ -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)
@ -426,6 +425,7 @@ func (ct *Cointop) SetPortfolioHoldings() error {
}
}
idx := ct.GetPortfolioCoinIndex(coin)
if err := ct.SetPortfolioEntry(coin.Name, holdings); err != nil {
return err
}
@ -433,16 +433,21 @@ func (ct *Cointop) SetPortfolioHoldings() error {
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
}
@ -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

@ -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())
@ -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())
@ -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
@ -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
@ -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
@ -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

@ -2,7 +2,6 @@ package cointop
import (
"fmt"
"math"
"net/url"
"strings"
@ -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()")
@ -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)
@ -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)
}
}

Loading…
Cancel
Save