Suunto app Forum Suunto Community Forum
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    [Tip] How to save/load persistent data

    Scheduled Pinned Locked Moved Suunto Plus Development
    tipsettingsvariablestutorialjsonmanifest
    1 Posts 1 Posters 63 Views 1 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • S Offline
      SuuntoPartnerTeam
      last edited by

      Hi all!
      This thread is for discussions on the use of the storage object in SuuntoPlus app development.

      The storage object allows you to save data as different variables, that you can later retrieve even during a different exercise. You can also edit the settings in the app and save them to make them appear in the Suunto mobile app.

      Storage functions

      To use the storage element, you need to make use of these functions via the localStorage object:

      # Getters:
      localStorage.getItem(key)
      localStorage.getObject(key)
      
      # Setters:
      localStorage.setItem(key, value)
      localStorage.setObject(key, value)
      

      Where …Item refers to variables/settings saved as strings, and …Object refers to variables/settings saved as objects. In the function call parameters the key is the name of the data in your data.json file and value is the new value to be written over the old one.

      Data.json

      In order to use settings and variables, you need a data.json -file in your file tree. To add this file, just right click the explorer area where your other files (manifest.json, main.js…) are and select new file and name it data.json.
      The contents of the file are within curly brackets { }, and they contain the keys and values of your variables/settings. For example, in the Settings example app, the data.json file looks like this:

      {
          "appSettings": {
              "toggleSound": true,
              "selectedSound": {
                  "availableSounds": [ "Button", "Confirm", "Info", "Interval", "StartTimer", "StopTimer" ],
                  "sound": 0
              }
          }
      }
      

      Here the data.json file contains one object: appSettings that has two fields.

      • One is a field named toggleSound that is defined as a boolean and given an initial value of true.
      • The other is an another object named selectedSound that contains a collection of strings and a field with an integer type value.

      Manifest.json

      Finally you also need to tie your variables and settings to your manifest.json file. They both need to be separately defined in the manifest file under the headers variables and settings. These are collections containing the information of your data defined in your data.json file.

      Mandatory information is the shownName, path and type.

      • shownName is the name of the variable/setting shown to the user in the Suunto mobile app.
      • path is the path to your variable/setting within the data.json file. (If your data is not within an object, the name (key) of the data is sufficient)
      • type options for settings and variables are int, float, string and boolean, with settings also having the option enum. For settings, there are additional fields depending on the type, which can be found explained in the Suuntoplus Reference documentation.

      In the Settings example app, this part of the manifest.json file looks like this:

      "settings": [
          {"shownName": "Start muted", "path": "appSettings.toggleSound", "type": "boolean"},
          {"shownName": "Sound to test", "path": "appSettings.selectedSound.sound", "type": "enum", "valuePath": "appSettings.selectedSound.availableSounds"}
        ]
      

      More Questions?

      Just type your questions related to saving/loading persistent data in this thread and we, or someone from our amazing community, will try to help you. 🙂

      You can also find more information under the “Sports app settings” header in the SuuntoPlus Reference documentation in your editor.

      1 Reply Last reply Reply Quote 1
      • Dimitrios KanellopoulosD Dimitrios Kanellopoulos pinned this topic
      • First post
        Last post

      Suunto Terms | Privacy Policy