Skip to content

Commit

Permalink
Handles back button, goes back to screen history
Browse files Browse the repository at this point in the history
And eventually quits the application, fixes #95
  • Loading branch information
AndreMiras committed Oct 27, 2017
1 parent 02a1605 commit 6448a46
Showing 1 changed file with 53 additions and 10 deletions.
63 changes: 53 additions & 10 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from kivy.app import App
from kivy.clock import Clock, mainthread
from kivy.core.clipboard import Clipboard
from kivy.core.window import Window
from kivy.logger import LOG_LEVELS, Logger
from kivy.metrics import dp
from kivy.properties import NumericProperty, ObjectProperty, StringProperty
Expand Down Expand Up @@ -1033,11 +1034,44 @@ def __init__(self, **kwargs):
self.screen_history = []
self.register_event_type('on_alias_updated')
Clock.schedule_once(lambda dt: self.load_landing_page())
Window.bind(on_keyboard=self.on_keyboard)

def on_keyboard(self, window, key, *args):
"""
Handles the back button (Android) and ESC key.
Goes back to the previous screen or quite the application
if there's none left.
"""
if key == 27:
screen_manager = self.screen_manager
# if we already are in the overview screen, also move back to
# the overview subtab of the overview screen
if screen_manager.current == 'overview':
overview_bnavigation = self.overview_bnavigation
tab_manager = overview_bnavigation.ids['tab_manager']
if tab_manager.current != 'overview':
tab_manager.current = 'overview'
return True
else:
# if we were already in the overview:overview subtab,
# then propagates the key which in this case will exit
# the application
return False
self.screen_manager_previous()
# stops the propagation
return True
return False

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

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

@property
Expand All @@ -1053,10 +1087,8 @@ def switch_account(self):

@property
def send(self):
screen_manager = self.screen_manager
overview_screen = screen_manager.get_screen('overview')
overview_bnavigation_id = overview_screen.ids.overview_bnavigation_id
return overview_bnavigation_id.ids.send_id
overview_bnavigation = self.overview_bnavigation
return overview_bnavigation.ids.send_id

@property
def manage_keystores(self):
Expand Down Expand Up @@ -1099,7 +1131,7 @@ def unbind_current_account_balance(self):
"""
self.unbind(current_account_balance=self.update_toolbar_title_balance)

def screen_manager_current(self, current, direction=None):
def screen_manager_current(self, current, direction=None, history=True):
screens = {
'overview': OverviewScreen,
'switch_account': SwitchAccountScreen,
Expand All @@ -1108,20 +1140,31 @@ def screen_manager_current(self, current, direction=None):
'about': AboutScreen,
}
screen_manager = self.screen_manager
# creates the Screen object if it doesn't exist
if not screen_manager.has_screen(current):
screen = screens[current](name=current)
screen_manager.add_widget(screen)
if direction is not None:
screen_manager.transition.direction = direction
screen_manager.current = current
self.screen_history.append(current)
if history:
# do not update history if it's the same screen because we do not
# want the go back button to behave like it was doing nothing
if not self.screen_history or self.screen_history[-1] != current:
self.screen_history.append(current)
# in this case let's reset since the overview is the root screen
# because we never want the back button to bring us from overview
# to another screen
if current == 'overview':
self.screen_history = []

def screen_manager_previous(self):
try:
previous_screen = self.screen_history[-2]
previous_screen = self.screen_history.pop(-2)
except IndexError:
previous_screen = 'overview'
self.screen_manager_current(previous_screen, direction='right')
self.screen_manager_current(
previous_screen, direction='right', history=False)

@staticmethod
def show_invalid_form_dialog():
Expand Down

0 comments on commit 6448a46

Please sign in to comment.