save content of field

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

save content of field

Post by Samuele » Thu Nov 25, 2021 11:44 am

hi, in my stack, i can't save the content of a field that the user fill, more specifically, the app asks the user to input a username and then i run this code

Code: Select all

on PutUsernameIntoGlobal
      put the text of widget "EnterUsername" into _guserName
      put _guserName into field "UserName"
   save this stack
end PutUsernameIntoGlobal
but when i close the app and reopen it it doesn't save the content of the field "UserName" and it's empty.
any ideas on how to save it? (if it is relevant, i'm trying to do it on a mobile app)
thanks!

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: save content of field

Post by Klaus » Thu Nov 25, 2021 11:59 am

So that stack is your standalone? Standalones cannot save themselves!
You need to export your text and read it in later.

On the mobile platform the only folder we have write permissions is -> specialfolderpath("documents")
So do like this:

Code: Select all

on PutUsernameIntoGlobal
      put the text of widget "EnterUsername" into _guserName
      put _guserName into field "UserName"
      ## save this stack

     ## Create a filename for the file:
     put specialfolderpath("documents") & "/username.txt" into tFile

     ## Now write the username to file
     put _guserName into url("file:" & tFile)
end PutUsernameIntoGlobal
Then add this in the openstack handler:

Code: Select all

...
put specialfolderpath("documents") & "/username.txt" into tFile
if there is a file tFile then
   set the text of widget "EnterUserName" to url("file:" & tFile)
else
  set the text of widget "EnterUserName" to EMPTY
end if
...

mrcoollion
Posts: 624
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: save content of field

Post by mrcoollion » Thu Nov 25, 2021 3:05 pm

Take a look at post

viewtopic.php?f=12&t=33338
Here is a stack that enables you to save data in an array and retrieve it including encryption.

Regards,

Paul

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7923
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: save content of field

Post by dunbarx » Thu Nov 25, 2021 5:49 pm

The other way to do this is to create what is known as a "Splash" stack, the only purpose of which is to be the executable. Any other stacks that are added to the standalone build can all be saved. Nothing has to happen in the splash stack, except perhaps to navigate to your "working" stacks when it opens.

This is always what I do. I find it much simpler.

The splash stack is the actual "app". In the "File" menu, under "Standalone Application Settings", you will see, in the "Stacks" pane, the option to add stack files to the splash stack file. You can add as many as you like, and of course, each such stack file is comprised of a mainStack that may have one or more subStacks.

There are several threads that deal with this. Just search for "splash stack".

Craig

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: save content of field

Post by Samuele » Thu Nov 25, 2021 6:21 pm

thanks but where can i create a splash stack?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7923
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: save content of field

Post by dunbarx » Fri Nov 26, 2021 6:03 am

A splash stack is just a stack.

So make a stack and name it "splash". Save it. Now make another stack and name it "worker". Save it. Put an openCard handler in the card script of the "splash" stack that navigates to stack "worker"

On stack "worker", put something similar to what you had in the stack that would not save its field contents. Make sure that you either have a handler in the stack script that saves that stack when it closes, perhaps using the "closeStackRequest" message, or maybe put a button on the card that saves the stack explicitly..

Save both stacks.

With the splash stack in front, go to the Standalone Application Settings of the "File" menu and open the "General"" pane. Name your standalone. Now in the "Stacks" pane, add the "worker" stack file.

There are other things that you need to do in that arena, and you probably already have experience there, such as inclusions, which platform you are building for etc. Now build the standalone.

If all is done properly, when you open the standalone, the worker stack should appear. If you place some text in the field and either hit that button or just close, then that text will be saved and will appear in the next session. It takes a little practice to get this right.

Craig

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: save content of field

Post by Samuele » Fri Nov 26, 2021 12:31 pm

dunbarx wrote:
Fri Nov 26, 2021 6:03 am

On stack "worker", put something similar to what you had in the stack that would not save its field contents. Make sure that you either have a handler in the stack script that saves that stack when it closes, perhaps using the "closeStackRequest" message, or maybe put a button on the card that saves the stack explicitly..
firs of all , thanks but sorry, i didn't understand what you said here...
Last edited by Samuele on Fri Nov 26, 2021 12:42 pm, edited 1 time in total.

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: save content of field

Post by Samuele » Fri Nov 26, 2021 12:41 pm

Klaus wrote:
Thu Nov 25, 2021 11:59 am
On the mobile platform the only folder we have write permissions is -> specialfolderpath("documents")
hi, thanks i tried and it didn't work, i did like this on the mouseDown handler of the button that saves the username

Code: Select all

on PutUsernameIntoGlobal
   put the text of widget "EnterUsername" into _guserName
   put _guserName into field "UserName"

   
   ## Create a filename for the file:
   put specialfolderpath("documents") & "/username.txt" into tFile
   
   ## Now write the username to file
   put _guserName into url("file:" & tFile)
end PutUsernameIntoGlobal
and on the openStack handler like this:

Code: Select all

on openStack
   SaveHighScore
   put specialfolderpath("documents") & "/username.txt" into tFile
   if there is a file tFile then
      set the text of widget "EnterUserName" to url("file:" & tFile)
      put url("file:" & tFile) into field "UserName" of card "HomeCard"
   else
      set the text of widget "EnterUserName" to EMPTY
   end if
end openStack
and when i reopened the app after the 1st time it has put literally "_guserName" on the field (not the content of the variable but the name)
any ideas? thanks!

Klaus
Posts: 12591
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: save content of field

Post by Klaus » Fri Nov 26, 2021 1:19 pm

Hm, this will only happen if a variable has not content!?
You could do a check here:

Code: Select all

on PutUsernameIntoGlobal
   ...
   ## Now write the username to file
   put _guserName into url("file:" & tFile)
   
   ## Seeing is believing! :-)
   ANSWER _guserName
end PutUsernameIntoGlobal
And did you declare _guserName as GLOBAL in every script you use it?
Otherwise _guserName might really be empty at some point!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7923
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: save content of field

Post by dunbarx » Fri Nov 26, 2021 6:07 pm

firs of all , thanks but sorry, i didn't understand what you said here...
Hmmm.

Didn't you already make a standalone that would not save? That meant to me that you had at least a little experience with the build process. All I want you to do in addition was to add another stack along the way.

Do what you already did again, but USING INSTEAD that NEW stack we named "Splash". Now, in the "Stacks" pane, add your ORIGINAL stack. You do this by clicking the "Add stack files..." button. This additional stack will become the "working" stack that you want to save in.

There were details I added so that when the splash stack opens, it navigates to the working stack. You need that, because when you open the standalone, you open the splash stack only, and you then need to navigate to your working stack in some way.

Do you see? You will be building with a package of two stacks instead of one. The problem is that with a one-stack build, that stack becomes the executable, which cannot be saved in any OS. But adding another stack to that build allows saving to that stack.

I told you this takes just a little practice. So practice, and please write back with your progress. We will get through this.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8901
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: save content of field

Post by FourthWorld » Fri Nov 26, 2021 8:04 pm

Saving data with a UI stack is great for simple things we make for ourselves, but is problematic the moment you want to release a new version with a new UI.

Most other dev systems require us to separate code from content, and while it's nice that LC leaves that as a choice for us sometimes the entire world isn't wrong. ;)

I burned myself too many times doing that with HC to suggest anyone else put themselves through it. Once you bind user content into the UI, you have to make exporters for the old version, and importers for the new version, and it adds complication for dev and user alike.

For apps delivered to other people, building habits of separating code, UI, and content wherever practical will make changing any one of them much easier by reducing impact on the others.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8901
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: save content of field

Post by FourthWorld » Fri Nov 26, 2021 8:09 pm

@Samuele: does this app communicate with a server?

If so, how are you hashing the password there?

If not, why would the app need a login in addition to the phone's login?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: save content of field

Post by Samuele » Sun Nov 28, 2021 6:48 pm

FourthWorld wrote:
Fri Nov 26, 2021 8:09 pm
@Samuele: does this app communicate with a server?

If so, how are you hashing the password there?

If not, why would the app need a login in addition to the phone's login?
what do you mean the phone's login?

Samuele
Posts: 79
Joined: Mon Oct 11, 2021 7:05 pm

Re: save content of field

Post by Samuele » Sun Nov 28, 2021 6:52 pm

Klaus wrote:
Fri Nov 26, 2021 1:19 pm
Hm, this will only happen if a variable has not content!?
You could do a check here:

Code: Select all

on PutUsernameIntoGlobal
   ...
   ## Now write the username to file
   put _guserName into url("file:" & tFile)
   
   ## Seeing is believing! :-)
   ANSWER _guserName
end PutUsernameIntoGlobal
And did you declare _guserName as GLOBAL in every script you use it?
Otherwise _guserName might really be empty at some point!
thanks i've found the problem, it was that in another command i put in the specialfolderpath something else (maybe empty), now it works, thanks!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 8901
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: save content of field

Post by FourthWorld » Mon Nov 29, 2021 6:02 am

Samuele wrote:
Sun Nov 28, 2021 6:48 pm
FourthWorld wrote:
Fri Nov 26, 2021 8:09 pm
@Samuele: does this app communicate with a server?

If so, how are you hashing the password there?

If not, why would the app need a login in addition to the phone's login?
what do you mean the phone's login?
Most mobile devices are protected by a user login, often a password but sometimes a fingerprint or face recognition.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”