Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Problems accessing settings in index.jsx and in companion index.js.

ANSWERED

I have a watch app with settings to control colors of the watch hands, numbers, etc. I'd like to create "themes" that would store all the settings as a group, and allow me to switch themes easily between them.

I have an array of themes like this in my app/index.js:

function getDefaultTheme(name) {
    let theme = {};
    theme.name=name;
    theme.handsColor = 'white';
    theme.secHandColor = 'white';
    theme.hourNumberColor = 'white';
    return theme;
}

let t1 = getDefaultTheme("default1");
let t2 = getDefaultTheme("default2");
settings = {selectedTheme:0, themes:[t1,t2]};

And I save them like this:

const SETTINGS_TYPE = "cbor";
const SETTINGS_FILE = "settings.cbor";
fs.writeFileSync(SETTINGS_FILE, settings, SETTINGS_TYPE);

I can't seem to access the settings in the settings/index.jsx. I have this in the index.jsx:

console.log("In mySettings" + JSON.stringify(props));
and the output is:

[1:30:06 PM]  Settings: In mySettings{"settings":{},"settingsStorage":{}}

Both settings and settingsStorage are null.

In the companion app, I have this:

import { settingsStorage } from "settings";
import * as messaging from "messaging";
import { me } from "companion";

let myKeyName = settingsStorage.key(0);

console.log("In companion index.js first key is " + myKeyName);

the output is this:
[1:30:06 PM] Companion: In companion index.js first key is null

Am I missing something?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

As far as I know, the settings module is only available for companion and settings, not in app.

So to sync both worlds (mobile <-> watch) you need to use some messaging magic.

A commonly used pattern is:

  1. Companion listens for settingsStorage change events.
  2. Send the each setting change throw messaging to app.
  3. App persists each setting in a local file.
  4. App always gets the settings from the local file.

Of course, you can share code at build-time, but not data at runtime without any messaging / file-transfer actions.

 

 

 

For example, given the previous checkpoint list:

  1. settingsStorage.addEventListener("change", sendTargetCoord);
  2. peerSocket.send(getTargetCoord());
  3. writeSettings({ target });
  4. let target: Coordinate | null = readSettings().target;

View best answer in original post

Best Answer
2 REPLIES 2

Maybe I figured out my problem. I was assuming the settings saved in the app on the device was accessible somehow on the companion app, but that seems to be not true. I think what I need to do is create the initial settings both on the app on the device, and in the companion app, maybe using the common folder so that the code isn't duplicated. Then, the settings index.jsx can see the settings created in the companion app, and if changes are made, they can be sent via a message from the companion to the app on the watch, which will have to save the updated settings in it's own settings file.

 

The initial settings have to be created on the app running on the watch, in case the user never runs the companion app - the watch app needs to have something to start with.

 

The initial settings have to be created in the companion also, since it can't see the default settings created on the watch. The app running on the watch can't change the settings, so if the default settings are created the same way on both the watch app and the companion app, then they should be in sync. Any changes made in the companion/settings app will be sent via message to the app, so they should stay in sync if the app saves the updated settings also.

 

Am I right?

Best Answer
0 Votes

As far as I know, the settings module is only available for companion and settings, not in app.

So to sync both worlds (mobile <-> watch) you need to use some messaging magic.

A commonly used pattern is:

  1. Companion listens for settingsStorage change events.
  2. Send the each setting change throw messaging to app.
  3. App persists each setting in a local file.
  4. App always gets the settings from the local file.

Of course, you can share code at build-time, but not data at runtime without any messaging / file-transfer actions.

 

 

 

For example, given the previous checkpoint list:

  1. settingsStorage.addEventListener("change", sendTargetCoord);
  2. peerSocket.send(getTargetCoord());
  3. writeSettings({ target });
  4. let target: Coordinate | null = readSettings().target;
Best Answer