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 api api.Interface
allcoins []*coin allcoins []*coin
coins []*coin coins []*coin
allcoinssymbolmap map[string]*coin
allcoinsslugmap map[string]*coin allcoinsslugmap map[string]*coin
page int page int
perpage int perpage int
@ -56,6 +55,8 @@ type Cointop struct {
searchfield *gocui.View searchfield *gocui.View
searchfieldviewname string searchfieldviewname string
searchfieldvisible bool searchfieldvisible bool
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions.
favoritesbysymbol map[string]bool
favorites map[string]bool favorites map[string]bool
filterByFavorites bool filterByFavorites bool
savemux sync.Mutex savemux sync.Mutex
@ -77,16 +78,18 @@ func New() *Cointop {
debug = true debug = true
} }
ct := &Cointop{ ct := &Cointop{
api: api.NewCMC(), api: api.NewCMC(),
refreshticker: time.NewTicker(1 * time.Minute), refreshticker: time.NewTicker(1 * time.Minute),
sortby: "rank", sortby: "rank",
sortdesc: false, sortdesc: false,
page: 0, page: 0,
perpage: 100, perpage: 100,
forcerefresh: make(chan bool), forcerefresh: make(chan bool),
maxtablewidth: 175, maxtablewidth: 175,
actionsmap: actionsMap(), actionsmap: actionsMap(),
shortcutkeys: defaultShortcuts(), shortcutkeys: defaultShortcuts(),
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions.
favoritesbysymbol: map[string]bool{},
favorites: map[string]bool{}, favorites: map[string]bool{},
cache: cache.New(1*time.Minute, 2*time.Minute), cache: cache.New(1*time.Minute, 2*time.Minute),
debug: debug, debug: debug,
@ -150,6 +153,18 @@ func New() *Cointop {
fcache.Get(coinscachekey, &allcoinsslugmap) fcache.Get(coinscachekey, &allcoinsslugmap)
ct.cache.Set(coinscachekey, allcoinsslugmap, 10*time.Second) 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 var globaldata []float64
chartcachekey := strings.ToLower(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.selectedchartrange, " ", "", -1))) chartcachekey := strings.ToLower(fmt.Sprintf("%s_%s", "globaldata", strings.Replace(ct.selectedchartrange, " ", "", -1)))
fcache.Get(chartcachekey, &globaldata) fcache.Get(chartcachekey, &globaldata)

@ -48,11 +48,19 @@ func (ct *Cointop) setupConfig() error {
func (ct *Cointop) loadFavoritesFromConfig() error { func (ct *Cointop) loadFavoritesFromConfig() error {
for k, arr := range ct.config.Favorites { for k, arr := range ct.config.Favorites {
// DEPRECATED: favorites by 'symbol' is deprecated because of collisions.
if k == "symbols" { if k == "symbols" {
for _, ifc := range arr { for _, ifc := range arr {
v, ok := ifc.(string) v, ok := ifc.(string)
if ok { 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{} var favorites []interface{}
for k, ok := range ct.favorites { for k, ok := range ct.favorites {
if ok { if ok {
var i interface{} = strings.ToUpper(k) var i interface{} = k
favorites = append(favorites, i) favorites = append(favorites, i)
} }
} }
var favoritesbysymbol []interface{}
favoritesIfcs := map[string][]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 var currencyIfc interface{} = ct.currencyconversion

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

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

@ -105,7 +105,7 @@ func (ct *Cointop) updateTable() error {
for i := range ct.allcoinsslugmap { for i := range ct.allcoinsslugmap {
v := ct.allcoinsslugmap[i] v := ct.allcoinsslugmap[i]
if ct.favorites[v.Symbol] { if ct.favorites[v.Name] {
v.Favorite = true v.Favorite = true
} }
} }
@ -199,3 +199,14 @@ func (ct *Cointop) allCoins() []*coin {
return ct.allcoins 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 return ret, err
} }
for _, v := range coins { for _, v := range coins {
ret[v.Symbol] = apitypes.Coin{ ret[v.Name] = apitypes.Coin{
ID: v.Slug, ID: v.Slug,
Name: v.Name, Name: v.Name,
Symbol: v.Symbol, Symbol: v.Symbol,
@ -79,7 +79,7 @@ func (s *Service) GetAllCoinData(convert string) (map[string]apitypes.Coin, erro
pricestr = fmt.Sprintf("%.5f", price) pricestr = fmt.Sprintf("%.5f", price)
} }
price, _ = strconv.ParseFloat(pricestr, 64) price, _ = strconv.ParseFloat(pricestr, 64)
ret[v.Symbol] = apitypes.Coin{ ret[v.Name] = apitypes.Coin{
ID: strings.ToLower(v.Name), ID: strings.ToLower(v.Name),
Name: v.Name, Name: v.Name,
Symbol: v.Symbol, Symbol: v.Symbol,

Loading…
Cancel
Save