Skip to content

Commit

Permalink
Refactor code to fix various issues
Browse files Browse the repository at this point in the history
- Use carousel `index` instead of part name and bind to carousel `index`
  instead of `on_touch_move`, fixes #26
- Set `ignore_perpendicular_swipes` to `True` to allow scrolling again,
  fixes #27
  • Loading branch information
tmolitor-stud-tu committed Apr 28, 2024
1 parent 9966d97 commit 443b759
Showing 1 changed file with 30 additions and 31 deletions.
61 changes: 30 additions & 31 deletions src/MobileCrashAnalyzer/ui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@

import os
import functools

from jnius import autoclass, cast

from shared.storage import CrashReport, Rawlog
from shared.utils import Paths
from shared.ui.mobile_about_dialog import MobileAboutDialog

# Just import if the os is Android to avoid Android peculiarities
try:
from android import activity, mActivity, permissions
Expand All @@ -26,10 +29,6 @@
except:
OPERATING_SYSTEM = None

from shared.storage import CrashReport, Rawlog
from shared.utils import Paths
from shared.ui.mobile_about_dialog import MobileAboutDialog

RAWLOG_TAIL = 256

import logging
Expand All @@ -44,17 +43,18 @@ def build(self):
self.title = "Monal Mobile Crash Analyzer"

self.report = None
self.currentPart = ""
self.lastCarouselIndex = -1

logger.debug("Creating ui elements")
self.layout = GridLayout(rows=2)

# Create actionbar
self.uiActionBar = Factory.ActionBar(pos_hint={'top': 1})
self.rebuildActionBar()

self.uiCarouselWidget = Carousel(direction='right')
self.uiCarouselWidget.bind(on_touch_move=self.onSlideChanged)
# use touch_up to make sure carousel movement has finished when invoking our handler
self.uiCarouselWidget.bind(index=self.onCarouselChanged)
self.uiCarouselWidget.ignore_perpendicular_swipes = True

self.layout.add_widget(self.uiActionBar)
self.layout.add_widget(self.uiCarouselWidget)
Expand All @@ -64,18 +64,25 @@ def build(self):
activity.bind(on_new_intent=self.on_new_intent)
permissions.request_permissions([permissions.Permission.READ_EXTERNAL_STORAGE, permissions.Permission.WRITE_EXTERNAL_STORAGE])

self.rebuildActionBar()
return self.layout

def quit(self, *args):
self.stop()

def onSlideChanged(self, *args):
def onCarouselChanged(self, *args):
if self.report == None:
return;
self.currentPart = self.report[self.uiCarouselWidget.index]["name"]

# Rebuild ActionBar with new parameters
self.rebuildActionBar()
# rebuild ui if the carousel index changed
if self.uiCarouselWidget.index != self.lastCarouselIndex:
logger.info("Showing report having index '%d' and name '%s'..." % (self.uiCarouselWidget.index, self.report[self.uiCarouselWidget.index]["name"]))

# save current carousel position used to display actionbar title
# this will be used to check if we have to reload our actionbar
self.lastCarouselIndex = self.uiCarouselWidget.index

# Rebuild ActionBar with new parameters
self.rebuildActionBar()

def on_start(self, *args):
# Use if the os is Android to avoid Android peculiarities
Expand Down Expand Up @@ -193,29 +200,18 @@ def loadFile(self, filename):

self.uiCarouselWidget.add_widget(uiTextInput)

logger.debug("Showing first report part...")
self.currentPart = self.report[0]["name"]
self.rebuildActionBar()
logger.debug("Loading completed...")
except Exception as ex:
logger.warn("Exception loading crash report: %s" % str(ex))
self.createPopup("Exception loading crash report: %s" % str(ex))
self.resetUi()
return

def switch_part(self, reportName, *args):
logger.info("Showing report part '%s'..." % reportName)
for index in range(len(self.report)):
if self.report[index]["name"] == reportName:
self.currentPart = reportName

# Rebuild ActionBar with new parameters
self.rebuildActionBar()

self.uiCarouselWidget.index = index

def switch_part(self, index, *args):
self.uiCarouselWidget.index = index

def rebuildActionBar(self, *args):
# Rebuild ActionBar because it's impossible to change it

logger.debug("Rebuilding ActionBar...")

# Delete old ActionBar content
Expand All @@ -227,8 +223,8 @@ def rebuildActionBar(self, *args):

# If report is open objects are loaded
if self.report != None:
for report in self.report:
button = Factory.ActionButton(text = report["name"], on_press = functools.partial(self.switch_part, report["name"]))
for index in range(len(self.report)):
button = Factory.ActionButton(text = self.report[index]["name"], on_press = functools.partial(self.switch_part, index))
self.uiActionGroup.add_widget(button)
button.texture_update()
self.uiActionGroup.dropdown_width = max(self.uiActionGroup.dropdown_width, button.texture_size[0]) + 16
Expand All @@ -237,13 +233,16 @@ def rebuildActionBar(self, *args):
self.uiActionGroup.add_widget(Factory.ActionButton(text = 'About', on_press = MobileAboutDialog))

self.uiActionView.add_widget(self.uiActionGroup)
self.uiActionView.add_widget(Factory.ActionPrevious(title = self.currentPart, with_previous=False, app_icon=Paths.get_art_filepath("quitIcon.png"), on_press = self.quit))
self.uiActionView.add_widget(Factory.ActionPrevious(title = self.report[self.uiCarouselWidget.index]["name"] if self.report != None else "", with_previous=False, app_icon=Paths.get_art_filepath("quitIcon.png"), on_press = self.quit))
self.uiActionBar.add_widget(self.uiActionView)

logger.info("ActionBar now repopulated...")

def resetUi(self):
logger.debug("Reseting ui...")
self.uiCarouselWidget.clear_widgets()
self.report = None
self.lastCarouselIndex = -1
self.rebuildActionBar()

def createPopup(self, message):
Expand Down

0 comments on commit 443b759

Please sign in to comment.