Saving preferences vs (or in addition to) saving data

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 1:47 am

I just finished reading Klaus' great article regarding saving user preferences (http://runrev.com/newsletter/december/i ... etter4.php)

Great article! This leads me to wonder if there is a difference between saving preferences (like a text style) and the contents of say, a datagrid. Different procedures and files?

Thanks

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 1:09 pm

This morning I successfully implemented Klaus' handlers/functions. The idea of creating a custom property called saveme works great for my purposes.

Note to Klaus: The handlers do not include the label of roundrect buttons...easily added but wanted to let you know.

If I could now be able to store the contents of my datagrids as well I would be in terrific shape.

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 2:12 pm

For the other newbies (now and in the future) like me.... continuing to update my progress.

Successful in implementing arrayEncode and arrayDecode as per this article:

http://lessons.runrev.com/s/lessons/m/4 ... -it-again-

Now to see if I can combine this process with Klaus' technique in order to save all button states, field contents, images, and datagrids in the same prefs file....

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 3:18 pm

Am able to use arrayEncode and then urlencode in order to get the array into one line in the prefs file. However, reversing the process... urldecode followed by arrayDecode fails:

Message execution error:
Error description: arrayDecode: failure
Hint:

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 4:54 pm

Lines breaks in the array... using arrayEncode and then base64encode, then replacing return with "" gets it all into 1 string...looking good!

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

Re: Saving preferences vs (or in addition to) saving data

Post by Klaus » Mon Jan 16, 2012 5:38 pm

Hi,

thanks for the flowers :D

I usually convert my datagrid contents to a TAB and CR delimited list that I can save and vise versa
to load them back into my datagrids.

The trick is to URLENCODE/URLDECODE multiline content, so it fits into one ITEM in the current line!

I have two functions for converting like this:

Code: Select all

## Will convert a TAB and CR delimited TEXT list into an array
## tList is the content of a simple text file that I saved before
function list2dgdata tText
   put empty into tArray
   set itemdel to TAB
   
   ## CYCLE through all lines and convert every item to an array key:
   put 1 into tIndex
   repeat for each line i in tText
       
      ## Multi line content!
      put urldecode(item 1 of i) into tArray[tIndex]["file"]

      ## Single line content!
      put item 2 of i into tArray[tIndex]["filepath"]
      put item 3 of i into tArray[tIndex]["date"]

      ## Multi line content!
      put urldecode(item 4 of i) into tArray[tIndex]["comments"]     
      add 1 to tIndex
   end repeat
   return tArray
end mk_list2dgdata

## And convert an ARRAY back into a TAB and CR delimited text file:
function dgdata2list tArray
   put empty into tList
   set itemdel to TAB

   ## Cycle through all keys of the array:
   repeat for each key tIndex in tArray   

      ## Multi line content!
      put urlencode(tArray[tIndex]["file"]) & TAB into tLine

      ## Single line content!
      put tArray[tIndex]["filepath"] & TAB after tLine
      put tArray[tIndex]["date"] & TAB after tLine

      ## Multi line content!
      put urlencode(tArray[tIndex]["comments"]) & TAB after tLine
      
      put tLine & CR after tList      
   end repeat
   delete char -1 of tList

   ## Ready to be saved as a simple textfile!
   return tList
end dgdata2list
Hope that helps!


Best

Klaus

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 5:46 pm

Thank you Klaus, here is what I ended up with, seems to work fine but please let me know what you think. I have added an additional "case" to your switches.

for mk_collectprefs:

Code: Select all

command mk_collectprefs
   lock screen
   
   put empty into tAllPrefs
   
   ## Loop through all cards:
   repeat with i = 1 to the num of cds
      put the num of controls of cd i into tNumofcontrols
      
      ## Loop through all control on card:
      repeat with k = 1 to tNumofcontrols
         
         ## This work for ALL controls, even if we add more of them later to our stack!
         ## As long as they have their custom property "saveme" set to TRUE!
         if the saveme of control k of cd i <> true then
            next repeat
         end if
         
         ## Now we can collect the contents of the control
         ## Gives -> button "name of button"
         put the name of control k of cd i into tName
         put word 1 of tName into tType
         put word 2 of tName into tShortName
         
         ## Important step!
         replace QUOTE with empty in tShortName
         
         ## Now we need to differ:
         switch tType
            case "field"
               put empty into tStyle
               
               ## Now we need to URLENCODE the text, so we can store it in ONE line!
               put urlencode(the text of fld tShortName of cd i) into tContent
               break
               
               case "group"
               put the dgData of group tShortName of cd i into gridArray
               put base64encode(arrayEncode( gridArray )) into tContent
               replace return with "" in tContent
               break
               
and for mk_loadprefs

Code: Select all

command mk_loadprefs
   put mk_getprefs() into tPrefs
   if tPrefs = empty then
      exit mk_loadprefs
   end if
   
   ## We use this tab delimited format here:
   ## 1. Number of card
   ## 2. Type of control
   ## 3. Name of control
   ## 4. Style of control (only neccessary for buttons!)
   ## 5. Content of control
   ## 6. Rect of control (only neccessary for images!)
   set itemdel to TAB
   repeat for each line i in tPrefs
      put item 1 of i into tCardNumber
      put item 2 of i into tType
      put item 3 of i into tShortName
      put item 4 of i into tStyle
      put item 5 of i into tContent
      put item 6 of i into tRect
      
      switch tType
         case "field"
            set the text of fld tShortName of cd tCardNumber to urldecode(tContent)
            break
            
         case "group"
            put base64decode(tContent) into pData
            put arrayDecode(pData) into gridArray
            set the dgData of group tShortName of cd tCardNumber to gridArray
            break
~Brian

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

Re: Saving preferences vs (or in addition to) saving data

Post by Klaus » Mon Jan 16, 2012 5:56 pm

Hi Brian,

yes, sure, if this will work for you!
My article just wanted to give you the idea how one can do something like this.

Of course it can be modified to fit your special needs! :D

In got used to use text files, since I learned LiveCode when it was born as MetaCard
way back in 1999 and we did not have such modernistic stuff like ARRAYDE-/ENCODE! 8)


Best

Klaus

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Mon Jan 16, 2012 5:59 pm

Yeah I'm still using all of the rest of your handlers/functions unaltered, working great with txt file.

Thanks much

kdjanz
Posts: 300
Joined: Fri Dec 09, 2011 12:12 pm
Location: Fort Saskatchewan, AB Canada

Re: Saving preferences vs (or in addition to) saving data

Post by kdjanz » Mon Jan 16, 2012 11:13 pm

Brian

Your scripts seem to have been cut off in the middle of the switch. Could you either zip them up or put them in a simple stack so that I make sure I get the whole thing. I'm about to embark on this journey any there seems to be a clear trail to follow here.

Thanks

Kelly

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Tue Jan 17, 2012 12:43 am

Kelly the rest of the scripts are unchanged from Klaus' article (part 2) and the stacks he makes available in the article.

http://lessons.runrev.com/s/lessons/m/4 ... -it-again-

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Tue Jan 17, 2012 3:13 pm

Klaus: Is there a way to modify your script so that png images are encoded/decoded correctly? The jpgs are fine but it seems that the transparency of png does not survive. :?

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

Re: Saving preferences vs (or in addition to) saving data

Post by Klaus » Tue Jan 17, 2012 5:51 pm

Hi teacherguy,

oh, did not know this!

In that case you should also collect "the alphadata" of that image and set it later back AFTER setting the imabedata:

Code: Select all

...
case "image"
   put empty into tStyle
   if the filename of img tShortName of cd i = empty AND \
      the text of img tShortname of cd i = empty then
            put empty into tContent
  else
   put the imagedata of img tShortName of cd i into tImageData                 
   ## Step 1: convert binary data to text data:
   put base64encode(tImageData) into tImageDataText
                  
   ## Step 2: convert this string to ONE line:
   put urlencode(tImageDataText) into tContent

   ## NEW!
   put the ALPHAdata of img tShortName of cd i into tAlphaData                 
   ## Step 1: convert binary data to text data:
   put base64encode(tAlphaData) into tAlphaDataText
                  
   ## Step 2: convert this string to ONE line:
   put urlencode(tAlphaDataText) into tContent2
end if
Get the picture?


Best

Klaus

teacherguy
Posts: 379
Joined: Thu Dec 08, 2011 2:43 am

Re: Saving preferences vs (or in addition to) saving data

Post by teacherguy » Tue Jan 17, 2012 6:27 pm

Great thanks. Another question: If a prefs file cannot be established, will the end user know?

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

Re: Saving preferences vs (or in addition to) saving data

Post by Klaus » Tue Jan 17, 2012 7:03 pm

teacherguy wrote:Great thanks. Another question: If a prefs file cannot be established, will the end user know?
Only if you tell him... 8)

Post Reply

Return to “Talking LiveCode”