store favorites by name instead of by symbol

pull/20/head 1.0.6
Miguel Mota 6 years ago
parent a437bf124b
commit e188845dc1

@ -42,7 +42,6 @@ type Cointop struct {
api api.Interface
allcoins []*coin
coins []*coin
allcoinssymbolmap map[string]*coin
allcoinsslugmap map[string]*coin
page int
perpage int
@ -56,6 +55,8 @@ type Cointop struct {
searchfield *gocui.View
searchfieldviewname string
searchfieldvisible bool
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions.
favoritesbysymbol map[string]bool
favorites map[string]bool
filterByFavorites bool
savemux sync.Mutex
@ -77,16 +78,18 @@ func New() *Cointop {
debug = true
}
ct := &Cointop{
api: api.NewCMC(),
refreshticker: time.NewTicker(1 * time.Minute),
sortby: "rank",
sortdesc: false,
page: 0,
perpage: 100,
forcerefresh: make(chan bool),
maxtablewidth: 175,
actionsmap: actionsMap(),
shortcutkeys: defaultShortcuts(),
api: api.NewCMC(),
refreshticker: time.NewTicker(1 * time.Minute),
sortby: "rank",
sortdesc: false,
page: 0,
perpage: 100,
forcerefresh: make(chan bool),
maxtablewidth: 175,
actionsmap: actionsMap(),
shortcutkeys: defaultShortcuts(),
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions.
favoritesbysymbol: map[string]bool{},
favorites: map[string]bool{},
cache: cache.New(1*time.Minute, 2*time.Minute),
debug: debug,
@ -150,6 +153,18 @@ func New() *Cointop {
fcache.Get(coinscachekey, &allcoinsslugmap)
ct.cache.Set(coinscachekey, allcoinsslugmap, 10*time.Second)
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
// Here we're doing a lookup based on symbol and setting the favorite to the coin name instead of coin symbol.
for i := range allcoinsslugmap {
coin := allcoinsslugmap[i]
for k := range ct.favoritesbysymbol {
if coin.Symbol == k {
ct.favorites[coin.Name] = true
delete(ct.favoritesbysymbol, k)
}
}
}
var globaldata []float64
chartcachekey := strings.ToLower(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.selectedchartrange, " ", "", -1)))
fcache.Get(chartcachekey, &globaldata)

@ -48,11 +48,19 @@ func (ct *Cointop) setupConfig() error {
func (ct *Cointop) loadFavoritesFromConfig() error {
for k, arr := range ct.config.Favorites {
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions.
if k == "symbols" {
for _, ifc := range arr {
v, ok := ifc.(string)
if ok {
ct.favorites[strings.ToUpper(v)] = true
ct.favoritesbysymbol[strings.ToUpper(v)] = true
}
}
} else if k == "names" {
for _, ifc := range arr {
v, ok := ifc.(string)
if ok {
ct.favorites[v] = true
}
}
}
@ -134,12 +142,15 @@ func (ct *Cointop) configToToml() ([]byte, error) {
var favorites []interface{}
for k, ok := range ct.favorites {
if ok {
var i interface{} = strings.ToUpper(k)
var i interface{} = k
favorites = append(favorites, i)
}
}
var favoritesbysymbol []interface{}
favoritesIfcs := map[string][]interface{}{
"symbols": favorites,
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions. Kept for backward compatibility.
"symbols": favoritesbysymbol,
"names": favorites,
}
var currencyIfc interface{} = ct.currencyconversion

@ -5,12 +5,12 @@ func (ct *Cointop) toggleFavorite() error {
if coin == nil {
return nil
}
_, ok := ct.favorites[coin.Symbol]
_, ok := ct.favorites[coin.Name]
if ok {
delete(ct.favorites, coin.Symbol)
delete(ct.favorites, coin.Name)
coin.Favorite = false
} else {
ct.favorites[coin.Symbol] = true
ct.favorites[coin.Name] = true
coin.Favorite = true
}
ct.updateTable()

@ -15,7 +15,6 @@ func (ct *Cointop) refreshAll() error {
defer ct.refreshmux.Unlock()
ct.setRefreshStatus()
ct.cache.Delete("allcoinsslugmap")
ct.cache.Delete("allcoinssymbolmap")
ct.cache.Delete("market")
ct.updateCoins()
ct.updateTable()

@ -105,7 +105,7 @@ func (ct *Cointop) updateTable() error {
for i := range ct.allcoinsslugmap {
v := ct.allcoinsslugmap[i]
if ct.favorites[v.Symbol] {
if ct.favorites[v.Name] {
v.Favorite = true
}
}
@ -199,3 +199,14 @@ func (ct *Cointop) allCoins() []*coin {
return ct.allcoins
}
func (ct *Cointop) coinBySymbol(symbol string) *coin {
for i := range ct.allcoins {
coin := ct.allcoins[i]
if coin.Symbol == symbol {
return coin
}
}
return nil
}

@ -46,7 +46,7 @@ func getLimitedCoinData(convert string, offset int) (map[string]apitypes.Coin, e
return ret, err
}
for _, v := range coins {
ret[v.Symbol] = apitypes.Coin{
ret[v.Name] = apitypes.Coin{
ID: v.Slug,
Name: v.Name,
Symbol: v.Symbol,
@ -79,7 +79,7 @@ func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, erro
pricestr = fmt.Sprintf("%.5f", price)
}
price, _ = strconv.ParseFloat(pricestr, 64)
ret[v.Symbol] = apitypes.Coin{
ret[v.Name] = apitypes.Coin{
ID: strings.ToLower(v.Name),
Name: v.Name,
Symbol: v.Symbol,

Loading…
Cancel
Save