02-19-2018 13:47
02-19-2018 13:47
Good evening fitbit community. I have successfully managed to create a watch face with a companion for changing the background colour.
Now when it is running in developer mode, I select the background colour and it changes fine and is fine most of the time.......except when I select another App (music, today, exercise etc) then back out of that App, the watch face then defaults to its original colour and not the one I had selected moments earlier.
Is this just because it's in the developer mode that its not saving the setting or is there something I am missing?
Thanks in advance
Answered! Go to the Best Answer.
02-19-2018 15:25
02-19-2018 15:25
Something like this in your app/index.js
import { me } from "appbit"; import * as fs from "fs"; const SETTINGS_FILE = "settings.json"; let settings = loadSettings(); applyTheme(); messaging.peerSocket.onmessage = function(evt) { settings.myColor = evt.data.value; applyTheme(); } function applyTheme() { myElement.style.fill = settings.myColor; } function loadSettings() { let obj; try { obj = fs.readFileSync(SETTINGS_FILE, "json"); } catch(ex) { obj = { myColor: "#FFFFFF" } } return obj; } me.onunload = () => { fs.writeFileSync(SETTINGS_FILE, settings, "json"); }
02-19-2018 13:52
02-19-2018 13:52
Are you saving/loading that setting somewhere?
You can see an example of saving the value on the device, then you can load it again when the clock relaunches.
02-19-2018 14:33
02-19-2018 14:33
Thanks for the reply but I am using the example in the fitbit docs here: https://dev.fitbit.com/build/guides/settings/
I looked at your code but can not work out which bit would relate to this example code as I'm not using a theme (foreground, background)
So how would I save and reload settings using that example code (i've used it as it is)
Thanks again.
02-19-2018 15:25
02-19-2018 15:25
Something like this in your app/index.js
import { me } from "appbit"; import * as fs from "fs"; const SETTINGS_FILE = "settings.json"; let settings = loadSettings(); applyTheme(); messaging.peerSocket.onmessage = function(evt) { settings.myColor = evt.data.value; applyTheme(); } function applyTheme() { myElement.style.fill = settings.myColor; } function loadSettings() { let obj; try { obj = fs.readFileSync(SETTINGS_FILE, "json"); } catch(ex) { obj = { myColor: "#FFFFFF" } } return obj; } me.onunload = () => { fs.writeFileSync(SETTINGS_FILE, settings, "json"); }
02-19-2018 15:33 - edited 02-19-2018 15:35
02-19-2018 15:33 - edited 02-19-2018 15:35
Excellent, I now have it working, thanks for the advice, much appreciated. 🙂