Browse Source

kivy: use exchange rates in amount dialog

283
ThomasV 9 years ago
parent
commit
b700340ff9
  1. 2
      gui/kivy/main.kv
  2. 83
      gui/kivy/main_window.py
  3. 3
      gui/kivy/uix/screens.py
  4. 12
      gui/kivy/uix/ui_screens/amount.kv
  5. 10
      plugins/exchange_rate/kivy.py

2
gui/kivy/main.kv

@ -221,7 +221,7 @@
<KButton@Button>: <KButton@Button>:
size_hint: 1, None size_hint: 1, None
height: '48dp' height: '48dp'
on_release: self.label.amount = app.update_amount(self.label.amount, self.text) on_release: app.update_amount(self.label, self.text)
<TabbedPanelStrip>: <TabbedPanelStrip>:

83
gui/kivy/main_window.py

@ -11,6 +11,7 @@ from electrum.i18n import _, set_language
from electrum.contacts import Contacts from electrum.contacts import Contacts
from electrum.util import profiler from electrum.util import profiler
from electrum.plugins import run_hook from electrum.plugins import run_hook
from electrum.util import format_satoshis, format_satoshis_plain
from kivy.app import App from kivy.app import App
from kivy.core.window import Window from kivy.core.window import Window
@ -35,7 +36,7 @@ Factory.register('ELTextInput', module='electrum_gui.kivy.uix.screens')
# delayed imports: for startup speed on android # delayed imports: for startup speed on android
notification = app = ref = format_satoshis = None notification = app = ref = None
util = False util = False
@ -76,35 +77,35 @@ class ElectrumWindow(App):
status = StringProperty(_('Not Connected')) status = StringProperty(_('Not Connected'))
fiat_unit = StringProperty('')
def decimal_point(self): def decimal_point(self):
return base_units[self.base_unit] return base_units[self.base_unit]
def _get_num_zeros(self): def toggle_fiat(self, a):
try: if not a.is_fiat:
return self.electrum_config.get('num_zeros', 0) if a.fiat_text:
except AttributeError: a.fiat_amount = str(a.fiat_text).split()[0]
return 0 else:
if a.btc_text:
def _set_num_zeros(self): a.amount = str(a.btc_text).split()[0]
try: a.is_fiat = not a.is_fiat
self.electrum_config.set_key('num_zeros', value, True)
except AttributeError: def btc_to_fiat(self, amount_str):
Logger.error('Electrum: Config not available ' if not amount_str:
'While trying to save value to config') return ''
satoshis = self.get_amount(amount_str + ' ' + self.base_unit)
num_zeros = AliasProperty(_get_num_zeros , _set_num_zeros) fiat_text = run_hook('format_amount_and_units', satoshis)
'''Number of zeros used while representing the value in base_unit. return fiat_text if fiat_text else ''
'''
def fiat_to_btc(self, fiat_amount):
def get_amount_text(self, amount_str, is_fiat): if not fiat_amount:
text = amount_str + ' ' + self.base_unit if amount_str else '' return ''
if text: satoshis = pow(10, 8)
amount = self.get_amount(text) x = run_hook('format_amount_and_units', satoshis)
x = run_hook('format_amount_and_units', amount) rate, unit = x.split()
if x: amount = satoshis * Decimal(fiat_amount) / Decimal(rate)
text += ' / ' + x return format_satoshis_plain(amount, self.decimal_point()) + ' ' + self.base_unit
return text
def get_amount(self, amount_str): def get_amount(self, amount_str):
a, u = amount_str.split() a, u = amount_str.split()
@ -480,29 +481,33 @@ class ElectrumWindow(App):
def get_max_amount(self): def get_max_amount(self):
from electrum.util import format_satoshis_plain
inputs = self.wallet.get_spendable_coins(None) inputs = self.wallet.get_spendable_coins(None)
amount, fee = self.wallet.get_max_amount(self.electrum_config, inputs, None) amount, fee = self.wallet.get_max_amount(self.electrum_config, inputs, None)
return format_satoshis_plain(amount, self.decimal_point()) return format_satoshis_plain(amount, self.decimal_point())
def update_amount(self, amount, c): def update_amount(self, label, c):
amount = label.fiat_amount if label.is_fiat else label.amount
if c == '<': if c == '<':
return amount[:-1] amount = amount[:-1]
if c == '.' and amount == '': elif c == '.' and amount == '':
return '0.' amount = '0.'
if c == '0' and amount == '0': elif c == '0' and amount == '0':
return '0' amount = '0'
else:
try: try:
Decimal(amount+c) Decimal(amount+c)
amount += c amount += c
except: except:
pass pass
return amount
if label.is_fiat:
label.fiat_amount = amount
else:
label.amount = amount
def format_amount(self, x, is_diff=False, whitespaces=False): def format_amount(self, x, is_diff=False, whitespaces=False):
from electrum.util import format_satoshis return format_satoshis(x, is_diff, 0, self.decimal_point(), whitespaces)
return format_satoshis(x, is_diff, self.num_zeros,
self.decimal_point(), whitespaces)
@profiler @profiler
def update_wallet(self, *dt): def update_wallet(self, *dt):
@ -786,7 +791,7 @@ class ElectrumWindow(App):
assert u == self.base_unit assert u == self.base_unit
popup.ids.a.amount = a popup.ids.a.amount = a
def cb(): def cb():
o = popup.ids.a.text o = popup.ids.a.btc_text
label.text = o if o else label.default_text label.text = o if o else label.default_text
if callback: if callback:
callback() callback()

3
gui/kivy/uix/screens.py

@ -258,9 +258,6 @@ class ReceiveScreen(CScreen):
self.update_qr() self.update_qr()
@profiler @profiler
def update_qrtt(self):
raise
def update_qr(self): def update_qr(self):
from electrum.util import create_URI from electrum.util import create_URI
address = self.screen.ids.get('address').text address = self.screen.ids.get('address').text

12
gui/kivy/uix/ui_screens/amount.kv

@ -17,8 +17,11 @@ Popup:
Label: Label:
id: a id: a
amount: '' amount: ''
fiat_amount: ''
is_fiat: False is_fiat: False
text: app.get_amount_text(self.amount, self.is_fiat) btc_text: app.fiat_to_btc(self.fiat_amount) if self.is_fiat else (self.amount + ' ' + app.base_unit if self.amount else '')
fiat_text: (self.fiat_amount + ' ' + app.fiat_unit if self.fiat_amount else '') if self.is_fiat else app.btc_to_fiat(self.amount)
text: (self.fiat_text + ' / ' + self.btc_text if self.is_fiat else self.btc_text + ' / ' + self.fiat_text) if self.btc_text else ''
size_hint: 1, 1 size_hint: 1, 1
Widget: Widget:
@ -75,12 +78,15 @@ Popup:
size_hint: 1, None size_hint: 1, None
height: '48dp' height: '48dp'
text: '/' text: '/'
on_release: a.is_fiat = not a.is_fiat on_release:
app.toggle_fiat(a)
Button: Button:
size_hint: 1, None size_hint: 1, None
height: '48dp' height: '48dp'
text: 'Clear' text: 'Clear'
on_release: a.amount = '' on_release:
a.amount = ''
a.fiat_amount = ''
Widget: Widget:
size_hint: 1, None size_hint: 1, None

10
plugins/exchange_rate/kivy.py

@ -1,3 +1,11 @@
from exchange_rate import FxPlugin from exchange_rate import FxPlugin
from electrum.plugins import hook
class Plugin(FxPlugin): class Plugin(FxPlugin):
pass @hook
def load_wallet(self, wallet, window):
self.window = window
def on_quotes(self):
self.print_error("on quotes", self.ccy)
self.window.fiat_unit = self.ccy

Loading…
Cancel
Save