about summary refs log tree commit diff
path: root/src/state/models/ui/preferences.ts
blob: bffb2d89b989771e997f5020be5d3066b2b11df8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import {makeAutoObservable} from 'mobx'
import {getLocales} from 'expo-localization'
import {isObj, hasProp} from 'lib/type-guards'

const deviceLocales = getLocales()

export class PreferencesModel {
  _contentLanguages: string[] | undefined

  constructor() {
    makeAutoObservable(this, {}, {autoBind: true})
  }

  // gives an array of BCP 47 language tags without region codes
  get contentLanguages() {
    if (this._contentLanguages) {
      return this._contentLanguages
    }
    return deviceLocales.map(locale => locale.languageCode)
  }

  serialize() {
    return {
      contentLanguages: this._contentLanguages,
    }
  }

  hydrate(v: unknown) {
    if (isObj(v)) {
      if (
        hasProp(v, 'contentLanguages') &&
        Array.isArray(v.contentLanguages) &&
        typeof v.contentLanguages.every(item => typeof item === 'string')
      ) {
        this._contentLanguages = v.contentLanguages
      }
    }
  }
}