diff options
author | Miguel Mota <miguelmota2@gmail.com> | 2021-02-26 15:35:42 -0800 |
---|---|---|
committer | Miguel Mota <miguelmota2@gmail.com> | 2021-02-26 15:36:12 -0800 |
commit | bea56532fa736527b32f93d63c9b372079b09168 (patch) | |
tree | 521b088d42044614a12523f9a6bd17dcb08d420f | |
parent | 3270d12ab895824e0e658ef11d21e2d1899def2e (diff) |
Fix coins and portfolio row selectionv1.6.3
-rw-r--r-- | cointop/coins_table.go | 5 | ||||
-rw-r--r-- | cointop/navigation.go | 12 | ||||
-rw-r--r-- | cointop/portfolio.go | 30 | ||||
-rw-r--r-- | cointop/price_alerts.go | 32 | ||||
-rw-r--r-- | cointop/table.go | 10 |
5 files changed, 75 insertions, 14 deletions
diff --git a/cointop/coins_table.go b/cointop/coins_table.go index 7c75b27..cd663e7 100644 --- a/cointop/coins_table.go +++ b/cointop/coins_table.go @@ -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()) +} diff --git a/cointop/navigation.go b/cointop/navigation.go index b2a3cc4..df7b12f 100644 --- a/cointop/navigation.go +++ b/cointop/navigation.go @@ -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() } diff --git a/cointop/portfolio.go b/cointop/portfolio.go index 7bd3738..7a3f0a6 100644 --- a/cointop/portfolio.go +++ b/cointop/portfolio.go @@ -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 diff --git a/cointop/price_alerts.go b/cointop/price_alerts.go index d9defcf..a6fad88 100644 --- a/cointop/price_alerts.go +++ b/cointop/price_alerts.go @@ -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 diff --git a/cointop/table.go b/cointop/table.go index c3bef13..c6178c8 100644 --- a/cointop/table.go +++ b/cointop/table.go @@ -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) } } |