# Settings

An app's settings are declared once, as a schema, and shown where each platform shows an
app's settings: on iOS and iPadOS in the Settings app, as the app's own page; on Android
behind the gear on the app's App info screen, which opens the app at its settings; on the
Mac behind Settings… in the app menu (⌘,); on the web wherever the page puts them.

```typescript
import { s } from 'tosijs-schema'

capabilities: {
  settings: s.object({
    sound: s.boolean.title('Sound effects').default(true),
    speed: s
      .union([s.const('slow').title('Slow'), s.const('fast').title('Fast')])
      .title('Game speed')
      .default('slow'),
    volume: s.number.min(0).max(100).title('Volume').default(70),
    name: s.string.max(20).title('Your name').default(''),
    display: s
      .object({ size: s.integer.min(1).max(5).title('Text size').default(3) })
      .title('Display'),
  }),
}
```

The schema is a [tosijs-schema](https://www.npmjs.com/package/tosijs-schema) builder or the
plain JSON Schema it makes. Each property is a setting, shown according to what it is:

| in the schema | the setting | iOS Settings app |
| --- | --- | --- |
| `s.boolean` | a toggle | a switch |
| a union of consts, each with a `title`, or `s.enum([...])` | a choice | a list of choices, labelled by the titles |
| an integer (or a `multipleOf`) with a `minimum` and a `maximum`, at most 25 values | a choice of the numbers | a list of the numbers |
| any other number with a `minimum` and a `maximum` | a slider, its value said under its title | a slider, under its title |
| a number without both, or `s.string` | text | a text field (`email` and `uri` formats choose the keyboard) |
| `readOnly` (`.meta({ readOnly: true })`) | shown, not changed by the player | a title and its value |
| an object with a `title` | a group of settings | a titled group, its `description` under it |

Every setting has a `default`, a `title` is its label, and a `description` is said under it.
A group holds settings, not groups. `SETTINGS_SCHEMA` is the schema these schemas must fit:
`defineConfig` refuses anything a settings page cannot show (an array, a record, a union of
shapes, a nested group, a keyword no page would honour), naming the setting. A key is a plain
name (`[A-Za-z][A-Za-z0-9_]*`), because it is also the key the platform stores the value
under: in a group, `display.size`.

`native.settings` reads and changes them:

```typescript
const settings = native.settings.values()   // { sound: true, speed: 'slow', ..., display: { size: 3 } }
await native.settings.set('volume', 30)      // checked against the schema, then stored
await native.settings.set('display.size', 4)
native.settings.onChange((keys) => apply(native.settings.values()))  // changed in the Settings app too
native.settings.onShow(() => openSettingsPage())  // Android's App info gear, the Mac's ⌘,
```

`values()` is synchronous, filled before `createNative` resolves, and always complete: a
setting never changed is its default. A value the platform hands back that the schema would
refuse (a text field in the Settings app holds whatever was typed into it) is read as the
setting's default, and said once in the console. `set()` refuses a key that is not a setting
and a value the schema refuses, and the shell refuses an undeclared key and a value of the
wrong kind again on its own side.

`settingsProblems(schema)` lists what is wrong with a settings schema, each naming the
setting it is in; `defineConfig` reports them with the rest of the config's.

`settingFields(schema)` is every setting in order, flat: `{ key, kind, schema, group }`, where
`key` is the stored key (`display.size` in a group), and `group` is the group's key and title,
or null. The API, the web fallback and the builds (the iOS Settings bundle) all read it.

`settingChoices(schema)` is a choice setting's `[{ value, title }]`, in order: a union of
consts gives each const and its title, an enum each value as its own title. A stepped number
(an integer, or one with a `multipleOf`) between a minimum and a maximum, with at most 25
values, lists its numbers too, each its own title: it is picked like a choice, since a slider
says nothing of its value (the owner found a page of them perplexing). Anything else has none.

`settingValue(field, raw)` is what a stored value means: `raw` read as the setting's kind (a
text field stores a number as a string; a toggle may come back as 0 or 1) and checked against
its schema. It is `{ value, valid }`, and the default when `raw` is missing or refused.

`settingsFrom(schema, flat)` turns stored values (`{ 'display.size': 4 }`) into the nested
object `values()` answers, every setting present: the default where nothing is stored.
