Sure this is old news to this awesome community, but this is a neat (and easy to implement) trick for dealing with mobile versions of Anvil Apps being used as PWAs saved to home screen.
Let’s make sure the app knows it’s being presented on mobile and as a PWA…
In your Startup form, import anvil.js and add this to your init:
Then add your function:
def is_pwa_on_mobile(self,**kwargs): is_pwa = anvil.js.window.matchMedia("(display-mode: standalone)").matches is_mobile = anvil.js.window.navigator.userAgent.lower().find("mobi") > -1 Globals.is_pwa = is_pwa Globals.is_mobile = is_mobile
In your Globals module (add one if you don’t have one) add:
is_pwa = False is_mobile = False
In your form where you want to refresh things and normally use a poll timer (for the sake of example), import anvil.js.
In your init, get your content_panel’s dom node and set up some listeners and measuring tools:
content_panel = anvil.js.get_dom_node(self.content_panel) content_panel.addEventListener('touchstart', self.on_touch_start) content_panel.addEventListener('touchmove', self.on_touch_move) content_panel.addEventListener('touchend', self.on_touch_end) self.touch_start_y = None self.touch_end_y = None
Also in your init, build in the check for whether it’s a mobile/PWA presentation of your app and turn off your poll timer if it is.
if Globals.is_pwa and Globals.is_mobile: self.timer_1.interval = None
Get ready to recycle your existing refresh_data() function (currently triggered by your poll timer), which could be anything, such as:
def refresh_data(self, **event_args): self.updatestuff_func()
Last step, add your touch event functions:
def on_touch_start(self, event): self.touch_start_y = event.touches.clientY def on_touch_move(self, event): self.touch_end_y = event.touches.clientY def on_touch_end(self, event): if self.touch_start_y is not None and self.touch_end_y is not None: pull_distance = self.touch_end_y - self.touch_start_y if pull_distance > 50: self.refresh_data() self.touch_start_y = None self.touch_end_y = None
You’ve added a check for whether your app is running on mobile AND as a PWA.
You’ve added touch event listeners which work out when a user pulls down and releases, rather than just touches or scrolls.
You’ve gotten rid of poll timers on the mobile version of your app, so expect less App Offline errors too.
And that’s it. Pull to refresh, no libraries or faff, just great mobile apps all built with Anvil.