Skip to content
This repository has been archived by the owner on Sep 1, 2021. It is now read-only.

Commit

Permalink
Fixes settings permission issue, closes #131
Browse files Browse the repository at this point in the history
When the app was ran for the first time and the settings panel was
opened first, it was crashing.
  • Loading branch information
AndreMiras committed Apr 17, 2019
1 parent 67e44d5 commit 91c5aa4
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 32 deletions.
3 changes: 2 additions & 1 deletion src/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
## [Unreleased]

- Pull minimum bet from contract, refs #129
- Fix permission issue on settings, refs #131


## [v20190217]
Expand All @@ -13,7 +14,7 @@
- Split dedicated Etheroll library, refs #97
- Remove legacy dependencies, refs #122
- Move to python3 recipe, refs #123
- Handles write storage permission, refs #125
- Handle write storage permission, refs #125
- Non root Docker user, refs #127


Expand Down
20 changes: 20 additions & 0 deletions src/etheroll/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,26 @@ def open_address_options(self):
lambda x: self.copy_address_clipboard(), icon='content-copy')
bottom_sheet.open()

@staticmethod
def check_request_write_permission():
"""
Android runtime storage permission check.
"""
if platform != "android":
return
from android.permissions import (
Permission, request_permission, check_permission)
permission = Permission.WRITE_EXTERNAL_STORAGE
if not check_permission(permission):
request_permission(permission)

@staticmethod
def on_permission_error(exception):
title = "Permission denied"
body = str(exception.args)
dialog = Dialog.create_dialog(title, body)
dialog.open()


class DebugRavenClient(object):
"""
Expand Down
32 changes: 24 additions & 8 deletions src/etheroll/settings.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from kivy.app import App
from pyetheroll.constants import DEFAULT_GAS_PRICE_GWEI, ChainID

from etheroll.store import Store
Expand All @@ -14,19 +15,34 @@ class SettingsScreen(SubScreen):
def __init__(self, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)

@staticmethod
def get_store():
"""
Wrappers around get_store for handling permissions on Android.
"""
controller = App.get_running_app().root
# would also work for read permission
controller.check_request_write_permission()
try:
store = Store.get_store()
except PermissionError as exception:
controller.on_permission_error(exception)
store = {}
return store

def store_network(self):
"""
Saves selected network to the store.
"""
store = Store.get_store()
store = self.get_store()
network = self.get_ui_network()
store.put('network', value=network.name)

def store_gas_price(self):
"""
Saves gas price value to the store.
"""
store = Store.get_store()
store = self.get_store()
gas_price = self.get_ui_gas_price()
store.put('gas_price', value=gas_price)

Expand All @@ -53,12 +69,12 @@ def is_ui_mainnet(self):
def is_ui_testnet(self):
return self.ids.testnet_checkbox_id.active

@staticmethod
def get_stored_network():
@classmethod
def get_stored_network(cls):
"""
Retrieves last stored network value, defaults to Mainnet.
"""
store = Store.get_store()
store = cls.get_store()
try:
network_dict = store['network']
except KeyError:
Expand All @@ -81,12 +97,12 @@ def is_stored_testnet(cls):
def get_ui_gas_price(self):
return self.ids.gas_price_slider_id.value

@staticmethod
def get_stored_gas_price():
@classmethod
def get_stored_gas_price(cls):
"""
Retrieves stored gas price value, defaults to DEFAULT_GAS_PRICE_GWEI.
"""
store = Store.get_store()
store = cls.get_store()
try:
gas_price_dict = store['gas_price']
except KeyError:
Expand Down
25 changes: 2 additions & 23 deletions src/etheroll/switchaccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from kivy.clock import Clock, mainthread
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform
from kivymd.list import OneLineListItem

from etheroll.ui_utils import Dialog, SubScreen, load_kv_from_py
Expand Down Expand Up @@ -51,19 +50,6 @@ def update_account_list(self, accounts):
list_item = self.create_item(account)
account_list_id.add_widget(list_item)

@staticmethod
def check_request_permission():
"""
Android runtime storage permission check.
"""
if platform != "android":
return
from android.permissions import (
Permission, request_permission, check_permission)
permission = Permission.WRITE_EXTERNAL_STORAGE
if not check_permission(permission):
request_permission(permission)

@run_in_thread
def load_account_list(self):
"""
Expand All @@ -74,12 +60,12 @@ def load_account_list(self):
self.controller = App.get_running_app().root
self.toggle_spinner(show=True)
accounts = []
self.check_request_permission()
self.controller.check_request_write_permission()
try:
accounts = self.controller.account_utils.get_account_list()
self.update_account_list(accounts)
except PermissionError as exception:
self.on_permission_error(exception)
self.controller.on_permission_error(exception)
self.toggle_spinner(show=False)

@staticmethod
Expand All @@ -91,13 +77,6 @@ def on_empty_account_list():
dialog = Dialog.create_dialog(title, body)
dialog.open()

@staticmethod
def on_permission_error(exception):
title = "Permission denied"
body = str(exception.args)
dialog = Dialog.create_dialog(title, body)
dialog.open()

def toggle_spinner(self, show):
spinner = self.ids.spinner_id
spinner.toggle(show)
Expand Down

0 comments on commit 91c5aa4

Please sign in to comment.