-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathapi.js
201 lines (176 loc) · 4.51 KB
/
api.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import Storage from 'react-native-storage';
import { AsyncStorage } from 'react-native';
import * as Speech from 'expo-speech';
import * as Localization from 'expo-localization';
// Check segment credentials
import UIText from './data/text.json';
import makeid from './js/makeid';
import Event from './js/event';
// For test cases
const NETWORK_STATUS = true;
const _FLUSH = true;
const _DEVELOPMENT = true;
const _DEVLANG = "";
let storage;
storage = new Storage({
size: 1000,
storageBackend: AsyncStorage,
defaultExpires: null,
enableCache: true,
sync: {}
});
class Api {
constructor(){
if(!_DEVELOPMENT){
}else{
this.segment = {screen: () => {}, trackWithProperties: () => {}, screenWithProperties: () => {}}
}
this.event = Event;
console.log("API: Created instance");
this.currentLang = _DEVLANG;
this.initApiCurrents();
if(_FLUSH){
this.flush();
}
}
flush(){
// Flush to the begining state
this.setData("lang", "");
this.setData("setup", "start");
console.log("API: flushed");
}
initApiCurrents(){
this.getData("userId").then(uid => {
if(!_DEVELOPMENT){
this.segment.identify(uid);
}
if(_FLUSH || uid == "" || uid == null){
let uid = makeid(8);
this.setData("userId", uid);
}
console.log("Segment: User identified" + uid);
}, err => {
if(err.name == "NotFoundError"){
let uid = makeid(8);
this.setData("userId", uid);
if(!_DEVELOPMENT){
this.segment.identify(uid);
}
console.log("API: First time userId set");
console.log("Segment: User identified" + uid);
}
});
this.getData("setup").then(setupStatus => {
console.log("Setup status is: ", setupStatus);
if(_FLUSH || setupStatus == "" || setupStatus == null){
this.setData("setup", "start");
}
}, err => {
if(err.name == "NotFoundError"){
this.setData("setup", "start");
console.log("Setup status set for the first time");
}
});
this.getData("lang").then(lang => {
console.log("API: serve with lang: ", lang);
if(lang.includes("tr")){
this.currentLang = "tr";
}else if(lang.includes("de")){
this.currentLang = "de";
}else if(lang.includes("fr")){
this.currentLang = "fr";
}else if(lang.includes("es")){
this.currentLang = "es";
}else{
this.currentLang = "en";
}
if(_DEVLANG){ this.currentLang = _DEVLANG; }
}, err => {
if(err.name == "NotFoundError"){
lang = Localization.locale;
this.setData("lang", lang);
console.log("API: first time lang init", lang);
if(lang.includes("tr")){
this.currentLang = "tr";
}else if(lang.includes("de")){
this.currentLang = "de";
}else if(lang.includes("fr")){
this.currentLang = "fr";
}else{
this.currentLang = "en";
}
if(_DEVLANG){ this.currentLang = _DEVLANG; }
}
});
this.getData("pitch").then(pitch => {
this.speakPitch = pitch;
if(_FLUSH || pitch == "" || pitch == null){
this.setData("pitch", 1.0);
}
}, err => {
if(err.name == "NotFoundError"){
this.speakPitch = 1.0;
}
});
this.getData("rate").then(rate => {
this.speakRate = rate;
if(_FLUSH || rate == "" || rate == null){
this.setData("rate", 1.0);
}
}, err => {
if(err.name == "NotFoundError"){
this.speakRate = 1.0;
}
});
this.getData("gridSize").then(gs => {
this.gridSize = gs;
if(_FLUSH || gs == "" || gs == null){
this.setData("gridSize", [3, 5]);
}
}, err => {
if(err.name == "NotFoundError"){
this.gridSize = [3, 5];
}
});
}
speak(speakText){
console.log("Speak With", {
language: this.currentLang,
pitch: this.speakPitch,
rate: this.speakRate
});
Speech.speak(speakText, {
language: this.currentLang,
pitch: this.speakPitch,
rate: this.speakRate
});
}
UIText(identifier, forcedLang){
if(UIText[identifier]){
if(forcedLang){
return UIText[identifier][forcedLang];
}else if(UIText[identifier][this.currentLang]){
return UIText[identifier][this.currentLang];
}else{
return UIText[identifier].en;
}
}else{
return "UndefinedUIText";
}
}
changeLang(toLang){
this.setData("lang", toLang);
this.currentLang = toLang;
}
// These are like kinda private;
// But xxx it, use them in the general app, who cares.
setData(key, data){
return storage.save({key, data});
}
getData(key){
// returns promise
return storage.load({key});
}
}
const _api = new Api();
export default _api;