1
0

kivy: use exchange rates in amount dialog

This commit is contained in:
ThomasV
2015-12-03 22:43:43 +01:00
parent 74a9e2296c
commit b700340ff9
5 changed files with 66 additions and 50 deletions

View File

@@ -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>:

View File

@@ -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:
a.amount = str(a.btc_text).split()[0]
a.is_fiat = not a.is_fiat
def _set_num_zeros(self): def btc_to_fiat(self, amount_str):
try: if not amount_str:
self.electrum_config.set_key('num_zeros', value, True) return ''
except AttributeError: satoshis = self.get_amount(amount_str + ' ' + self.base_unit)
Logger.error('Electrum: Config not available ' fiat_text = run_hook('format_amount_and_units', satoshis)
'While trying to save value to config') return fiat_text if fiat_text else ''
num_zeros = AliasProperty(_get_num_zeros , _set_num_zeros)
'''Number of zeros used while representing the value in base_unit.
'''
def get_amount_text(self, amount_str, is_fiat):
text = amount_str + ' ' + self.base_unit if amount_str else ''
if text:
amount = self.get_amount(text)
x = run_hook('format_amount_and_units', amount)
if x:
text += ' / ' + x
return text
def fiat_to_btc(self, fiat_amount):
if not fiat_amount:
return ''
satoshis = pow(10, 8)
x = run_hook('format_amount_and_units', satoshis)
rate, unit = x.split()
amount = satoshis * Decimal(fiat_amount) / Decimal(rate)
return format_satoshis_plain(amount, self.decimal_point()) + ' ' + self.base_unit
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'
try: else:
Decimal(amount+c) try:
amount += c Decimal(amount+c)
except: amount += c
pass except:
return amount pass
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()

View File

@@ -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

View File

@@ -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

View File

@@ -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