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>

  • when you run app and you have and error ide shows the line, so fix this line...

    1. app.xxxProperty() is deprecated so you shouldn't use it.

    2. when no steps in storage geValue returns null and overwrites view.steps

    3. use geValue(“steps“) this code can't be compiled use copy paste

  • Hi found the problem,

    ahead of every storage I wrote AppBase thinking it's part of this class so I'll need it. Reducing it it works now.

    Now I'll try to store more arrays. I'll tell wether it worked. Thanks already for the help!

    import Toybox.Application;
    import Toybox.Lang;
    import Toybox.WatchUi;
    
    class movergraApp extends Application.AppBase {
    var view;
        function initialize() {
            AppBase.initialize();
        }
    
        // onStop() is called when your application is exiting
        function onStop(state) {
            Storage.setValue("steps", view.steps);
            Storage.setValue("uhrzeitarray", view.uhrzeitarray);
        }
        function onStart(state) {
        }
    
        function getInitialView(){
    
            view = new movergraView();
            view.steps = Storage.getValue("steps");
            if (view.steps == null){
                view.steps = [0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0];
            }
            view.uhrzeitarray = Storage.getValue("uhrzeitarray");
            if (view.uhrzeitarray == null){
                view.uhrzeitarray = ["init", "init", "init", "init", "init"];
            }
    
            return [view];
            //return [uhrzeitarray];
        }
    }
    
    function getApp() as movergraApp {
        return Application.getApp() as movergraApp;
    }

  • Hi _psx,

    and thanks a lot for the help. Now it finally works as it is supposed to!

    Biggest shout out to you and also thanks to everybody else who contributed and was up to help.

    Thanks a lot!

    Regards Marcus

  • yes, now looks good but for CIQ should be:

    function getInitialView(){
            var
            _init_ = "init",
            _view_ = new movergraView(),
            temp   = Storage.getValue("steps");
            _view_.steps = temp 
                        ? temp 
                        : [0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0,
                            0, 0, 0, 0, 0, 0];
            }
            temp = Storage.getValue("uhrzeitarray");
            _view_.uhrzeitarray = temp
                ?  temp
                : [_init_, _init_, _init_, _init_, _init_];
            }
            view = _view_;
            return [_view_];
        }