Skip to content

Commit

Permalink
Account balance title refactoring
Browse files Browse the repository at this point in the history
Binds/unbinds balance update on screen enter/leave, fixes #80
  • Loading branch information
AndreMiras committed Aug 3, 2017
1 parent 32c7cfc commit 7262fae
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
33 changes: 18 additions & 15 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,18 +390,13 @@ def setup(self):
# triggers the update
self.current_account = self.controller.current_account

def is_selected(self):
def on_current_account(self, instance, account):
"""
Returns True if the overview sub-screen is selected,
otherwise returns False.
Updates current_account_string and fetches the new account balance.
"""
return self.parent.manager.current == 'overview'

def on_current_account(self, instance, account):
address = "0x" + account.address.encode("hex")
self.current_account_string = address
if self.is_selected():
self.controller.fetch_and_update_balance()
self.controller.fetch_balance()


class PWSelectList(BoxLayout):
Expand Down Expand Up @@ -916,6 +911,18 @@ def screen_manager(self):
def set_toolbar_title(self, title):
self.toolbar.title_property = title

def bind_current_account_balance(self):
"""
Binds the current_account_balance to the Toolbar title.
"""
self.bind(current_account_balance=self.update_toolbar_title_balance)

def unbind_current_account_balance(self):
"""
Unbinds the current_account_balance from the Toolbar title.
"""
self.unbind(current_account_balance=self.update_toolbar_title_balance)

def screen_manager_current(self, current, direction=None):
screens = {
'overview': OverviewScreen,
Expand Down Expand Up @@ -1047,7 +1054,7 @@ def show_not_implemented_dialog():
dialog.open()

@mainthread
def update_toolbar_title_balance(self):
def update_toolbar_title_balance(self, instance=None, value=None):
title = "%s ETH" % (self.current_account_balance)
self.set_toolbar_title(title)

Expand All @@ -1067,21 +1074,17 @@ def load_landing_page(self):
except IndexError:
self.load_create_new_account()

@run_in_thread
def fetch_and_update_balance(self):
def fetch_balance(self):
"""
Fetches the new balance and updates the UI.
Fetches the new balance and current_account_balance property.
"""
# pre-updates balance with last known value
self.update_toolbar_title_balance()
account = self.current_account
try:
self.current_account_balance = self.pywalib.get_balance(
account.address.encode("hex"))
except ConnectionError:
Controller.on_balance_connection_error()
return
self.update_toolbar_title_balance()

def load_switch_account(self):
"""
Expand Down
15 changes: 11 additions & 4 deletions src/pywallet.kv
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@
text: "Overview"
icon: 'format-list-bulleted'
on_pre_enter:
root.parent.set_title("0 ETH")
# TODO: not necessarily, e.g. probably not if the account didn't change
# TODO: but update title
app.controller.fetch_and_update_balance()
# sets to the last known value
app.controller.update_toolbar_title_balance()
# and binds on update
app.controller.bind_current_account_balance()
on_pre_leave:
# makes ure the title doesn't get updated async
# if we're not in this screen
app.controller.unbind_current_account_balance()
Overview:
id: overview_id
MDBottomNavigationItem:
Expand Down Expand Up @@ -358,6 +362,9 @@
# restores title,
# e.g. when coming back from the account selection screen
app.controller.set_toolbar_title(root.title_property)
overview_bnavigation_id.ids.tab_manager.current_screen.dispatch('on_pre_enter')
on_pre_leave:
overview_bnavigation_id.ids.tab_manager.current_screen.dispatch('on_pre_leave')
on_title_property:
app.controller.set_toolbar_title(root.title_property)
OverviewBottomNavigation:
Expand Down

0 comments on commit 7262fae

Please sign in to comment.