How to Make the Switch Widget (widget.newSwitch) remember last chosen state from text file?

So I have a created a switch widget.  I have managed to record the values of the switch (true or false) to a text file in the documents directory.  

So the switch works and records the correct values in the text file.  

I have also created (per object:setSelected()) a variable (file1) that I can place into the “isOn” state so the state of the switch can change dynamically.  The problem is that the switch state always defaults to on/true, even when the text file has off/false.

 I am doing something very wrong… Please help. 

Code: 

[lua] local path = system.pathForFile( “sound.txt”, system.DocumentsDirectory )
local file1 = io.open( path, “r” )

for line in file1:lines() do
print( line )
end

onOffSwitch:setState{ isOn = (file1), isAnimated = true, onComplete = print(“COMPLETE”)}[/lua]

I hope that made sense. If not, please let me know and I will try to clarify… 

I basically want the switch to: initially default to on, then if switched to off, remember that position at a later time.  This is my own way of creating a “simple” settings page. ha… 

Thanks in advance!

What does print(line) show?

Hi JonPM,

Thanks for your reply.

Print (line) prints either true or false depending on the text file.

Maybe I can simplify my issue… I have a Switch Widget.  It returns True or False (and I have made the results get recorded in a text file).  

I want it so that the next time a user pulls up the page for that particular Switch, it’s state is set to what is was set to previously.  Not to have it always default to either True OR False. 

So: 

If the text file has False, Switch should be set to Off.  If text file has True, then Switch should be set to On.  No other options.  Unfortunately, as it is now, regardless of the text file (which is correct), when I return to the page that has the Switch, it’s always either on True or False.  

For saving/loading settings I would really recommend GGData(by Glitch Games).  

I’ m using GGData for saving switch state and it works ok.

local GGData = require "GGData" local function onSwitchPress () checked = true settings:set("checked", true) settings:save() end --onswitchpress local switchState = settings:get( "checked") local box = widget.newSwitch { style = "checkbox", --id = "Checkbox button", onPress = onSwitchPress, initialSwitchState = switchState }

What does print(line) show?

Hi JonPM,

Thanks for your reply.

Print (line) prints either true or false depending on the text file.

Maybe I can simplify my issue… I have a Switch Widget.  It returns True or False (and I have made the results get recorded in a text file).  

I want it so that the next time a user pulls up the page for that particular Switch, it’s state is set to what is was set to previously.  Not to have it always default to either True OR False. 

So: 

If the text file has False, Switch should be set to Off.  If text file has True, then Switch should be set to On.  No other options.  Unfortunately, as it is now, regardless of the text file (which is correct), when I return to the page that has the Switch, it’s always either on True or False.  

For saving/loading settings I would really recommend GGData(by Glitch Games).  

I’ m using GGData for saving switch state and it works ok.

local GGData = require "GGData" local function onSwitchPress () checked = true settings:set("checked", true) settings:save() end --onswitchpress local switchState = settings:get( "checked") local box = widget.newSwitch { style = "checkbox", --id = "Checkbox button", onPress = onSwitchPress, initialSwitchState = switchState }

Hi all,

I am having the same issue as reported above. It might be fixed in the daily bugs but as I don’t have a pro license I can only access the release version.

As other users do I save the status of the switch and retrieve it back when I go back to the screen containing the switch. 

Even though I try using “initialSwitchState = savedState” it seem to always default to true.

My work around was to enclose the creation of the newSwitch widget within an if clause so I create a switch with a different initialSwitchState depending on the condition in the if returning true or false.

I know it is a bit of a dirty solution but it did the trick for me.

Hope it helps.

local OnOffSwitch if(SavedState) then -- Create the widget onOffSwitch = widget.newSwitch { left = 320, top = 300, style = "onOff", id = "onOffSwitch", initialSwitchState = true, onPress = onSwitchPress } else onOffSwitch = widget.newSwitch { left = 320, top = 300, style = "onOff", id = "onOffSwitch", initialSwitchState = false, onPress = onSwitchPress } end

Hi all,

I am having the same issue as reported above. It might be fixed in the daily bugs but as I don’t have a pro license I can only access the release version.

As other users do I save the status of the switch and retrieve it back when I go back to the screen containing the switch. 

Even though I try using “initialSwitchState = savedState” it seem to always default to true.

My work around was to enclose the creation of the newSwitch widget within an if clause so I create a switch with a different initialSwitchState depending on the condition in the if returning true or false.

I know it is a bit of a dirty solution but it did the trick for me.

Hope it helps.

local OnOffSwitch if(SavedState) then -- Create the widget onOffSwitch = widget.newSwitch { left = 320, top = 300, style = "onOff", id = "onOffSwitch", initialSwitchState = true, onPress = onSwitchPress } else onOffSwitch = widget.newSwitch { left = 320, top = 300, style = "onOff", id = "onOffSwitch", initialSwitchState = false, onPress = onSwitchPress } end