Variables value gets lost in Watchface

Hello everybody,

I added a function to my watchface which isn't working but I really don't have a clue why.

The goal is simply to sum up the min and hours moved on a day. To a certain extend that works but every once in a while he resets and I don't know why. I never reached vaules over 40 min before it got reset for no reason I understand.

Anybody aware of the fault I made?

The view.mc

using Toybox.Lang as Lang;
using Toybox.System as Sys;
import Toybox.WatchUi;
using Toybox.Time.Gregorian as Date;
using Toybox.Application as App;
using Toybox.ActivityMonitor as Mon;


class Last_MoveView extends WatchUi.WatchFace {
    var stepsolds = 0;
    
    var summoved = 0;
    var hoursmoved=0;
    var minmoved=0;
    var minalt = 0;

    function initialize() {
        WatchFace.initialize();
    }

    // Load your resources here
    function onLayout(dc) {
        setLayout(Rez.Layouts.WatchFace(dc));
    }

    // Called when this View is brought to the foreground. Restore
    // the state of this View and prepare it to be shown. This includes
    // loading resources into memory.
    function onShow() {
    }

    // Update the view
    function onUpdate(dc) {
        setsummoved();

        View.onUpdate(dc);
    }

    // Called when this View is removed from the screen. Save the
    // state of this View here. This includes freeing resources from
    // memory.
    function onHide() {
    }

    // The user has just looked at their watch. Timers and animations may be started here.
    function onExitSleep() {
    }

    // Terminate any active timers and prepare for slow updates.
    function onEnterSleep() {
    }

the function:

function setsummoved() {

        var zeit = Sys.getClockTime();
        var stepnews = Mon.getInfo().steps;
        //stepnews =50;
        //summoved =100;
        if (zeit.hour>5) {        //datealt
            if (stepnews>stepsolds+20){      //and (now.min>minalt)
                if (zeit.min>minalt) {
                    summoved=summoved+1;
                    if (summoved>60){
                        hoursmoved=(summoved/60); //summoved % 60;
                        minmoved = summoved % 60;//summoved - (hoursmoved*60);
                    } else {
                        hoursmoved=0;
                        minmoved = summoved;
                    }
                }
            }
            var timeStrings = Lang.format("$1$:$2$", [hoursmoved, minmoved.format("%02d")]);
            var view = View.findDrawableById("summovedDisplay");   //Anzeige Mitte Z3  Uhrzeit der letzten Bewegung 55 85
            view.setText(timeStrings); 
            minalt=zeit.min;
            stepsolds=stepnews;
        }else{
            summoved=null;
            hoursmoved=null;
            minmoved=null;
        }

    }

The Layout:

<layout id="WatchFace">
    <label id="summovedDisplay"    x="55%"     y="85%"     font="Graphics.FONT_XTINY"              justification="Graphics.TEXT_JUSTIFY_CENTER" />
</layout>

  • What's happening is each time you leave the WF (go to widgets/glances for example), the watch face restarts, so you lose your numbers.  You need to save things in Application.Storage and load those values when you restart,

  • Hi Jim,

    and thanks for once again answering my questions.

    You're definetly right that he looses the values everytime I interact with the watch (except for light on) and I'll try to store them. But what confuses me was that also when I don't interact with the watch he sometimes resets as well.

    No clue why this is happening.

  • Anything that results in the WF restarting can cause this, so things like notifications.  You want to use Storage and that will  solve it in all cases.

  • Ahh ok- I'll try and let you now wether I was able to make it work :-).

    Thanks so far!

  • Hi Jim,

    appreciated your answer but it didn't work. No idea why. From time to time he looses the variable summoved. Never got more then half an hour.

    Any ideas?

        function setsummoved() {

            var zeit = Sys.getClockTime();
            var stepnews = Mon.getInfo().steps;
            //stepnews =50;
            //summoved =100;
            if (zeit.hour>5) {        //datealt
                if (App.Storage.getValue("stepsolds")!=null){
                    stepsolds=App.Storage.getValue("stepsolds");
                }
                if (App.Storage.getValue("minalt")!=null){
                    minalt=App.Storage.getValue("minalt");
                }else{
                    minalt=0;
                }
                if (stepnews>stepsolds+20){      //and (now.min>minalt)
                    if (zeit.min>minalt) {
                        if (App.Storage.getValue("summoved")!=null){
                            summoved=App.Storage.getValue("summoved");
                        }else{
                            summoved=0;
                        }
                        summoved=summoved+1;
                        App.Storage.setValue("summoved", summoved);
                        if (summoved>60){
                            hoursmoved=(summoved/60); //summoved % 60;
                            minmoved = summoved % 60;//summoved - (hoursmoved*60);
                        } else {
                            hoursmoved=0;
                            minmoved = summoved;
                        }
                    }
                }
                var timeStrings = Lang.format("$1$:$2$", [hoursmoved, minmoved.format("%02d")]);
                var view = View.findDrawableById("summovedDisplay");  
                view.setText(timeStrings);
                //minalt=zeit.min;
                App.Storage.setValue("minalt", zeit.min);
                //stepsolds=stepnews;
                App.Storage.setValue("stepsolds", stepnews);
            }else{
                summoved=null;
                App.Storage.setValue("summoved", 0);
                App.Storage.setValue("minalt",0);
                App.Storage.setValue("stepsolds", 0);
                hoursmoved=null;
                minmoved=null;
            }
        }
  • Put in some println calls where you are doing the getValuse() and setValue() calls. It's unclear when you are calling the setsummoved function.

    In the sim, you can simulate leaving the watch face with File>Kill App.  Then just run it again and you should see the values you saved.  You can also simulate steps with  Simulation>Activity Monitoring

  • Hi Jim,

    that's how I'm calling it.
    P.s. Haven't found the error yet. But found how to simulate :-).
    function onUpdate(dc) {
            // Get and show the current time
    
            setsummoved();
            
            
            
            // Call the parent onUpdate function to redraw the layout
            View.onUpdate(dc);
        }
  • You only need to do the getValue() call when your app starts and then the setValue() when it's changed or when the app exits..  

  • Well in the simulator it is working just fine. Also a kill doesn't make him loose the values.

    But the actual watch does loose them Scream

    But we'll see - for now I got it running for 8 min.

  • you will kill battery by using storage in every update...

    you can put all values in dictionary/array and save/read in one step

    you double reading storage e.g. instead of

    if (App.Storage.getValue("stepsolds")!=null){
                    stepsolds=App.Storage.getValue("stepsolds");
                }
    use
    stepsolds=App.Storage.getValue("stepsolds");
    if (stepsolds==null){
                    stepsolds=0;
                }