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

Cannot make toggles default

ANSWERED

Hi all,

 

I am creating a very cool Clockface where you can choose from settings which stats you want to see and also other stuff like colors of background, gradient and text.

I have an issue with preselecting the toggles, making some of the stats turn on by default. There is nothing about it in the documentation.

 

This is my code in settings/index.jsx

 

const activityOptions = [...].    // array elements here

 

{activityOptions.map(([title, settingsKey]) =>
    <Toggle
        settingsKey={settingsKey}
        label={title}
    />
)}

Any ideas?

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

You could do what I suggested before in your initialize() function.

Peter McLennan
Gondwana Software

View best answer in original post

Best Answer
5 REPLIES 5

Hi @uezbe  your post is being moved to the Developer's Forum area. Where you posted is the area where people discuss the apps they have gotten from the Gallery that were already released. I'm sure you'll get more relevant responses once someone sees it in that area.

Stepping in the U.S.A. since September 2013. Android 14

Best Answer
0 Votes

Get your companion code to check whether the settingsStorage item already has a value. If it hasn't, give it whatever value selects the toggle (which you can determine by logging what you get in onSettingsChange).

Peter McLennan
Gondwana Software
Best Answer

Hi @Gondwana,

 

Here is the implementation of the initialize method I call in the companion/index.js

this is a separate file

 

 

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

export function initialize() {
  settingsStorage.addEventListener("change", evt => {
    if (evt.oldValue !== evt.newValue) {
      sendValue(evt.key, evt.newValue);
    }
  });
}

function sendValue(key, val) {
  if (val) {
    sendSettingData({
      key: key,
      value: JSON.parse(val)
    });
  }
}

function sendSettingData(data) {
  if (messaging.peerSocket.readyState === messaging.peerSocket.OPEN) {
    messaging.peerSocket.send(data);
  } else {
    console.log("No peerSocket connection");
  }
}

 

I'm just initializing it

 

import * as simpleSettings from "./custom/companion-settings";

mySettings.initialize();

 

Best Answer
0 Votes

You could do what I suggested before in your initialize() function.

Peter McLennan
Gondwana Software
Best Answer

Yes, set up the default values in the init function.

Thanks @Gondwana, this was very helpful

 

Cheers

Best Answer