From a1eaf351a70d6b779eb278148cb6c62584d21b2a Mon Sep 17 00:00:00 2001 From: ThomasV Date: Mon, 25 Jan 2016 19:30:16 +0100 Subject: [PATCH] kivy: use EventDispatcher with exchange rates plugin --- gui/kivy/main_window.py | 10 +++++++ gui/kivy/uix/dialogs/fx_dialog.py | 24 ++++++++++++----- plugins/exchange_rate/exchange_rate.py | 2 +- plugins/exchange_rate/kivy.py | 37 ++++++++++++++++++++++---- 4 files changed, 60 insertions(+), 13 deletions(-) diff --git a/gui/kivy/main_window.py b/gui/kivy/main_window.py index 693affea..a0e64ab6 100644 --- a/gui/kivy/main_window.py +++ b/gui/kivy/main_window.py @@ -64,6 +64,15 @@ class ElectrumWindow(App): electrum_config = ObjectProperty(None) + def on_quotes(self, d): + print "main_window: on_quotes" + pass + + def on_history(self, d): + print "main_window: on_history" + if self.history_screen: + self.history_screen.update() + def _get_bu(self): return self.electrum_config.get('base_unit', 'mBTC') @@ -299,6 +308,7 @@ class ElectrumWindow(App): self.on_size(win, win.size) self.init_ui() self.load_wallet_by_name(self.electrum_config.get_wallet_path()) + run_hook('init_kivy', self) def load_wallet_by_name(self, wallet_path): if not wallet_path: diff --git a/gui/kivy/uix/dialogs/fx_dialog.py b/gui/kivy/uix/dialogs/fx_dialog.py index 345ffb49..df74d9c4 100644 --- a/gui/kivy/uix/dialogs/fx_dialog.py +++ b/gui/kivy/uix/dialogs/fx_dialog.py @@ -80,6 +80,8 @@ 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 @@ -88,34 +90,42 @@ 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: - p = self.plugins.enable('exchange_rate') - p.init_kivy(self.app) + p = self.plugins.get('exchange_rate') + 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(partial(self.add_exchanges, values, text), 0.1) + Clock.schedule_once(lambda dt: self.add_exchanges(values, text)) + Clock.schedule_once(lambda dt: self.add_currencies()) - def add_exchanges(self, values, text, dt): + def add_exchanges(self, values, text): ex = self.ids.exchanges ex.values = values ex.text = text - def on_exchange(self, text): if not text: return p = self.plugins.get('exchange_rate') if p and text != p.exchange.name(): p.set_exchange(text) - Clock.schedule_once(self.add_currencies, 1) - def add_currencies(self, dt): + def add_currencies(self): p = self.plugins.get('exchange_rate') currencies = sorted(p.exchange.quotes.keys()) if p else [] self.ids.ccy.values = currencies diff --git a/plugins/exchange_rate/exchange_rate.py b/plugins/exchange_rate/exchange_rate.py index 53bb1145..1d06341d 100644 --- a/plugins/exchange_rate/exchange_rate.py +++ b/plugins/exchange_rate/exchange_rate.py @@ -55,9 +55,9 @@ class ExchangeBase(PrintError): self.print_error("getting fx quotes for", ccy) self.quotes = self.get_rates(ccy) self.print_error("received fx quotes") - self.on_quotes() except BaseException as e: self.print_error("failed fx quotes:", e) + self.on_quotes() def update(self, ccy): t = Thread(target=self.update_safe, args=(ccy,)) diff --git a/plugins/exchange_rate/kivy.py b/plugins/exchange_rate/kivy.py index ca9d7367..a68c2926 100644 --- a/plugins/exchange_rate/kivy.py +++ b/plugins/exchange_rate/kivy.py @@ -1,14 +1,38 @@ -from exchange_rate import FxPlugin +from __future__ import absolute_import + +from .exchange_rate import FxPlugin from electrum.plugins import hook + +from kivy.event import EventDispatcher + +class MyEventDispatcher(EventDispatcher): + + def __init__(self, **kwargs): + self.register_event_type('on_quotes') + self.register_event_type('on_history') + super(MyEventDispatcher, self).__init__(**kwargs) + + def on_quotes(self, *args): + pass + + def on_history(self, *args): + pass + + class Plugin(FxPlugin): + def __init__(self, parent, config, name): + FxPlugin.__init__(self, parent, config, name) + self.dispatcher = MyEventDispatcher() + def on_quotes(self): - self.print_error("on quotes", self.ccy) + self.print_error("on_quotes") + self.dispatcher.dispatch('on_quotes') def on_history(self): - self.print_error("on history") - self.window.history_screen.update() + self.print_error("on_history") + self.dispatcher.dispatch('on_history') def on_close(self): self.print_error("on close") @@ -17,9 +41,12 @@ class Plugin(FxPlugin): @hook def init_kivy(self, window): + self.print_error("init_kivy") self.window = window + self.dispatcher.bind(on_quotes=window.on_quotes) + self.dispatcher.bind(on_history=window.on_history) self.window.fiat_unit = self.ccy - self.window.history_screen.update() + self.dispatcher.dispatch('on_history') @hook def load_wallet(self, wallet, window):