kivy: use EventDispatcher with exchange rates plugin
This commit is contained in:
@@ -64,6 +64,15 @@ class ElectrumWindow(App):
|
|||||||
|
|
||||||
electrum_config = ObjectProperty(None)
|
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):
|
def _get_bu(self):
|
||||||
return self.electrum_config.get('base_unit', 'mBTC')
|
return self.electrum_config.get('base_unit', 'mBTC')
|
||||||
|
|
||||||
@@ -299,6 +308,7 @@ class ElectrumWindow(App):
|
|||||||
self.on_size(win, win.size)
|
self.on_size(win, win.size)
|
||||||
self.init_ui()
|
self.init_ui()
|
||||||
self.load_wallet_by_name(self.electrum_config.get_wallet_path())
|
self.load_wallet_by_name(self.electrum_config.get_wallet_path())
|
||||||
|
run_hook('init_kivy', self)
|
||||||
|
|
||||||
def load_wallet_by_name(self, wallet_path):
|
def load_wallet_by_name(self, wallet_path):
|
||||||
if not wallet_path:
|
if not wallet_path:
|
||||||
|
|||||||
@@ -80,6 +80,8 @@ from functools import partial
|
|||||||
|
|
||||||
class FxDialog(Factory.Popup):
|
class FxDialog(Factory.Popup):
|
||||||
|
|
||||||
|
__events__ = ('on_quotes', )
|
||||||
|
|
||||||
def __init__(self, app, plugins, config, callback):
|
def __init__(self, app, plugins, config, callback):
|
||||||
Factory.Popup.__init__(self)
|
Factory.Popup.__init__(self)
|
||||||
self.app = app
|
self.app = app
|
||||||
@@ -88,34 +90,42 @@ class FxDialog(Factory.Popup):
|
|||||||
self.plugins = plugins
|
self.plugins = plugins
|
||||||
p = self.plugins.get('exchange_rate')
|
p = self.plugins.get('exchange_rate')
|
||||||
self.ids.enabled.active = bool(p)
|
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):
|
def on_active(self, b):
|
||||||
if b:
|
if b:
|
||||||
p = self.plugins.enable('exchange_rate')
|
p = self.plugins.get('exchange_rate')
|
||||||
p.init_kivy(self.app)
|
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())
|
values = sorted(p.exchanges.keys())
|
||||||
text = p.exchange.name()
|
text = p.exchange.name()
|
||||||
else:
|
else:
|
||||||
self.plugins.disable('exchange_rate')
|
self.plugins.disable('exchange_rate')
|
||||||
values = []
|
values = []
|
||||||
text = ''
|
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 = self.ids.exchanges
|
||||||
ex.values = values
|
ex.values = values
|
||||||
ex.text = text
|
ex.text = text
|
||||||
|
|
||||||
|
|
||||||
def on_exchange(self, text):
|
def on_exchange(self, text):
|
||||||
if not text:
|
if not text:
|
||||||
return
|
return
|
||||||
p = self.plugins.get('exchange_rate')
|
p = self.plugins.get('exchange_rate')
|
||||||
if p and text != p.exchange.name():
|
if p and text != p.exchange.name():
|
||||||
p.set_exchange(text)
|
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')
|
p = self.plugins.get('exchange_rate')
|
||||||
currencies = sorted(p.exchange.quotes.keys()) if p else []
|
currencies = sorted(p.exchange.quotes.keys()) if p else []
|
||||||
self.ids.ccy.values = currencies
|
self.ids.ccy.values = currencies
|
||||||
|
|||||||
@@ -55,9 +55,9 @@ class ExchangeBase(PrintError):
|
|||||||
self.print_error("getting fx quotes for", ccy)
|
self.print_error("getting fx quotes for", ccy)
|
||||||
self.quotes = self.get_rates(ccy)
|
self.quotes = self.get_rates(ccy)
|
||||||
self.print_error("received fx quotes")
|
self.print_error("received fx quotes")
|
||||||
self.on_quotes()
|
|
||||||
except BaseException as e:
|
except BaseException as e:
|
||||||
self.print_error("failed fx quotes:", e)
|
self.print_error("failed fx quotes:", e)
|
||||||
|
self.on_quotes()
|
||||||
|
|
||||||
def update(self, ccy):
|
def update(self, ccy):
|
||||||
t = Thread(target=self.update_safe, args=(ccy,))
|
t = Thread(target=self.update_safe, args=(ccy,))
|
||||||
|
|||||||
@@ -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 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):
|
class Plugin(FxPlugin):
|
||||||
|
|
||||||
|
def __init__(self, parent, config, name):
|
||||||
|
FxPlugin.__init__(self, parent, config, name)
|
||||||
|
self.dispatcher = MyEventDispatcher()
|
||||||
|
|
||||||
def on_quotes(self):
|
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):
|
def on_history(self):
|
||||||
self.print_error("on history")
|
self.print_error("on_history")
|
||||||
self.window.history_screen.update()
|
self.dispatcher.dispatch('on_history')
|
||||||
|
|
||||||
def on_close(self):
|
def on_close(self):
|
||||||
self.print_error("on close")
|
self.print_error("on close")
|
||||||
@@ -17,9 +41,12 @@ class Plugin(FxPlugin):
|
|||||||
|
|
||||||
@hook
|
@hook
|
||||||
def init_kivy(self, window):
|
def init_kivy(self, window):
|
||||||
|
self.print_error("init_kivy")
|
||||||
self.window = window
|
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.fiat_unit = self.ccy
|
||||||
self.window.history_screen.update()
|
self.dispatcher.dispatch('on_history')
|
||||||
|
|
||||||
@hook
|
@hook
|
||||||
def load_wallet(self, wallet, window):
|
def load_wallet(self, wallet, window):
|
||||||
|
|||||||
Reference in New Issue
Block a user