Skip to content

Commit

Permalink
Major refactoring screen and events
Browse files Browse the repository at this point in the history
refs #70 and refs #71
  • Loading branch information
AndreMiras committed Jul 23, 2017
1 parent ebabc55 commit dfe84d5
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 61 deletions.
100 changes: 74 additions & 26 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from kivy.properties import NumericProperty, ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen
from kivy.uix.scrollview import ScrollView
from kivy.utils import platform
from kivymd.button import MDFlatButton, MDIconButton
Expand Down Expand Up @@ -225,6 +226,7 @@ def on_password(self, instance, password):

class Receive(BoxLayout):

current_account = ObjectProperty()
address_property = StringProperty()

def __init__(self, **kwargs):
Expand All @@ -236,13 +238,14 @@ def setup(self):
Binds Controller.current_account property.
"""
self.controller = App.get_running_app().controller
self.controller.bind(
current_account=lambda _, value: self.on_current_account(value))
self.controller.bind(current_account=self.setter('current_account'))
# triggers the update
self.current_account = self.controller.current_account

def show_address(self, address):
self.ids.qr_code_id.data = address

def on_current_account(self, account):
def on_current_account(self, instance, account):
address = "0x" + account.address.encode("hex")
self.address_property = address

Expand All @@ -252,7 +255,20 @@ def on_address_property(self, instance, value):

class History(BoxLayout):

current_account = ObjectProperty(None, allownone=True)
current_account = ObjectProperty()

def __init__(self, **kwargs):
super(History, self).__init__(**kwargs)
Clock.schedule_once(lambda dt: self.setup())

def setup(self):
"""
Binds Controller.current_account property.
"""
self.controller = App.get_running_app().controller
self.controller.bind(current_account=self.setter('current_account'))
# triggers the update
self.current_account = self.controller.current_account

def on_current_account(self, instance, account):
self._load_history()
Expand Down Expand Up @@ -315,9 +331,6 @@ def _load_history(self):

class SwitchAccount(BoxLayout):

selected_list_item = ObjectProperty()
selected_account = ObjectProperty()

def __init__(self, **kwargs):
super(SwitchAccount, self).__init__(**kwargs)
Clock.schedule_once(lambda dt: self.setup())
Expand All @@ -328,11 +341,11 @@ def setup(self):

def on_release(self, list_item):
"""
Sets current account & item and switches to previous screen.
Sets Controller.current_account and switches to previous screen.
"""
# sets current account & item
# sets Controller.current_account
self.selected_list_item = list_item
self.selected_account = list_item.account
self.controller.current_account = list_item.account
# switches to previous screen
self.controller.screen_manager_previous()

Expand Down Expand Up @@ -360,7 +373,7 @@ def load_account_list(self):

class Overview(BoxLayout):

current_account = ObjectProperty(None, allownone=True)
current_account = ObjectProperty()
current_account_string = StringProperty()

def __init__(self, **kwargs):
Expand All @@ -369,6 +382,8 @@ def __init__(self, **kwargs):

def setup(self):
self.controller = App.get_running_app().controller
# triggers the update
self.current_account = self.controller.current_account

def on_current_account(self, instance, account):
address = "0x" + account.address.encode("hex")
Expand Down Expand Up @@ -419,7 +434,7 @@ def setup(self):
# TODO: create a generic account form
class ManageExisting(BoxLayout):

current_account = ObjectProperty(None, allownone=True)
current_account = ObjectProperty()
address_property = StringProperty()
current_password = StringProperty()
new_password1 = StringProperty()
Expand Down Expand Up @@ -728,6 +743,35 @@ def run_tests(self):
stream=stream, verbosity=verbosity).run(test_suite)


class OverviewScreen(Screen):

title_property = StringProperty()

def __init__(self, **kwargs):
super(OverviewScreen, self).__init__(**kwargs)

def set_title(self, title):
self.title_property = title


class SwitchAccountScreen(Screen):

def __init__(self, **kwargs):
super(SwitchAccountScreen, self).__init__(**kwargs)


class ManageKeystoreScreen(Screen):

def __init__(self, **kwargs):
super(ManageKeystoreScreen, self).__init__(**kwargs)


class AboutScreen(Screen):

def __init__(self, **kwargs):
super(AboutScreen, self).__init__(**kwargs)


class Controller(FloatLayout):

current_account = ObjectProperty()
Expand All @@ -740,14 +784,13 @@ def __init__(self, **kwargs):
keystore_path = Controller.get_keystore_path()
self.pywalib = PyWalib(keystore_path)
self.screen_history = []
self.switch_account.bind(
selected_account=lambda _, value: self.on_selected_account(value))
Clock.schedule_once(lambda dt: self.load_landing_page())

@property
def overview(self):
overview_bnavigation_id = self.ids.overview_bnavigation_id
return overview_bnavigation_id.ids.overview_id
overview_screen = self.ids.overview_screen_id
overview_bnavigation = overview_screen.ids.overview_bnavigation_id
return overview_bnavigation.ids.overview_id

@property
def history(self):
Expand Down Expand Up @@ -780,9 +823,20 @@ def set_toolbar_title(self, title):
self.toolbar.title_property = title

def screen_manager_current(self, current, direction=None):
screens = {
'overview': OverviewScreen,
'switch_account': SwitchAccountScreen,
'manage_keystores': ManageKeystoreScreen,
'about': AboutScreen,
}
screen_manager = self.screen_manager
if not screen_manager.has_screen(current):
screen = screens[current](name=current)
# screen_manager.switch_to(screen, direction=direction)
screen_manager.add_widget(screen)
if direction is not None:
self.screen_manager.transition.direction = direction
self.screen_manager.current = current
screen_manager.transition.direction = direction
screen_manager.current = current
self.screen_history.append(current)

def screen_manager_previous(self):
Expand All @@ -801,13 +855,6 @@ def on_selected_account(self, account):
def set_current_account(self, account):
self.current_account = account

def on_current_account(self, instance, value):
"""
Updates Overview.current_account and History.current_account.
"""
self.overview.current_account = value
self.history.current_account = value

@staticmethod
def show_invalid_form_dialog():
title = "Invalid form"
Expand Down Expand Up @@ -933,7 +980,8 @@ def fetch_and_update_balance(self):
"""
account = self.current_account
try:
self.current_account_balance = self.pywalib.get_balance(account.address.encode("hex"))
self.current_account_balance = self.pywalib.get_balance(
account.address.encode("hex"))
except ConnectionError:
Controller.on_balance_connection_error()
return
Expand Down
85 changes: 50 additions & 35 deletions src/pywallet.kv
Original file line number Diff line number Diff line change
Expand Up @@ -317,24 +317,27 @@
name: 'overview'
text: "Overview"
icon: 'format-list-bulleted'
on_tab_press:
on_pre_enter:
root.parent.set_title("0 ETH (updating...)")
# TODO: not necessarily, e.g. probably not if the account didn't change
# TODO: but update title
app.controller.fetch_and_update_balance()
Overview:
id: overview_id
MDBottomNavigationItem:
name: 'send'
text: "Send"
icon: 'arrow-up-bold'
on_tab_press:
app.controller.set_toolbar_title("Send ETH")
on_pre_enter:
root.parent.set_title("Send ETH")
Send:
id: send_id
MDBottomNavigationItem:
name: 'receive'
text: "Receive"
icon: 'arrow-down-bold'
on_tab_press:
app.controller.set_toolbar_title("Receive ETH")
on_pre_enter:
root.parent.set_title("Receive ETH")
Receive:
id: receive_id

Expand All @@ -346,6 +349,48 @@
background_hue: '500'


<OverviewScreen>:
name: 'overview'
on_pre_enter:
# restores title,
# e.g. when coming back from the account selection screen
app.controller.set_toolbar_title(root.title_property)
on_title_property:
app.controller.set_toolbar_title(root.title_property)
OverviewBottomNavigation:
id: overview_bnavigation_id


<SwitchAccountScreen>:
name: 'switch_account'
on_pre_enter:
app.controller.set_toolbar_title("Switch account")
SwitchAccount:
id: switch_account_id


<ManageKeystoreScreen>:
name: 'manage_keystores'
on_pre_enter:
app.controller.set_toolbar_title("Manage existing")
# workaround for broken MDBottomNavigation, refs
# https://github.com/AndreMiras/PyWallet/issues/38
Window.dispatch('on_resize', *Window.size)
ManageKeystoreBottomNavigation:
id: manage_keystores_id


<AboutScreen>:
name: 'about'
on_pre_enter:
app.controller.set_toolbar_title("About PyWallet")
# workaround for broken MDBottomNavigation, refs
# https://github.com/AndreMiras/PyWallet/issues/38
Window.dispatch('on_resize', *Window.size)
About:
id: about_id


<Controller>:
Navigation:
id: navigation_id
Expand All @@ -355,33 +400,3 @@
id: toolbar_id
ScreenManager:
id: screen_manager_id
Screen:
name: 'overview'
on_pre_enter:
root.update_toolbar_title_balance()
OverviewBottomNavigation:
id: overview_bnavigation_id
Screen:
name: 'switch_account'
on_pre_enter:
root.set_toolbar_title("Switch account")
SwitchAccount:
id: switch_account_id
Screen:
name: 'manage_keystores'
on_pre_enter:
root.set_toolbar_title("Manage existing")
# workaround for broken MDBottomNavigation, refs
# https://github.com/AndreMiras/PyWallet/issues/38
Window.dispatch('on_resize', *Window.size)
ManageKeystoreBottomNavigation:
id: manage_keystores_id
Screen:
name: 'about'
on_pre_enter:
root.set_toolbar_title("About PyWallet")
# workaround for broken MDBottomNavigation, refs
# https://github.com/AndreMiras/PyWallet/issues/38
Window.dispatch('on_resize', *Window.size)
About:
id: about_id

0 comments on commit dfe84d5

Please sign in to comment.