How to identify the current active view?

I have a widget where I need to read some data using makeWebRequest at app start (in a separated API class).
A result field should be displayed in the main view. Because of the asynchronous processing, the view is shown first with a placeholder for the missing field value and should be refreshed when the http-result is ready.

When the web request is ready, I call WatchUi.requestUpdate(); to redrax the main view including the new data.
But if the user already changed the view (SELECT button), anoter view is visible. In this case I don't want to refresh the view because this could cause a reload of the view data including web requests which would slow down the widget usage.

Is is possible to identify the current view (by name or class name) to decide if it should be refreshed with async data or not?

  • A simple thing to do is keep track of pushView and popView calls (inc/dec a counter) if you want to see if you are displaying the main view.

  • So you mean a global array (app class attribute) where I add or remove keys for views types with each pushView/popView in every delegate class?
    Only using the onShow method would not be right I think. That would be called several times when a popup is displayed (mobile notification...) and the current view is shown again.

  • I never mentioned onShow.  In your code you are doing pushView() and popView() calls.  With a button press, it sounds like you are doing a pushView() and when that view closes, a popView().

    You may be over thinking this.  In many of my widgets/device apps, I have a timer that updates the screen (WatchUi.requestUpdate()) on a regular basis, or I trigger the requestUpdate() on something like a button press/swipe/onBackgroundData().  Plus, in the case of a widget on watches, it will run at most about 2 minutes with no user interaction and then time out and close.

  • hi,

    you could just declare a global variable like var WebRq = true,

    when your web data is ready turn it to false and where you call your WebRequest put if(WebRq==true){ do your web update }

    else on my widget I use a global variable PAGE

    so first page PAGE=0; when user push select I turn PAGE = 1 etc.

  • I haven't found a method to keep track of it. I came to realize that you need to

    A) Either do it yourself somewhere. Which means you need to extend Toybox.WatchUI and override popView, pushView and switchToView to get what you want with a global var somewhere to keep track of things.

    B) Use different delegators for different parts of your code so you know where you are.

    C) You can also inject some stuff into your delegators so they know where they are. I would probably go for constants.

    I chose for option B

    Now, if Garmin had some more introspection on the WatchUI so you can ask what the current view was... that would be nice to have.

  • Many thanks @all for your suggestions. Now I know I have to do it by myself :-)
    I will try the simple version first. A variable in the main view that is changed on the first putView (jump away). In that case, a view update is not needed.
    If I need such information in other views, too, a page counter or array would be better.

  • In my widgets they mainly only have a single view, and then I use something like a page variable in onUpdate to show the desired screen.  It's cheaper than using multiple views. 

    Using multiple views in a widget also means finding a way for the user to easily exit.

  • Make another class and it's object as global and this object should make webREQ and handle onRecive so it and data from web will be accessible by any view

  • I mixed these things. I use it for a widget to control a smarthome hub (or his devices).

    apps.garmin.com/.../02c86289-5478-48c3-8509-cd1bb36fe51a

    First is the main view.

    SELECT button switched to category view (scolling is done by page counter and redraw).

    Another SELECT switched to device view (scolling is done by page counter and redraw).

  • Because the API is returning very large JSON responses, I can only request the data of each device when it's shown on the device view, So I have a central class for getting the API URLs, but need to call webRequest in every view independent. I haven't tried to pass the view callback function to a method of the API class, make the request there and call the callback function of the view.