2
0
mirror of https://github.com/miguelmota/cointop synced 2024-11-16 21:25:38 +00:00

Price alert entry fix

This commit is contained in:
Miguel Mota 2021-04-11 19:07:53 -07:00
parent 65626536e3
commit c79d601bbf
No known key found for this signature in database
GPG Key ID: 67EC1161588A00F9
4 changed files with 81 additions and 31 deletions

View File

@ -61,6 +61,29 @@ func (ct *Cointop) CoinBySymbol(symbol string) *Coin {
return coin return coin
} }
} }
return nil
}
// CoinByName returns the coin struct given the name
func (ct *Cointop) CoinByName(name string) *Coin {
ct.debuglog("CoinByName()")
for i := range ct.State.allCoins {
coin := ct.State.allCoins[i]
if coin.Name == name {
return coin
}
}
return nil
}
// CoinByID returns the coin struct given the ID
func (ct *Cointop) CoinByID(id string) *Coin {
ct.debuglog("CoinByID()")
for i := range ct.State.allCoins {
coin := ct.State.allCoins[i]
if coin.ID == id {
return coin
}
}
return nil return nil
} }

View File

@ -79,6 +79,7 @@ type State struct {
chartHeight int chartHeight int
priceAlerts *PriceAlerts priceAlerts *PriceAlerts
priceAlertEditID string priceAlertEditID string
priceAlertNewID string
} }
// Cointop cointop // Cointop cointop

View File

@ -227,26 +227,33 @@ func (ct *Cointop) CheckPriceAlert(alert *PriceAlert) error {
} }
// UpdatePriceAlertsUpdateMenu updates the alerts update menu view // UpdatePriceAlertsUpdateMenu updates the alerts update menu view
func (ct *Cointop) UpdatePriceAlertsUpdateMenu(isNew bool) error { func (ct *Cointop) UpdatePriceAlertsUpdateMenu(isNew bool, coin *Coin) error {
ct.debuglog("updatePriceAlertsUpdateMenu()") ct.debuglog("updatePriceAlertsUpdateMenu()")
exists := false isEdit := false
var value string var value string
var currentPrice string var currentPrice string
var coinName string
ct.State.priceAlertEditID = "" ct.State.priceAlertEditID = ""
if !isNew && ct.IsPriceAlertsVisible() { ct.State.priceAlertNewID = ""
if coin != nil {
if isNew {
ct.State.priceAlertNewID = coin.ID
} else {
ct.State.priceAlertEditID = coin.ID
}
}
if !isNew && ct.IsPriceAlertsVisible() && coin != nil {
rowIndex := ct.HighlightedRowIndex() rowIndex := ct.HighlightedRowIndex()
entry := ct.State.priceAlerts.Entries[rowIndex] entry := ct.State.priceAlerts.Entries[rowIndex]
ifc, ok := ct.State.allCoinsSlugMap.Load(entry.CoinName) ifc, ok := ct.State.allCoinsSlugMap.Load(entry.CoinName)
if ok { if ok {
coin, ok := ifc.(*Coin) coin, ok := ifc.(*Coin)
if ok { if ok {
coinName = entry.CoinName coin.Name = entry.CoinName
currentPrice = strconv.FormatFloat(coin.Price, 'f', -1, 64) currentPrice = strconv.FormatFloat(coin.Price, 'f', -1, 64)
value = fmt.Sprintf("%s %v", entry.Operator, entry.TargetPrice) value = fmt.Sprintf("%s %v", entry.Operator, entry.TargetPrice)
ct.State.priceAlertEditID = entry.ID ct.State.priceAlertEditID = entry.ID
exists = true isEdit = true
} }
} }
} }
@ -255,14 +262,15 @@ func (ct *Cointop) UpdatePriceAlertsUpdateMenu(isNew bool) error {
var current string var current string
var submitText string var submitText string
var offset int var offset int
if exists { if isEdit {
mode = "Edit" mode = "Edit"
current = fmt.Sprintf("(current %s%s)", ct.CurrencySymbol(), currentPrice) current = fmt.Sprintf("(current %s%s)", ct.CurrencySymbol(), currentPrice)
submitText = "Set" submitText = "Set"
offset = ct.width() - 21 offset = ct.width() - 21
} else { } else {
coin := ct.HighlightedRowCoin() if coin == nil {
coinName = coin.Name coin = ct.HighlightedRowCoin()
}
currentPrice = strconv.FormatFloat(coin.Price, 'f', -1, 64) currentPrice = strconv.FormatFloat(coin.Price, 'f', -1, 64)
value = fmt.Sprintf("> %s", currentPrice) value = fmt.Sprintf("> %s", currentPrice)
mode = "Create" mode = "Create"
@ -270,7 +278,7 @@ func (ct *Cointop) UpdatePriceAlertsUpdateMenu(isNew bool) error {
offset = ct.width() - 23 offset = ct.width() - 23
} }
header := ct.colorscheme.MenuHeader(fmt.Sprintf(" %s Alert Entry %s\n\n", mode, pad.Left("[q] close ", offset, " "))) header := ct.colorscheme.MenuHeader(fmt.Sprintf(" %s Alert Entry %s\n\n", mode, pad.Left("[q] close ", offset, " ")))
label := fmt.Sprintf(" Enter target price for %s %s", ct.colorscheme.MenuLabel(coinName), current) label := fmt.Sprintf(" Enter target price for %s %s", ct.colorscheme.MenuLabel(coin.Name), current)
content := fmt.Sprintf("%s\n%s\n\n%s%s\n\n\n [Enter] %s [ESC] Cancel", header, label, strings.Repeat(" ", 29), ct.State.currencyConversion, submitText) content := fmt.Sprintf("%s\n%s\n\n%s%s\n\n\n [Enter] %s [ESC] Cancel", header, label, strings.Repeat(" ", 29), ct.State.currencyConversion, submitText)
ct.UpdateUI(func() error { ct.UpdateUI(func() error {
@ -286,8 +294,9 @@ func (ct *Cointop) UpdatePriceAlertsUpdateMenu(isNew bool) error {
// ShowPriceAlertsAddMenu shows the alert add menu // ShowPriceAlertsAddMenu shows the alert add menu
func (ct *Cointop) ShowPriceAlertsAddMenu() error { func (ct *Cointop) ShowPriceAlertsAddMenu() error {
ct.debuglog("showPriceAlertsAddMenu()") ct.debuglog("showPriceAlertsAddMenu()")
coin := ct.HighlightedRowCoin()
ct.SetSelectedView(PriceAlertsView) ct.SetSelectedView(PriceAlertsView)
ct.UpdatePriceAlertsUpdateMenu(true) ct.UpdatePriceAlertsUpdateMenu(true, coin)
ct.ui.SetCursor(true) ct.ui.SetCursor(true)
ct.SetActiveView(ct.Views.Menu.Name()) ct.SetActiveView(ct.Views.Menu.Name())
ct.g.SetViewOnTop(ct.Views.Input.Name()) ct.g.SetViewOnTop(ct.Views.Input.Name())
@ -298,8 +307,9 @@ func (ct *Cointop) ShowPriceAlertsAddMenu() error {
// ShowPriceAlertsUpdateMenu shows the alerts update menu // ShowPriceAlertsUpdateMenu shows the alerts update menu
func (ct *Cointop) ShowPriceAlertsUpdateMenu() error { func (ct *Cointop) ShowPriceAlertsUpdateMenu() error {
ct.debuglog("showPriceAlertsUpdateMenu()") ct.debuglog("showPriceAlertsUpdateMenu()")
coin := ct.HighlightedRowCoin()
ct.SetSelectedView(PriceAlertsView) ct.SetSelectedView(PriceAlertsView)
ct.UpdatePriceAlertsUpdateMenu(false) ct.UpdatePriceAlertsUpdateMenu(false, coin)
ct.ui.SetCursor(true) ct.ui.SetCursor(true)
ct.SetActiveView(ct.Views.Menu.Name()) ct.SetActiveView(ct.Views.Menu.Name())
ct.g.SetViewOnTop(ct.Views.Input.Name()) ct.g.SetViewOnTop(ct.Views.Input.Name())
@ -337,11 +347,24 @@ func (ct *Cointop) EnterKeyPressHandler() error {
func (ct *Cointop) CreatePriceAlert() error { func (ct *Cointop) CreatePriceAlert() error {
ct.debuglog("createPriceAlert()") ct.debuglog("createPriceAlert()")
defer ct.HidePriceAlertsUpdateMenu() defer ct.HidePriceAlertsUpdateMenu()
var coinName string
isNew := ct.State.priceAlertEditID == "" isNew := ct.State.priceAlertNewID != ""
operator, targetPrice, err := ct.ReadAndParsePriceAlertInput()
if err != nil {
return err
}
shouldDelete := targetPrice == -1
if shouldDelete {
if ct.State.priceAlertEditID != "" {
err := ct.RemovePriceAlert(ct.State.priceAlertEditID)
if err != nil {
return err
}
}
} else {
var coinName string
if isNew { if isNew {
coin := ct.HighlightedRowCoin() coin := ct.CoinByID(ct.State.priceAlertNewID)
coinName = coin.Name coinName = coin.Name
} else { } else {
for i, entry := range ct.State.priceAlerts.Entries { for i, entry := range ct.State.priceAlerts.Entries {
@ -350,15 +373,6 @@ func (ct *Cointop) CreatePriceAlert() error {
} }
} }
} }
operator, targetPrice, err := ct.ReadAndParsePriceAlertInput()
if err != nil {
return err
}
shouldDelete := targetPrice == -1
if shouldDelete {
ct.RemovePriceAlert(ct.State.priceAlertEditID)
} else {
if err := ct.SetPriceAlert(coinName, operator, targetPrice); err != nil { if err := ct.SetPriceAlert(coinName, operator, targetPrice); err != nil {
return err return err
} }
@ -455,18 +469,21 @@ func (ct *Cointop) SetPriceAlert(coinName string, operator string, targetPrice f
if err := ct.Save(); err != nil { if err := ct.Save(); err != nil {
return err return err
} }
return nil return nil
} }
// RemovePriceAlert removes a price alert entry // RemovePriceAlert removes a price alert entry
func (ct *Cointop) RemovePriceAlert(id string) { func (ct *Cointop) RemovePriceAlert(id string) error {
ct.debuglog("removePriceAlert()") ct.debuglog("removePriceAlert()")
for i, entry := range ct.State.priceAlerts.Entries { for i, entry := range ct.State.priceAlerts.Entries {
if entry.ID == ct.State.priceAlertEditID { if entry.ID == ct.State.priceAlertEditID {
ct.State.priceAlerts.Entries = append(ct.State.priceAlerts.Entries[:i], ct.State.priceAlerts.Entries[i+1:]...) ct.State.priceAlerts.Entries = append(ct.State.priceAlerts.Entries[:i], ct.State.priceAlerts.Entries[i+1:]...)
} }
} }
if err := ct.Save(); err != nil {
return err
}
return nil
} }
// ActivePriceAlerts returns the active price alerts // ActivePriceAlerts returns the active price alerts

View File

@ -157,10 +157,19 @@ func (ct *Cointop) HighlightedRowIndex() int {
func (ct *Cointop) HighlightedRowCoin() *Coin { func (ct *Cointop) HighlightedRowCoin() *Coin {
ct.debuglog("HighlightedRowCoin()") ct.debuglog("HighlightedRowCoin()")
idx := ct.HighlightedRowIndex() idx := ct.HighlightedRowIndex()
if len(ct.State.coins) == 0 { coins := ct.State.coins
if ct.IsPriceAlertsVisible() {
rows := ct.ActivePriceAlerts()
for i, row := range rows {
if i == idx {
return ct.CoinByName(row.CoinName)
}
}
}
if len(coins) == 0 {
return nil return nil
} }
return ct.State.coins[idx] return coins[idx]
} }
// HighlightedPageRowIndex returns the index of page row of the highlighted row // HighlightedPageRowIndex returns the index of page row of the highlighted row