-
Notifications
You must be signed in to change notification settings - Fork 4
/
local-storage.js
88 lines (71 loc) · 2.5 KB
/
local-storage.js
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { AsyncStorage } from 'react-native'
import Observable from 'obv'
import { TEMP_SCALE_MIN, TEMP_SCALE_MAX, TEMP_SCALE_UNITS } from './config'
export const scaleObservable = Observable()
setObvWithInitValue('tempScale',
scaleObservable, { min: TEMP_SCALE_MIN, max: TEMP_SCALE_MAX })
export const unitObservable = Observable()
unitObservable.set(TEMP_SCALE_UNITS)
scaleObservable((scale) => {
const scaleRange = scale.max - scale.min
if (scaleRange <= 1.5) {
unitObservable.set(0.1)
} else {
unitObservable.set(0.5)
}
})
export async function saveTempScale(scale) {
await AsyncStorage.setItem('tempScale', JSON.stringify(scale))
scaleObservable.set(scale)
}
export const tempReminderObservable = Observable()
setObvWithInitValue('tempReminder', tempReminderObservable, {
enabled: false
})
export async function saveTempReminder(reminder) {
await AsyncStorage.setItem('tempReminder', JSON.stringify(reminder))
tempReminderObservable.set(reminder)
}
export const periodReminderObservable = Observable()
setObvWithInitValue('periodReminder', periodReminderObservable, {
enabled: false
})
export async function savePeriodReminder(reminder) {
await AsyncStorage.setItem('periodReminder', JSON.stringify(reminder))
periodReminderObservable.set(reminder)
}
export const useCervixObservable = Observable()
setObvWithInitValue('useCervix', useCervixObservable, false)
export async function saveUseCervix(bool) {
await AsyncStorage.setItem('useCervix', JSON.stringify(bool))
useCervixObservable.set(bool)
}
export const hasEncryptionObservable = Observable()
setObvWithInitValue('hasEncryption', hasEncryptionObservable, false)
export async function saveEncryptionFlag(bool) {
await AsyncStorage.setItem('hasEncryption', JSON.stringify(bool))
hasEncryptionObservable.set(bool)
}
export async function getLicenseFlag() {
return AsyncStorage.getItem('agreedToLicense')
}
export async function saveLicenseFlag() {
await AsyncStorage.setItem('agreedToLicense', JSON.stringify(true))
}
export async function getChartFlag() {
const isFirstChartView = await AsyncStorage.getItem('isFirstChartView')
return isFirstChartView === null ? 'true' : isFirstChartView
}
export async function setChartFlag() {
await AsyncStorage.setItem('isFirstChartView', JSON.stringify(false))
}
async function setObvWithInitValue(key, obv, defaultValue) {
const result = await AsyncStorage.getItem(key)
let value
if (result) {
value = JSON.parse(result)
} else {
value = defaultValue
}
obv.set(value)
}