kivy: cache dialogs

This commit is contained in:
ThomasV 2016-03-06 11:46:09 +01:00
parent 74b12f02b5
commit 8bc0fcf541
3 changed files with 103 additions and 73 deletions

View File

@ -216,6 +216,9 @@ class ElectrumWindow(App):
Clock.create_trigger(self.update_status, .5) Clock.create_trigger(self.update_status, .5)
self._trigger_notify_transactions = \ self._trigger_notify_transactions = \
Clock.create_trigger(self.notify_transactions, 5) Clock.create_trigger(self.notify_transactions, 5)
# cached dialogs
self._settings_dialog = None
self._password_dialog = None
def on_pr(self, pr): def on_pr(self, pr):
@ -425,11 +428,15 @@ class ElectrumWindow(App):
#self.gui.main_gui.toggle_settings(self) #self.gui.main_gui.toggle_settings(self)
return True return True
def settings_dialog(self):
if self._settings_dialog is None:
from uix.dialogs.settings import SettingsDialog
self._settings_dialog = SettingsDialog(self)
self._settings_dialog.open()
def popup_dialog(self, name): def popup_dialog(self, name):
if name == 'settings': if name == 'settings':
from uix.dialogs.settings import SettingsDialog self.settings_dialog()
d = SettingsDialog(self)
d.open()
elif name == 'wallets': elif name == 'wallets':
from uix.dialogs.wallets import WalletDialog from uix.dialogs.wallets import WalletDialog
d = WalletDialog() d = WalletDialog()
@ -749,7 +756,7 @@ class ElectrumWindow(App):
self.protected(_("Enter your PIN code in order to decrypt your seed"), self._show_seed, (label,)) self.protected(_("Enter your PIN code in order to decrypt your seed"), self._show_seed, (label,))
def _show_seed(self, label, password): def _show_seed(self, label, password):
if not password: if self.wallet.use_encryption and password is None:
return return
try: try:
seed = self.wallet.get_seed(password) seed = self.wallet.get_seed(password)
@ -758,31 +765,39 @@ class ElectrumWindow(App):
return return
label.text = _('Seed') + ':\n' + seed label.text = _('Seed') + ':\n' + seed
def change_password(self): def change_password(self, cb):
self.protected(_("Changing PIN code.") + '\n' + _("Enter your current PIN:"), self._change_password, ())
def _change_password(self, old_password):
if self.wallet.use_encryption: if self.wallet.use_encryption:
self.protected(_("Changing PIN code.") + '\n' + _("Enter your current PIN:"), self._change_password, (cb,))
else:
self._change_password(cb, None)
def _change_password(self, cb, old_password):
if self.wallet.use_encryption:
if old_password is None:
return
try: try:
self.wallet.check_password(old_password) self.wallet.check_password(old_password)
except InvalidPassword: except InvalidPassword:
self.show_error("Invalid PIN") self.show_error("Invalid PIN")
return return
self.password_dialog(_('Enter new PIN'), self._change_password2, (old_password,)) self.password_dialog(_('Enter new PIN'), self._change_password2, (cb, old_password,))
def _change_password2(self, old_password, new_password): def _change_password2(self, cb, old_password, new_password):
self.password_dialog(_('Confirm new PIN'), self._change_password3, (old_password, new_password)) self.password_dialog(_('Confirm new PIN'), self._change_password3, (cb, old_password, new_password))
def _change_password3(self, old_password, new_password, confirmed_password): def _change_password3(self, cb, old_password, new_password, confirmed_password):
if new_password == confirmed_password: if new_password == confirmed_password:
self.wallet.update_password(old_password, new_password) self.wallet.update_password(old_password, new_password)
cb()
else: else:
self.show_error("PIN numbers do not match") self.show_error("PIN numbers do not match")
def password_dialog(self, msg, f, args): def password_dialog(self, msg, f, args):
from uix.dialogs.password_dialog import PasswordDialog
def callback(pw): def callback(pw):
Clock.schedule_once(lambda x: apply(f, args + (pw,)), 0.1) Clock.schedule_once(lambda x: apply(f, args + (pw,)), 0.1)
popup = PasswordDialog(msg, callback) if self._password_dialog is None:
popup.open() from uix.dialogs.password_dialog import PasswordDialog
self._password_dialog = PasswordDialog()
self._password_dialog.init(msg, callback)
self._password_dialog.open()

View File

@ -75,11 +75,14 @@ Builder.load_string('''
class PasswordDialog(Factory.Popup): class PasswordDialog(Factory.Popup):
def __init__(self, message, callback): #def __init__(self, message, callback):
Factory.Popup.__init__(self) # Factory.Popup.__init__(self)
def init(self, message, callback):
self.pw = None
self.message = message self.message = message
self.callback = callback self.callback = callback
self.pw = None self.ids.kb.password = ''
def update_password(self, c): def update_password(self, c):
kb = self.ids.kb kb = self.ids.kb

View File

@ -63,7 +63,7 @@ Builder.load_string('''
action: partial(root.language_dialog, self) action: partial(root.language_dialog, self)
CardSeparator CardSeparator
SettingsItem: SettingsItem:
status: 'ON' if app.wallet.use_encryption else 'OFF' status: root.password_status()
disabled: app.wallet.is_watching_only() disabled: app.wallet.is_watching_only()
title: _('PIN code') + ': ' + self.status title: _('PIN code') + ': ' + self.status
description: _("Change your PIN code.") description: _("Change your PIN code.")
@ -104,18 +104,10 @@ Builder.load_string('''
title: _('Coin selection') + ': ' + self.status title: _('Coin selection') + ': ' + self.status
description: "Coin selection method" description: "Coin selection method"
action: partial(root.coinselect_dialog, self) action: partial(root.coinselect_dialog, self)
BoxLayout:
size_hint: 1, 0.1
Widget:
size_hint: 0.5, None
Button:
size_hint: 0.5, None
height: '48dp'
text: _('OK')
on_release:
settings.dismiss()
''') ''')
class SettingsDialog(Factory.Popup): class SettingsDialog(Factory.Popup):
def __init__(self, app): def __init__(self, app):
@ -125,54 +117,73 @@ class SettingsDialog(Factory.Popup):
Factory.Popup.__init__(self) Factory.Popup.__init__(self)
layout = self.ids.scrollviewlayout layout = self.ids.scrollviewlayout
layout.bind(minimum_height=layout.setter('height')) layout.bind(minimum_height=layout.setter('height'))
# cached dialogs
self._fx_dialog = None
self._fee_dialog = None
self._network_dialog = None
self._language_dialog = None
self._unit_dialog = None
self._coinselect_dialog = None
def get_language_name(self): def get_language_name(self):
return languages.get(self.config.get('language', 'en_UK'), '') return languages.get(self.config.get('language', 'en_UK'), '')
def change_password(self, label, dt): def password_status(self):
self.app.change_password() if self.app.wallet.is_watching_only():
return 'watching-only'
return 'ON' if self.app.wallet.use_encryption else 'OFF'
def change_password(self, item, dt):
def cb():
item.status = self.password_status()
self.app.change_password(cb)
def language_dialog(self, item, dt): def language_dialog(self, item, dt):
l = self.config.get('language', 'en_UK') if self._language_dialog is None:
def cb(key): l = self.config.get('language', 'en_UK')
self.config.set_key("language", key, True) def cb(key):
item.lang = self.get_language_name() self.config.set_key("language", key, True)
self.app.language = key item.lang = self.get_language_name()
d = ChoiceDialog(_('Language'), languages, l, cb) self.app.language = key
d.open() self._language_dialog = ChoiceDialog(_('Language'), languages, l, cb)
self._language_dialog.open()
def unit_dialog(self, item, dt): def unit_dialog(self, item, dt):
def cb(text): if self._unit_dialog is None:
self.app._set_bu(text) def cb(text):
item.bu = self.app.base_unit self._set_bu(text)
d = ChoiceDialog(_('Denomination'), base_units.keys(), self.app.base_unit, cb) item.bu = self.app.base_unit
d.open() self._unit_dialog = ChoiceDialog(_('Denomination'), base_units.keys(), self.app.base_unit, cb)
self._unit_dialog.open()
def coinselect_status(self): def coinselect_status(self):
return self.app.wallet.coin_chooser_name(self.app.electrum_config) return self.app.wallet.coin_chooser_name(self.app.electrum_config)
def coinselect_dialog(self, item, dt): def coinselect_dialog(self, item, dt):
from electrum import COIN_CHOOSERS if self._coinselect_dialog is None:
choosers = sorted(COIN_CHOOSERS.keys()) from electrum import COIN_CHOOSERS
chooser_name = self.app.wallet.coin_chooser_name(self.config) choosers = sorted(COIN_CHOOSERS.keys())
def cb(text): chooser_name = self.app.wallet.coin_chooser_name(self.config)
self.config.set_key('coin_chooser', text) def cb(text):
item.status = text self.config.set_key('coin_chooser', text)
d = ChoiceDialog(_('Coin selection'), choosers, chooser_name, cb) item.status = text
d.open() self._coinselect_dialog = ChoiceDialog(_('Coin selection'), choosers, chooser_name, cb)
self._coinselect_dialog.open()
def network_dialog(self, item, dt): def network_dialog(self, item, dt):
server, port, protocol, proxy, auto_connect = self.app.network.get_parameters() if self._network_dialog is None:
def cb(popup): server, port, protocol, proxy, auto_connect = self.app.network.get_parameters()
server = popup.ids.host.text def cb(popup):
auto_connect = popup.ids.auto_connect.active server = popup.ids.host.text
self.app.network.set_parameters(server, port, protocol, proxy, auto_connect) auto_connect = popup.ids.auto_connect.active
item.status = self.network_status() self.app.network.set_parameters(server, port, protocol, proxy, auto_connect)
popup = Builder.load_file('gui/kivy/uix/ui_screens/network.kv') item.status = self.network_status()
popup.ids.host.text = server popup = Builder.load_file('gui/kivy/uix/ui_screens/network.kv')
popup.ids.auto_connect.active = auto_connect popup.ids.host.text = server
popup.on_dismiss = lambda: cb(popup) popup.ids.auto_connect.active = auto_connect
popup.open() popup.on_dismiss = lambda: cb(popup)
self._network_dialog = popup
self._network_dialog.open()
def network_status(self): def network_status(self):
server, port, protocol, proxy, auto_connect = self.app.network.get_parameters() server, port, protocol, proxy, auto_connect = self.app.network.get_parameters()
@ -200,11 +211,12 @@ class SettingsDialog(Factory.Popup):
return self.app.format_amount_and_units(F) + '/kB' return self.app.format_amount_and_units(F) + '/kB'
def fee_dialog(self, label, dt): def fee_dialog(self, label, dt):
from fee_dialog import FeeDialog if self._fee_dialog is None:
def cb(): from fee_dialog import FeeDialog
label.status = self.fee_status() def cb():
d = FeeDialog(self.app, self.config, cb) label.status = self.fee_status()
d.open() self._fee_dialog = FeeDialog(self.app, self.config, cb)
self._fee_dialog.open()
def fx_status(self): def fx_status(self):
p = self.plugins.get('exchange_rate') p = self.plugins.get('exchange_rate')
@ -216,9 +228,9 @@ class SettingsDialog(Factory.Popup):
return 'Disabled' return 'Disabled'
def fx_dialog(self, label, dt): def fx_dialog(self, label, dt):
from fx_dialog import FxDialog if self._fx_dialog is None:
def cb(): from fx_dialog import FxDialog
label.status = self.fx_status() def cb():
d = FxDialog(self.app, self.plugins, self.config, cb) label.status = self.fx_status()
d.open() self._fx_dialog = FxDialog(self.app, self.plugins, self.config, cb)
self._fx_dialog.open()