Defining Resource Values

A common and very useful technique when developing software is to stow often-used values in constants. This helps give semantic meaning to what would otherwise be simple values. And it provides for a single location where the value can be maintained and changed, regardless of how many places the value is used.

APL offers a similar means of declaring constants called resources. Resources let us declare constant values for dimensions, colors, or just arbitrary text in a single place and refer to them throughout the APL document.

For example, rather than hard-code “12dp” in the paddingTop property of the two Text components for the trip dates, we can declare it once, as a resource, giving it a semantically significant name like this:

 "resources"​: [
  {
 "dimensions"​: {
 "dateSpacing"​: ​"12dp"
  }
  }
 ]

Here, the name dateSpacing describes exactly what the purpose of that resource value is used for: To define spacing between the two date components. Now we can use that resource name everywhere we previously hard-coded “12dp”:

 {
 "type"​: ​"Text"​,
 "paddingTop"​: ​"@dateSpacing"​,
 "style"​: ​"datesText"​,
 "text"​: ​"<b>Departing</b>: 2020-06-09"
 },
 {
 "type"​: ​"Text"​,
 "paddingTop"​: ​"@dateSpacing"​,
 "style"​: ​"datesText"​,
 "text"​: ​"<b>Returning</b>: 2020-06-12"
 }

Here the paddingTop property is set to “@dateSpacing”. The “@” prefix indicates that this is not a literal value of “dateSpacing”, but a reference to the resource with that name. Later, if we decide to change the spacing to “50dp” or some other value, we won’t need to change the value of paddingTop for every Text component. We only need to change the value once where it’s defined as a resource.

Dimension resources are only one of three types of resources. We can also define color and string resource values. Using color resources and a few more dimension resources, we can capture the title and body text color as well as the font sizes for all text like this:

 "resources"​: [
  {
 "dimensions"​: {
 "dateSpacing"​: ​"12dp"​,
»"titleFontSize"​: ​"48dp"​,
»"dateFontSize"​: ​"24dp"
  },
»"colors"​: {
»"titleTextColor"​: ​"yellow"​,
»"dateTextColor"​: ​"lightblue"
» }
  }
 ]

Then we can change styles for the Text components in the document to reference those resources:

 {
 "titleText"​: {
 "values"​: [
  {
 "fontWeight"​: ​"heavy"​,
 "fontSize"​: ​"@titleFontSize"​,
 "color"​: ​"@titleTextColor"
  }
  ]
  },
 "datesText"​: {
 "values"​: [
  {
 "color"​: ​"@dateTextColor"​,
 "fontSize"​: ​"@dateFontSize"
  }
  ]
  }
 }

Now, if we decide to change those values later or reuse them on new components, they’re all defined in a single, easy to find location in the APL document.

A third type of resource, String resources, enables you to declare resources that hold any arbitrary textual values. For example, you could assign a couple of resources that hold the URLs for the starfield and logo images:

 "resources"​: [
  {
 "dimensions"​: {
 "dateSpacing"​: ​"12dp"​,
 "titleFontSize"​: ​"48dp"​,
 "dateFontSize"​: ​"24dp"
  },
 "colors"​: {
 "titleTextColor"​: ​"yellow"​,
 "dateTextColor"​: ​"lightblue"
  },
»"strings"​: {
»"stars"​: ​"https://starport75.dev/images/planets/stars.jpg"​,
»"earth"​: ​"https://starport75.dev/images/planets/earth_cropped.png"​,
»"spaceship"​: ​"https://starport75.dev/images/planets/spaceship.png"
» }
  }
 ]

With those defined, you would change the source property of the Image components to reference the resource strings:

 {
 "type"​: ​"Image"​,
 "width"​: ​"100vw"​,
 "height"​: ​"100vh"​,
»"source"​: ​"@stars"​,
 "scale"​: ​"fill"​,
 "position"​: ​"absolute"
 },
 {
 "type"​: ​"Image"​,
 "position"​: ​"absolute"​,
 "bottom"​: ​"0"​,
 "width"​: ​"85vw"​,
 "height"​: ​"175dp"​,
»"source"​: ​"@earth"​,
 "scale"​: ​"fill"
 },
 {
 "type"​: ​"Image"​,
 "position"​: ​"absolute"​,
 "bottom"​: ​"90dp"​,
 "left"​: ​"200dp"​,
 "width"​: ​"300dp"​,
 "height"​: ​"250dp"​,
»"source"​: ​"@spaceship"​,
 "scale"​: ​"best-fill"
 },

You could also use string resources to hold the values for the title and date components. But that would lock those text components into a specific set of text in a given language. Rather than write those values in the template, let’s have a look at how they can be given to the template from the skill’s fulfillment code as model data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset