From a5fd6d2e6c99e566272b59ce450a4be224b67335 Mon Sep 17 00:00:00 2001 From: ThomasV Date: Fri, 29 Jan 2016 19:58:40 +0100 Subject: [PATCH] kivy: select currency first, then source. show only sources with history data --- gui/kivy/uix/dialogs/fx_dialog.py | 55 +++++++++++--------------- plugins/exchange_rate/exchange_rate.py | 34 +++++++++++++--- 2 files changed, 50 insertions(+), 39 deletions(-) diff --git a/gui/kivy/uix/dialogs/fx_dialog.py b/gui/kivy/uix/dialogs/fx_dialog.py index df74d9c4d..e95a63903 100644 --- a/gui/kivy/uix/dialogs/fx_dialog.py +++ b/gui/kivy/uix/dialogs/fx_dialog.py @@ -29,12 +29,12 @@ Builder.load_string(''' orientation: 'horizontal' size_hint: 1, 0.1 Label: - text: _('Source') + text: _('Currency') height: '48dp' Spinner: height: '48dp' - id: exchanges - on_text: popup.on_exchange(self.text) + id: ccy + on_text: popup.on_currency(self.text) Widget: size_hint: 1, 0.1 @@ -43,12 +43,13 @@ Builder.load_string(''' orientation: 'horizontal' size_hint: 1, 0.1 Label: - text: _('Currency') + text: _('Source') height: '48dp' Spinner: height: '48dp' - id: ccy - on_text: popup.on_currency(self.text) + id: exchanges + on_text: popup.on_exchange(self.text) + Widget: size_hint: 1, 0.2 @@ -80,8 +81,6 @@ from functools import partial class FxDialog(Factory.Popup): - __events__ = ('on_quotes', ) - def __init__(self, app, plugins, config, callback): Factory.Popup.__init__(self) self.app = app @@ -90,11 +89,6 @@ class FxDialog(Factory.Popup): self.plugins = plugins p = self.plugins.get('exchange_rate') self.ids.enabled.active = bool(p) - if p: - p.dispatcher.bind(on_quotes=self.on_quotes) - - def on_quotes(self, b): - self.add_currencies() def on_active(self, b): if b: @@ -102,21 +96,17 @@ class FxDialog(Factory.Popup): if p is None: p = self.plugins.enable('exchange_rate') p.init_kivy(self.app) - p.dispatcher.bind(on_quotes=self.on_quotes) - - values = sorted(p.exchanges.keys()) - text = p.exchange.name() else: self.plugins.disable('exchange_rate') - values = [] - text = '' - Clock.schedule_once(lambda dt: self.add_exchanges(values, text)) Clock.schedule_once(lambda dt: self.add_currencies()) - def add_exchanges(self, values, text): + def add_exchanges(self): + p = self.plugins.get('exchange_rate') + exchanges = sorted(p.exchanges_by_ccy.get(p.get_currency())) if p else [] + mx = p.exchange.name() if p else '' ex = self.ids.exchanges - ex.values = values - ex.text = text + ex.values = exchanges + ex.text = (mx if mx in exchanges else exchanges[0]) if p else '' def on_exchange(self, text): if not text: @@ -127,16 +117,15 @@ class FxDialog(Factory.Popup): def add_currencies(self): p = self.plugins.get('exchange_rate') - currencies = sorted(p.exchange.quotes.keys()) if p else [] + currencies = sorted(p.exchanges_by_ccy.keys()) if p else [] + my_ccy = p.get_currency() if p else '' self.ids.ccy.values = currencies - my_ccy = p.get_currency() if p else None - if currencies: - self.ids.ccy.text = my_ccy if my_ccy in currencies else currencies[0] + self.ids.ccy.text = my_ccy def on_currency(self, ccy): - if not ccy: - return - p = self.plugins.get('exchange_rate') - if p and ccy != p.get_currency(): - p.set_currency(ccy) - self.app.fiat_unit = ccy + if ccy: + p = self.plugins.get('exchange_rate') + if p and ccy != p.get_currency(): + p.set_currency(ccy) + self.app.fiat_unit = ccy + Clock.schedule_once(lambda dt: self.add_exchanges()) diff --git a/plugins/exchange_rate/exchange_rate.py b/plugins/exchange_rate/exchange_rate.py index c08aa83f4..fb6f7292d 100644 --- a/plugins/exchange_rate/exchange_rate.py +++ b/plugins/exchange_rate/exchange_rate.py @@ -260,6 +260,32 @@ class Winkdex(ExchangeBase): for h in history]) + +def dictinvert(d): + inv = {} + for k, vlist in d.iteritems(): + for v in vlist: + keys = inv.setdefault(v, []) + keys.append(k) + return inv + +def get_exchanges(): + is_exchange = lambda obj: (inspect.isclass(obj) + and issubclass(obj, ExchangeBase) + and obj != ExchangeBase) + return dict(inspect.getmembers(sys.modules[__name__], is_exchange)) + +def get_exchanges_by_ccy(): + "return only the exchanges that have history rates (which is hardcoded)" + d = {} + exchanges = get_exchanges() + for name, klass in exchanges.items(): + exchange = klass(None, None) + d[name] = exchange.history_ccys() + return dictinvert(d) + + + class FxPlugin(BasePlugin, ThreadJob): def __init__(self, parent, config, name): @@ -268,11 +294,8 @@ class FxPlugin(BasePlugin, ThreadJob): self.history_used_spot = False self.ccy_combo = None self.hist_checkbox = None - is_exchange = lambda obj: (inspect.isclass(obj) - and issubclass(obj, ExchangeBase) - and obj != ExchangeBase) - self.exchanges = dict(inspect.getmembers(sys.modules[__name__], - is_exchange)) + self.exchanges = get_exchanges() + self.exchanges_by_ccy = get_exchanges_by_ccy() self.set_exchange(self.config_exchange()) def ccy_amount_str(self, amount, commas): @@ -370,4 +393,3 @@ class FxPlugin(BasePlugin, ThreadJob): def historical_value_str(self, satoshis, d_t): rate = self.history_rate(d_t) return self.value_str(satoshis, rate) -