App setting variable location

The only way so far I can get onSettingChanged to work is for the function in the watch face view class to use global variables not in the class. All attempts to use class variables in one way or another have failed for either compiler errors or symbol not found runtime errors. It is just not possible to use class variables for application settings? If so, what is the incantation magic for it to work?

  • The magic is to include your relevant code, 'cause we don't read minds. OnSettingsChanged is in AppBase, how you call your view's code?

  • In the App:

    class myWatch extends Application.AppBase {
    
    public function onSettingsChanged() as Void {
    	$.myView.appSettings();
    	WatchUi.requestUpdate();
    }

    In the View:

    var g_squash as Boolean = false;
    var g_reSettings as Boolean = false;
    
    class myView extends WatchUi.WatchFace {
    
    public function appSettings() as Void {
    	$.g_squash = $.myView.getProp("sq", true);
    	$.g_reSettings = true;
    }

    This only works when the variables are not in any class. Otherwise it either doesn't compile or doesn't run.

  • This is just part of the code but guessing the missing parts: 

    1. You should use the usual conventions (especially that you don't post relevant parts) and call classes with CapitalCase (MyView) and variables only with camelCase....

    2. The real problem is that you use myView not as an object but as a class. Fix your code by changing: class name to MyView, then save it to a variable: var myView = new MyView() and use the variable.

  • It's not really named myView, I just called it that for a more generic representation of the problem. I'm pretty sure the reason it's not working isn't because of upper vs. lower case. The class part is straight from the Analog example from the SDK. Yes, I did leave out a few thousand lines of code. I believe this is the part from the app class you are referring to:

    .

    class MyWatch extends Application.AppBase {
    
    	public function initialize() {
    		AppBase.initialize();
    	}
    	
    	public function getInitialView() as Array<Views or InputDelegates>? {
    		if (WatchUi has :WatchFaceDelegate) {
    			var view = new $.MyView();
    			var delegate = new $.MyDelegate(view);
    			return [view, delegate] as Array<Views or InputDelegates>;
    		} else {
    			return [new $.MyView()] as Array<Views>;
    		}
    	}
    

    Back to the original question. What syntax do I need to use so that when the onSettingsChanged() function in the AppBase class calls a function in the WatchFace class it can update variables in the WatchFace class which were affected by a settings change. Right now it only works if I set global variables.

  • I’m sort of curious what your code which *fails* looks like, although I guess it doesn’t matter and I can kinda guess anyway.

    Try something along the lines of this - save the instance of your initial view in the app class, and refer to the saved instance in your app class’s onSettingsChanged():

    (I didn’t run and test this code, and I’m aware it’s missing type checking stuff. The general idea should be valid, tho)

    class myWatch extends Application.AppBase {
        var view;
        public function getInitialView() {
            view = new myView();
            return [view];
        }
        public function onSettingsChanged() as Void {
    	    view.appSettings();
    	    WatchUi.requestUpdate();
        }
    }
    
    class myView extends WatchUi.WatchFace {
        var squash = false;
        var reSettings = false;
        public function appSettings() as Void {
    	    squash = getProp("sq", true);
    	    reSettings = true;
        }
    }

    (I’m also aware that this code will crash if onSettingsChanged() is somehow called before getInitialView(), as unlikely/impractical/impossible as that might be. That’s easily fixable — I would change it except the forum doesn’t want to let me edit the code snippet.)

  • "Promoting" variable view from a local in the function to the class solved it. Thanks, I sure wouldn't have guessed that. And, since I had left this part basically as is from the SDK Analog example, heads up to anyone else using it as an example.