Skip to content

Commit 5292535

Browse files
Refactor code to fix various issues
- 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
1 parent 0ccd802 commit 5292535

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/MobileCrashAnalyzer/ui/main_window.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212

1313
import os
1414
import functools
15-
1615
from jnius import autoclass, cast
1716

17+
from shared.storage import CrashReport, Rawlog
18+
from shared.utils import Paths
19+
from shared.ui.mobile_about_dialog import MobileAboutDialog
20+
1821
# Just import if the os is Android to avoid Android peculiarities
1922
try:
2023
from android import activity, mActivity, permissions
@@ -26,10 +29,6 @@
2629
except:
2730
OPERATING_SYSTEM = None
2831

29-
from shared.storage import CrashReport, Rawlog
30-
from shared.utils import Paths
31-
from shared.ui.mobile_about_dialog import MobileAboutDialog
32-
3332
RAWLOG_TAIL = 256
3433

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

4645
self.report = None
47-
self.currentPart = ""
46+
self.lastCarouselIndex = -1
4847

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

5251
# Create actionbar
5352
self.uiActionBar = Factory.ActionBar(pos_hint={'top': 1})
54-
self.rebuildActionBar()
5553

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

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

67+
self.rebuildActionBar()
6768
return self.layout
6869

6970
def quit(self, *args):
7071
self.stop()
7172

72-
def onSlideChanged(self, *args):
73+
def onCarouselChanged(self, *args):
7374
if self.report == None:
7475
return;
75-
self.currentPart = self.report[self.uiCarouselWidget.index]["name"]
76-
77-
# Rebuild ActionBar with new parameters
78-
self.rebuildActionBar()
76+
# rebuild ui if the carousel index changed
77+
if self.uiCarouselWidget.index != self.lastCarouselIndex:
78+
logger.info("Showing report having index '%d' and name '%s'..." % (self.uiCarouselWidget.index, self.report[self.uiCarouselWidget.index]["name"]))
79+
80+
# save current carousel position used to display actionbar title
81+
# this will be used to check if we have to reload our actionbar
82+
self.lastCarouselIndex = self.uiCarouselWidget.index
83+
84+
# Rebuild ActionBar with new parameters
85+
self.rebuildActionBar()
7986

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

194201
self.uiCarouselWidget.add_widget(uiTextInput)
195202

196-
logger.debug("Showing first report part...")
197-
self.currentPart = self.report[0]["name"]
198-
self.rebuildActionBar()
203+
logger.debug("Loading completed...")
199204
except Exception as ex:
200205
logger.warn("Exception loading crash report: %s" % str(ex))
201206
self.createPopup("Exception loading crash report: %s" % str(ex))
202207
self.resetUi()
203208
return
204209

205-
def switch_part(self, reportName, *args):
206-
logger.info("Showing report part '%s'..." % reportName)
207-
for index in range(len(self.report)):
208-
if self.report[index]["name"] == reportName:
209-
self.currentPart = reportName
210-
211-
# Rebuild ActionBar with new parameters
212-
self.rebuildActionBar()
213-
214-
self.uiCarouselWidget.index = index
215-
210+
def switch_part(self, index, *args):
211+
self.uiCarouselWidget.index = index
212+
216213
def rebuildActionBar(self, *args):
217214
# Rebuild ActionBar because it's impossible to change it
218-
219215
logger.debug("Rebuilding ActionBar...")
220216

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

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

239235
self.uiActionView.add_widget(self.uiActionGroup)
240-
self.uiActionView.add_widget(Factory.ActionPrevious(title = self.currentPart, with_previous=False, app_icon=Paths.get_art_filepath("quitIcon.png"), on_press = self.quit))
236+
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))
241237
self.uiActionBar.add_widget(self.uiActionView)
238+
239+
logger.info("ActionBar now repopulated...")
242240

243241
def resetUi(self):
244242
logger.debug("Reseting ui...")
245243
self.uiCarouselWidget.clear_widgets()
246244
self.report = None
245+
self.lastCarouselIndex = -1
247246
self.rebuildActionBar()
248247

249248
def createPopup(self, message):

0 commit comments

Comments
 (0)